Example #1
0
 public function insert($ignore = false)
 {
     $query = new Query('INSERT', $this->dbengine);
     if ($ignore) {
         $query->ignore();
     }
     $query->intoTable(static::$tableNoPrefix);
     $vals = array();
     $template = '(';
     foreach ($this->stored as $key => $val) {
         $valid = true;
         if ($key == static::$primaryKey && static::$primaryKeyIsAutoIncrement) {
             $valid = false;
         }
         if ($valid) {
             $query->intoField($key);
             if ($this->sqlkeys[$key]) {
                 $template .= $val . ', ';
             } else {
                 $template .= '?, ';
                 $vals[] = $val;
             }
         }
     }
     if ($template == '(') {
         throw new NoStoredValuesException('INSERT cannot be completed: No values to be inserted.');
     }
     $template = substr($template, 0, -2) . ')';
     $query->values($template, $vals);
     $stmt = $query->prepare();
     if (!$stmt->execute()) {
         $info = $stmt->errorInfo();
         $e = new QueryFailedException($info[2]);
         $e->errorInfo = $info;
         throw $e;
     }
     return true;
 }
Example #2
0
 protected function indexName($name, $id)
 {
     $iwords = $this->getIndexable($name);
     if (count($iwords) > 0) {
         $sem = SemaphoreEngineFactory::getEngine();
         $sindex = array();
         $create = array();
         $docreate = false;
         $query = new Query('SELECT');
         $query->field('term');
         $query->field('app_id_array');
         $query->from('search_name_index');
         foreach ($iwords as $iword) {
             $sindex[$iword] = array();
             $create[$iword] = true;
             $docreate = true;
             $query->where('term = ?', $iword, 'OR');
         }
         $stmt = $query->prepare();
         $semkey = Config::getVal('recache', 'unique_name') . ":LOCK:sindex";
         $sem->acquire($semkey);
         $stmt->execute();
         while ($row = $stmt->fetchObj()) {
             $create[$row->term] = false;
             $sindex[$row->term] = unserialize($row->app_id_array);
         }
         unset($query);
         unset($stmt);
         $iquery = new Query('INSERT');
         $iquery->ignore();
         $iquery->intoTable('search_name_index');
         $iquery->intoField('term');
         $iquery->intoField('app_id_array');
         $uqueries = array();
         foreach ($iwords as $iword) {
             if (!in_array($id, $sindex[$iword])) {
                 $sindex[$iword][] = $id;
                 $this->cm->set("search_name_index:{$iword}", $sindex[$iword], 0);
             }
             if ($create[$iword]) {
                 $iquery->VALUES('(?, ?)', array($iword, serialize($sindex[$iword])));
             } else {
                 $query = new Query('UPDATE');
                 $query->table('search_name_index');
                 $query->set('app_id_array = ?', serialize($sindex[$iword]));
                 $query->where('term = ?', $iword);
                 $uqueries[] = $query;
             }
         }
         if ($docreate) {
             $stmt = $iquery->prepare();
             unset($iquery);
             $stmt->execute();
         }
         foreach ($uqueries as $uquery) {
             $stmt = $uquery->prepare();
             $stmt->execute();
         }
         $sem->release($semkey);
     }
 }