OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / sql / DB.php
index 18b9898..d8f1d5a 100644 (file)
@@ -1,3 +1,324 @@
+<<<<<<< HEAD
+<?php\r
+\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+\r
+/**\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2012 The Nucleus Group\r
+ * @version $Id$\r
+ */\r
+\r
+class DB\r
+{\r
+       private static $dbh;\r
+       private static $execCount = 0;\r
+       private static $dateFormat = '\'%Y-%m-%d %H:%M:%S\'';\r
+       \r
+       /**\r
+        * DB::setConnectionInfo()\r
+        * Set the information to connect to the database, it will attempt to connect.\r
+        * @param string $engine Engine\r
+        * @param string $host Host\r
+        * @param string $user User\r
+        * @param string $password Password\r
+        * @param string $database Database\r
+        * @return bool Returns TRUE if able to connect, otherwise it returns FALSE.\r
+        */\r
+       public static function setConnectionInfo($engine, $host, $user, $password, $database)\r
+       {\r
+               self::disConnect();\r
+\r
+               try\r
+               {\r
+                       if ( i18n::strpos($host, ':') === false )\r
+                       {\r
+                               $portnum = '';\r
+                       }\r
+                       else\r
+                       {\r
+                               list($host, $portnum) = i18n::explode(":", $host);\r
+                               if ( isset($portnum) )\r
+                               {\r
+                                       $portnum = trim($portnum);\r
+                               }\r
+                               else\r
+                               {\r
+                                       $portnum = '';\r
+                               }\r
+                       }\r
+\r
+                       switch ( $engine )\r
+                       {\r
+                               case 'sybase':\r
+                               case 'dblib':\r
+                                       $port = is_numeric($portnum) ? ':' . intval($portnum) : '';\r
+                                       $db = $database ? ';dbname=' . $database : '';\r
+                                       self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);\r
+                                       break;\r
+                               case 'mssql':\r
+                                       $port = is_numeric($portnum) ? ',' . intval($portnum) : '';\r
+                                       $db = $database ? ';dbname=' . $database : '';\r
+                                       self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);\r
+                                       break;\r
+                               case 'oci':\r
+                                       $port = is_numeric($portnum) ? ':' . intval($portnum) : '';\r
+                                       self::$dbh = new PDO($engine . ':dbname=//' . $host . $port . '/' . $database, $user, $password);\r
+                                       break;\r
+                               case 'odbc':\r
+                                       $port = is_numeric($portnum) ? ';PORT=' . intval($portnum) : '';\r
+                                       self::$dbh = new PDO(\r
+                                               $engine . ':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME=' . $host . $port . ';DATABASE=' . $database . ';PROTOCOL=TCPIP;UID='\r
+                                                       . $user . ';PWD=' . $password);\r
+                                       break;\r
+                               case 'pgsql':\r
+                                       $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';\r
+                                       $db = $database ? ';dbname=' . $database : '';\r
+                                       self::$dbh = new PDO($engine . ':host=' . $host . $port . $db, $user, $password);\r
+                                       break;\r
+                               case 'sqlite':\r
+                               case 'sqlite2':\r
+                                       $port = is_numeric($portnum) ? ':' . intval($portnum) : '';\r
+                                       self::$dbh = new PDO($engine . ':' . $database, $user, $password);\r
+                                       if ( self::$dbh )\r
+                                       {\r
+                                               self::$dbh->sqliteCreateFunction('SUBSTRING', 'substr', 3);\r
+                                               self::$dbh->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);\r
+                                       }\r
+                                       break;\r
+                               case 'mysql':\r
+                                       $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';\r
+                                       $db = $database ? ';dbname=' . $database : '';\r
+                                       self::$dbh = new PDO(\r
+                                               'mysql' . ':host=' . $host . $port . $db,\r
+                                               $user,\r
+                                               $password,\r
+                                               array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));\r
+                                       break;\r
+                               default: // mysql\r
+                                       if ( !class_exists('MysqlPDO') )\r
+                                       {\r
+                                               include_once realpath(dirname(__FILE__)) . '/MYSQLPDO.php';\r
+                                       }\r
+                                       $port = is_numeric($portnum) ? ';port=' . intval($portnum) : '';\r
+                                       $db = $database ? ';dbname=' . $database : '';\r
+                                       self::$dbh = new MysqlPDO(\r
+                                               'mysql' . ':host=' . $host . $port . $db,\r
+                                               $user,\r
+                                               $password,\r
+                                               array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''));\r
+                                       break;\r
+                       }\r
+               }\r
+               catch (PDOException $e)\r
+               {\r
+                       self::disConnect();\r
+                       return FALSE;\r
+               }\r
+               return TRUE;\r
+       }\r
+\r
+       /**\r
+        * DB::disConnect()\r
+        * Disconnect the connection to the database.\r
+        */\r
+       public static function disConnect()\r
+       {\r
+               self::$dbh = null;\r
+       }\r
+\r
+       /**\r
+        * DB::getExecCount()\r
+        * To get the number of times you run the statement.\r
+        * @return int Number of executions\r
+        */\r
+       public static function getExecCount()\r
+       {\r
+               return self::$execCount;\r
+       }\r
+\r
+       /**\r
+        * DB::formatDateTime()\r
+        * The value converted to a format that can be passed to the database datetime.\r
+        * @param int $timestamp UNIX timestamp\r
+        * @param int $offset timestamp offset\r
+        * @return string formatted datetime (quart treated)\r
+        */\r
+       public static function formatDateTime($timestamp = null, $offset=0)\r
+       {\r
+               if ( $timestamp == null )\r
+               {\r
+                       $timestamp = time();\r
+               }\r
+               $timestamp += $offset;\r
+               return preg_replace_callback('/(%[a-z%])/i',\r
+                       create_function('$matches', 'return strftime($matches[1], ' . intval($timestamp) . ');'),\r
+                       self::$dateFormat\r
+               );\r
+       }\r
+       \r
+       /**\r
+        * DB::getValue()\r
+        * Gets the value of the first column of the first row of the results obtained in the statement.\r
+        * @param string $statement SQL Statement\r
+        * @return mixed Result value. If the call fails, it will return FALSE.\r
+        */\r
+       public static function getValue($statement)\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               self::$execCount++;\r
+               $result = self::callQuery($statement);\r
+               if ( $row = $result->fetch(PDO::FETCH_NUM) )\r
+               {\r
+                       return $row[0];\r
+               }\r
+               return FALSE;\r
+       }\r
+\r
+       /**\r
+        * DB::getRow()\r
+        * Gets the first row of the results obtained in the statement.\r
+        * @param string $statement SQL Statement\r
+        * @return array Result row. If the call fails, it will return FALSE.\r
+        */\r
+       public static function getRow($statement)\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               self::$execCount++;\r
+               $result = self::callQuery($statement);\r
+               return $result->fetch(PDO::FETCH_BOTH);\r
+       }\r
+\r
+       /**\r
+        * DB::getResult()\r
+        * Gets the set of results obtained in the statement.\r
+        * @param string $statement SQL Statement\r
+        * @return PDOStatement Result set object. If the call fails, it will return FALSE.\r
+        */\r
+       public static function getResult($statement)\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               self::$execCount++;\r
+               return self::callQuery($statement);\r
+       }\r
+\r
+       /**\r
+        * DB::execute()\r
+        * Execute an SQL statement and return the number of affected rows.\r
+        * @param string $statement SQL Statement\r
+        * @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
+        */\r
+       public static function execute($statement)\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               self::$execCount++;\r
+               return self::callExec($statement);\r
+       }\r
+\r
+       /**\r
+        * DB::callQuery()\r
+        * Run the query to retrieve the result set.\r
+        * @param string $statement query to be executed\r
+        * @return PDOStatement Result set object. If the call fails, it will return FALSE.\r
+        */\r
+       private static function callQuery($statement)\r
+       {\r
+               $result = self::$dbh->query($statement);\r
+               if ( $result === FALSE )\r
+               {\r
+                       self::showErrorDisplay($statement);\r
+               }\r
+               return $result;\r
+       }\r
+       \r
+       /**\r
+        * DB::callExec()\r
+        * Run the query and returns the number of rows affected.\r
+        * @param string $statement query to be executed\r
+        * @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
+        */\r
+       private static function callExec($statement)\r
+       {\r
+               $result = self::$dbh->exec($statement);\r
+               if ( $result === FALSE )\r
+               {\r
+                       self::showErrorDisplay($statement);\r
+               }\r
+               return $result;\r
+       }\r
+       \r
+       /**\r
+        * DB::showErrorDisplay()\r
+        * The error message is output to the screen of the query.\r
+        * @param string $statement query output to the screen\r
+        */\r
+       private static function showErrorDisplay($statement)\r
+       {\r
+               global $CONF;\r
+               if ( array_key_exists('debug', $CONF) && $CONF['debug'] )\r
+               {\r
+                       $err = self::getError();\r
+                       print("mySQL error with query '{$statement}' : " . $err[2]);\r
+               }\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * DB::getError()\r
+        * Gets the error information associated with the last operation.\r
+        * @return array Error info\r
+        */\r
+       public static function getError()\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               return self::$dbh->errorInfo();\r
+       }\r
+\r
+       /**\r
+        * DB::quoteValue()\r
+        * Quotes a string for use in a query.\r
+        * @param string $value Value to quote\r
+        * @return string Quoted value\r
+        */\r
+       public static function quoteValue($value)\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               return self::$dbh->quote($value);\r
+       }\r
+\r
+       /**\r
+        * DB::getInsertId()\r
+        * Get the value of the ID of the rows that are inserted at the end.\r
+        * @return string ID of the row\r
+        */\r
+       public static function getInsertId()\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               return self::$dbh->lastInsertId();\r
+       }\r
+\r
+       /**\r
+        * DB::getAttribute()\r
+        * Gets the attribute of the database.\r
+        * @return string Attribute\r
+        */\r
+       public static function getAttribute($attribute)\r
+       {\r
+               if ( self::$dbh == null ) return FALSE;\r
+               return self::$dbh->getAttribute($attribute);\r
+       }\r
+       \r
+}\r
+=======
 <?php
 
 /*
@@ -316,3 +637,4 @@ class DB
        }
        
 }
+>>>>>>> skinnable-master