4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2012 The Nucleus Group
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)
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2012 The Nucleus Group
23 private static $execCount = 0;
24 private static $dateFormat = '\'%Y-%m-%d %H:%M:%S\'';
27 * DB::setConnectionInfo()
28 * Set the information to connect to the database, it will attempt to connect.
29 * @param string $engine Engine
30 * @param string $host Host
31 * @param string $user User
32 * @param string $password Password
33 * @param string $database Database
34 * @return bool Returns TRUE if able to connect, otherwise it returns FALSE.
36 public static function setConnectionInfo($engine, $host, $user, $password, $database)
42 if ( i18n::strpos($host, ':') === false )
48 list($host, $portnum) = i18n::explode(":", $host);
49 if ( isset($portnum) )
51 $portnum = trim($portnum);
63 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
64 $db = $database ? ';dbname=' . $database : '';
65 self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);
68 $port = is_numeric($portnum) ? ',' . intval($portnum) : '';
69 $db = $database ? ';dbname=' . $database : '';
70 self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);
73 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
74 self::$dbh = new PDO($engine . ':dbname=//' . $host . $port . '/' . $database, $user, $password);
77 $port = is_numeric($portnum) ? ';PORT=' . intval($portnum) : '';
79 $engine . ':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME=' . $host . $port . ';DATABASE=' . $database . ';PROTOCOL=TCPIP;UID='
80 . $user . ';PWD=' . $password);
83 $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';
84 $db = $database ? ';dbname=' . $database : '';
85 self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);
89 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
90 self::$dbh = new PDO($engine . ':' . $database, $user, $password);
93 self::$dbh->sqliteCreateFunction('SUBSTRING', 'substr', 3);
94 self::$dbh->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);
98 $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';
99 $db = $database ? ';dbname=' . $database : '';
100 self::$dbh = new PDO(
101 'mysql' . ':host=' . $host . $port . $db,
104 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));
107 if ( !class_exists('MysqlPDO') )
109 include_once realpath(dirname(__FILE__)) . '/MYSQLPDO.php';
111 $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';
112 $db = $database ? ';dbname=' . $database : '';
113 self::$dbh = new MysqlPDO(
114 'mysql' . ':host=' . $host . $port . $db,
117 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));
121 catch (PDOException $e)
131 * Disconnect the connection to the database.
133 public static function disConnect()
140 * To get the number of times you run the statement.
141 * @return int Number of executions
143 public static function getExecCount()
145 return self::$execCount;
149 * DB::formatDateTime()
150 * The value converted to a format that can be passed to the database datetime.
151 * @param int $timestamp UNIX timestamp
152 * @param int $offset timestamp offset
153 * @return string formatted datetime (quart treated)
155 public static function formatDateTime($timestamp = null, $offset=0)
157 if ( $timestamp == null )
161 $timestamp += $offset;
162 return preg_replace_callback('/(%[a-z%])/i',
163 create_function('$matches', 'return strftime($matches[1], ' . intval($timestamp) . ');'),
170 * Gets the value of the first column of the first row of the results obtained in the statement.
171 * @param string $statement SQL Statement
172 * @return mixed Result value. If the call fails, it will return FALSE.
174 public static function getValue($statement)
176 if ( self::$dbh == null ) return FALSE;
178 $result = self::callQuery($statement);
179 if ( $row = $result->fetch(PDO::FETCH_NUM) )
188 * Gets the first row of the results obtained in the statement.
189 * @param string $statement SQL Statement
190 * @return array Result row. If the call fails, it will return FALSE.
192 public static function getRow($statement)
194 if ( self::$dbh == null ) return FALSE;
196 $result = self::callQuery($statement);
197 return $result->fetch(PDO::FETCH_BOTH);
202 * Gets the set of results obtained in the statement.
203 * @param string $statement SQL Statement
204 * @return PDOStatement Result set object. If the call fails, it will return FALSE.
206 public static function getResult($statement)
208 if ( self::$dbh == null ) return FALSE;
210 return self::callQuery($statement);
215 * Execute an SQL statement and return the number of affected rows.
216 * @param string $statement SQL Statement
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.
219 public static function execute($statement)
221 if ( self::$dbh == null ) return FALSE;
223 return self::callExec($statement);
228 * Run the query to retrieve the result set.
229 * @param string $statement query to be executed
230 * @return PDOStatement Result set object. If the call fails, it will return FALSE.
232 private static function callQuery($statement)
234 $result = self::$dbh->query($statement);
235 if ( $result === FALSE )
237 self::showErrorDisplay($statement);
244 * Run the query and returns the number of rows affected.
245 * @param string $statement query to be executed
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.
248 private static function callExec($statement)
250 $result = self::$dbh->exec($statement);
251 if ( $result === FALSE )
253 self::showErrorDisplay($statement);
259 * DB::showErrorDisplay()
260 * The error message is output to the screen of the query.
261 * @param string $statement query output to the screen
263 private static function showErrorDisplay($statement)
266 if ( array_key_exists('debug', $CONF) && $CONF['debug'] )
268 $err = self::getError();
269 print("mySQL error with query '{$statement}' : " . $err[2]);
276 * Gets the error information associated with the last operation.
277 * @return array Error info
279 public static function getError()
281 if ( self::$dbh == null ) return FALSE;
282 return self::$dbh->errorInfo();
287 * Quotes a string for use in a query.
288 * @param string $value Value to quote
289 * @return string Quoted value
291 public static function quoteValue($value)
293 if ( self::$dbh == null ) return FALSE;
294 return self::$dbh->quote($value);
299 * Get the value of the ID of the rows that are inserted at the end.
300 * @return string ID of the row
302 public static function getInsertId()
304 if ( self::$dbh == null ) return FALSE;
305 return self::$dbh->lastInsertId();
310 * Gets the attribute of the database.
311 * @return string Attribute
313 public static function getAttribute($attribute)
315 if ( self::$dbh == null ) return FALSE;
316 return self::$dbh->getAttribute($attribute);