OSDN Git Service

FIX:変数名の誤記を修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / sql / DB.php
1 <?php\r
2 \r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2012 The Nucleus Group\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version 2\r
10  * of the License, or (at your option) any later version.\r
11  * (see nucleus/documentation/index.html#license for more info)\r
12  */\r
13 \r
14 /**\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2012 The Nucleus Group\r
17  * @version $Id$\r
18  */\r
19 \r
20 class DB\r
21 {\r
22         private static $dbh;\r
23         private static $execCount = 0;\r
24         private static $dateFormat = '\'%Y-%m-%d %H:%M:%S\'';\r
25         \r
26         /**\r
27          * DB::setConnectionInfo()\r
28          * Set the information to connect to the database, it will attempt to connect.\r
29          * @param string $engine Engine\r
30          * @param string $host Host\r
31          * @param string $user User\r
32          * @param string $password Password\r
33          * @param string $database Database\r
34          * @return bool Returns TRUE if able to connect, otherwise it returns FALSE.\r
35          */\r
36         public static function setConnectionInfo($engine, $host, $user, $password, $database)\r
37         {\r
38                 self::disConnect();\r
39 \r
40                 try\r
41                 {\r
42                         if ( i18n::strpos($host, ':') === false )\r
43                         {\r
44                                 $portnum = '';\r
45                         }\r
46                         else\r
47                         {\r
48                                 list($host, $portnum) = i18n::explode(":", $host);\r
49                                 if ( isset($portnum) )\r
50                                 {\r
51                                         $portnum = trim($portnum);\r
52                                 }\r
53                                 else\r
54                                 {\r
55                                         $portnum = '';\r
56                                 }\r
57                         }\r
58 \r
59                         switch ( $engine )\r
60                         {\r
61                                 case 'sybase':\r
62                                 case 'dblib':\r
63                                         $port = is_numeric($portnum) ? ':' . intval($portnum) : '';\r
64                                         $db = $database ? ';dbname=' . $database : '';\r
65                                         self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);\r
66                                         break;\r
67                                 case 'mssql':\r
68                                         $port = is_numeric($portnum) ? ',' . intval($portnum) : '';\r
69                                         $db = $database ? ';dbname=' . $database : '';\r
70                                         self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);\r
71                                         break;\r
72                                 case 'oci':\r
73                                         $port = is_numeric($portnum) ? ':' . intval($portnum) : '';\r
74                                         self::$dbh = new PDO($engine . ':dbname=//' . $host . $port . '/' . $database, $user, $password);\r
75                                         break;\r
76                                 case 'odbc':\r
77                                         $port = is_numeric($portnum) ? ';PORT=' . intval($portnum) : '';\r
78                                         self::$dbh = new PDO(\r
79                                                 $engine . ':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME=' . $host . $port . ';DATABASE=' . $database . ';PROTOCOL=TCPIP;UID='\r
80                                                         . $user . ';PWD=' . $password);\r
81                                         break;\r
82                                 case 'pgsql':\r
83                                         $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';\r
84                                         $db = $database ? ';dbname=' . $database : '';\r
85                                         self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);\r
86                                         break;\r
87                                 case 'sqlite':\r
88                                 case 'sqlite2':\r
89                                         $port = is_numeric($portnum) ? ':' . intval($portnum) : '';\r
90                                         self::$dbh = new PDO($engine . ':' . $database, $user, $password);\r
91                                         if ( self::$dbh )\r
92                                         {\r
93                                                 self::$dbh->sqliteCreateFunction('SUBSTRING', 'substr', 3);\r
94                                                 self::$dbh->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);\r
95                                         }\r
96                                         break;\r
97                                 case 'mysql':\r
98                                         $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';\r
99                                         $db = $database ? ';dbname=' . $database : '';\r
100                                         self::$dbh = new PDO(\r
101                                                 'mysql' . ':host=' . $host . $port . $db,\r
102                                                 $user,\r
103                                                 $password,\r
104                                                 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));\r
105                                         break;\r
106                                 default: // mysql\r
107                                         if ( !class_exists('MysqlPDO') )\r
108                                         {\r
109                                                 include_once realpath(dirname(__FILE__)) . '/MYSQLPDO.php';\r
110                                         }\r
111                                         $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';\r
112                                         $db = $database ? ';dbname=' . $database : '';\r
113                                         self::$dbh = new MysqlPDO(\r
114                                                 'mysql' . ':host=' . $host . $port . $db,\r
115                                                 $user,\r
116                                                 $password,\r
117                                                 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));\r
118                                         break;\r
119                         }\r
120                 }\r
121                 catch (PDOException $e)\r
122                 {\r
123                         self::disConnect();\r
124                         return FALSE;\r
125                 }\r
126                 return TRUE;\r
127         }\r
128 \r
129         /**\r
130          * DB::disConnect()\r
131          * Disconnect the connection to the database.\r
132          */\r
133         public static function disConnect()\r
134         {\r
135                 self::$dbh = null;\r
136         }\r
137 \r
138         /**\r
139          * DB::getExecCount()\r
140          * To get the number of times you run the statement.\r
141          * @return int Number of executions\r
142          */\r
143         public static function getExecCount()\r
144         {\r
145                 return self::$execCount;\r
146         }\r
147 \r
148         /**\r
149          * DB::formatDateTime()\r
150          * The value converted to a format that can be passed to the database datetime.\r
151          * @param int $timestamp UNIX timestamp\r
152          * @param int $offset timestamp offset\r
153          * @return string formatted datetime (quart treated)\r
154          */\r
155         public static function formatDateTime($timestamp = null, $offset=0)\r
156         {\r
157                 if ( $timestamp == null )\r
158                 {\r
159                         $timestamp = time();\r
160                 }\r
161                 $timestamp += $offset;\r
162                 return preg_replace_callback('/(%[a-z%])/i',\r
163                         create_function('$matches', 'return strftime($matches[1], ' . intval($timestamp) . ');'),\r
164                         self::$dateFormat\r
165                 );\r
166         }\r
167         \r
168         /**\r
169          * DB::getValue()\r
170          * Gets the value of the first column of the first row of the results obtained in the statement.\r
171          * @param string $statement SQL Statement\r
172          * @return mixed Result value. If the call fails, it will return FALSE.\r
173          */\r
174         public static function getValue($statement)\r
175         {\r
176                 if ( self::$dbh == null ) return FALSE;\r
177                 self::$execCount++;\r
178                 $result = self::callQuery($statement);\r
179                 if ( $row = $result->fetch(PDO::FETCH_NUM) )\r
180                 {\r
181                         return $row[0];\r
182                 }\r
183                 return FALSE;\r
184         }\r
185 \r
186         /**\r
187          * DB::getRow()\r
188          * Gets the first row of the results obtained in the statement.\r
189          * @param string $statement SQL Statement\r
190          * @return array Result row. If the call fails, it will return FALSE.\r
191          */\r
192         public static function getRow($statement)\r
193         {\r
194                 if ( self::$dbh == null ) return FALSE;\r
195                 self::$execCount++;\r
196                 $result = self::callQuery($statement);\r
197                 return $result->fetch(PDO::FETCH_BOTH);\r
198         }\r
199 \r
200         /**\r
201          * DB::getResult()\r
202          * Gets the set of results obtained in the statement.\r
203          * @param string $statement SQL Statement\r
204          * @return PDOStatement Result set object. If the call fails, it will return FALSE.\r
205          */\r
206         public static function getResult($statement)\r
207         {\r
208                 if ( self::$dbh == null ) return FALSE;\r
209                 self::$execCount++;\r
210                 return self::callQuery($statement);\r
211         }\r
212 \r
213         /**\r
214          * DB::execute()\r
215          * Execute an SQL statement and return the number of affected rows.\r
216          * @param string $statement SQL Statement\r
217          * @return int number of rows that were modified or deleted by the SQL statement you issued. If the call fails, it will return FALSE.\r
218          */\r
219         public static function execute($statement)\r
220         {\r
221                 if ( self::$dbh == null ) return FALSE;\r
222                 self::$execCount++;\r
223                 return self::callExec($statement);\r
224         }\r
225 \r
226         /**\r
227          * DB::callQuery()\r
228          * Run the query to retrieve the result set.\r
229          * @param string $statement query to be executed\r
230          * @return PDOStatement Result set object. If the call fails, it will return FALSE.\r
231          */\r
232         private static function callQuery($statement)\r
233         {\r
234                 $result = self::$dbh->query($statement);\r
235                 if ( $result === FALSE )\r
236                 {\r
237                         self::showErrorDisplay($statement);\r
238                 }\r
239                 return $result;\r
240         }\r
241         \r
242         /**\r
243          * DB::callExec()\r
244          * Run the query and returns the number of rows affected.\r
245          * @param string $statement query to be executed\r
246          * @return int number of rows that were modified or deleted by the SQL statement you issued. If the call fails, it will return FALSE.\r
247          */\r
248         private static function callExec($statement)\r
249         {\r
250                 $result = self::$dbh->exec($statement);\r
251                 if ( $result === FALSE )\r
252                 {\r
253                         self::showErrorDisplay($statement);\r
254                 }\r
255                 return $result;\r
256         }\r
257         \r
258         /**\r
259          * DB::showErrorDisplay()\r
260          * The error message is output to the screen of the query.\r
261          * @param string $statement query output to the screen\r
262          */\r
263         private static function showErrorDisplay($statement)\r
264         {\r
265                 global $CONF;\r
266                 if ( array_key_exists('debug', $CONF) && $CONF['debug'] )\r
267                 {\r
268                         $err = self::getError();\r
269                         print("mySQL error with query '{$statement}' : " . $err[2]);\r
270                 }\r
271                 return;\r
272         }\r
273         \r
274         /**\r
275          * DB::getError()\r
276          * Gets the error information associated with the last operation.\r
277          * @return array Error info\r
278          */\r
279         public static function getError()\r
280         {\r
281                 if ( self::$dbh == null ) return FALSE;\r
282                 return self::$dbh->errorInfo();\r
283         }\r
284 \r
285         /**\r
286          * DB::quoteValue()\r
287          * Quotes a string for use in a query.\r
288          * @param string $value Value to quote\r
289          * @return string Quoted value\r
290          */\r
291         public static function quoteValue($value)\r
292         {\r
293                 if ( self::$dbh == null ) return FALSE;\r
294                 return self::$dbh->quote($value);\r
295         }\r
296 \r
297         /**\r
298          * DB::getInsertId()\r
299          * Get the value of the ID of the rows that are inserted at the end.\r
300          * @return string ID of the row\r
301          */\r
302         public static function getInsertId()\r
303         {\r
304                 if ( self::$dbh == null ) return FALSE;\r
305                 return self::$dbh->lastInsertId();\r
306         }\r
307 \r
308         /**\r
309          * DB::getAttribute()\r
310          * Gets the attribute of the database.\r
311          * @return string Attribute\r
312          */\r
313         public static function getAttribute($attribute)\r
314         {\r
315                 if ( self::$dbh == null ) return FALSE;\r
316                 return self::$dbh->getAttribute($attribute);\r
317         }\r
318         \r
319 }\r