Esempio n. 1
0
 public function query($sql, $table = null)
 {
     // 判斷要用 Master 還是 Slave
     $type = 'master';
     if (!Pix_Table::$_force_master and preg_match('#^SELECT #', strtoupper($sql))) {
         $type = 'slave';
     }
     if (Pix_Setting::get('Table:ExplainFileSortEnable')) {
         if (preg_match('#^SELECT #', strtoupper($sql))) {
             $res = $this->_getLink($type)->query("EXPLAIN {$sql}");
             $row = $res->fetch_assoc();
             if (preg_match('#Using filesort#', $row['Extra'])) {
                 trigger_error("Using Filesort Query {$sql}", E_USER_WARNING);
             }
             $res->free_result();
         }
     }
     if (Pix_Setting::get('Table:SQLNoCache')) {
         if (preg_match('#^SELECT #', strtoupper($sql))) {
             $sql = 'SELECT SQL_NO_CACHE ' . substr($sql, 7);
         }
     }
     // 加上 Query Comment
     if ($comment = Pix_Table::getQueryComment()) {
         $sql = trim($sql, '; ') . ' #' . $comment;
     }
     for ($i = 0; $i < 3; $i++) {
         if (!($link = $this->_getLink($type))) {
             throw new Exception('找不到 Link');
         }
         $starttime = microtime(true);
         $res = $link->query($sql);
         $this->insert_id = $link->insert_id;
         $delta = microtime(true) - $starttime;
         if (array_key_exists(Pix_Table::LOG_QUERY, Pix_Table::$_log_groups) and Pix_Table::$_log_groups[Pix_Table::LOG_QUERY]) {
             Pix_Table::debug(sprintf("[%s-%s](%f)%s", strval($link->host_info), $type, $delta, $sql));
         } elseif ($t = Pix_Table::getLongQueryTime() and $delta > $t) {
             Pix_Table::debug(sprintf("[%s-%s](%f)%s", strval($link->host_info), $type, $delta, $sql));
         }
         if ($res === false) {
             if ($errno = $link->errno) {
                 $message = (is_null($table) ? '' : "Table: {$table->getClass()}") . "SQL Error: ({$errno}){$link->error} " . substr($sql, 0, 128);
                 switch ($errno) {
                     case 1146:
                         throw new Pix_Table_TableNotFoundException($message);
                     case 1062:
                         throw new Pix_Table_DuplicateException((is_null($table) ? '' : "(Table: {$table->getClass()})") . $link->error, $errno);
                     case 1406:
                         throw new Pix_Table_DataTooLongException($message);
                     case 2006:
                         // MySQL server gone away
                     // MySQL server gone away
                     case 2013:
                         // Lost connection to MySQL server during query
                         trigger_error("Pix_Table " . $message, E_USER_WARNING);
                         $this->resetConnect();
                         continue 2;
                 }
             }
             throw new Pix_Table_Exception($message);
         }
         if ($link->warning_count) {
             $e = $link->get_warnings();
             do {
                 if (1592 == $e->errno) {
                     continue;
                 }
                 if (Pix_Table::$_throw_incorrect_string_exception and 1366 == $e->errno) {
                     throw new Pix_Table_IncorrectStringException($e->message);
                 }
                 trigger_error("Pix_Table " . (is_null($table) ? '' : "Table: {$table->getClass()}") . "SQL Warning: ({$e->errno}){$e->message} " . substr($sql, 0, 128), E_USER_WARNING);
             } while ($e->next());
         }
         return $res;
     }
     throw new Pix_Table_Exception("query 三次失敗");
 }
 /**
  * 測試未指定 DropTableEnable 必需要 dropTable 失敗
  *
  * @access public
  */
 public function testDropTableProtect()
 {
     $old_value = Pix_Setting::get('Table:DropTableEnable');
     Pix_Setting::set('Table:DropTableEnable', false);
     try {
         Pix_Table_TableTest_Table::dropTable();
         $this->assertTrue(false);
     } catch (Pix_Table_Exception $e) {
         $this->assertTrue(true);
     }
     Pix_Setting::set('Table:DropTableEnable', $old_value);
 }
Esempio n. 3
0
 /**
  * dropTable 從資料庫內移除 $table 這個 Table
  *
  * @param Pix_Table $table
  * @access public
  * @return void
  */
 public function dropTable($table)
 {
     if (!Pix_Setting::get('Table:DropTableEnable')) {
         throw new Pix_Table_Exception("要 DROP TABLE 前請加上 Pix_Setting::set('Table:DropTableEnable', true);");
     }
     $sql = "DROP TABLE " . $this->column_quote($table->getTableName());
     return $this->query($sql, $table);
 }
Esempio n. 4
0
 /**
  * dropTable 刪除這個 table
  * 
  * @static
  * @access public
  * @return void
  */
 public static function dropTable()
 {
     if (!Pix_Setting::get('Table:DropTableEnable')) {
         throw new Pix_Table_Exception("要 DROP TABLE 前請加上 Pix_Setting::set('Table:DropTableEnable', true);");
     }
     $table = self::getTable();
     $table->_cache_rows = array();
     return $table->getDb()->dropTable($table);
 }