[Solar-svn] Revision 2836
pmjones at solarphp.com
pmjones at solarphp.com
Sat Oct 6 11:24:33 CDT 2007
removed deprecated class sets: Solar_Sql_Result, Table, Row, and Rowset
Deleted: trunk/Solar/Sql/Result.php
===================================================================
--- trunk/Solar/Sql/Result.php 2007-10-06 16:20:43 UTC (rev 2835)
+++ trunk/Solar/Sql/Result.php 2007-10-06 16:24:33 UTC (rev 2836)
@@ -1,224 +0,0 @@
-<?php
-/**
- *
- * Class for iterating through selected row results.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Class for iterating through selected row results.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- * @deprecated
- *
- */
-class Solar_Sql_Result extends Solar_Base implements Iterator {
-
- /**
- *
- * User-defined configuration keys.
- *
- * Keys are ...
- *
- * `PDOStatement`
- * : (object) A PDOStatement object to be used as the
- * result source.
- *
- * @var array
- *
- */
- protected $_Solar_Sql_Result = array(
- 'PDOStatement' => null,
- );
-
- /**
- *
- * Collection of rows fetched from the result source.
- *
- * @var array
- *
- */
- protected $_rows = array();
-
- /**
- *
- * Pointer to the current iteration.
- *
- * @var int
- *
- */
- protected $_curr = null;
-
- /**
- *
- * Have we filled Solar_Sql_Result::$_rows with all results?
- *
- * @var bool
- *
- */
- protected $_full = false;
-
- /**
- *
- * Constructor.
- *
- * @param array $config User-defined configuration.
- *
- */
- public function __construct($config = null)
- {
- parent::__construct($config);
- if (! ($this->_config['PDOStatement'] instanceof PDOStatement)) {
- throw $this->_exception('ERR_NOT_PDOSTATEMENT');
- }
- }
-
- /**
- *
- * Rewinds the iterator back to the beginning.
- *
- * @return void
- *
- */
- public function rewind()
- {
- $this->_curr = 0;
- }
-
- /**
- *
- * Returns the current iterator key.
- *
- * @return int The current iterator key.
- *
- */
- public function key()
- {
- return $this->_curr;
- }
-
- /**
- *
- * Increments the iterator key.
- *
- * @return int The incremented iterator key.
- *
- */
- public function next()
- {
- // increment the counter
- $this->_curr += 1;
-
- // populate the row
- $this->_fetch();
-
- // return the incremented value
- return $this->_curr;
- }
-
- /**
- *
- * Determines if the current iterator key is valid.
- *
- * Also populates the current row from the result source.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function valid()
- {
- // do we already have a row in the current position?
- if (! empty($this->_rows[$this->_curr])) {
- return true;
- } else {
- // populate the current position
- return $this->_fetch();
- }
- }
-
- /**
- *
- * Returns the current row for the iterator.
- *
- * @return mixed The current row element.
- *
- */
- public function current()
- {
- return $this->_rows[$this->_curr];
- }
-
- /**
- *
- * Returns the next row from the result source.
- *
- * @return mixed Boolean false if there is no
- * next row, or the next row element.
- *
- */
- public function fetch()
- {
- if ($this->valid()) {
- $row = $this->_rows[$this->_curr];
- $this->next();
- return $row;
- } else {
- return false;
- }
- }
-
- /**
- *
- * Returns all rows from the result source.
- *
- * @return array An array of row elements.
- *
- */
- public function fetchAll()
- {
- if (! $this->_full) {
- // populate all of $_rows
- foreach ($this as $val) {}
- }
-
- // return them all
- return $this->_rows;
- }
-
- /**
- *
- * Support method to populate Solar_Sql_Result::$_rows from the result source.
- *
- * @return bool True if a row was populated, false if not.
- *
- */
- protected function _fetch()
- {
- // is there a next row?
- $data = $this->_config['PDOStatement']->fetch(PDO::FETCH_ASSOC);
- if (! $data) {
- // no new rows, which means the $_rows array
- // must be fully populated.
- $this->_full = true;
- return false;
- }
-
- // found a row, retain it internally
- $this->_rows[$this->_curr] = $data;
- return true;
- }
-}
Deleted: trunk/Solar/Sql/Row.php
===================================================================
--- trunk/Solar/Sql/Row.php 2007-10-06 16:20:43 UTC (rev 2835)
+++ trunk/Solar/Sql/Row.php 2007-10-06 16:24:33 UTC (rev 2836)
@@ -1,131 +0,0 @@
-<?php
-/**
- *
- * Represents a single row of SQL SELECT results, optionally with a
- * source object to support saving the data.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Represents a single row of SQL SELECT results, optionally with a
- * source object to support saving the data.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- */
-class Solar_Sql_Row extends Solar_Struct {
-
- /**
- *
- * User-defined configuration values.
- *
- * Keys are ...
- *
- * `data`
- * : (array) Key-value pairs of colname => value.
- *
- * `save`
- * : (object) Source object for save() calls.
- *
- * @var array
- *
- */
- protected $_Solar_Sql_Row = array(
- 'data' => array(),
- 'save' => null,
- );
-
- /**
- *
- * Source object for save() calls.
- *
- * @var object
- *
- */
- protected $_save;
-
- /**
- *
- * Constructor.
- *
- * @param array $config User-defined values.
- *
- */
- public function __construct($config = null)
- {
- parent::__construct($config);
- if ($this->_config['save']) {
- $this->setSave($this->_config['save']);
- }
- }
-
- /**
- *
- * Saves the current values via the $this->_save object.
- *
- * @return void
- *
- * @see Solar_Sql_Row::$_save
- *
- * @see Solar_Sql_Row::setSave()
- *
- * @see Solar_Sql_Row::getSave()
- *
- */
- public function save()
- {
- if ($this->_save) {
- $result = $this->_save->save($this->_data);
- foreach ($result as $key => $val) {
- $this->$key = $val;
- }
- }
- }
-
- /**
- *
- * Sets the source object for save() calls.
- *
- * @param object $obj A source object with a save() method. Use
- * null to explicitly turn off saving.
- *
- * @return void
- *
- */
- public function setSave($obj)
- {
- if (is_null($obj)) {
- $this->_save = null;
- } elseif (is_callable(array($obj, 'save'))) {
- $this->_save = $obj;
- } else {
- $this->_save = null;
- throw $this->_exception('ERR_SAVE_NOT_CALLABLE');
- }
- }
-
- /**
- *
- * Gets the current source object for save() calls.
- *
- * @return void
- *
- */
- public function getSave()
- {
- return $this->_save;
- }
-}
Deleted: trunk/Solar/Sql/Rowset.php
===================================================================
--- trunk/Solar/Sql/Rowset.php 2007-10-06 16:20:43 UTC (rev 2835)
+++ trunk/Solar/Sql/Rowset.php 2007-10-06 16:24:33 UTC (rev 2836)
@@ -1,149 +0,0 @@
-<?php
-/**
- *
- * Represents multiple Solar_Sql_Row objects from a SELECT.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Represents multiple Solar_Sql_Row objects from a SELECT.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- */
-class Solar_Sql_Rowset extends Solar_Sql_Row {
-
- /**
- *
- * The class of individual row objects.
- *
- * @var string
- *
- */
- protected $_row_class = 'Solar_Sql_Row';
-
- /**
- *
- * Individual row objects.
- *
- * @var array
- *
- */
- protected $_rows = array();
-
- /**
- *
- * ArrayAccess: get a key value.
- *
- * @param string $key The requested key.
- *
- * @return mixed
- *
- */
- public function offsetGet($key)
- {
- // don't return rows that don't exist in the original data
- if (empty($this->_data[$key])) {
- return null;
- }
-
- // load the row if needed
- if (empty($this->_rows[$key])) {
- $this->_rows[$key] = Solar::factory(
- $this->_row_class,
- array(
- 'data' => $this->_data[$key],
- 'save' => $this->_save,
- )
- );
- }
-
- // return the row
- return $this->_rows[$key];
- }
-
- /**
- *
- * ArrayAccess: set a key value (not allowed).
- *
- * @param string $key The requested key.
- *
- * @param string $val The value to set it to.
- *
- * @return void
- *
- */
- public function offsetSet($key, $val)
- {
- }
-
- /**
- *
- * ArrayAccess: unset a key (not allowed).
- *
- * @param string $key The requested key.
- *
- * @return void
- *
- */
- public function offsetUnset($key)
- {
- }
-
- /**
- *
- * Iterator: get the current key value.
- *
- * @return mixed
- *
- */
- public function current()
- {
- return $this->offsetGet($this->key());
- }
-
- /**
- *
- * Returns a copy of the object data as an array.
- *
- * @return array
- *
- */
- public function toArray()
- {
- $array = array();
- foreach ($this as $row) {
- $array[] = $row->toArray();
- }
- return $array;
- }
-
- /**
- *
- * Load new data (not allowed).
- *
- * @param array $data An array of new data.
- *
- * @param bool $reset Blank out the data array first so that only keys
- * in the $spec will be in the struct.
- *
- * @return void
- *
- */
- public function load($data, $reset = false)
- {
- }
-}
Deleted: trunk/Solar/Sql/Table.php
===================================================================
--- trunk/Solar/Sql/Table.php 2007-10-06 16:20:43 UTC (rev 2835)
+++ trunk/Solar/Sql/Table.php 2007-10-06 16:24:33 UTC (rev 2836)
@@ -1,1104 +0,0 @@
-<?php
-/**
- *
- * Class for representing an SQL table.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Class for representing an SQL table.
- *
- * @category Solar
- *
- * @package Solar_Sql
- *
- */
-class Solar_Sql_Table extends Solar_Base {
-
- /**
- *
- * User-provided configuration.
- *
- * Keys are ...
- *
- * `sql`
- * : (dependency) A Solar_Sql dependency object.
- *
- * `paging`
- * : (int) Number of rows per page.
- *
- * `create`
- * : (bool) Attempt to auto-create the table?
- *
- * @var array
- *
- */
- protected $_Solar_Sql_Table = array(
- 'sql' => 'sql',
- 'paging' => 10,
- 'create' => true,
- );
-
- /**
- *
- * The table name.
- *
- * @var string
- *
- */
- protected $_name = null;
-
- /**
- *
- * The default order when fetching rows.
- *
- * @var array
- *
- */
- protected $_order = array('id');
-
- /**
- *
- * The numer of rows per page when selecting.
- *
- * @var int
- *
- */
- protected $_paging = 10;
-
- /**
- *
- * Have we connected to the database?
- *
- * @var bool
- *
- */
- protected $_connected = false;
-
- /**
- *
- * The column specification array for all columns in this table.
- *
- * Each element in this array looks like this...
- *
- * {{code: php
- * $col = array(
- * 'col_name' => array(
- * 'name' => (string) the col_name, same as the key
- * 'type' => (string) char, varchar, date, etc
- * 'size' => (int) column size
- * 'scope' => (int) decimal places
- * 'valid' => (array) Solar_Valid methods and args
- * 'require' => (bool) is this a required (non-null) column?
- * 'autoinc' => (bool) auto-increment
- * 'default' => (string|array) default value
- * 'primary' => (bool) is this part of the primary key?
- * ),
- * );
- * }}
- *
- * @var array
- *
- */
- protected $_col = array();
-
- /**
- *
- * The index specification array for all indexes on this table.
- *
- * The array should be in this format ...
- *
- * {{code: php
- * // the index type: 'normal' or 'unique'
- * $type = 'normal';
- *
- * // index on a single column:
- * // CREATE INDEX idx_name ON table_name (col_name)
- * $this->_idx['idx_name'] = array($type, 'col_name');
- *
- * // index on multiple columns:
- * // CREATE INDEX idx_name ON table_name (col1, col2, ... colN)
- * $this->_idx['idx_name'] = array(
- * $type,
- * array('col1', 'col2', ..., 'colN')
- * );
- *
- * // easy shorthand for an index on a single column,
- * // giving the index the same name as the column:
- * // CREATE INDEX col_name ON table_name (col_name)
- * $this->_idx['col_name'] = $type;
- * }}
- *
- * The $type may be 'normal' or 'unique'.
- *
- * @var array
- *
- * @see addIndex()
- *
- */
- protected $_idx = array();
-
- /**
- *
- * A Solar_Sql dependency object.
- *
- * @var Solar_Sql
- *
- */
- protected $_sql = null;
-
- /**
- *
- * The object class returned by fetch(), fetchNew(), and fetchRow().
- *
- * @var string
- *
- */
- protected $_row_class = 'Solar_Sql_Row';
-
- /**
- *
- * The object class returned by fetchAll().
- *
- * @var string
- *
- */
- protected $_all_class = 'Solar_Sql_Rowset';
-
- /**
- *
- * Constructor.
- *
- * @param array $config User-provided configuration values.
- *
- * @return void
- *
- */
- public function __construct($config = null)
- {
- // main construction
- parent::__construct($config);
- $this->setPaging($this->_config['paging']);
-
- // perform column and index setup, then fix everything.
- $this->_setup();
- $this->_autoSetup();
- }
-
- /**
- *
- * Connects to the database and auto-creates the table if requested.
- *
- * @return void
- *
- */
- protected function _connect()
- {
- if (! $this->_connected) {
- $this->_sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
-
- if ($this->_config['create']) {
- $this->_autoCreate();
- }
-
- $this->_connected = true;
- }
- }
-
- /**
- *
- * Allows reading of protected properties.
- *
- * @param string $key The property name.
- *
- * @return mixed The property value.
- *
- */
- public function __get($key = null)
- {
- $prop = array('col', 'idx', 'name', 'paging', 'all_class', 'row_class', 'order', );
- if (in_array($key, $prop)) {
- $key = "_$key";
- return $this->$key;
- } else {
- return null;
- }
- }
-
- /**
- *
- * Sets the number of rows per page.
- *
- * @param int $val The number of rows per page.
- *
- * @return void
- *
- */
- public function setPaging($val)
- {
- $this->_paging = (int) $val;
- }
-
- /**
- *
- * Sets the class returned by fetchRow() calls.
- *
- * @param string $class The class name. If empty, uses 'Solar_Sql_Row'.
- *
- * @return void
- *
- */
- public function setFetchRowClass($class)
- {
- if (! $class) {
- $class = 'Solar_Sql_Row';
- }
- $this->_row_class = $class;
- }
-
- /**
- *
- * Sets the class returned by fetchAll() calls.
- *
- * @param string $class The class name. If empty, uses 'Solar_Sql_Rowset'.
- *
- * @return void
- *
- */
- public function setFetchAllClass($class)
- {
- if (! $class) {
- $class = 'Solar_Sql_Rowset';
- }
- $this->_all_class = $class;
- }
-
- /**
- *
- * Returns a fully-qualified column name ("tablename.colname").
- *
- * If the column does not exist in the table, returns null.
- *
- * @param string $key The column name itself.
- *
- * @return string The column name prefixed with the table name.
- *
- */
- public function getColName($key)
- {
- if (! empty($this->_col[$key])) {
- return $this->_name . '.' . $key;
- }
- }
-
- /**
- *
- * Inserts or updates a single row based on its ID.
- *
- * @param array $data An associative array of data to be saved, in
- * the format (field => value).
- *
- * @return array The data as inserted or updated.
- *
- */
- public function save($data)
- {
- $this->_connect();
- if (empty($data['id'])) {
- return $this->insert($data);
- } else {
- $where = $this->_sql->quoteInto('id = ?', $data['id']);
- return $this->update($data, $where);
- }
- }
-
- /**
- *
- * Validates and inserts data into the table.
- *
- * @param array $data An associative array of data to be inserted, in
- * the format (field => value).
- *
- * @return array The data as inserted.
- *
- */
- public function insert($data)
- {
- // set defaults
- $data = array_merge($this->fetchNew()->toArray(), (array) $data);
-
- // auto-add sequential values
- foreach ($this->_col as $colname => $colinfo) {
- // does this column autoincrement, and is no data provided?
- if ($colinfo['autoinc'] && empty($data[$colname])) {
- $data[$colname] = $this->increment($colname);
- }
- }
-
- // add created/updated timestamps
- $now = date('Y-m-d\TH:i:s');
-
- if (empty($data['created'])) {
- $data['created'] = $now;
- }
-
- if (empty($data['updated'])) {
- $data['updated'] = $now;
- }
-
- // validate and recast the data.
- $result = $this->_autoValid($data);
-
- // attempt the insert.
- $this->_connect();
- $result = $this->_sql->insert($this->_name, $data);
-
- // return the data as inserted
- return $data;
- }
-
- /**
- *
- * Validates and updates data in the table based on a WHERE clause.
- *
- * @param array $data An associative array of data to be updated, in
- * the format (field => value).
- *
- * @param string $where An SQL WHERE clause limiting the updated
- * rows.
- *
- * @return array The data as updated.
- *
- */
- public function update($data, $where)
- {
- // retain primary key data in this array for return values
- $retain = array();
-
- // disallow the changing of primary key data
- foreach (array_keys($data) as $field) {
- if (! empty($this->_col[$field]['primary'])) {
- $retain[$field] = $data[$field];
- unset($data[$field]);
- }
- }
-
- // forcibly set the "updated" timestamp
- $data['updated'] = date('Y-m-d\TH:i:s');
-
- // validate and recast the data
- $result = $this->_autoValid($data);
-
- // attempt the update
- $this->_connect();
- $result = $this->_sql->update($this->_name, $data, $where);
-
- // restore retained primary key data and return
- $data = array_merge($data, $retain);
- return $data;
- }
-
- /**
- *
- * Deletes rows in the table based on a WHERE clause.
- *
- * @param string $where An SQL WHERE clause limiting the deleted rows.
- *
- * @return void
- *
- */
- public function delete($where)
- {
- $this->_connect();
- $result = $this->_sql->delete($this->_name, $where);
- return $result;
- }
-
- /**
- *
- * Convenience method to select rows from this table.
- *
- * @param string $type The type of select to execute: 'all', 'one',
- * 'value', etc. Default is 'pdo'.
- *
- * @param string|array $where A Solar_Sql_Select::multiWhere() parameter.
- *
- * @param string|array $order A Solar_Sql_Select::order() parameter.
- *
- * @param int $page The page number of rows to fetch.
- *
- * @return mixed
- *
- */
- public function select($type = 'pdo', $where = null, $order = null,
- $page = null)
- {
- if ($type == 'rowset') {
- $class = $this->_all_class;
- } elseif ($type == 'row') {
- $class = $this->_row_class;
- } else {
- $class = null;
- }
-
- $select = $this->_newSelect();
- $result = $select->from($this->_name, array_keys($this->_col))
- ->multiWhere($where)
- ->order($order)
- ->setPaging($this->_paging)
- ->limitPage($page)
- ->fetch($type, $class);
-
- if ($result instanceof Solar_Sql_Row) {
- $result->setSave($this);
- }
-
- return $result;
- }
-
- /**
- *
- * Increments and returns the sequence value for a column.
- *
- * @param string $name The column name.
- *
- * @return int The next sequence number for the column.
- *
- */
- public function increment($name)
- {
- $this->_connect();
- // only increment if auto-increment is set
- if (! empty($this->_col[$name]['autoinc'])) {
- // table__column
- $seqname = $this->_name . '__' . $name;
- $result = $this->_sql->nextSequence($seqname);
- return $result;
- } else {
- return null;
- }
- }
-
- /**
- *
- * Fetches one row from the table by its primary key ID.
- *
- * @param int $id The primary key ID value.
- *
- * @return Solar_Sql_Row
- *
- */
- public function fetch($id)
- {
- $where = array('id = ?' => $id);
- return $this->select('row', $where);
- }
-
- /**
- *
- * Fetches all rows by arbitrary criteria.
- *
- * @param string|array $where A Solar_Sql_Select::multiWhere() parameter.
- *
- * @param string|array $order A Solar_Sql_Select::order() parameter.
- *
- * @param int $page The page number of rows to fetch.
- *
- * @return Solar_Sql_Rowset
- *
- */
- public function fetchAll($where = null, $order = null, $page = null)
- {
- return $this->select('rowset', $where, $order, $page);
- }
-
- /**
- *
- * Fetches one row by arbitrary criteria.
- *
- * @param string|array $where A Solar_Sql_Select::multiWhere() parameter.
- *
- * @param string|array $order A Solar_Sql_Select::order() parameter.
- *
- * @return Solar_Sql_Row
- *
- */
- public function fetchRow($where = null, $order = null)
- {
- return $this->select('row', $where, $order);
- }
-
- /**
- *
- * Returns a new row of column keys and default values.
- *
- * @return Solar_Sql_Row
- *
- */
- public function fetchNew()
- {
- // the array of default data
- $data = array();
-
- // loop through each specified column and collect default data
- $spec = array_keys($this->_col);
- foreach ($spec as $name) {
-
- // skip columns that don't exist
- if (empty($this->_col[$name])) {
- continue;
- }
-
- // get the column info
- $info = $this->_col[$name];
-
- // is there a default set?
- if (empty($info['default'])) {
- // no default, so it's null.
- $data[$name] = null;
- continue;
- }
-
- // yes, so get it based on the kind of default.
- // we shift off the front of the array as we go.
- // element 0 is the type (literal or callback),
- // element 1 is the literal (or callback name),
- // elements 2+ are any arguments for a callback.
- $type = array_shift($info['default']);
- switch ($type) {
-
- case 'callback':
- $func = array_shift($info['default']);
- $data[$name] = call_user_func_array($func, $info['default']);
- break;
-
- case 'literal':
- $data[$name] = array_shift($info['default']);
- break;
-
- default:
- $data[$name] = null;
- }
- }
-
- // done!
- $row = Solar::factory($this->_row_class, array('data' => $data));
- $row->setSave($this);
- return $row;
- }
-
- /**
- *
- * Fetches one row by arbitrary criteria.
- *
- * @param string|array $where A Solar_Sql_Select::multiWhere() parameter.
- *
- * @param string|array $order A Solar_Sql_Select::order() parameter.
- *
- * @return Solar_Sql_Row
- *
- * @deprecated Use Solar_Sql_Table::fetchRow() instead.
- *
- */
- public function fetchWhere($where = null, $order = null)
- {
- return $this->select('row', $where, $order);
- }
-
- // -----------------------------------------------------------------
- //
- // Support and management methods.
- //
- // -----------------------------------------------------------------
-
- /**
- *
- * Gets a new Solar_Sql_Select tool, with the proper SQL object injected
- * automatically.
- *
- * Note: this forces a database connection so that we have the SQL object
- * properly constructed.
- *
- * @return Solar_Sql_Select
- *
- */
- protected function _newSelect()
- {
- $this->_connect();
- return Solar::factory(
- 'Solar_Sql_Select',
- array('sql' => $this->_sql)
- );
- }
-
- /**
- *
- * Use this to set up extended table objects.
- *
- * @return void
- *
- */
- protected function _setup()
- {
- }
-
- /**
- *
- * Fixes the $col and $idx properties after user setup.
- *
- * @return void
- *
- */
- protected function _autoSetup()
- {
- // make sure there's a table name. defaults to the
- // part after the last underscore, then converts camelCaps
- // to underscore_words.
- if (empty($this->_name)) {
-
- // get the class name
- $tmp = get_class($this);
-
- // get the part after the last underscore
- $pos = strrpos($tmp, '_');
- if ($pos !== false) {
- $tmp = substr($tmp, $pos + 1);
- }
-
- // camels to unders
- $this->_name = preg_replace('/([a-z])([A-Z])/', "$1_$2", $tmp);
- }
-
- // make sure table name is lower case regardless
- $this->_name = strtolower($this->_name);
-
- // a baseline column definition
- $basecol = array(
- 'name' => null,
- 'type' => null,
- 'size' => null,
- 'scope' => null,
- 'primary' => false,
- 'require' => false,
- 'autoinc' => false,
- 'default' => null,
- 'valid' => array(),
- );
-
- // baseline index definition
- $baseidx = array(
- 'name' => null,
- 'type' => 'normal',
- 'cols' => null,
- );
-
- // auto-added columns and indexes
- $autocol = array();
- $autoidx = array();
-
- // auto-add an ID column and index for unique identification
- if (! array_key_exists('id', $this->_col)) {
- $autocol['id'] = array(
- 'type' => 'int',
- 'primary' => true,
- 'require' => true,
- 'autoinc' => true,
- );
-
- $autoidx['id'] = array(
- 'type' => 'unique',
- 'cols' => 'id',
- );
- }
-
- // auto-add a "created" column to track when created
- if (! array_key_exists('created', $this->_col)) {
- $autocol['created'] = array(
- 'type' => 'timestamp',
- 'default' => array('callback', 'date', 'Y-m-d\TH:i:s'),
- );
-
- $autoidx['created'] = array(
- 'type' => 'normal',
- 'cols' => 'created',
- );
- }
-
- // auto-add an "updated" column and index
- // to track when last updated
- if (! array_key_exists('updated', $this->_col)) {
- $autocol['updated'] = array(
- 'type' => 'timestamp',
- 'default' => array('callback', 'date', 'Y-m-d\TH:i:s'),
- );
-
- $autoidx['updated'] = array(
- 'type' => 'normal',
- 'cols' => 'updated',
- );
- }
-
- // merge the auto-added items on top of the rest
- $this->_col = array_merge($autocol, $this->_col);
- $this->_idx = array_merge($autoidx, $this->_idx);
-
- // fix up each column to have a full set of info
- foreach ($this->_col as $name => $info) {
-
- // fill in missing elements
- $info = array_merge($basecol, $info);
-
- // make sure there's a name
- $info['name'] = $name;
-
- // if 'valid' is a string, make the validation a simple
- // Solar_Valid method call.
- if (is_string($info['valid'])) {
- $info['valid'] = array(
- $info['valid'],
- 'VALID_' . strtoupper($info['valid']),
- );
- }
-
-
- // if 'default' is not already an array, make it
- // one as a literal. this lets you avoid the array
- // when setting up simple literals.
- if (! is_array($info['default'])) {
- $info['default'] = array('literal', $info['default']);
- }
-
- // save back into the column info
- $this->_col[$name] = $info;
- }
-
- // fix up each index to have a full set of info
- foreach ($this->_idx as $key => $val) {
-
- if (is_int($key) && is_string($val)) {
- // array('col')
- $info = array(
- 'name' => $val,
- 'type' => 'normal',
- 'cols' => array($val),
- );
- } elseif (is_string($key) && is_string($val)) {
- // array('col' => 'unique')
- $info = array(
- 'name' => $key,
- 'type' => $val,
- 'cols' => array($key),
- );
- } else {
- // array('alt' => array('type' => 'normal', 'cols' => array(...)))
- $info = array_merge($baseidx, (array) $val);
- $info['name'] = (string) $key;
- settype($info['cols'], 'array');
- }
-
- $this->_idx[$key] = $info;
- }
- }
-
- /**
- *
- * Creates the table in the database if it does not already exist.
- *
- * @return bool False if the table already existed and didn't
- * need to be created, or true if the table did not exist and was
- * successfully created.
- *
- */
- protected function _autoCreate()
- {
- // is a table with the same name already there?
- $tmp = $this->_sql->fetchTableList();
- $here = strtolower($this->_name);
- foreach ($tmp as $there) {
- if ($here == strtolower($there)) {
- // table already exists
- return false;
- }
- }
-
- // create the table itself
- $this->_sql->createTable(
- $this->_name,
- $this->_col
- );
-
- // create each of the indexes
- foreach ($this->_idx as $name => $info) {
- try {
- // create this index
- $this->_sql->createIndex(
- $this->_name,
- $info['name'],
- $info['type'] == 'unique',
- $info['cols']
- );
- } catch (Exception $e) {
- /** @todo Does this throw a TableNotCreated exception too? */
- // cancel the whole deal.
- $this->_sql->dropTable($this->_name);
- throw $e;
- }
- }
-
- // post-creation
- try {
- $this->_postCreate();
- } catch (Exception $e) {
- /** @todo Does this throw a TableNotCreated exception too? */
- // cancel the whole deal.
- $this->_sql->dropTable($this->_name);
- throw $e;
- }
-
- // creation of the table and its indexes is complete
- return true;
- }
-
- /**
- *
- * Additional table-creation tasks, such as inserting rows.
- *
- * @return void
- *
- */
- protected function _postCreate()
- {
- }
-
- /**
- *
- * Validates and recasts an array of input/update data in-place.
- *
- * @param array &$data An associative array of data as (field => value).
- * Note that this is a reference; the array will be modified in-place.
- *
- * @return void
- *
- * @todo Better error codes and exceptions?
- *
- */
- protected function _autoValid(&$data)
- {
- // object methods for validation
- $valid = Solar::factory('Solar_Valid');
-
- // low and high range values for integers
- $int_range = array(
- 'smallint' => array(pow(-2, 15), pow(+2, 15) - 1),
- 'int' => array(pow(-2, 31), pow(+2, 31) - 1),
- 'bigint' => array(pow(-2, 63), pow(+2, 63) - 1)
- );
-
- // collect all errors captured for all fields
- $err = array();
-
- // the list of available fields; discard data that
- // does not correspond to one of the known fields.
- $known = array_keys($this->_col);
-
- // loop through each data field
- foreach ($data as $field => $value) {
-
- // is this field recognized?
- if (! in_array($field, $known)) {
- // drop it and loop to the next field.
- unset($data[$field]);
- continue;
- }
-
- // if 'require' not present, it's not required
- if (isset($this->_col[$field]['require'])) {
- $require = $this->_col[$field]['require'];
- } else {
- $require = false;
- }
-
- // if null and required, it's not valid.
- if ($require && is_null($value)) {
- $err[$field][] = array(
- 'code' => 'VALID_NOTBLANK',
- 'text' => $this->locale('VALID_NOTBLANK'),
- 'data' => $value,
- 'info' => array(),
- );
- continue;
- }
-
- // if null and not required, it's valid.
- if (! $require && is_null($value)) {
- continue;
- }
-
-
- // -------------------------------------------------------------
- //
- // Recast first, then validate for column type
- //
-
- $type = $this->_col[$field]['type'];
- switch ($type) {
-
- case 'bool':
- $value = ($value) ? 1 : 0;
- break;
-
- case 'char':
- case 'varchar':
- settype($value, 'string');
- $max = $this->_col[$field]['size'];
- if (! $valid->maxLength($value, $max, Solar_Valid::OR_BLANK)) {
- $err[$field][] = array(
- 'code' => 'VALID_MAXLENGTH',
- 'text' => $this->locale('VALID_MAXLENGTH'),
- 'data' => $value,
- 'info' => array(
- 'max' => $max,
- ),
- );
- }
- break;
-
- case 'int':
- case 'bigint':
- case 'smallint':
- settype($value, 'int');
- $min = $int_range[$type][0];
- $max = $int_range[$type][1];
- if (! $valid->range($value, $min, $max)) {
- $err[$field][] = array(
- 'code' => 'VALID_RANGE',
- 'text' => $this->locale('VALID_RANGE'),
- 'data' => $value,
- 'info' => array(
- 'min' => $min,
- 'max' => $max,
- ),
- );
- }
- break;
-
- case 'float':
- settype($value, 'float');
- break;
-
- case 'numeric':
- settype($value, 'float');
- $size = $this->_col[$field]['size'];
- $scope = $this->_col[$field]['scope'];
- if (! $valid->scope($value, $size, $scope)) {
- $err[$field][] = array(
- 'code' => 'VALID_SCOPE',
- 'text' => $this->locale('VALID_SCOPE'),
- 'data' => $value,
- 'info' => array(
- 'size' => $size,
- 'scope' => $scope,
- ),
- );
- }
- break;
-
- case 'date':
- settype($value, 'string');
- if (! $valid->isoDate($value)) {
- $err[$field][] = array(
- 'code' => 'VALID_DATE',
- 'text' => $this->locale('VALID_DATE'),
- 'data' => $value,
- 'info' => array(),
- );
- }
- break;
-
- case 'time':
- settype($value, 'string');
- if (strlen($value) == 5) {
- // add seconds if only hours and minutes
- $value .= ":00";
- }
- if (! $valid->isoTime($value)) {
- $err[$field][] = array(
- 'code' => 'VALID_TIME',
- 'text' => $this->locale('VALID_TIME'),
- 'data' => $value,
- 'info' => array(),
- );
- }
- break;
-
- case 'timestamp':
- settype($value, 'string');
- // make sure it's in the format yyyy-mm-ddThh:ii:ss
- $value = substr($value, 0, 10) . 'T' . substr($value, 11, 8);
- if (! $valid->isoTimestamp($value)) {
- $err[$field][] = array(
- 'code' => 'VALID_TIMESTAMP',
- 'text' => $this->locale('VALID_TIMESTAMP'),
- 'data' => $value,
- 'info' => array(),
- );
- }
- break;
- }
-
- // -------------------------------------------------------------
- //
- // Content validations, if any
- //
-
- if ($this->_col[$field]['valid']) {
-
- // the error code if validation fails.
- // (0 is the method, 1 is the message, 2... are params)
- $code = $this->_col[$field]['valid'][1];
-
- // if there was a message returned, then validation failed.
- if ($valid->feedback($value, $this->_col[$field]['valid'])) {
- $err[$field][] = array(
- 'code' => $code,
- 'text' => $this->locale($code),
- 'data' => $value,
- 'info' => array(),
- );
- }
- }
-
- // ---------------------------------------------------------
- //
- // Retain the recasted and validated value, since it was
- // passed by reference.
- //
-
- $data[$field] = $value;
-
-
- } // endforeach()
-
-
- // -------------------------------------------------------------
- //
- // Done.
- //
-
- if ($err) {
- // there were errors, throw an exception
- throw $this->_exception('ERR_INVALID_DATA', $err);
- }
- }
-}
More information about the Solar-svn
mailing list