aae192c0fd31d34bac13e867c06bf6b4fb6fa7a9
[kivitendo-erp.git] / lxo-import / db.php
1 <?
2 require_once "DB.php";
3 class myDB extends DB {
4
5  var $db = false;
6  var $rc = false;
7  var $showErr = false; // Browserausgabe
8  var $debug = false; // 1 = SQL-Ausgabe, 2 = zusätzlich Ergebnis
9  var $log = true;  // Alle Abfragen mitloggen
10  var $path = "/tmp/";
11  var $lfh = false;
12  
13     function dbFehler($sql,$err) {
14         $efh=fopen($this->path."lxcrm".date("w").".err","a");
15         fputs($efh,date("Y-m-d H:i:s ->"));
16         fputs($efh,$sql."\n");
17         fputs($efh,$err."\n");
18         fputs($efh,print_r($this->rc,true));
19         fputs($efh,"\n");
20         fclose($efh);
21         if ($this->showErr)
22             echo "</td></tr></table><font color='red'>$sql : $err</font><br>";
23     }
24
25     function showDebug($sql) {
26         echo $sql."<br>";
27         if ($this->debug==2) {
28             echo "<pre>";
29             print_r($this->rc);
30             echo "</pre>";
31         };
32     }
33
34     function writeLog($txt) {
35         if ($this->lfh===false)
36             $this->lfh=fopen($this->path."lxcrm".date("w").".log","a");
37         fputs($this->lfh,date("Y-m-d H:i:s ->"));
38         fputs($this->lfh,$txt."\n");
39         fputs($this->lfh,print_r($this->rc,true));
40         fputs($this->lfh,"\n");
41     }
42
43     function closeLogfile() {
44         fclose($this->lfh);
45     }
46     
47     function myDB($host,$user,$pwd,$db,$port,$showErr=false) {
48         $dsn = array(
49                     'phptype'  => 'pgsql',
50                     'username' => $user,
51                     'password' => $pwd,
52                     'hostspec' => $host,
53                     'database' => $db,
54                     'port'     => $port
55                 );
56         $this->showErr=$showErr;
57         $this->db=DB::connect($dsn);
58         if (!$this->db || DB::isError($this->db)) {
59             if ($this->log) $this->writeLog("Connect $dns");
60             $this->dbFehler("Connect ".print_r($dsn,true),$this->db->getMessage()); 
61             die ($this->db->getMessage());
62         }
63         if ($this->log) $this->writeLog("Connect: ok ");
64         return $this->db;
65     }
66
67     function query($sql) {
68         $this->rc=@$this->db->query($sql);
69         if ($this->debug) $this->showDebug($sql);
70         if ($this->log) $this->writeLog($sql);
71         if(DB::isError($this->rc)) {
72             $this->dbFehler($sql,$this->rc->getMessage());
73             $this->rollback();
74             return false;
75         } else {
76             return $this->rc;
77         }
78     }
79
80     function begin() {
81         $this->query("BEGIN");
82     }
83     function commit() {
84         $this->query("COMMIT");
85     }
86     function rollback() {
87         $this->query("ROLLBACK");
88     }
89
90     function getAll($sql) {
91         $this->rc=$this->db->getAll($sql,DB_FETCHMODE_ASSOC);
92         if ($this->debug) $this->showDebug($sql);
93         if ($this->log) $this->writeLog($sql);
94         if(DB::isError($this->rc)) {
95             $this->dbFehler($sql,$this->rc->getMessage());
96             return false;
97         } else {
98             return $this->rc;
99         }
100     }
101
102     function saveData($txt) {
103         if (get_magic_quotes_gpc()) {     
104             return $txt;
105         } else {
106             return DB::quoteSmart($string); 
107         }
108     }
109
110     function execute($statement, $data){
111         $sth = $this->db->prepare($statement);           //Prepare
112         /*if (PEAR::isError($sth)) {
113             $this->dbFehler($statement,$sth->getMessage());
114             $this->rollback();
115             return false;
116         }*/
117         $rc = $this->db->execute($sth,$data);
118         if(PEAR::isError($rc)) {
119             $this->dbFehler(print_r($data,true),$rc->getMessage()."\n".print_r($rc,true));
120             $this->rollback();
121             return false;
122         }
123         $this->db->commit();
124         return true;
125     }
126
127     function chkcol($tbl) {
128         // gibt es die Spalte import schon?
129         $rc=$this->db->query("select import from $tbl limit 1");
130         if(DB::isError($rc)) {
131            $rc=$this->db->query("alter table $tbl add column import int4");
132            if(DB::isError($rc)) { return false; }
133            else { return true; }
134         } else { return true; };
135     }
136
137            
138 }
139 ?>