Ejemplo n.º 1
0
 /**
  * Выполняет запросы переданные в строке $sql.
  *
  * @throws SqlTaskException если при выполнение запросов произошла ошибка
  * @param string $sql
  */
 private static function execQuery($sql)
 {
     try {
         DB::multiQuery($sql);
     } catch (DBException $e) {
         throw new SqlTaskException('Mysql Error:' . PHP_EOL . 'Query: ' . $e->getQuery() . PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL . 'Code : ' . $e->getCOde() . PHP_EOL);
     }
 }
Ejemplo n.º 2
0
 public function save($deep = true)
 {
     // fire event
     Emergence\EventBus::fireEvent('beforeRecordSave', $this->getRootClass(), array('Record' => $this, 'deep' => $deep));
     // set creator
     if (static::_fieldExists('CreatorID') && !$this->CreatorID) {
         $Creator = $this->getUserFromEnvironment();
         $this->CreatorID = $Creator ? $Creator->ID : null;
     }
     // set created
     if (static::_fieldExists('Created') && (!$this->Created || $this->Created == 'CURRENT_TIMESTAMP')) {
         $this->Created = time();
     }
     // validate
     if (!$this->validate($deep)) {
         throw new RecordValidationException($this, 'Cannot save invalid record');
     }
     // clear caches
     foreach ($this->getClassFields() as $field => $options) {
         if (!empty($options['unique']) || !empty($options['primary'])) {
             $key = sprintf('%s/%s:%s', static::$tableName, $field, $this->getValue($field));
             DB::clearCachedRecord($key);
         }
     }
     // traverse relationships
     if ($deep) {
         $this->_saveRelationships();
     }
     if ($this->isDirty) {
         if (!$this->_isPhantom && static::$trackModified) {
             $this->Modified = time();
             $Modifier = $this->getUserFromEnvironment();
             $this->ModifierID = $Modifier ? $Modifier->ID : null;
         }
         // prepare record values
         $recordValues = $this->_prepareRecordValues();
         // transform record to set array
         $set = static::_mapValuesToSet($recordValues);
         // create new or update existing
         if ($this->_isPhantom) {
             $insertQuery = DB::prepareQuery('INSERT INTO `%s` SET %s', array(static::$tableName, join(',', $set)));
             try {
                 try {
                     DB::nonQuery($insertQuery);
                 } catch (TableNotFoundException $e) {
                     // auto-create table and try insert again
                     DB::multiQuery(SQL::getCreateTable(get_called_class()));
                     DB::nonQuery($insertQuery);
                 }
                 $this->_record['ID'] = DB::insertID();
                 $this->_isPhantom = false;
                 $this->_isNew = true;
             } catch (DuplicateKeyException $e) {
                 if (static::$updateOnDuplicateKey && preg_match('/Duplicate entry \'.*?\' for key \'([^\']+)\'/', $e->getMessage(), $errorMatches) && ($duplicateKeyName = $errorMatches[1]) && ($duplicateKeyName == 'PRIMARY' || ($duplicateKeyConfig = static::getStackedConfig('indexes', $duplicateKeyName)))) {
                     if (!empty($duplicateKeyConfig)) {
                         $keyFields = $duplicateKeyConfig['fields'];
                     } else {
                         $keyFields = array();
                         foreach (static::getClassFields() as $fieldName => $fieldConfig) {
                             if (!empty($fieldConfig['primary'])) {
                                 $keyFields[] = $fieldName;
                             }
                         }
                     }
                     $keyValues = array_intersect_key($recordValues, array_flip($keyFields));
                     $deltaValues = array_diff_key($recordValues, array_flip(array('Created', 'CreatorID')));
                     DB::nonQuery('UPDATE `%s` SET %s WHERE %s', array(static::$tableName, join(',', static::_mapValuesToSet($deltaValues)), join(' AND ', static::_mapConditions($keyValues))));
                     $this->_record = static::getRecordByWhere($keyValues);
                     $this->_isPhantom = false;
                     $this->_isUpdated = true;
                 } else {
                     throw $e;
                 }
             }
         } elseif (count($set)) {
             DB::nonQuery('UPDATE `%s` SET %s WHERE `%s` = %u', array(static::$tableName, join(',', $set), static::_cn('ID'), $this->ID));
             $this->_isUpdated = true;
         }
         // clear cache
         static::_invalidateRecordCaches($this->ID);
         // update state
         $this->_isDirty = false;
     }
     // traverse relationships again
     if ($deep) {
         $this->_postSaveRelationships();
     }
     // fire event
     Emergence\EventBus::fireEvent('afterRecordSave', $this->getRootClass(), array('Record' => $this, 'deep' => $deep));
 }
Ejemplo n.º 3
0
function moveDown($table, $priority_field, $id_field, $id, $priority = null)
{
    if ($priority === null) {
        $r = DB::query('SELECT ' . $priority_field . ' as priority FROM ' . $table . ' WHERE ' . $id_field . '="' . $id . '"');
        $rating = $r[0]['priority'];
    } else {
        $rating = $priority;
    }
    $minq = DB::query('SELECT ' . $id_field . ' as id, ' . $priority_field . '  as priority FROM ' . $table . ' WHERE ' . $priority_field . '=(SELECT min(' . $priority_field . ') FROM ' . $table . ' WHERE ' . $priority_field . '>' . $rating . ')');
    if (!$minq) {
        return;
    }
    $minid = $minq[0]['id'];
    $min = $minq[0]['priority'];
    return $down = DB::multiQuery('UPDATE ' . $table . ' SET ' . $priority_field . '="' . $min . '" WHERE ' . $id_field . '="' . $id . '";UPDATE ' . $table . ' SET ' . $priority_field . '="' . $rating . '" WHERE ' . $id_field . '="' . $minid . '"');
}