4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2012 The Nucleus Group
\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
15 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
16 * @copyright Copyright (C) 2012 The Nucleus Group
\r
22 private static $dbh;
\r
23 private static $execCount = 0;
\r
24 private static $dateFormat = '\'%Y-%m-%d %H:%M:%S\'';
\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
36 public static function setConnectionInfo($engine, $host, $user, $password, $database)
\r
42 if ( i18n::strpos($host, ':') === false )
\r
48 list($host, $portnum) = i18n::explode(":", $host);
\r
49 if ( isset($portnum) )
\r
51 $portnum = trim($portnum);
\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
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
73 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
\r
74 self::$dbh = new PDO($engine . ':dbname=//' . $host . $port . '/' . $database, $user, $password);
\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
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
89 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
\r
90 self::$dbh = new PDO($engine . ':' . $database, $user, $password);
\r
93 self::$dbh->sqliteCreateFunction('SUBSTRING', 'substr', 3);
\r
94 self::$dbh->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);
\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
104 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));
\r
107 if ( !class_exists('MysqlPDO') )
\r
109 include_once realpath(dirname(__FILE__)) . '/MYSQLPDO.php';
\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
117 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));
\r
121 catch (PDOException $e)
\r
123 self::disConnect();
\r
131 * Disconnect the connection to the database.
\r
133 public static function disConnect()
\r
139 * DB::getExecCount()
\r
140 * To get the number of times you run the statement.
\r
141 * @return int Number of executions
\r
143 public static function getExecCount()
\r
145 return self::$execCount;
\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
155 public static function formatDateTime($timestamp = null, $offset=0)
\r
157 if ( $timestamp == null )
\r
159 $timestamp = time();
\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
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
174 public static function getValue($statement)
\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
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
192 public static function getRow($statement)
\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
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
206 public static function getResult($statement)
\r
208 if ( self::$dbh == null ) return FALSE;
\r
209 self::$execCount++;
\r
210 return self::callQuery($statement);
\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
219 public static function execute($statement)
\r
221 if ( self::$dbh == null ) return FALSE;
\r
222 self::$execCount++;
\r
223 return self::callExec($statement);
\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
232 private static function callQuery($statement)
\r
234 $result = self::$dbh->query($statement);
\r
235 if ( $result === FALSE )
\r
237 self::showErrorDisplay($statement);
\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
248 private static function callExec($statement)
\r
250 $result = self::$dbh->exec($statement);
\r
251 if ( $result === FALSE )
\r
253 self::showErrorDisplay($statement);
\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
263 private static function showErrorDisplay($statement)
\r
266 if ( array_key_exists('debug', $CONF) && $CONF['debug'] )
\r
268 $err = self::getError();
\r
269 print("mySQL error with query '{$statement}' : " . $err[2]);
\r
276 * Gets the error information associated with the last operation.
\r
277 * @return array Error info
\r
279 public static function getError()
\r
281 if ( self::$dbh == null ) return FALSE;
\r
282 return self::$dbh->errorInfo();
\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
291 public static function quoteValue($value)
\r
293 if ( self::$dbh == null ) return FALSE;
\r
294 return self::$dbh->quote($value);
\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
302 public static function getInsertId()
\r
304 if ( self::$dbh == null ) return FALSE;
\r
305 return self::$dbh->lastInsertId();
\r
309 * DB::getAttribute()
\r
310 * Gets the attribute of the database.
\r
311 * @return string Attribute
\r
313 public static function getAttribute($attribute)
\r
315 if ( self::$dbh == null ) return FALSE;
\r
316 return self::$dbh->getAttribute($attribute);
\r