Merge branch 'master' of git@vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / peppershop / dblib.php
1 <?php
2
3 include_once("MDB2.php");
4
5 class mydb {
6
7    var $db = false;
8    var $error = false;
9    var $debug = true;
10    var $dbf = false;
11
12    function mydb($host,$db,$user,$pass,$port,$proto,$error) {
13        $this->error = $error;
14        $dsn = array('phptype'  => $proto,
15                'username' => $user,
16                'password' => $pass,
17                'hostspec' => $host,
18                'database' => $db,
19                'port'     => $port);
20        if ( $this->debug ) {
21             $this->dbf = fopen ("tmp/shop.log","w");
22             if ( !$this->dbf ) $this->debug = false;
23        }
24        $this->connect($dsn);
25     }
26
27     function log($txt) {
28        $now = date('Y-m-d H:i:s');
29        fputs($this->dbf,$now." : ".$txt."\n");
30     }
31     function connect($dsn) {
32        $options = array('result_buffering' => false,);
33        $this->db = MDB2::connect($dsn,$options);
34        if ( $this->debug ) $this->log('Connect:');
35        if (PEAR::isError($this->db)) {
36            if ( $this->debug ) $this->log($this->db->getMessage());
37            $this->error->write('dblib->connect',$this->db->getMessage());
38            $this->error->write('dblib->connect',print_r($dsn,true));
39            $this->db = false;
40            return false;
41        }
42        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
43     }
44     function Begin() {
45         return $this->db->beginTransaction();
46     }
47     function Commit() {
48         return $this->db->commit();
49     }
50     function Rollback() {
51         return $this->db->rollback();
52     }
53
54     function getAll($sql) {
55         $rs = $this->db->queryAll($sql);
56         if ( $this->debug ) $this->log($sql);
57         if (PEAR::isError($rs)) {
58             if ( $this->debug ) $this->log($rs->getUserinfo());
59             $this->error->write('dblib->getAll',$rs->getUserinfo());
60             return false;
61         }
62         return $rs;
63     } 
64  
65     function getOne($sql) {
66         $rs = $this->db->queryRow($sql);
67         if ( $this->debug ) $this->log($sql);
68         if (PEAR::isError($rs)) {
69             if ( $this->debug ) $this->log($rs->getUserinfo());
70             $this->error->write('dblib->getOne',$rs->getUserinfo());
71             return false;
72         }
73         return $rs;
74     }
75     function query($sql) {
76         $rc = $this->db->query($sql);
77         if ( $this->debug ) $this->log($sql);
78         if (PEAR::isError($rc)) {
79             if ( $this->debug ) $this->log($rc->getUserinfo());
80             $this->error->write('dblib->query',$rc->getUserinfo());
81             return false;
82         }
83         return $rc;
84     } 
85     function insert($statement,$data) {
86         if ( $this->debug ) $this->log("INSERT ".$statement);
87         $sth = $this->db->prepare($statement);                      //Prepare
88         if (PEAR::isError($sth)) {
89             $this->error->write('dblib->insert 1',$sth->getMessage());
90             $this->error->write('dblib->insert 2',$sth->getUserinfo());
91             $this->rollback();
92             return false;
93         }
94         if ( $this->debug ) $this->log(print_r($data,true));
95         $rc =& $sth->execute($data);
96         if (PEAR::isError($rc)) {
97             if ( $this->debug ) $this->log($rc->getUserinfo());
98             $this->error->write('dblib->insert 3',$rc->getUserinfo());
99             return false;
100         }//else{
101         //    $rc = $this->commit();
102         //}
103         return $rc;
104     }
105     function update($statement,$data) {  
106         if ( $this->debug ) $this->log("UPDATE ".$statement);
107         $sth = $this->db->prepare($statement);                      //Prepare
108         if (PEAR::isError($sth)) {
109             if ( $this->debug ) $this->log("ERRPOR ".$rc->getUserinfo());
110             $this->error->write('dblib->update 1',$sth->getMessage());
111             $this->error->write('dblib->update 2',$sth->getUserinfo());
112             $this->rollback();
113             return false;
114         }
115         if ( $this->debug ) $this->log(print_r($data,true));
116         $rc =& $sth->execute($data);
117         if (PEAR::isError($rc)) {
118             if ( $this->debug ) $this->log("ERRPOR ".$rc->getUserinfo());
119             $this->error->write('dblib->update 3',$rc->getUserinfo());
120             return false;
121         }//else{
122         //    $rc = $this->commit();
123         //}
124         return $rc;
125     }
126     function insertMultipe($statement,$data) {
127         $this->db->loadModule('Extended');
128         if (!$this->db->supports('transactions')){
129             return false;
130         }
131         $sth = $this->db->prepare($statement);                      //Prepare
132         if (PEAR::isError($sth)) {
133             $this->error->write('dblib->insertMultiple',$sth->getMessage());
134             $this->rollback();
135             return false;
136         }
137         $rc =& $this->db->beginTransaction();
138         $rc =& $this->db->extended->executeMultiple($sth, $data);
139         if (PEAR::isError($rc)) {
140             $this->error->write('dblib->insertMultiple',$rc->getUserinfo());
141             $this->rollback();
142             return false;
143         }else{
144                 $rc = $this->commit();
145         }
146         return $rc;
147     }
148 }
149
150 ?>