Merge branch 'master' of 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    var $database = false;
12
13    function mydb($host,$db,$user,$pass,$port,$proto,$error,$debug) {
14        $this->error = $error;
15        $dsn = array('phptype'  => $proto,
16                'username' => $user,
17                'password' => $pass,
18                'hostspec' => $host,
19                'database' => $db,
20                'port'     => $port);
21        $this->debug = $debug;
22        $this->database = "-< $db >-";
23        $this->connect($dsn);
24     }
25
26     function connect($dsn) {
27        $options = array('result_buffering' => false,);
28        $this->db = MDB2::connect($dsn,$options);
29        if ( $this->debug ) $this->error->write('dblib->connect '.$this->database,'Connect:');
30        if (PEAR::isError($this->db)) {
31            $this->error->write('dblib->connect '.$this->database,$this->db->getMessage());
32            $this->error->write('dblib->connect '.$this->database,print_r($dsn,true));
33            $this->db = false;
34            return false;
35        }
36        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
37     }
38     function Begin() {
39         return $this->db->beginTransaction();
40     }
41     function Commit() {
42         return $this->db->commit();
43     }
44     function Rollback() {
45         return $this->db->rollback();
46     }
47
48     function getAll($sql) {
49         $rs = $this->db->queryAll($sql);
50         if ( $this->debug ) $this->error->write('dblib->getAll '.$this->database,$sql);
51         if (PEAR::isError($rs)) {
52             $this->error->write('dblib->getAll '.$this->database,$rs->getUserinfo());
53             return false;
54         }
55         return $rs;
56     } 
57  
58     function getOne($sql) {
59         $rs = $this->db->queryRow($sql);
60         if ( $this->debug ) $this->error->write('dblib->getOne '.$this->database,$sql);
61         if (PEAR::isError($rs)) {
62             $this->error->write('dblib->getOne '.$this->database,$rs->getUserinfo());
63             return false;
64         }
65         return $rs;
66     }
67     function query($sql) {
68         $rc = $this->db->query($sql);
69         if ( $this->debug ) $this->error->write('dblib->query '.$this->database,$sql);
70         if (PEAR::isError($rc)) {
71             $this->error->write('dblib->query '.$this->database,$rc->getUserinfo());
72             return false;
73         }
74         return $rc;
75     } 
76     function insert($statement,$data) {
77         if ( $this->debug ) $this->error->write("dblib->insert ".$this->database,$statement);
78         $sth = $this->db->prepare($statement);                      //Prepare
79         if (PEAR::isError($sth)) {
80             $this->error->write('dblib->insert 1 '.$this->database,$sth->getMessage());
81             $this->error->write('dblib->insert 2',$sth->getUserinfo());
82             $this->rollback();
83             return false;
84         }
85         if ( $this->debug ) $this->error->write('dblib->insert',print_r($data,true));
86         $rc =& $sth->execute($data);
87         if (PEAR::isError($rc)) {
88             $this->error->write('dblib->insert 3 '.$this->database,$rc->getUserinfo());
89             return false;
90         }//else{
91         //    $rc = $this->commit();
92         //}
93         return $rc;
94     }
95     function update($statement,$data) {  
96         if ( $this->debug ) $this->error->write("dblib->update ".$this->database,$statement);
97         $sth = $this->db->prepare($statement);                      //Prepare
98         if (PEAR::isError($sth)) {
99             $this->error->write('dblib->update 1 '.$this->database,$sth->getMessage());
100             $this->error->write('dblib->update 2',$sth->getUserinfo());
101             $this->rollback();
102             return false;
103         }
104         if ( $this->debug ) $this->error->write('dblib->insert',print_r($data,true));
105         $rc =& $sth->execute($data);
106         if (PEAR::isError($rc)) {
107             $this->error->write('dblib->update 3 '.$this->database,$rc->getUserinfo());
108             return false;
109         }//else{
110         //    $rc = $this->commit();
111         //}
112         return $rc;
113     }
114     function insertMultipe($statement,$data) {
115         $this->db->loadModule('Extended');
116         if (!$this->db->supports('transactions')){
117             return false;
118         }
119         $sth = $this->db->prepare($statement);                      //Prepare
120         if (PEAR::isError($sth)) {
121             $this->error->write('dblib->insertMultiple '.$this->database,$sth->getMessage());
122             $this->rollback();
123             return false;
124         }
125         $rc =& $this->db->beginTransaction();
126         $rc =& $this->db->extended->executeMultiple($sth, $data);
127         if (PEAR::isError($rc)) {
128             $this->error->write('dblib->insertMultiple '.$this->database,$rc->getUserinfo());
129             $this->rollback();
130             return false;
131         }else{
132                 $rc = $this->commit();
133         }
134         return $rc;
135     }
136 }
137
138 ?>