tax_id in acc_trans
[kivitendo-erp.git] / shopxtc / 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             $this->error->write('SQL ',$statement);
90             $this->error->write('Data ',print_r($data,true));
91             return false;
92         }//else{
93         //    $rc = $this->commit();
94         //}
95         return $rc;
96     }
97     function update($statement,$data) {  
98         if ( $this->debug ) $this->error->write("dblib->update ".$this->database,$statement);
99         $sth = $this->db->prepare($statement);                      //Prepare
100         if (PEAR::isError($sth)) {
101             $this->error->write('dblib->update 1 '.$this->database,$sth->getMessage());
102             $this->error->write('dblib->update 2',$sth->getUserinfo());
103             $this->rollback();
104             return false;
105         }
106         if ( $this->debug ) $this->error->write('dblib->insert',print_r($data,true));
107         $rc =& $sth->execute($data);
108         if (PEAR::isError($rc)) {
109             $this->error->write('dblib->update 3 '.$this->database,$rc->getUserinfo());
110             return false;
111         }//else{
112         //    $rc = $this->commit();
113         //}
114         return $rc;
115     }
116     function insertMultipe($statement,$data) {
117         $this->db->loadModule('Extended');
118         if (!$this->db->supports('transactions')){
119             return false;
120         }
121         $sth = $this->db->prepare($statement);                      //Prepare
122         if (PEAR::isError($sth)) {
123             $this->error->write('dblib->insertMultiple '.$this->database,$sth->getMessage());
124             $this->rollback();
125             return false;
126         }
127         $rc =& $this->db->beginTransaction();
128         $rc =& $this->db->extended->executeMultiple($sth, $data);
129         if (PEAR::isError($rc)) {
130             $this->error->write('dblib->insertMultiple '.$this->database,$rc->getUserinfo());
131             $this->rollback();
132             return false;
133         }else{
134                 $rc = $this->commit();
135         }
136         return $rc;
137     }
138 }
139
140 ?>