Exemplo n.º 1
0
 public function setUp()
 {
     $sql = new SCAR_SQL('insert', null, 'sql_test_SCAR');
     $sql->table = 'foo';
     $sql->set = array(array('col' => 'bar', 'value' => 'baz'), array('col' => 'quux', 'value' => 'qwe'), array('col' => 'asd', 'value' => 'zxc'));
     $this->result = $sql->render();
 }
Exemplo n.º 2
0
 /**
  * Perform an SQL Select
  * @return array
  **/
 protected function doSQLSelect()
 {
     if (md5(serialize($this->_data)) == $this->_cksum) {
         return $this->_lastrs;
     }
     // if where count is 1 and where value is an array and where col == primary key (where IN pkey)
     $where = $this->getWhere();
     if (count($where) == 1 && is_array($where[0]['value']) && array($where[0]['col']) == $this->getPrimaryKey()) {
         // clear the where condition completely
         $old_values = $where[0]['value'];
         $byColumn = Inflector::byMethodize($where[0]['col']);
         $values = array();
         $keys = array();
         // for each where value
         foreach ($old_values as $key) {
             // if in datastore, add to key
             if ($this->getDatastore()->get($this->getName(), $key)) {
                 $keys[] = $key;
             } else {
                 $values[] = $key;
             }
         }
         // if there are no missing values
         // then our key list is valid
         if (count($values) == 0) {
             if ($this->_keys == array()) {
                 $this->_keys = $keys;
             }
             return true;
         }
         // clear our where clause
         // set the keys to everything we have
         // set new byKeyName() with only missing values
         $this->_data['where'] = array();
         $this->_keys = $keys;
         $this->{$byColumn}($values);
     } elseif (count($where) == count($this->getPrimaryKey())) {
         // if every where matches a pkey and none of the where values are an array
         $key_check = array_flip($this->getPrimaryKey());
         // keycheck is now 'key' => $position
         $lookup = array();
         foreach ($where as $w) {
             if (!isset($key_check[$w['col']])) {
                 break;
             }
             $lookup[$key_check[$w['col']]] = $w['value'];
         }
         if (count($lookup) == count($where)) {
             // (we are looking for one row, either pk or cpk)
             ksort($lookup);
             // lookup is now in order
             $lookup = implode(',', $lookup);
             // turned into key
             // check datastore
             $result = $this->getDatastore()->get($this->getName(), $lookup);
             if ($result) {
                 $this->iAm($lookup);
                 // prevent infinite loop, only set keys if not set
                 if ($this->_keys == array()) {
                     $this->_keys = array($lookup);
                 }
                 return true;
             }
         }
     }
     // not in datastore (ordered things by default won't)
     $sql = new SCAR_SQL('select', $this->getDSN(), $this->getSCAR());
     $sql->table = $this->getTable();
     $sql->columns = $this->getColumns() ? array_unique(array_merge($this->getColumns(), $this->getPrimaryKey())) : '*';
     $sql->where = $this->getWhere();
     $sql->order = $this->getOrder();
     $sql->limit = $this->getLimit();
     $stmt = $sql->render();
     $this->stmt($stmt);
     $rs = call_user_func_array(array($this->getSCAR(), 'query'), array($this->getDSN(), $stmt, $this->getPrimaryKey()));
     // write these keys to the datastore
     foreach ($rs['keys'] as $key) {
         foreach ($key as $key_value) {
             $this->_keys[] = $key_value;
             $this->getDatastore()->set($this->getName(), $key_value, $rs['data'][$key_value]);
         }
     }
     // one row, set iAm()
     if ($rs['rows'] === 1) {
         $this->iAm($this->_keys[0]);
     }
     // no rows, iAm becomes null
     if ($rs['rows'] === 0) {
         $this->iAm(null);
     }
     $this->_cksum = md5(serialize($this->_data));
     $this->_lastrs = $rs;
     return $rs;
 }