OSDN Git Service

CHANGE:$CONF['debug']がTRUEの時にクエリエラーを画面に出力するように変更
authorreine <reine@users.sourceforge.jp>
Sun, 6 May 2012 13:17:38 +0000 (22:17 +0900)
committerreine <reine@users.sourceforge.jp>
Sun, 6 May 2012 13:24:15 +0000 (22:24 +0900)
nucleus/libs/sql/DB.php

index 1f788a8..17d96c1 100644 (file)
@@ -24,6 +24,7 @@ class DB
        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
@@ -126,6 +127,7 @@ class DB
        }\r
 \r
        /**\r
+        * DB::disConnect()\r
         * Disconnect the connection to the database.\r
         */\r
        public static function disConnect()\r
@@ -134,6 +136,7 @@ class DB
        }\r
 \r
        /**\r
+        * DB::getExecCount()\r
         * To get the number of times you run the statement.\r
         * @return int Number of executions\r
         */\r
@@ -143,6 +146,7 @@ class DB
        }\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
@@ -162,6 +166,7 @@ class DB
        }\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
@@ -170,15 +175,16 @@ class DB
        {\r
                if ( self::$dbh == null ) return FALSE;\r
                self::$execCount++;\r
-               $stmt = &self::$dbh->query($statement);\r
-               if ( $result = $stmt->fetch(PDO::FETCH_NUM) )\r
+               $result = self::showErrorDisplay(self::$dbh->query($statement));\r
+               if ( $row = $result->fetch(PDO::FETCH_NUM) )\r
                {\r
-                       return $result[0];\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
@@ -187,10 +193,12 @@ class DB
        {\r
                if ( self::$dbh == null ) return FALSE;\r
                self::$execCount++;\r
-               return self::$dbh->query($statement)->fetch(PDO::FETCH_BOTH);\r
+               $result = self::showErrorDisplay(self::$dbh->query($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
@@ -199,10 +207,11 @@ class DB
        {\r
                if ( self::$dbh == null ) return FALSE;\r
                self::$execCount++;\r
-               return self::$dbh->query($statement);\r
+               return self::showErrorDisplay(self::$dbh->query($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.\r
@@ -211,10 +220,31 @@ class DB
        {\r
                if ( self::$dbh == null ) return FALSE;\r
                self::$execCount++;\r
-               return self::$dbh->exec($statement);\r
+               return self::showErrorDisplay(self::$dbh->exec($statement));\r
+       }\r
+\r
+       /**\r
+        * DB::showErrorDisplay()\r
+        * On the display query execution result query has an error in the case of FALSE.\r
+        * @param mixed $result Query execution result\r
+        * @return mixes Query execution result\r
+        */\r
+       private static function showErrorDisplay($result)\r
+       {\r
+               global $CONF;\r
+               if ( array_key_exists('debug', $CONF) && $CONF['debug'] )\r
+               {\r
+                       if ( $result === FALSE )\r
+                       {\r
+                               $err = self::getError();\r
+                               print('mySQL error with query $query: ' . $err[2]);\r
+                       }\r
+               }\r
+               return $result;\r
        }\r
        \r
        /**\r
+        * DB::getError()\r
         * Gets the error information associated with the last operation.\r
         * @return array Error info\r
         */\r
@@ -225,6 +255,7 @@ class DB
        }\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
@@ -236,6 +267,7 @@ class DB
        }\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
@@ -246,6 +278,7 @@ class DB
        }\r
 \r
        /**\r
+        * DB::getAttribute()\r
         * Gets the attribute of the database.\r
         * @return string Attribute\r
         */\r