X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;ds=sidebyside;f=nucleus%2Flibs%2Fsql%2FDB.php;h=d8f1d5aa953fa43a06efe267a3c3b9e8800b0772;hb=refs%2Fheads%2Fmaster;hp=18b989847ab779b9af9bfe7b2ba393fa22924219;hpb=6575e866f3fd1938601432841d80f82e9d259265;p=nucleus-jp%2Fnucleus-next.git diff --git a/nucleus/libs/sql/DB.php b/nucleus/libs/sql/DB.php index 18b9898..d8f1d5a 100644 --- a/nucleus/libs/sql/DB.php +++ b/nucleus/libs/sql/DB.php @@ -1,3 +1,324 @@ +<<<<<<< HEAD +sqliteCreateFunction('SUBSTRING', 'substr', 3); + self::$dbh->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1); + } + break; + case 'mysql': + $port = is_numeric($portnum) ? ';port=' . intval($portnum) : ''; + $db = $database ? ';dbname=' . $database : ''; + self::$dbh = new PDO( + 'mysql' . ':host=' . $host . $port . $db, + $user, + $password, + array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\'')); + break; + default: // mysql + if ( !class_exists('MysqlPDO') ) + { + include_once realpath(dirname(__FILE__)) . '/MYSQLPDO.php'; + } + $port = is_numeric($portnum) ? ';port=' . intval($portnum) : ''; + $db = $database ? ';dbname=' . $database : ''; + self::$dbh = new MysqlPDO( + 'mysql' . ':host=' . $host . $port . $db, + $user, + $password, + array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\'')); + break; + } + } + catch (PDOException $e) + { + self::disConnect(); + return FALSE; + } + return TRUE; + } + + /** + * DB::disConnect() + * Disconnect the connection to the database. + */ + public static function disConnect() + { + self::$dbh = null; + } + + /** + * DB::getExecCount() + * To get the number of times you run the statement. + * @return int Number of executions + */ + public static function getExecCount() + { + return self::$execCount; + } + + /** + * DB::formatDateTime() + * The value converted to a format that can be passed to the database datetime. + * @param int $timestamp UNIX timestamp + * @param int $offset timestamp offset + * @return string formatted datetime (quart treated) + */ + public static function formatDateTime($timestamp = null, $offset=0) + { + if ( $timestamp == null ) + { + $timestamp = time(); + } + $timestamp += $offset; + return preg_replace_callback('/(%[a-z%])/i', + create_function('$matches', 'return strftime($matches[1], ' . intval($timestamp) . ');'), + self::$dateFormat + ); + } + + /** + * DB::getValue() + * Gets the value of the first column of the first row of the results obtained in the statement. + * @param string $statement SQL Statement + * @return mixed Result value. If the call fails, it will return FALSE. + */ + public static function getValue($statement) + { + if ( self::$dbh == null ) return FALSE; + self::$execCount++; + $result = self::callQuery($statement); + if ( $row = $result->fetch(PDO::FETCH_NUM) ) + { + return $row[0]; + } + return FALSE; + } + + /** + * DB::getRow() + * Gets the first row of the results obtained in the statement. + * @param string $statement SQL Statement + * @return array Result row. If the call fails, it will return FALSE. + */ + public static function getRow($statement) + { + if ( self::$dbh == null ) return FALSE; + self::$execCount++; + $result = self::callQuery($statement); + return $result->fetch(PDO::FETCH_BOTH); + } + + /** + * DB::getResult() + * Gets the set of results obtained in the statement. + * @param string $statement SQL Statement + * @return PDOStatement Result set object. If the call fails, it will return FALSE. + */ + public static function getResult($statement) + { + if ( self::$dbh == null ) return FALSE; + self::$execCount++; + return self::callQuery($statement); + } + + /** + * DB::execute() + * Execute an SQL statement and return the number of affected rows. + * @param string $statement SQL Statement + * @return int number of rows that were modified or deleted by the SQL statement you issued. If the call fails, it will return FALSE. + */ + public static function execute($statement) + { + if ( self::$dbh == null ) return FALSE; + self::$execCount++; + return self::callExec($statement); + } + + /** + * DB::callQuery() + * Run the query to retrieve the result set. + * @param string $statement query to be executed + * @return PDOStatement Result set object. If the call fails, it will return FALSE. + */ + private static function callQuery($statement) + { + $result = self::$dbh->query($statement); + if ( $result === FALSE ) + { + self::showErrorDisplay($statement); + } + return $result; + } + + /** + * DB::callExec() + * Run the query and returns the number of rows affected. + * @param string $statement query to be executed + * @return int number of rows that were modified or deleted by the SQL statement you issued. If the call fails, it will return FALSE. + */ + private static function callExec($statement) + { + $result = self::$dbh->exec($statement); + if ( $result === FALSE ) + { + self::showErrorDisplay($statement); + } + return $result; + } + + /** + * DB::showErrorDisplay() + * The error message is output to the screen of the query. + * @param string $statement query output to the screen + */ + private static function showErrorDisplay($statement) + { + global $CONF; + if ( array_key_exists('debug', $CONF) && $CONF['debug'] ) + { + $err = self::getError(); + print("mySQL error with query '{$statement}' : " . $err[2]); + } + return; + } + + /** + * DB::getError() + * Gets the error information associated with the last operation. + * @return array Error info + */ + public static function getError() + { + if ( self::$dbh == null ) return FALSE; + return self::$dbh->errorInfo(); + } + + /** + * DB::quoteValue() + * Quotes a string for use in a query. + * @param string $value Value to quote + * @return string Quoted value + */ + public static function quoteValue($value) + { + if ( self::$dbh == null ) return FALSE; + return self::$dbh->quote($value); + } + + /** + * DB::getInsertId() + * Get the value of the ID of the rows that are inserted at the end. + * @return string ID of the row + */ + public static function getInsertId() + { + if ( self::$dbh == null ) return FALSE; + return self::$dbh->lastInsertId(); + } + + /** + * DB::getAttribute() + * Gets the attribute of the database. + * @return string Attribute + */ + public static function getAttribute($attribute) + { + if ( self::$dbh == null ) return FALSE; + return self::$dbh->getAttribute($attribute); + } + +} +======= >>>>>> skinnable-master