/** * Get the number of rows in the current filtered set. This leaves the * actual counting up to `$this->get_rows()`, rather than doing the query * itself, because filtering is applied in that method, and I didn't want to * duplicate that here (or anywhere else). * * @todo Rename this to `row_count()`. * @return integer */ public function count_records() { if (!$this->_row_count) { $query = new Database_Query_Builder_Select(); $query->select(array(DB::expr('COUNT(*)'), 'total')); $query->from($this->get_name()); $this->apply_filters($query); $result = $query->execute($this->_db); $total = $result->current(); $this->_row_count = $total['total']; } return $this->_row_count; }
/** * 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; }
/** * Choose the tables to select "FROM ..." * * @param mixed $tables table name or array($table, $alias) or object * @return Jelly_Builder */ public function from($tables) { $tables = func_get_args(); foreach ($tables as $i => $table) { $table = $this->_model_alias($table); parent::from($table); } 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; }