1 |
<? |
2 |
/* |
3 |
* $Id: dbi_odbc.php,v 1.2 2001/05/18 12:37:22 cfsl Exp $ |
4 |
* |
5 |
* This class is based on the Perl DBI. |
6 |
* New functionality should be added accordingly. |
7 |
* Please refer to the DBI documentation (perldoc DBI). |
8 |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
11 |
* the Free Software Foundation; either version 2 of the License, or |
12 |
* (at your option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the GNU General Public License |
20 |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
*/ |
23 |
class DBI { |
24 |
var $session; |
25 |
var $debug = 1; |
26 |
var $Driver_name = "odbc"; |
27 |
|
28 |
function DBI ($db, $user, $password, $debug = 0) { |
29 |
$this->session = odbc_pconnect($db, $user, $password); |
30 |
$this->debug = $debug; |
31 |
} |
32 |
|
33 |
function prepare ($query) { |
34 |
return new STH($this, $this->session, $query, $this->debug); |
35 |
} |
36 |
|
37 |
function autocommit ($value) { |
38 |
return odbc_autocommit ($this->session, $value); |
39 |
} |
40 |
|
41 |
function commit () { |
42 |
return odbc_commit($this->session); |
43 |
} |
44 |
|
45 |
function rollback () { |
46 |
return odbc_rollback($this->session); |
47 |
} |
48 |
|
49 |
function quote ($str) { |
50 |
if (get_magic_quotes_gpc()) { |
51 |
$str = stripslashes($str); |
52 |
} |
53 |
return "'".str_replace ("'", "''", $str)."'"; |
54 |
} |
55 |
|
56 |
function insert_id ($sequence) { |
57 |
$sth = new STH($this, $this->session, "SELECT $sequence.CURRVAL FROM DUAL", $this->mode, $this->debug); |
58 |
$sth->execute(); |
59 |
list($res) = $sth->fetchrow_array(); |
60 |
return $res; |
61 |
} |
62 |
} |
63 |
|
64 |
class STH { |
65 |
var $query; |
66 |
var $statement; |
67 |
var $debug; |
68 |
var $dbi; |
69 |
var $placeholders; |
70 |
|
71 |
function STH (&$dbi, &$session, $query, $debug) { |
72 |
$this->dbi = &$dbi; |
73 |
$this->query = $query; |
74 |
$this->debug = $debug; |
75 |
$this->session = &$session; |
76 |
|
77 |
$this->statement = odbc_prepare($this->session, $this->query); |
78 |
return $this->statement; |
79 |
} |
80 |
|
81 |
function execute () { |
82 |
global $SERVER_NAME; |
83 |
$numargs = func_num_args(); |
84 |
$arg_list = func_get_args(); |
85 |
|
86 |
$parms = array(); |
87 |
|
88 |
for ($i = 0; $i < $numargs; $i++) { |
89 |
if (is_array($arg_list[$i])) { |
90 |
while (list($dummy,$parm) = each ($arg_list[$i])) { |
91 |
array_push($parms, $parm); |
92 |
} |
93 |
} else { |
94 |
array_push($parms,$arg_list[$i]); |
95 |
} |
96 |
} |
97 |
|
98 |
if ($this->debug) { // Log the query |
99 |
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file"); |
100 |
fputs($fd, date("M d H:i:s",time()).": ".$query."\nParameters: ".join(',',$parms)."\n==================\n"); |
101 |
fclose($fd); |
102 |
} |
103 |
|
104 |
if (!odbc_execute($this->statement, $parms)) { |
105 |
print "<br><b>Could not execute SQL query: \"".$query."\"</b><br>"; |
106 |
exit; |
107 |
} |
108 |
} |
109 |
|
110 |
function fetchrow_array () { |
111 |
$res = array(); |
112 |
odbc_fetch_into($this->statement, $res); |
113 |
return $res; |
114 |
} |
115 |
|
116 |
function fetchrow_hash () { |
117 |
$res = array(); |
118 |
odbc_fetch_into($this->statement, $res); |
119 |
$res2 = array(); |
120 |
for ($i = 0; $i < sizeof($res); ++$i) { |
121 |
$res2[odbc_field_name($this->statement, $i + 1)] = $res[$i]; |
122 |
} |
123 |
return $res2; |
124 |
} |
125 |
|
126 |
function rows () { |
127 |
return odbc_num_rows ($this->statement); |
128 |
} |
129 |
|
130 |
function finish () { |
131 |
odbc_free_result($this->statement); |
132 |
} |
133 |
} |
134 |
?> |