Esempio n. 1
0
 protected function _safeQuote($str)
 {
     $str = $this->_dbh->quote($str);
     if (!strstr($str, ':')) {
         return $str;
     }
     // workaround: replace '\0:D\0' to CONCAT('\0:','D\0')
     return "CONCAT(" . join(":','", explode(':', $str)) . ")";
 }
Esempio n. 2
0
 /**
  *	Updates used space on device.
  *
  *	@return		void
  */
 public function devChSpace($devId, $size)
 {
     if ($size == 0) {
         return;
     }
     $cnt = $this->_dbh->exec("UPDATE {$this->_devTable} SET used_size = used_size + ? WHERE id = ? AND used_size + ? < total_size", $size, $devId, $size);
     if ($cnt != 1) {
         throw new RM_ObjectFs_Exception_Unavailable("Not enough space on device#{$devId} for {$size} byte(s)");
     }
 }
Esempio n. 3
0
 protected function _saveObjectHelper($insert, RM_Store_Object $object, RM_Validator_iValidator $validator = NULL)
 {
     if (isNull($validator)) {
         $validator = $object->validator();
     }
     if (!isNull($validator) && !$validator->check($object->props())) {
         return FALSE;
     }
     $meta = $this->_mediator->meta;
     $fields = array();
     $allFields = array();
     $props = $object->props();
     foreach ($meta->propList() as $prop) {
         $allFields[$meta->dbField($prop)] = $props[$prop];
         if ($insert or $props[$prop] !== $object->_propInitial($prop)) {
             $field = $meta->dbQuoted($prop);
             $fields[$insert ? $field : "{$field} = ?"] = $props[$prop];
         }
     }
     if (!$fields) {
         return FALSE;
     }
     if ($insert) {
         $binds = $fields;
         $query = "INSERT INTO {$this->_table}(" . join(',', array_keys($fields)) . ") VALUES(" . sqlBinds($fields) . ")";
     } else {
         list($where, $wbinds) = $this->_getDbWhere($object);
         $binds = array_merge($fields, $wbinds);
         $query = "UPDATE {$this->_table} SET " . join(',', array_keys($fields)) . " WHERE {$where}";
     }
     $this->_dbh->exec($query, array_values($binds));
     if (!isNull($this->_tableHistory)) {
         $obHistory = M('Store')->history();
         if ($obHistory->_closeStamp() || $object->isChildren()) {
             list($where, $wbinds) = $this->_getDbWhere($object);
             $query = "UPDATE {$this->_tableHistory} SET stamp_close = " . $obHistory->getStamp() . ", last_state = 0 WHERE " . $where . " AND last_state = 1";
             $this->_dbh->exec($query, $wbinds);
         }
         $binds = array_merge(array('stamp_open' => $obHistory->getStamp(), 'stamp_close' => $obHistory->getFarFuture(), 'last_state' => 1), $allFields);
         $query = "REPLACE INTO {$this->_tableHistory}( " . join(',', array_keys($binds)) . ") VALUES(" . sqlBinds($binds) . ")";
         $this->_dbh->exec($query, array_values($binds));
         $obHistory->register($object, $insert ? RM_Store_History::OBJECT_CREATE : RM_Store_History::OBJECT_EDIT);
     }
     if ($this->_autoId and !$object->{$this->_autoId}) {
         $object->{$this->_autoId} = $this->_dbh->lastInsertID();
         $allFields[$meta->dbField($this->_autoId)] = $object->{$this->_autoId};
     }
     if ($this->_cacheNs) {
         $this->cacheUpdate($object, $allFields);
     }
     return TRUE;
 }
Esempio n. 4
0
 /**
  *	Executes query and return either Iterator (for SELECT queries) or number of affected rows
  *	(for other query types).
  *
  *	@return		Iterator | int
  */
 public function execute()
 {
     $temp = M('Db')->getConnection()->getConnectionName();
     M('Db')->setCurrentConnection($this->_connection->getConnectionName());
     list($sql, $binds) = $this->render();
     $res = $this->_command == 'select' ? M('Db')->query($sql, $binds) : M('Db')->exec($sql, $binds);
     M('Db')->setCurrentConnection($temp);
     return $res;
 }
Esempio n. 5
0
 /**
  *	Prepare SQL query.
  *	Use replica connection for SELECT queries. Use master server for others.
  *	@see RM_Db_Connection::prepare for details
  */
 public function prepare($sql, $driver_options = array())
 {
     $sql = trim($sql);
     if ($this->_log) {
         $sql_log = substr(str_replace("\n", " ", $sql), 0, 60);
     }
     if (stristr($sql, 'select') === $sql) {
         if ($this->getReplicaConnection()) {
             if ($this->_log) {
                 M('Log')->record('replica', 'slave-' . $this->getReplicaConnection()->getConnectionName(), $sql_log);
             }
             return $this->getReplicaConnection()->prepare($sql);
         }
     } else {
         if (stristr($sql, 'set') !== $sql) {
             $this->useReplica(FALSE);
         }
     }
     if ($this->_log) {
         M('Log')->record('replica', 'master-' . $this->getConnectionName(), $sql_log);
     }
     return parent::prepare($sql);
 }