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)
14 * @license http://nucleuscms.org/license.txt GNU General Public License
15 * @copyright Copyright (C) 2012 The Nucleus Group
22 private static $execCount = 0;
23 private static $dateFormat = '\'%Y-%m-%d %H:%M:%S\'';
26 * DB::setConnectionInfo()
27 * Set the information to connect to the database, it will attempt to connect.
28 * @param string $engine Engine
29 * @param string $host Host
30 * @param string $user User
31 * @param string $password Password
32 * @param string $database Database
33 * @return bool Returns TRUE if able to connect, otherwise it returns FALSE.
35 public static function setConnectionInfo($engine, $host, $user, $password, $database)
41 if ( i18n::strpos($host, ':') === false )
47 list($host, $portnum) = i18n::explode(":", $host);
48 if ( isset($portnum) )
50 $portnum = trim($portnum);
62 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
63 $db = $database ? ';dbname=' . $database : '';
64 self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);
67 $port = is_numeric($portnum) ? ',' . intval($portnum) : '';
68 $db = $database ? ';dbname=' . $database : '';
69 self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);
72 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
73 self::$dbh = new PDO($engine . ':dbname=//' . $host . $port . '/' . $database, $user, $password);
76 $port = is_numeric($portnum) ? ';PORT=' . intval($portnum) : '';
78 $engine . ':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME=' . $host . $port . ';DATABASE=' . $database . ';PROTOCOL=TCPIP;UID='
79 . $user . ';PWD=' . $password);
82 $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';
83 $db = $database ? ';dbname=' . $database : '';
84 self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);
88 $port = is_numeric($portnum) ? ':' . intval($portnum) : '';
89 self::$dbh = new PDO($engine . ':' . $database, $user, $password);
92 self::$dbh->sqliteCreateFunction('SUBSTRING', 'substr', 3);
93 self::$dbh->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);
97 $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';
98 $db = $database ? ';dbname=' . $database : '';
100 'mysql' . ':host=' . $host . $port . $db,
103 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));
106 if ( !class_exists('MysqlPDO') )
108 include_once realpath(dirname(__FILE__)) . '/MYSQLPDO.php';
110 $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';
111 $db = $database ? ';dbname=' . $database : '';
112 self::$dbh = new MysqlPDO(
113 'mysql' . ':host=' . $host . $port . $db,
116 array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));
120 catch (PDOException $e)
130 * Disconnect the connection to the database.
132 public static function disConnect()
139 * To get the number of times you run the statement.
140 * @return int Number of executions
142 public static function getExecCount()
144 return self::$execCount;
148 * DB::formatDateTime()
149 * The value converted to a format that can be passed to the database datetime.
150 * @param int $timestamp UNIX timestamp
151 * @param int $offset timestamp offset
152 * @return string formatted datetime (quart treated)
154 public static function formatDateTime($timestamp = null, $offset=0)
156 if ( $timestamp == null )
160 $timestamp += $offset;
161 return preg_replace_callback('/(%[a-z%])/i',
162 create_function('$matches', 'return strftime($matches[1], ' . intval($timestamp) . ');'),
169 * Gets the value of the first column of the first row of the results obtained in the statement.
170 * @param string $statement SQL Statement
171 * @return mixed Result value. If the call fails, it will return FALSE.
173 public static function getValue($statement)
175 if ( self::$dbh == null ) return FALSE;
177 $result = self::callQuery($statement);
178 if ( $row = $result->fetch(PDO::FETCH_NUM) )
187 * Gets the first row of the results obtained in the statement.
188 * @param string $statement SQL Statement
189 * @return array Result row. If the call fails, it will return FALSE.
191 public static function getRow($statement)
193 if ( self::$dbh == null ) return FALSE;
195 $result = self::callQuery($statement);
196 return $result->fetch(PDO::FETCH_BOTH);
201 * Gets the set of results obtained in the statement.
202 * @param string $statement SQL Statement
203 * @return PDOStatement Result set object. If the call fails, it will return FALSE.
205 public static function getResult($statement)
207 if ( self::$dbh == null ) return FALSE;
209 return self::callQuery($statement);
214 * Execute an SQL statement and return the number of affected rows.
215 * @param string $statement SQL Statement
216 * @return int number of rows that were modified or deleted by the SQL statement you issued. If the call fails, it will return FALSE.
218 public static function execute($statement)
220 if ( self::$dbh == null ) return FALSE;
222 return self::callExec($statement);
227 * Run the query to retrieve the result set.
228 * @param string $statement query to be executed
229 * @return PDOStatement Result set object. If the call fails, it will return FALSE.
231 private static function callQuery($statement)
233 $result = self::$dbh->query($statement);
234 if ( $result === FALSE )
236 self::showErrorDisplay($statement);
243 * Run the query and returns the number of rows affected.
244 * @param string $statement query to be executed
245 * @return int number of rows that were modified or deleted by the SQL statement you issued. If the call fails, it will return FALSE.
247 private static function callExec($statement)
249 $result = self::$dbh->exec($statement);
250 if ( $result === FALSE )
252 self::showErrorDisplay($statement);
258 * DB::showErrorDisplay()
259 * The error message is output to the screen of the query.
260 * @param string $statement query output to the screen
262 private static function showErrorDisplay($statement)
265 if ( array_key_exists('debug', $CONF) && $CONF['debug'] )
267 $err = self::getError();
268 print("mySQL error with query '{$statement}' : " . $err[2]);
275 * Gets the error information associated with the last operation.
276 * @return array Error info
278 public static function getError()
280 if ( self::$dbh == null ) return FALSE;
281 return self::$dbh->errorInfo();
286 * Quotes a string for use in a query.
287 * @param string $value Value to quote
288 * @return string Quoted value
290 public static function quoteValue($value)
292 if ( self::$dbh == null ) return FALSE;
293 return self::$dbh->quote($value);
298 * Get the value of the ID of the rows that are inserted at the end.
299 * @return string ID of the row
301 public static function getInsertId()
303 if ( self::$dbh == null ) return FALSE;
304 return self::$dbh->lastInsertId();
309 * Gets the attribute of the database.
310 * @return string Attribute
312 public static function getAttribute($attribute)
314 if ( self::$dbh == null ) return FALSE;
315 return self::$dbh->getAttribute($attribute);