/[docman]/dbi/dbi_pgsql.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /dbi/dbi_pgsql.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Wed Jun 20 10:30:01 2001 UTC (22 years, 9 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +2 -2 lines
bugfix

1 <?
2 /*
3 * $Id: dbi_pgsql.php,v 1.13 2001/04/04 09:30:15 erwin 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
24 class DBI {
25 var $session;
26 var $autocommit = 1;
27 var $debug = 1;
28 var $transaction = 0;
29
30 var $errstr="";
31 var $RaiseError = 1;
32 var $Driver_name = "pgsql";
33
34 function DBI ($db, $user, $password) {
35 $this->session = pg_connect("dbname=$db user=$user password=$password");
36 }
37
38 function prepare ($query) {
39 if (!$this->autocommit && !$this->transaction) {
40 $this->transaction = 1;
41 pg_exec("BEGIN");
42 }
43 return new STH($this, $this->session, $query, $this->autocommit, $this->debug);
44 }
45
46 function autocommit ($value) {
47 if ($this->transaction && $value) {
48 pg_exec("COMMIT");
49 $this->transaction = 0;
50 } elseif ($value) {
51 $this->transaction = 0;
52 }
53 $this->autocommit = $value;
54 }
55
56 function commit () {
57 return pg_exec("COMMIT");
58 $this->transaction = 0;
59 }
60
61 function rollback () {
62 return pg_exec("ROLLBACK");
63 $this->transaction = 0;
64 }
65
66 function quote ($str) {
67 if (get_magic_quotes_gpc()) {
68 $str = stripslashes($str);
69 }
70 return "'".AddSlashes($str)."'";
71 }
72
73 function insert_id ($sequence) {
74 $sth = new STH($this, $this->session, "SELECT currval('$sequence')", $this->mode, $this->debug);
75 $sth->execute();
76 list($res) = $sth->fetchrow_array();
77 return $res;
78 }
79 }
80
81 class STH {
82 var $query;
83 var $res;
84 var $autocommit;
85 var $debug;
86 var $row;
87 var $session;
88 var $placeholders;
89 var $dbi;
90
91 function STH (&$dbi, $session, $query, $autocommit, $debug) {
92 $this->dbi = &$dbi;
93 $this->query = $query;
94 $this->autocommit = $autocommit;
95 $this->debug = $debug;
96 $this->session = $session;
97
98 // Scan for placeholders
99
100 $this->placeholders = array();
101 $quote = '';
102 for ($i = 0; $i < strlen($query); ++$i) {
103 if ($query[$i] == "'") {
104 if (empty($quote)) {
105 $quote = "'";
106 } elseif ($quote == "'") {
107 $quote = '';
108 }
109 } elseif ($query[$i] == '"') {
110 if (empty($quote)) {
111 $quote = '"';
112 } elseif ($quote == '"') {
113 $quote = '';
114 }
115 } elseif ($query[$i] == '?') {
116 if (empty($quote)) {
117 array_push($this->placeholders, $i);
118 }
119 }
120 }
121 }
122
123 function execute () {
124 global $SERVER_NAME;
125 global $SCRIPT_FILENAME;
126
127 $numargs = func_num_args();
128 $arg_list = func_get_args();
129
130 $parms = array();
131
132 for ($i = 0; $i < $numargs; $i++) {
133 if (is_array($arg_list[$i])) {
134 while (list($dummy,$parm) = each ($arg_list[$i])) {
135 array_push($parms, $parm);
136 }
137 } else {
138 array_push($parms,$arg_list[$i]);
139 }
140 }
141
142 if (sizeof($parms) != sizeof($this->placeholders)) {
143 trigger_error("<br><b>SQL Query (".$this->query.") contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed</b> on page $SCRIPT_FILENAME<br>", E_USER_ERROR);
144 }
145
146 if (sizeof($parms) > 0) {
147 $query = substr($this->query, 0, $this->placeholders[0]);
148 for ($i = 0; $i < sizeof($parms) - 1; ++$i) {
149 $query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1);
150 }
151 $query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1);
152 } else {
153 $query = $this->query;
154 }
155
156 if ($this->debug) { // Log the query
157 $fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file");
158 fputs($fd, date("M d H:i:s",time()).": ".$query."\n==================\n");
159 fclose($fd);
160 }
161
162 $this->res = @pg_exec($this->session, $query);
163 if (!$this->res) {
164 if ($this->dbi->RaiseError) {
165 trigger_error("<br><b>Could not execute SQL query: \"".$query."\"</b><br><br>".pg_errormessage($this->session)."<br>", E_USER_ERROR);
166 }
167 $this->dbi->errstr = pg_errormessage($this->session);
168 }
169
170 $this->row = 0;
171 return $this->res;
172 }
173
174 function fetchrow_array () {
175 if($this->row<$this->rows())
176 return @pg_fetch_row($this->res, $this->row++);
177 }
178
179 function fetchrow_hash () {
180 if($this->row<$this->rows())
181 return @pg_fetch_array($this->res, $this->row++);
182 }
183
184 function finish () {
185 pg_freeresult($this->res);
186 }
187
188 function rows () {
189 return pg_numrows($this->res);
190 }
191 }
192 ?>

  ViewVC Help
Powered by ViewVC 1.1.26