/[corp_html]/mnogo/db_func.inc
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 /mnogo/db_func.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Wed Mar 27 16:00:18 2002 UTC (17 years, 1 month ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
FILE REMOVED
*** empty log message ***

1 <?
2
3 /* mnoGoSearch-db_func-php v.1.0
4 * free convenient API to database backends
5 * (C) 1999,2000 by Sergey Kartashoff <gluke@biosys.net>
6 * oracle7 support by Roman V. Petrov (poma@mail.ru)
7 *
8 * supports: MySQL , Oracle (both v7 and v8) and PostgreSQL
9 * also supports connection through ODBC interface
10 */
11
12 // API:
13 // ----
14 // db_init($dbtype,$dbname,$dbhost,
15 // $dbuser,$dbpass,$dbport,
16 // $dbodbc,$dbodbc_cursor_type) ; return nothing dbodbc can be 0 or 1
17 // db_numrows($res) ; return number of rown in result
18 // db_query($query) ; return handle to result or false if error
19 // db_fetchrow($res) ; return array with fetched row or false if error or end of rows
20 // db_dataseek($res,$from) ; return true if success or false if error
21 // db_freeresult($res) ; return nothing
22 // db_error() ; return database error string
23
24 // old function calls:
25 // -------------------
26 // initdb()
27 // query($query)
28 // fetch_row($res)
29 // db_data_seek($res,$from)
30 // free_result($res)
31
32 // -----------------------------------------------
33 // initdb() - OLD
34 // -----------------------------------------------
35 function initdb(){
36 global $dbtype,$dbname,$dbhost,$dbuser,$dbpass,$dbport;
37 global $dbodbc,$dbodbc_cursor_type;
38 db_init($dbtype,$dbname,$dbhost,$dbuser,$dbpass,$dbport,$dbodbc,$dbodbc_cursor_type);
39 }
40
41 // -----------------------------------------------
42 // query($query)
43 // -----------------------------------------------
44 function query($query) {
45 return db_query($query);
46 }
47
48 // -----------------------------------------------
49 // fetch_row($res)
50 // -----------------------------------------------
51 function fetch_row($res) {
52 return db_fetchrow($res);
53 }
54
55 // -----------------------------------------------
56 // db_data_seek($res,$from)
57 // -----------------------------------------------
58 function db_data_seek($res,$from) {
59 return db_dataseek($res,$from);
60 }
61
62 // -----------------------------------------------
63 // free_result($res)
64 // -----------------------------------------------
65 function free_result($res) {
66 db_freeresult($res);
67 }
68
69 // -----------------------------------------------
70 // db_init($dbtype,$dbname,$dbhost,$dbuser,$dbpass,$dbport)
71 // -----------------------------------------------
72 function db_init($dbtype,$dbname,$dbhost,$dbuser,$dbpass,$dbport,$dbodbc,$dbodbc_cursor_type){
73 global $dbconn;
74 global $cur_row_array;
75 global $num_rows_array;
76
77 if ($dbodbc) {
78 if ($dbodbc_cursor_type) {
79 if (! $dbconn=odbc_pconnect($dbname,$dbuser,$dbpass,$dbodbc_cursor_type))
80 print_db_error("Cannot connect to odbc datasource $dbname as user $dbuser");
81 } else {
82 if (! $dbconn=odbc_pconnect($dbname,$dbuser,$dbpass))
83 print_db_error("Cannot connect to odbc datasource $dbname as user $dbuser");
84 }
85 } else {
86 switch($dbtype) {
87 case 'mysql':
88 if (($dbhost == '') || ($dbhost == '_socket')) {
89 if (!mysql_pconnect('',$dbuser,$dbpass))
90 print_db_error("Cannot connect to local mysql socket as user $dbuser");
91 } else {
92 if ($dbport == '') {
93 if (!mysql_pconnect($dbhost,$dbuser,$dbpass))
94 print_db_error("Cannot connect to mysql host $dbhost as user $dbuser");
95 } else {
96 if (!mysql_pconnect("$dbhost:$dbport",$dbuser,$dbpass))
97 print_db_error("Cannot connect to mysql host $dbhost:$dbport as user $dbuser");
98 }
99 }
100 if (! mysql_select_db($dbname))
101 print_db_error("DB $dbname select error");
102
103 break;
104 case 'oracle':
105 case 'oracle8':
106 if (! $dbconn=OCIPLogon($dbuser,$dbpass,$dbname))
107 print_db_error("Cannot connect to oracle db $dbname as user $dbuser");
108
109 break;
110 case 'oracle7':
111 if (! $dbconn=ora_plogon("$dbuser/$dbpass@$dbname",""))
112 print_db_error("Cannot connect to oracle db $dbname as user $dbuser");
113 if (! $dbconn=ora_open($dbconn))
114 print_db_error("Cannot connect to oracle db $dbname as user $dbuser");
115 break;
116 case 'pgsql':
117 if (($dbhost == '') || ($dbhost == '_socket')) {
118 if (! $dbconn=pg_pconnect("dbname=$dbname user=$dbuser password=$dbpass"))
119 print_db_error("Cannot connect to local pgsql socket as user $dbuser");
120 } else {
121 if ($dbport == '') {
122 if (! $dbconn=pg_pconnect("dbname=$dbname host=$dbhost user=$dbuser password=$dbpass"))
123 print_db_error("Cannot connect to pgsql host $dbhost as user $dbuser");
124 } else {
125 if (! $dbconn=pg_pconnect("dbname=$dbname host=$dbhost port=$dbport user=$dbuser password=$dbpass"))
126 print_db_error("Cannot connect to pgsql host $dbhost:$dbport as user $dbuser");
127 }
128 }
129
130 $cur_row_array = array();
131 $num_rows_array = array();
132
133 break;
134 default:
135 print_db_error("Unknown DBType: $dbtype");
136 }
137 }
138 }
139
140 // -----------------------------------------------
141 // db_numrows($res)
142 // -----------------------------------------------
143 function db_numrows($res) {
144 global $dbtype;
145 global $dbodbc;
146
147 if ($dbodbc) {
148 $rows=odbc_num_rows($res);
149 } else {
150 switch($dbtype) {
151 case 'mysql':
152 $rows=mysql_numrows($res);
153 break;
154 case 'pgsql':
155 $rows=pg_numrows($res);
156 break;
157 default:
158 print_db_error("Unknown DBType: $dbtype");
159 }
160 }
161 return($rows);
162 }
163
164 // -----------------------------------------------
165 // db_query($query)
166 // -----------------------------------------------
167 function db_query($query) {
168 global $dbtype;
169 global $dbodbc;
170 global $dbconn;
171 global $cur_row_array;
172 global $num_rows_array;
173
174 if ($dbodbc) {
175 $res=odbc_exec($dbconn,$query);
176 } else {
177 switch($dbtype) {
178 case 'mysql':
179 $res=mysql_query($query);
180 break;
181 case 'oracle':
182 case 'oracle8':
183 if (! $pres=OCIPArse($dbconn,$query)) return $pres;
184 $res=OCIExecute($pres);
185 if ($res) return ($pres);
186 break;
187 case 'oracle7':
188 if (! $pres=ora_parse($dbconn, $query)) return $pres;
189 $res=ora_exec($dbconn);
190 if ($res) return ($pres);
191 break;
192 case 'pgsql':
193 $res=pg_exec($dbconn,$query);
194 $cur_row_array[$res]=0;
195 $num_rows_array[$res]=db_numrows($res);
196 break;
197 default:
198 print_db_error("Unknown DBType: $dbtype");
199 }
200 }
201 return($res);
202 }
203
204 // -----------------------------------------------
205 // db_fetchrow($res)
206 // -----------------------------------------------
207 function db_fetchrow($res) {
208 global $dbtype;
209 global $dbodbc;
210 global $dbconn;
211 global $cur_row_array;
212 global $num_rows_array;
213
214 if ($dbodbc) {
215 if (! odbc_fetch_into($res,&$row)) $row=array();
216 } else {
217 switch($dbtype) {
218 case 'mysql':
219 $row=mysql_fetch_row($res);
220 break;
221 case 'oracle':
222 case 'oracle8':
223 $result=OCIFetchInto($res,&$row,OCI_NUM+OCI_RETURN_NULLS );
224 if (! $result) $row=array();
225 break;
226 case 'oracle7':
227 $result=ora_fetch($dbconn);
228 $errno=ora_errorcode($dbconn);
229 if(1403 == $errno || !$result)
230 { // 1043 means no more records found
231 return array();
232 }
233
234 for($ix=0;$ix<ora_numcols($dbconn);$ix++)
235 {
236 $col=strtolower(ora_columnname($dbconn,$ix));
237 $value=ora_getcolumn($dbconn,$ix);
238 $row[ "$col" ] = $value;
239 $row[ $ix ] = $value;
240 }
241 break;
242 case 'pgsql':
243 if ($cur_row_array[$res] >= $num_rows_array[$res]) {
244 $row=array();
245 } else {
246 $row=pg_fetch_row($res,$cur_row_array[$res]);
247 $cur_row_array[$res]++;
248 }
249 break;
250 default:
251 print_db_error("Unknown DBType: $dbtype");
252 }
253 }
254 return($row);
255 }
256
257 // -----------------------------------------------
258 // db_dataseek($res,$from)
259 // -----------------------------------------------
260 function db_dataseek($res,$from) {
261 global $dbtype;
262 global $dbodbc;
263 global $cur_row_array;
264
265 if ($dbodbc) {
266 $dataseek=1;
267 for ($i=0; $i<$from; $i++) {
268 $row=db_fetchrow($res);
269 if (! $row) {
270 $dataseek=0;
271 break;
272 }
273 }
274 } else {
275 switch($dbtype) {
276 case 'mysql':
277 $dataseek=mysql_data_seek($res,$from);
278 break;
279 case 'oracle':
280 case 'oracle8':
281 case 'oracle7':
282 $dataseek=1;
283 for ($i=0; $i<$from; $i++) {
284 $row=db_fetchrow($res);
285 if (! $row) {
286 $dataseek=0;
287 break;
288 }
289 }
290 break;
291 case 'pgsql':
292 $dataseek=1;
293 $cur_row_array[$res]=$from;
294 break;
295 default:
296 print_db_error("Unknown DBType: $dbtype");
297 }
298 }
299 return($dataseek);
300 }
301
302 // -----------------------------------------------
303 // db_freeresult($res)
304 // -----------------------------------------------
305 function db_freeresult($res) {
306 global $dbtype;
307 global $dbodbc;
308
309 if ($dbodbc) {
310 odbc_free_result($res);
311 } else {
312 switch($dbtype) {
313 case 'mysql':
314 mysql_free_result($res);
315 break;
316 case 'oracle':
317 case 'oracle8':
318 OCIFreeStatement($res);
319 break;
320 case 'oracle7':
321 break;
322 case 'pgsql':
323 pg_freeresult($res);
324 break;
325 default:
326 print_db_error("Unknown DBType: $dbtype");
327 }
328 }
329 }
330
331 // -----------------------------------------------
332 // db_error()
333 // -----------------------------------------------
334 function db_error() {
335 global $dbtype;
336 global $dbodbc;
337 global $dbconn;
338
339 if ($dbodbc) {
340 $error='ODBC error occured';
341 } else {
342 switch($dbtype) {
343 case 'mysql':
344 $error=mysql_error();
345 break;
346 case 'oracle':
347 case 'oracle8':
348 $error=OCIError();
349 break;
350 case 'oracle7':
351 $error=Ora_Error($dbconn);
352 break;
353 case 'pgsql':
354 $error=pg_errormessage();
355 break;
356 default:
357 print_db_error("Unknown DBType: $dbtype");
358 }
359 }
360 return $error;
361 }
362
363 // -----------------------------------------------
364 // print_db_error($str)
365 // -----------------------------------------------
366 function print_db_error($str){
367 print "$str\n";
368 exit;
369 }
370
371 ?>

  ViewVC Help
Powered by ViewVC 1.1.26