Vereinfachung.
[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 $errfile = "tmp/lxcrm.err";
11  var $logfile = "tmp/lxcrm.log";
12  var $lfh = false;
13  
14         function dbFehler($sql,$err) {
15                 $efh=fopen($this->errfile,"a");
16                 fputs($efh,date("Y-m-d H:i:s ->"));
17                 fputs($efh,$sql."\n");
18                 fputs($efh,$err."\n");
19                 fputs($efh,print_r($this->rc,true));
20                 fputs($efh,"\n");
21                 fclose($efh);
22                 if ($this->showErr)
23                         echo "</td></tr></table><font color='red'>$sql : $err</font><br>";
24         }
25
26         function showDebug($sql) {
27                 echo $sql."<br>";
28                 if ($this->debug==2) {
29                         echo "<pre>";
30                         print_r($this->rc);
31                         echo "</pre>";
32                 };
33         }
34
35         function writeLog($txt) {
36                 if ($this->lfh===false)
37                         $this->lfh=fopen($this->logfile,"a");
38                 fputs($this->lfh,date("Y-m-d H:i:s ->"));
39                 fputs($this->lfh,$txt."\n");
40                 fputs($this->lfh,print_r($this->rc,true));
41                 fputs($this->lfh,"\n");
42         }
43
44         function closeLogfile() {
45                 fclose($this->lfh);
46         }
47         
48         function myDB($host,$user,$pwd,$db,$port,$showErr=false) {
49                 $dsn = array(
50                     'phptype'  => 'pgsql',
51                     'username' => $user,
52                     'password' => $pwd,
53                     'hostspec' => $host,
54                     'database' => $db,
55                     'port'     => $port
56                 );
57                 $this->showErr=$showErr;
58                 $this->db=DB::connect($dsn);
59                 if (!$this->db || DB::isError($this->db)) {
60                         if ($this->log) $this->writeLog("Connect $dns");
61                         $this->dbFehler("Connect ".print_r($dsn,true),$this->db->getMessage()); 
62                         die ($this->db->getMessage());
63                 }
64                 if ($this->log) $this->writeLog("Connect: ok ");
65                 return $this->db;
66         }
67
68         function query($sql) {
69                 $this->rc=@$this->db->query($sql);
70                 if ($this->debug) $this->showDebug($sql);
71                 if ($this->log) $this->writeLog($sql);
72                 if(DB::isError($this->rc)) {
73                         $this->dbFehler($sql,$this->rc->getMessage());
74                         $this->rollback();
75                         return false;
76                 } else {
77                         return $this->rc;
78                 }
79         }
80
81         function begin() {
82                 $this->query("BEGIN");
83         }
84         function commit() {
85                 $this->query("COMMIT");
86         }
87         function rollback() {
88                 $this->query("ROLLBACK");
89         }
90
91         function getAll($sql) {
92                 $this->rc=$this->db->getAll($sql,DB_FETCHMODE_ASSOC);
93                 if ($this->debug) $this->showDebug($sql);
94                 if ($this->log) $this->writeLog($sql);
95                 if(DB::isError($this->rc)) {
96                         $this->dbFehler($sql,$this->rc->getMessage());
97                         return false;
98                 } else {
99                         return $this->rc;
100                 }
101         }
102
103         function saveData($txt) {
104                 if (get_magic_quotes_gpc()) {   
105                         return $txt;
106                 } else {
107                         return DB::quoteSmart($string); 
108                 }
109         }
110
111         function chkcol($tbl) {
112         // gibt es die Spalte import schon?
113                 $rc=$this->db->query("select import from $tbl limit 1");
114                 if(DB::isError($rc)) {
115                         $rc=$this->db->query("alter table $tbl add column import int4");
116                         if(DB::isError($rc)) { return false; }
117                         else { return true; }
118                 } else { return true; };
119         }
120
121            
122 }
123 ?>