Пример #1
0
 public function execute()
 {
     if (isset($this->orderby)) {
         $this->orderbySQL = sprintf('ORDER BY %s', $this->orderby);
     }
     if (isset($this->paging)) {
         $this->limitSQL = sprintf('LIMIT %d,%d', $this->paging->offset, $this->paging->limit);
     }
     if (isset($this->where)) {
         $this->whereSQL = sprintf('WHERE %s', $this->where);
     }
     if (isset($this->data)) {
         $columns = [];
         $bindings = [];
         foreach ($this->data as $key => $value) {
             $columns[] = SqlQuery::validateColumn($key) . ' = ?';
             $bindings[] =& $value;
         }
         // prepend bindings because SET comes before WHERE
         $this->bindings = array_merge($bindings, $this->bindings);
         $this->setSQL = sprintf('SET %s', implode(',', $columns));
     }
     switch ($this->query_type) {
         case 'update':
             $sql = sprintf('UPDATE %s %s %s', $this->table_name, $this->setSQL, $this->whereSQL);
             break;
         case 'select':
         default:
             $sql = sprintf('SELECT * FROM %s %s %s %s', $this->table_name, $this->whereSQL, $this->orderbySQL, $this->limitSQL);
     }
     if (isset($this->paging) && !isset($this->paging->total_records)) {
         $this->paging->total_records = SqlQuery::getRecordCount($this->db, $this->table_name, $this->whereSQL, $this->bindings, $this->types);
     }
     //dbg($sql);
     //var_dump($this->bindings);
     return SqlQuery::executeSQL($this->db, $sql, $this->bindings, $this->types);
 }
Пример #2
0
 public function save()
 {
     $result = false;
     $id = $this->ival($this->id_name);
     if (isset($id) && $id > 0) {
         $columns = [];
         $bindings = [];
         $types = '';
         foreach ($this->data as $key => $value) {
             if ($key != $this->id_name) {
                 $columns[] = SqlQuery::validateColumn($key) . ' = ?';
                 $bindings[] =& $this->data[$key];
                 $types .= SqlQuery::getTypeChar($value);
             }
         }
         $bindings[] =& $this->data[$this->id_name];
         $types .= 'i';
         array_unshift($bindings, $types);
         $sql = sprintf('UPDATE %s SET %s WHERE %s = ?', $this->table_name, implode(',', $columns), $this->id_name);
         if ($st = $this->db->prepare($sql)) {
             call_user_func_array(array($st, 'bind_param'), $bindings);
             if ($st->execute()) {
                 $result = true;
             } else {
                 dbErr($this->table_name, 'execute', $sql, $this->db->error);
             }
             $st->close();
         } else {
             dbErr($this->table_name, 'prepare', $sql, $this->db->error);
         }
     } else {
         $columns = [];
         $values = [];
         $bindings = [];
         $types = '';
         foreach ($this->data as $key => $value) {
             if ($key != $this->id_name) {
                 $columns[] = SqlQuery::validateColumn($key);
                 $values[] = '?';
                 $bindings[] =& $this->data[$key];
                 $types .= SqlQuery::getTypeChar($value);
             }
         }
         array_unshift($bindings, $types);
         $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->table_name, implode(',', $columns), implode(',', $values));
         if ($st = $this->db->prepare($sql)) {
             //echo $sql;
             call_user_func_array(array($st, 'bind_param'), $bindings);
             if ($st->execute()) {
                 $this->is_loaded = true;
                 $result = true;
                 $this->data[$this->id_name] = $this->db->insert_id;
             } else {
                 dbErr($this->table_name, 'execute', $sql, $this->db->error);
             }
             $st->close();
         } else {
             dbErr($this->table_name, 'prepare', $sql, $this->db->error);
         }
     }
     return $result;
 }