Ticket #696: SC_Query.patch

File SC_Query.patch, 3.1 KB (added by miningbrownie, 14 years ago)
  • SC_Query.php

    old new  
    504504     * @param string $table_name テーブル名 
    505505     * @return integer 
    506506     */ 
    507     function get_auto_increment($table_name){ 
    508         // ロックする 
    509         $this->query("LOCK TABLES $table_name WRITE"); 
    510  
    511         // 次のIncrementを取得 
    512         $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); 
    513         $auto_inc_no = $arrRet[0]["Auto_increment"]; 
    514  
    515         // 値をカウントアップしておく 
    516         $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); 
    517  
    518         // 解除する 
    519         $this->query('UNLOCK TABLES'); 
    520  
    521         return $auto_inc_no; 
     507    function get_auto_increment($table_name,$old = true){ 
     508        if($old){ 
     509            // ロックする 
     510            $this->query("LOCK TABLES $table_name WRITE"); 
     511            // 次のIncrementを取得 
     512            $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); 
     513            $auto_inc_no = $arrRet[0]["Auto_increment"]; 
     514            // 値をカウントアップしておく 
     515            $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); 
     516            // 解除する 
     517            $this->query('UNLOCK TABLES'); 
     518            return $auto_inc_no; 
     519        }else{ 
     520            // ロックする 
     521            $this->query("LOCK TABLES $table_name WRITE"); 
     522            // 次のIncrementを取得 
     523            $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); 
     524            $auto_inc_no = $arrRet[0]["Auto_increment"]; 
     525            $next_ain = $auto_inc_no +1; 
     526            $arrNotNull = $this->getAll("SHOW COLUMNS FROM {$table_name} WHERE `NULL` = 'NO' AND `Default` IS NULL"); 
     527            $data = array(); 
     528            $pkfield = ""; 
     529            foreach ( $arrNotNull as $field ){ 
     530                $value = null; 
     531     
     532                switch(true){ 
     533                    case ($field['Key'] == "PRI" && preg_match('/auto_increment/',$field['Extra']) ): 
     534                        //primary key & auto_increment 
     535                        $value = (string)$auto_inc_no; 
     536                        $pkfield = $field['Field']; 
     537                        break; 
     538                    case preg_match('/^[datetime|date|time]/',$field['Type']): 
     539                        $value = date("Y-m-d H:i:s"); 
     540                        break; 
     541                    default : 
     542                        $value = "0"; 
     543                } 
     544                $data[$field['Field']]    = $value; 
     545            } 
     546            if($pkfield != ""){ 
     547                // 値をカウントアップさせる 
     548                $this->insert($table_name,$data); 
     549                $this->delete($table_name,$pkfield." = ?",array($auto_inc_no)); 
     550            }else{ 
     551                // 値をカウントアップしておく 
     552                $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); 
     553            } 
     554     
     555            // 解除する 
     556            $this->query('UNLOCK TABLES'); 
     557     
     558            return $auto_inc_no; 
     559          } 
    522560    } 
    523561} 
    524  
    525 ?>