OSDN Git Service

06fe2e348e80c394577f4cf8f82d8699f1c54189
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / sql / pdo.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2007 The Nucleus Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  */
13 /**
14  * @license http://nucleuscms.org/license.txt GNU General Public License
15  * @copyright Copyright (C) 2002-2007 The Nucleus Group
16  * @version $Id: mysql.php 1279 2008-10-23 08:18:26Z shizuki $
17  */
18  
19 /*
20  * complete sql_* wrappers for mysql functions
21  *
22  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
23  */
24  
25
26 $MYSQL_CONN = 0;
27 global $SQL_DBH;
28 $SQL_DBH = NULL;
29
30 if (!function_exists('sql_fetch_assoc'))
31 {
32         /**
33          * Errors before the database connection has been made
34          */
35         function startUpError($msg, $title) {
36                 ?>
37                 <html xmlns="http://www.w3.org/1999/xhtml">
38                         <head><title><?php echo htmlspecialchars($title)?></title></head>
39                         <body>
40                                 <h1><?php echo htmlspecialchars($title)?></h1>
41                                 <?php echo $msg?>
42                         </body>
43                 </html>
44                 <?php   exit;
45         }
46         
47         /**
48           * Connects to mysql server
49           */
50         function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
51                 global $MYSQL_HANDLER;
52                 
53                 try {
54                         if (strpos($mysql_host,':') === false) {
55                                 $host = $mysql_host;
56                                 $port = '';
57                         }
58                         else {
59                                 list($host,$port) = explode(":",$mysql_host);
60                                 if (isset($port)) 
61                                         $port = ';port='.trim($port);
62                                 else
63                                         $port = '';
64                         }
65                         
66                         $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
67                                                 
68                 } catch (PDOException $e) {
69                         $DBH =NULL;
70                         startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
71                 }
72 //echo '<hr />DBH: '.print_r($DBH,true).'<hr />';               
73                 return $DBH;
74         }
75         
76         /**
77           * Connects to mysql server
78           */
79         function sql_connect() {
80                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
81                 $SQL_DBH = NULL;
82                 try {
83                         if (strpos($MYSQL_HOST,':') === false) {
84                                 $host = $MYSQL_HOST;
85                                 $port = '';
86                         }
87                         else {
88                                 list($host,$port) = explode(":",$MYSQL_HOST);
89                                 if (isset($port)) 
90                                         $port = ';port='.trim($port);
91                                 else
92                                         $port = '';
93                         }
94                         
95                         $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
96 /*/ <add for garble measure>
97                         if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
98                                 $resource = $SQL_DBH->query("show variables LIKE 'character_set_database'");
99                                 $resource->bindColumn('Value', $charset);
100                                 $resource->fetchAll();
101                                 $SQL_DBH->exec("SET CHARACTER SET " . $charset);
102                         }
103 // </add for garble measure>*/
104                 } catch (PDOException $e) {
105                         $SQL_DBH = NULL;
106                         startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
107                 }
108 //              echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';             
109                 $MYSQL_CONN &= $SQL_DBH;
110                 return $SQL_DBH;
111
112         }
113
114         /**
115           * disconnects from SQL server
116           */
117         function sql_disconnect(&$dbh=NULL) {
118                 global $SQL_DBH;
119                 if (is_null($dbh)) $SQL_DBH = NULL;
120                 else $dbh = NULL;
121         }
122         
123         function sql_close(&$dbh=NULL) {
124                 global $SQL_DBH;
125                 if (is_null($dbh)) $SQL_DBH = NULL;
126                 else $dbh = NULL;
127         }
128         
129         /**
130           * executes an SQL query
131           */
132         function sql_query($query,$dbh=NULL) {
133                 global $SQLCount,$SQL_DBH;
134                 $SQLCount++;
135 //echo '<hr />SQL_DBH: ';
136 //print_r($SQL_DBH);
137 //echo '<hr />DBH: ';
138 //print_r($dbh);
139 //echo '<hr />';
140 //echo $query.'<hr />';
141                 if (is_null($dbh)) $res = $SQL_DBH->query($query);
142                 else $res = $dbh->query($query);
143                 if ($res->errorCode() != '00000') {
144                         $errors = $res->errorInfo();
145                         print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
146                 }
147                 
148                 return $res;
149         }
150         
151         /**
152           * executes an SQL error
153           */
154         function sql_error($dbh=NULL)
155         {
156                 global $SQL_DBH;
157                 if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
158                 else $error = $dbh->errorInfo();
159                 if ($error[0] != '00000') {
160                         return $error[0].'-'.$error[1].' '.$error[2];
161                 }
162                 else return '';
163         }
164         
165         /**
166           * executes an SQL db select
167           */
168         function sql_select_db($db,&$dbh=NULL)
169         {
170                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
171 //echo '<hr />'.print_r($dbh,true).'<hr />';
172 //exit;
173                 if (is_null($dbh)) { 
174                         try {
175                                 $SQL_DBH = NULL;
176                                 list($host,$port) = explode(":",$MYSQL_HOST);
177                                 if (isset($port)) 
178                                         $port = ';port='.trim($port);
179                                 else
180                                         $port = '';
181                                 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
182                                 return 1;
183                         } catch (PDOException $e) {
184                                 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
185                                 return 0;
186                         }
187                 }
188                 else {
189                         if ($dbh->exec("USE $db") !== false) return 1;
190                         else return 0;
191                 }
192         }
193         
194         /**
195           * executes an SQL real escape 
196           */
197         function sql_real_escape_string($val,$dbh=NULL)
198         {
199                 return addslashes($val);
200         }
201         
202         /**
203           * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed 
204           */
205         function sql_quote_string($val,$dbh=NULL) {
206                 global $SQL_DBH;
207                 if (is_null($dbh))
208                         return $SQL_DBH->quote($val);
209                 else
210                         return $dbh->quote($val);
211         }
212         
213         /**
214           * executes an SQL insert id
215           */
216         function sql_insert_id($dbh=NULL)
217         {       
218                 global $SQL_DBH;
219                 if (is_null($dbh))
220                         return $SQL_DBH->lastInsertId();
221                 else
222                         return $dbh->lastInsertId();
223         }
224         
225         /**
226           * executes an SQL result request
227           */
228         function sql_result($res, $row = 0, $col = 0)
229         {
230                 $results = array();
231                 if (intval($row) < 1) {
232                         $results = $res->fetch(PDO::FETCH_BOTH);
233                         return $results[$col];
234                 }
235                 else {
236                         for ($i = 0; $i < intval($row); $i++) {
237                                 $results = $res->fetch(PDO::FETCH_BOTH);
238                         }
239                         $results = $res->fetch(PDO::FETCH_BOTH);
240                         return $results[$col];
241                 }
242         }
243         
244         /**
245           * frees sql result resources
246           */
247         function sql_free_result($res)
248         {
249                 $res = NULL;
250                 return true;
251         }
252         
253         /**
254           * returns number of rows in SQL result
255           */
256         function sql_num_rows($res)
257         {
258                 return $res->rowCount();
259         }
260         
261         /**
262           * returns number of rows affected by SQL query
263           */
264         function sql_affected_rows($res)
265         {
266                 return $res->rowCount();
267         }
268         
269         /**
270           * Get number of fields in result
271           */
272         function sql_num_fields($res)
273         {
274                 return $res->columnCount();
275         }
276         
277         /**
278           * fetches next row of SQL result as an associative array
279           */
280         function sql_fetch_assoc($res)
281         {
282                 $results = array();
283                 $results = $res->fetch(PDO::FETCH_ASSOC);       
284                 return $results;
285         }
286         
287         /**
288           * Fetch a result row as an associative array, a numeric array, or both
289           */
290         function sql_fetch_array($res)
291         {
292                 $results = array();
293                 $results = $res->fetch(PDO::FETCH_BOTH);
294                 return $results;
295         }
296         
297         /**
298           * fetches next row of SQL result as an object
299           */
300         function sql_fetch_object($res)
301         {
302                 $results = NULL;
303                 $results = $res->fetchObject(); 
304                 return $results;
305         }
306         
307         /**
308           * Get a result row as an enumerated array
309           */
310         function sql_fetch_row($res)
311         {
312                 $results = array();
313                 $results = $res->fetch(PDO::FETCH_NUM); 
314                 return $results;
315         }
316         
317         /**
318           * Get column information from a result and return as an object
319           */
320         function sql_fetch_field($res,$offset = 0)
321         {
322                 $results = array();
323                 $obj = NULL;
324                 $results = $res->getColumnMeta($offset);
325                 foreach($results as $key=>$value) {
326                         $obj->$key = $value;
327                 }
328                 return $obj;
329         }
330         
331         /**
332           * Get current system status (returns string)
333           */
334         function sql_stat($dbh=NULL)
335         {
336                 //not implemented
337                 global $SQL_DBH;
338                 if (is_null($dbh))
339                         return '';
340                 else
341                 return '';
342         }
343         
344         /**
345           * Returns the name of the character set
346           */
347         function sql_client_encoding($dbh=NULL)
348         {
349                 //not implemented
350                 global $SQL_DBH;
351                 if (is_null($dbh))
352                         return '';
353                 else
354                         return '';
355         }
356         
357         /**
358           * Get SQL client version
359           */
360         function sql_get_client_info()
361         {
362                 global $SQL_DBH;
363                 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION"));
364         }
365         
366         /**
367           * Get SQL server version
368           */
369         function sql_get_server_info($dbh=NULL)
370         {
371                 global $SQL_DBH;
372                 if (is_null($dbh))
373                         return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
374                 else
375                         return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
376         }
377         
378         /**
379           * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
380           */
381         function sql_get_host_info($dbh=NULL)
382         {
383                 global $SQL_DBH;
384                 if (is_null($dbh))
385                         return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
386                 else
387                         return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
388         }
389         
390         /**
391           * Returns the SQL protocol on success, or FALSE on failure. 
392           */
393         function sql_get_proto_info($dbh=NULL)
394         {
395                 //not implemented
396                 global $SQL_DBH;
397                 if (is_null($dbh))
398                         return false;
399                 else
400                         return false;
401                 return '';
402         }
403
404 }
405
406 /**************************************************************************
407         Unimplemented mysql_* functions
408         
409 # mysql_ change_ user
410 # mysql_ create_ db
411 # mysql_ data_ seek
412 # mysql_ db_ name
413 # mysql_ db_ query
414 # mysql_ drop_ db
415 # mysql_ errno
416 # mysql_ escape_ string
417 # mysql_ fetch_ lengths
418 # mysql_ field_ flags
419 # mysql_ field_ len
420 # mysql_ field_ name
421 # mysql_ field_ seek
422 # mysql_ field_ table
423 # mysql_ field_ type
424 # mysql_ info
425 # mysql_ list_ dbs
426 # mysql_ list_ fields
427 # mysql_ list_ processes
428 # mysql_ list_ tables
429 # mysql_ pconnect
430 # mysql_ ping
431 # mysql_ set_ charset
432 # mysql_ tablename
433 # mysql_ thread_ id
434 # mysql_ unbuffered_ query
435 *******************************************************************/
436
437
438
439 ?>