OSDN Git Service

MERGE: リビジョン1873〜1893。skinnable-masterのマージ
[nucleus-jp/nucleus-next.git] / nucleus / libs / sql / sql.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2012 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) 2012 The Nucleus Group
16  * @version $Id$
17  */
18 /*
19  * sql_* wrappers for DB class access
20  */
21 if ( !class_exists('DB') )
22 {
23         include_once realpath(dirname(__FILE__)) . '/DB.php';
24 }
25
26 /**
27  * Connects to database server with arguments
28  * @deprecated
29  */
30 function sql_connect_args($host = 'localhost', $user = '', $password = '', $database = '', $new_link = FALSE)
31 {
32         global $MYSQL_HANDLER;
33         return DB::setConnectionInfo($MYSQL_HANDLER[1], $host, $user, $password, $database);
34 }
35
36 /**
37  * Connects to database server
38  * @deprecated
39  */
40 function sql_connect()
41 {
42         global $MYSQL_HANDLER, $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE;
43
44         return DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE);
45 }
46
47 /**
48  * disconnects from SQL server
49  * @deprecated
50  */
51 function sql_disconnect($conn = false)
52 {
53         DB::disConnect();
54 }
55
56 /**
57  * disconnects from SQL server
58  * @deprecated
59  * @param unknown_type $conn
60  */
61 function sql_close($conn = false)
62 {
63         DB::disConnect();
64 }
65
66 /**
67  * executes an SQL query
68  * @deprecated
69  */
70 function sql_query($query, $conn = false)
71 {
72         global $mysql_affected_row;
73         $mysql_affected_row = -1;
74         if ( preg_match('/^\W*(INSERT|UPDATE|DELETE|DROP)/i', $query) )
75         {
76                 $mysql_affected_row = DB::execute($query);
77                 return $mysql_affected_row !== FALSE ? TRUE : FALSE;
78         }
79         return DB::getResult($query);
80 }
81
82 /**
83  * executes an SQL error
84  * @deprecated
85  */
86 function sql_error($conn = false)
87 {
88         $err = DB::getError();
89         return $err[2];
90 }
91
92 /**
93  * executes an SQL db select
94  * @deprecated
95  */
96 function sql_select_db($db, $conn = false)
97 {
98         sql_disconnect();
99         return sql_connect();
100 }
101
102 /**
103  * executes an SQL real escape
104  * @deprecated
105  */
106 function sql_real_escape_string($val, $conn = false)
107 {
108         if ( is_numeric($val) )
109         {
110                 return $val;
111         }
112         $escapeval = DB::quoteValue($val);
113         if ( preg_match("/^'(.*)'$/", $escapeval, $matches) )
114         {
115                 $escapeval = $matches[1];
116         }
117         return $escapeval;
118 }
119
120 /**
121  * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
122  * @deprecated
123  */
124 function sql_quote_string($val, $conn = false)
125 {
126         if ( is_numeric($val) )
127         {
128                 return $val;
129         }
130         return DB::quoteValue($val);
131 }
132
133 /**
134  * executes an SQL insert id
135  * @deprecated
136  */
137 function sql_insert_id($conn = false)
138 {
139         return DB::getInsertId();
140 }
141
142 /**
143  * executes an SQL result request
144  * @deprecated
145  */
146 function sql_result($res, $row, $col)
147 {
148         if ( !method_exists($res, 'fetch') ) return FALSE;
149         for ( $i = 0; $i < $row; $i++ )
150                 $res->fetch();
151         return $res->fetchColumn($col);
152 }
153
154 /**
155  * frees sql result resources
156  * @deprecated
157  */
158 function sql_free_result($res)
159 {
160         if ( !method_exists($res, 'closeCursor') ) return FALSE;
161         return $res->closeCursor();
162 }
163
164 /**
165  * returns number of rows in SQL result
166  * @deprecated
167  */
168 function sql_num_rows($res)
169 {
170         if ( !method_exists($res, 'rowCount') ) return FALSE;
171         return $res->rowCount();
172 }
173
174 /**
175  * returns number of rows affected by SQL query
176  * @deprecated
177  */
178 function sql_affected_rows($conn = false)
179 {
180         global $mysql_affected_row;
181         return $mysql_affected_row;
182 }
183
184 /**
185  * Get number of fields in result
186  * @deprecated
187  */
188 function sql_num_fields($res)
189 {
190         if ( !method_exists($res, 'columnCount') ) return FALSE;
191         return $res->columnCount();
192 }
193
194 /**
195  * fetches next row of SQL result as an associative array
196  * @deprecated
197  */
198 function sql_fetch_assoc($res)
199 {
200         if ( !method_exists($res, 'fetch') ) return FALSE;
201         return $res->fetch(PDO::FETCH_ASSOC);
202 }
203
204 /**
205  * Fetch a result row as an associative array, a numeric array, or both
206  * @deprecated
207  */
208 function sql_fetch_array($res)
209 {
210         if ( !method_exists($res, 'fetch') ) return FALSE;
211         return $res->fetch(PDO::FETCH_BOTH);
212 }
213
214 /**
215  * fetches next row of SQL result as an object
216  * @deprecated
217  */
218 function sql_fetch_object($res)
219 {
220         if ( !method_exists($res, 'fetch') ) return FALSE;
221         return $res->fetch(PDO::FETCH_OBJ);
222 }
223
224 /**
225  * Get a result row as an enumerated array
226  * @deprecated
227  */
228 function sql_fetch_row($res)
229 {
230         if ( !method_exists($res, 'fetch') ) return FALSE;
231         return $res->fetch(PDO::FETCH_NUM);
232 }
233
234 /**
235  * Get column information from a result and return as an object
236  * @deprecated
237  */
238 function sql_fetch_field($res, $offset = 0)
239 {
240         return FALSE;
241 }
242
243 /**
244  * Get current system status (returns string)
245  * @deprecated
246  */
247 function sql_stat($conn = false)
248 {
249         return FALSE;
250 }
251
252 /**
253  * Returns the name of the character set
254  * @deprecated
255  */
256 function sql_client_encoding($conn = false)
257 {
258         return FALSE;
259 }
260
261 /**
262  * Get SQL client version
263  * @deprecated
264  */
265 function sql_get_client_info()
266 {
267         return DB::getAttribute(PDO::ATTR_CLIENT_VERSION);
268 }
269
270 /**
271  * Get SQL server version
272  * @deprecated
273  */
274 function sql_get_server_info($conn = false)
275 {
276         return DB::getAttribute(PDO::ATTR_SERVER_VERSION);
277 }
278
279 /**
280  * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
281  * @deprecated
282  */
283 function sql_get_host_info($conn = false)
284 {
285         return FALSE;
286 }
287
288 /**
289  * Returns the SQL protocol on success, or FALSE on failure.
290  * @deprecated
291  */
292 function sql_get_proto_info($conn = false)
293 {
294         return FALSE;
295 }
296
297 /**
298  * Get the name of the specified field in a result
299  * @deprecated
300  */
301 function sql_field_name($res, $offset = 0)
302 {
303         if ( !method_exists($res, 'getColumnMeta') ) return FALSE;
304         $column = $res->getColumnMeta($offset);
305         return $column['name'];
306 }
307
308 /**
309  * Set character encodings in each fields related to MySQL connection.
310  * @deprecated
311  */
312 function sql_set_charset($charset)
313 {
314         return TRUE;
315 }