public static function delete_all($options = array()) { $cm = \ActiveRecord\ConnectionManager::instance(); $conn = $cm::get_connection('master'); $table = static::table(); $sql = new SQLBuilder($conn, $table->get_fully_qualified_table_name()); $conditions = is_array($options) ? $options['conditions'] : $options; if (is_array($conditions) && !eh_um_hash($conditions)) { call_user_func_array(array($sql, 'delete'), $conditions); } else { $sql->delete($conditions); } if (isset($options['limit'])) { $sql->limit($options['limit']); } if (isset($options['order'])) { $sql->order($options['order']); } $values = $sql->bind_values(); $ret = $conn->query($table->last_sql = $sql->to_s(), $values); return $ret->rowCount(); }
public function options_to_sql($options) { $table = array_key_exists('from', $options) ? $options['from'] : $this->get_fully_qualified_table_name(); $sql = new SQLBuilder($this->conn, $table); if (array_key_exists('joins', $options)) { $sql->joins($this->create_joins($options['joins'])); // by default, an inner join will not fetch the fields from the joined table if (!array_key_exists('select', $options)) { $options['select'] = $this->get_fully_qualified_table_name() . '.*'; } } if (array_key_exists('select', $options)) { $sql->select($options['select']); } if (array_key_exists('conditions', $options)) { if (!is_hash($options['conditions'])) { if (is_string($options['conditions'])) { $options['conditions'] = array($options['conditions']); } call_user_func_array(array($sql, 'where'), $options['conditions']); } else { if (!empty($options['mapped_names'])) { $options['conditions'] = $this->map_names($options['conditions'], $options['mapped_names']); } $sql->where($options['conditions']); } } if (array_key_exists('order', $options)) { $sql->order($options['order']); } if (array_key_exists('limit', $options)) { $sql->limit($options['limit']); } if (array_key_exists('offset', $options)) { $sql->offset($options['offset']); } if (array_key_exists('group', $options)) { $sql->group($options['group']); } if (array_key_exists('having', $options)) { $sql->having($options['having']); } return $sql; }
public function optionsToSql($options) { $table = \array_key_exists('from', $options) ? $options['from'] : $this->getFullyQualifiedTableName(); $sql = new SQLBuilder($this->conn, $table); if (\array_key_exists('joins', $options)) { $sql->joins($this->createJoins($options['joins'])); // by default, an inner join will not fetch the fields from the joined table if (!\array_key_exists('select', $options)) { $options['select'] = $this->getFullyQualifiedTableName() . '.*'; } } if (\array_key_exists('select', $options)) { $sql->select($options['select']); } if (\array_key_exists('conditions', $options)) { if (!Utils::isHash($options['conditions'])) { if (\is_string($options['conditions'])) { $options['conditions'] = [$options['conditions']]; } \call_user_func_array([$sql, 'where'], $options['conditions']); } else { if (!empty($options['mapped_names'])) { $options['conditions'] = $this->mapNames($options['conditions'], $options['mapped_names']); } $sql->where($options['conditions']); } } if (\array_key_exists('order', $options)) { $sql->order($options['order']); } if (\array_key_exists('limit', $options)) { $sql->limit($options['limit']); } if (\array_key_exists('offset', $options)) { $sql->offset($options['offset']); } if (\array_key_exists('group', $options)) { $sql->group($options['group']); } if (\array_key_exists('having', $options)) { $sql->having($options['having']); } return $sql; }
/** * Updates records using set in $options * * Does not instantiate models and therefore does not invoke callbacks * * Update all using a hash: * * <code> * YourModel::updateAll(array('set' => array('name' => "Bob"))); * </code> * * Update all using a string: * * <code> * YourModel::updateAll(array('set' => 'name = "Bob"')); * </code> * * An options array takes the following parameters: * * <ul> * <li><b>set:</b> String/hash of field names and their values to be updated with * <li><b>conditions:</b> Conditions using a string/hash/array</li> * <li><b>limit:</b> Limit number of records to update (MySQL & Sqlite only)</li> * <li><b>order:</b> A SQL fragment for ordering such as: 'name asc', 'id desc, name asc' (MySQL & Sqlite only)</li> * </ul> * * @params array $options * return integer Number of rows affected */ public static function updateAll($options = []) { $table = static::table(); $conn = static::connection(); $sql = new SQLBuilder($conn, $table->getFullyQualifiedTableName()); $sql->update($options['set']); if (isset($options['conditions']) && ($conditions = $options['conditions'])) { if (\is_array($conditions) && !isHash($conditions)) { \call_user_func_array([$sql, 'where'], $conditions); } else { $sql->where($conditions); } } if (isset($options['limit'])) { $sql->limit($options['limit']); } if (isset($options['order'])) { $sql->order($options['order']); } $values = $sql->bindValues(); $ret = $conn->query($table->last_sql = $sql->toString(), $values); return $ret->rowCount(); }