Revision fc47e483
Von Holger Lindemann vor etwa 15 Jahren hinzugefügt
lxo-import/db.php | ||
---|---|---|
7 | 7 |
var $showErr = false; // Browserausgabe |
8 | 8 |
var $debug = false; // 1 = SQL-Ausgabe, 2 = zusätzlich Ergebnis |
9 | 9 |
var $log = true; // Alle Abfragen mitloggen |
10 |
var $errfile = "/tmp/lxcrm.err"; |
|
11 |
var $logfile = "/tmp/lxcrm.log"; |
|
10 |
var $path = "/tmp/"; |
|
12 | 11 |
var $lfh = false; |
13 | 12 |
|
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 |
}
|
|
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 |
}
|
|
25 | 24 |
|
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 |
}
|
|
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 |
}
|
|
34 | 33 |
|
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 |
}
|
|
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 |
}
|
|
43 | 42 |
|
44 |
function closeLogfile() {
|
|
45 |
fclose($this->lfh);
|
|
46 |
}
|
|
47 |
|
|
48 |
function myDB($host,$user,$pwd,$db,$port,$showErr=false) {
|
|
49 |
$dsn = array(
|
|
43 |
function closeLogfile() {
|
|
44 |
fclose($this->lfh);
|
|
45 |
}
|
|
46 |
|
|
47 |
function myDB($host,$user,$pwd,$db,$port,$showErr=false) {
|
|
48 |
$dsn = array(
|
|
50 | 49 |
'phptype' => 'pgsql', |
51 | 50 |
'username' => $user, |
52 | 51 |
'password' => $pwd, |
... | ... | |
54 | 53 |
'database' => $db, |
55 | 54 |
'port' => $port |
56 | 55 |
); |
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 |
}
|
|
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 |
}
|
|
67 | 66 |
|
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 |
}
|
|
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 |
}
|
|
80 | 79 |
|
81 |
function begin() {
|
|
82 |
$this->query("BEGIN");
|
|
83 |
}
|
|
84 |
function commit() {
|
|
85 |
$this->query("COMMIT");
|
|
86 |
}
|
|
87 |
function rollback() {
|
|
88 |
$this->query("ROLLBACK");
|
|
89 |
}
|
|
80 |
function begin() {
|
|
81 |
$this->query("BEGIN");
|
|
82 |
}
|
|
83 |
function commit() {
|
|
84 |
$this->query("COMMIT");
|
|
85 |
}
|
|
86 |
function rollback() {
|
|
87 |
$this->query("ROLLBACK");
|
|
88 |
}
|
|
90 | 89 |
|
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 |
}
|
|
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 |
}
|
|
102 | 101 |
|
103 |
function saveData($txt) {
|
|
104 |
if (get_magic_quotes_gpc()) {
|
|
105 |
return $txt;
|
|
106 |
} else {
|
|
107 |
return DB::quoteSmart($string);
|
|
108 |
}
|
|
109 |
}
|
|
102 |
function saveData($txt) {
|
|
103 |
if (get_magic_quotes_gpc()) {
|
|
104 |
return $txt;
|
|
105 |
} else {
|
|
106 |
return DB::quoteSmart($string);
|
|
107 |
}
|
|
108 |
}
|
|
110 | 109 |
|
111 |
function chkcol($tbl) { |
|
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) { |
|
112 | 128 |
// 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 |
}
|
|
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 |
}
|
|
120 | 136 |
|
121 | 137 |
|
122 | 138 |
} |
Auch abrufbar als: Unified diff
Code überarbeitet
Beispielimportdateien zugefügt