示例#1
0
 public function insertINTO()
 {
     //----------------------------------------------------------
     //init var
     //----------------------------------------------------------
     $chk = array("bool" => true, 'function' => "insertINTO");
     $this->updateArr = array();
     //----------------------------------------------------------
     $chk = GenFun::errorKey_chk($this->setArr, NULL, true);
     //----------------------------------------------------------
     $this->setArr = $chk['valueArr'];
     //----------------------------------------------------------
     if (!$chk['bool']) {
         return $chk;
     }
     //----------------------------------------------------------
     $this->tableID = Constants::$mysqli->real_escape_string($this->tableID);
     //----------------------------------------------------------
     $this->queryString = $this->insertType . " " . $this->tableID . " (";
     foreach ($this->setArr as $i => $value) {
         if (strrpos($value, "{hash}") === 0) {
             $this->updateArr[$i] = $value;
         } else {
             $this->queryString .= $i . ",";
         }
     }
     //----------------------------------------------------------
     foreach ($this->updateArr as $i => $value) {
         unset($this->setArr[$i]);
     }
     //----------------------------------------------------------
     $this->queryString = substr($this->queryString, 0, -1);
     $this->queryString .= ") ";
     //----------------------------------------------------------
     $keys = array_keys($this->setArr);
     $this->queryString .= "VALUES ";
     //----------------------------------------------------------
     $high = sizeof($this->setArr[$keys[0]]);
     for ($i = 1; $i < sizeof($this->setArr); $i++) {
         if (is_array($this->setArr[$keys[$i]])) {
             $cur = sizeof($this->setArr[$keys[$i]]);
             if ($high < $cur) {
                 $high = $cur;
             }
         }
     }
     //----------------------------------------------------------
     for ($i = 0; $i < 1; $i++) {
         $value = $this->setArr[$keys[$i]];
         $num = is_array($value) ? sizeof($value) : 1;
         for ($j = 0; $j < $high; $j++) {
             if (is_array($this->setArr[$keys[$i]])) {
                 $value = $this->setArr[$keys[$i]][$j];
             }
             if ($i == 0) {
                 $this->queryString .= "(";
             }
             if (strrpos($value, "(") !== false && strrpos($value, "(") == 0 && !$this->ignore) {
                 $this->queryString .= $value;
                 //$this->queryString .= ",";
             } else {
                 if ($value == "DEFAULT") {
                     $this->queryString .= $value;
                 } else {
                     $this->queryString .= "'" . Constants::$mysqli->escape_string($value) . "'";
                 }
             }
             //-------------------------------------------------
             for ($k = 0; $k < sizeof($this->setArr); $k++) {
                 if ($keys[$k] != $keys[$i]) {
                     $this->queryString .= ",";
                     if (is_array($this->setArr[$keys[$k]])) {
                         $v = $this->setArr[$keys[$k]][$j];
                         $this->queryString .= "'" . Constants::$mysqli->escape_string($v) . "'";
                     } else {
                         $v = $this->setArr[$keys[$k]];
                         $this->queryString .= "'" . Constants::$mysqli->escape_string($v) . "'";
                     }
                 }
             }
             //-------------------------------------------------
             //if ($i == sizeof($this->setArr)-1) {
             //$this->queryString = substr($this->queryString, 0, -1);
             $this->queryString .= ")";
             //}
             //-------------------------------------------------
             if ($j < $high - 1) {
                 $this->queryString .= ",";
             }
         }
     }
     //----------------------------------------------------------
     if (!is_null($this->onDuplicate)) {
         $this->queryString .= " ON DUPLICATE KEY UPDATE ";
         $this->queryString = MySQL::condition($this->tableID, $this->onDuplicate, NULL, $this->queryString);
     }
     //----------------------------------------------------------
     if (!$this->returnQuery) {
         $chk = $this->getResult();
         $insert_id = $chk['insert_id'];
         if ($chk['duplicate']) {
             $chk['duplicate'] = GetTable::go($this->tableID, $this->setArr);
         } else {
             if (sizeof($this->updateArr) > 0) {
                 foreach ($this->updateArr as $i => $value) {
                     if (strrpos($value, "{hash}") === 0) {
                         $value = strtr($value, array('{hash}' => ""));
                         /*if (!is_numeric($value)) {
                         			$chk['bool'] = false;
                         			$chk['messgae'] = "In order to use the alphaID() utility ".$value.' must be numeric!!!';
                         			return $chk;
                         		}*/
                         $this->updateArr[$i] = alphaID($insert_id . $value);
                     }
                 }
                 $this->setArr['id'] = $insert_id;
                 $chk = Update::go($this->tableID, $this->updateArr, $this->setArr);
             }
         }
         if (!$chk['bool']) {
             return $chk;
         }
         if ($this->returnRow) {
             $chk['row'] = GetTable::go($this->tableID, array("id" => $insert_id));
             if ($chk['row']['bool']) {
                 $chk['row'] = $chk['row']['result'][0];
             }
         }
         $chk['insert_id'] = $insert_id;
         return $chk;
     } else {
         return $this->queryString;
     }
 }