protected function _compile_conditions(Database $db, array $conditions) { foreach ($conditions as &$group) { foreach ($group as &$condition) { if (is_array($condition)) { $condition[0] = Jam_Query_Builder::resolve_attribute_name($condition[0], $this->meta()->model(), $condition[2]); } } } return parent::_compile_conditions($db, $conditions); }
/** * Init a SELECT builder. * @param string $table * @param string|array $aData * @return bool|object */ public function select($table, $aData = '*') { $db = new Database_Query_Builder_Select($table, $this->config_scope, $this->config); $db->setFields($aData); return $db; }
public function export() { $export_dir = Kohana::$cache_dir . DIRECTORY_SEPARATOR . 'exports'; @mkdir($export_dir); $filename = $export_dir . DIRECTORY_SEPARATOR . uniqid() . '.csv'; if (Kohana::$is_windows) { $filename = str_replace('\\', '/', $filename); } $headers_query = new Database_Query_Builder_Select(); $data_query = new Database_Query_Builder_Select(); $headers_query->union($data_query); $alias_num = 1; foreach ($this->get_columns() as $col) { $header = "'" . WebDB_Text::titlecase($col->get_name()) . "'"; $headers_query->select(DB::expr($header)); if ($col->is_foreign_key()) { $fk1_alias = "e{$alias_num}"; $alias_num++; $fk1_table = $col->get_referenced_table(); $data_query->join(array($fk1_table->get_name(), $fk1_alias), 'LEFT OUTER'); $data_query->on($this->get_name() . '.' . $col->get_name(), '=', "{$fk1_alias}.id"); $select = $fk1_alias . '.' . $fk1_table->get_title_column()->get_name(); if ($fk1_table->get_title_column()->is_foreign_key()) { $fk2_alias = "e{$alias_num}"; $alias_num++; $fk2_table = $fk1_table->get_title_column()->get_referenced_table(); $data_query->join(array($fk2_table->get_name(), $fk2_alias), 'LEFT OUTER'); $data_query->on($fk1_alias . '.' . $fk1_table->get_title_column()->get_name(), '=', "{$fk2_alias}.id"); $select = $fk2_alias . '.' . $fk2_table->get_title_column()->get_name(); } } else { $select = $this->get_name() . '.' . $col->get_name(); } $data_query->select(DB::expr("REPLACE(IFNULL({$select}, ''),'\r\n', '\n')")); } $data_query->from($this->get_name()); $this->add_get_filters(); $this->apply_filters($data_query); $data_query->outfile($filename); $headers_query->outfile(); $headers_query->execute($this->_db); if (!file_exists($filename)) { throw new Kohana_Exception("Export file not created: {$filename}"); } return $filename; }
/** * Count the number of records in the table. * * @return integer */ public function count_all() { $selects = array(); foreach ($this->_db_pending as $key => $method) { if ($method['name'] == 'select') { // Ignore any selected columns for now $selects[] = $method; unset($this->_db_pending[$key]); } } if (!empty($this->_load_with)) { foreach ($this->_load_with as $alias) { // Bind relationship $this->with($alias); } } $this->_build(Database::SELECT); $records = $this->_db_builder->from(array($this->_table_name, $this->_object_name))->select(array(DB::expr('COUNT(' . $this->_db->quote_column($this->_object_name . '.' . $this->_primary_key) . ')'), 'records_found'))->execute($this->_db)->get('records_found'); // Add back in selected columns $this->_db_pending += $selects; $this->reset(); // Return the total number of records in a table return (int) $records; }
/** * Get a single row as an associative array. * * @param integer $id The ID of the row to get. * @return array */ public function get_row($id) { $query = new Database_Query_Builder_Select(); $query->from($this->get_name()); $query->limit(1); $pk_column = $this->get_pk_column(); $pk_name = !$pk_column ? 'id' : $pk_column->get_name(); $query->where($pk_name, '=', $id); $row = $query->execute($this->_db)->current(); return $row; }
/** * Resets the query builder to an empty state. * * The query type and model is not reset. * * @return Jelly_Builder */ public function reset() { parent::reset(); $this->_set = $this->_columns = $this->_values = array(); $this->_result = NULL; // Re-register the model $this->_initialize(); return $this; }
/** * Get an array of permissions for the current user. * * @return array */ public function get_permissions() { $default_permissions = array(array('database_name' => '*', 'table_name' => '*', 'column_names' => '*', 'where_clause' => NULL, 'permission' => '*', 'identifier' => '*')); $config = Kohana::$config->load('webdb'); // See if WebDB permissions are being used. if (!isset($config->permissions) or empty($config->permissions['table'])) { return $default_permissions; } // Fully-qualify the database name. $db_name = ''; if (!empty($config->permissions['database'])) { $db_name = $config->permissions['database'] . '.'; } // For individual permissions tables per database, see if the // permissions table exists in the current database. if (empty($db_name) and !empty($config->permissions['table'])) { if ($current_db = $this->get_database()) { if (!in_array($config->permissions['table'], $current_db->list_tables())) { return $default_permissions; } } } // Finally, fetch the permissions rows. $query = new Database_Query_Builder_Select(); $query->from($db_name . $config->permissions['table']); $query->where('identifier', 'IN', array(Auth::instance()->get_user(), '*')); $rows = $query->execute($this->_db)->as_array(); return $rows; }
/** * Get an array of permissions for the current user. * * @return array */ public function get_permissions() { $default_permissions = array(array('database_name' => '*', 'table_name' => '*', 'column_names' => '*', 'where_clause' => NULL, 'permission' => '*', 'identifier' => '*')); $config = Kohana::config('webdb'); if (!isset($config->permissions) || empty($config->permissions['table'])) { return $default_permissions; } $query = new Database_Query_Builder_Select(); $query->from($config['database'] . '.' . $config['table']); $query->where('identifier', 'IN', array(Auth::instance()->get_user(), '*')); $rows = $query->execute($this->_db)->as_array(); return $rows; }