Permalink
Browse files

Merge branch 'feature-db' into develop

  • Loading branch information...
2 parents 81e22c8 + 30cae6a commit ec67e74bee272a75760b83741631e99f01de8d69 @geekforbrains committed Mar 20, 2012
View
@@ -2,12 +2,20 @@
class Db_Query extends Module {
- //public $tableName = null;
- //public $fieldNames = array();
-
+ /**
+ * Stores the current models table name, generated by the _getTableName method.
+ */
private $_table = null;
+
+ /**
+ * Stores the class name of the model.
+ */
private $_class = null;
+ /**
+ * The following properties hold strings related to methods run in this
+ * class. They are used to build queries.
+ */
private $_select = '';
private $_distinct = false;
private $_from = '';
@@ -17,19 +25,84 @@ class Db_Query extends Module {
private $_limit = '';
private $_bindings = array();
+ /**
+ * Because Caffeine may call the describe method multiple times, we
+ * store the value of each describe method for each model so its
+ * only queries once. See the describe() method for details.
+ */
+ private static $_describes = array();
+
+ /**
+ * As as the above describe property, multiple calls may be made.
+ * Store their values to avoid uneeded queries. See the exists()
+ * method for details.
+ */
+ private static $_exists = array();
+
+ /**
+ * Can be set in a model to specify wether to automatically create
+ * an auto incremented primary id. This is the default.
+ */
protected $_createId = true;
+
+ /**
+ * Can be set to automatically create the "created_at" and "updated_at"
+ * fields in a models table. In addition, each query that inserts or modifies
+ * a table, the two timestamp fields will be updated automatically.
+ */
protected $_timestamps = false;
+
+ /**
+ * An array of table fields to create, see documentation for full syntax.
+ *
+ * Example:
+ * $_fields = array(
+ * 'title' => array(
+ * 'type' => 'varchar',
+ * 'length' => 255,
+ * 'not null' => true
+ * )
+ * );
+ */
protected $_fields = array();
+
+ /**
+ * An array of field names to create indexes for.
+ */
protected $_indexes = array();
+
+ /**
+ * An array of field names to create fulltext indexes for.
+ */
protected $_fulltext = array();
- protected $_hasOne = array();
+ /**
+ NOT CURRENTLY USED, WILL BE USED WHEN PROPER ORM IS WRITTEN
+ protected $_hasOne = array();
protected $_hasMany = array();
+ */
+
+ /**
+ * An array of models that the current model is related to. This will
+ * automatically create foreign key fields based on the model names
+ * in the array.
+ *
+ * Example:
+ * $_indexes = array('module.model');
+ *
+ * blog.post creates the field "post_id"
+ * media.file creates the field "field_id"
+ */
protected $_belongsTo = array();
- protected $_hasAndBelongsToMany = array();
- private static $_describes = array();
- private static $_exists = array();
+ /**
+ * An array of models that this model has a many-to-many relationship
+ * with. The syntax is the same as the above $_belongsTo property.
+ *
+ * See the Db_Runner::_createHABTM() method for details on how a many-to-many
+ * table is created and named.
+ */
+ protected $_hasAndBelongsToMany = array();
/**
* Allow read access to private properties.
@@ -49,25 +122,10 @@ public function __construct($table = null)
if(is_null($this->_table))
$this->_table = $this->_getTableName();
- //$this->_table = $this->tableName;
$this->_from = sprintf(' FROM %s', $this->_table);
// Produce query results that are of the same class as this model
$this->_class = get_called_class();
-
- // Setup blank properties for this class based on table description
- /*
- $fields = $this->describe();
- if($fields && is_array($fields))
- {
- foreach($fields as $field)
- {
- $this->fieldNames[] = $field->Field;
- if(!isset($this->{$field->Field}))
- $this->{$field->Field} = null;
- }
- }
- */
}
/**
@@ -77,11 +135,8 @@ public function addIndex($key) {
array_push($this->_indexes, $key);
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function select()
{
@@ -97,11 +152,8 @@ public function select()
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function insert($data, $getInsertId = true)
{
@@ -130,11 +182,8 @@ public function insert($data, $getInsertId = true)
return Db::query($sql, $values, $getInsertId);
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function update($data)
{
@@ -156,11 +205,8 @@ public function update($data)
return Db::query($sql, $values);
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function delete($id = null)
{
@@ -175,23 +221,17 @@ public function delete($id = null)
return Db::query($sql, $this->_bindings);
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function truncate()
{
$this->_where = '';
return $this->delete();
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function describe()
{
@@ -214,7 +254,6 @@ public function describe()
return self::$_describes[$this->_table];
}
-
/**
* Determines if a table exists. After this first call to this, the response is stored
* to avoid multiple, unecessary calls to the database.
@@ -229,11 +268,8 @@ public function exists($force = false)
return self::$_exists[$this->_table];
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function distinct()
{
@@ -242,7 +278,6 @@ public function distinct()
}
/**
- * ---------------------------------------------------------------------------
* Regular SELECT query. If no other options are set, this will get all
* records from the current table.
*
@@ -253,7 +288,6 @@ public function distinct()
*
* Db()->table('users')->get(); // Get all records and all fields
* Db()->table('users')->get('first_name', 'age'); // Only two fields
- * ---------------------------------------------------------------------------
*/
public function get()
{
@@ -269,25 +303,19 @@ public function get()
return Db::query($sql, $this->_bindings, false, $this->_class);
}
-
/**
- * ---------------------------------------------------------------------------
* Alias of get()
- * ---------------------------------------------------------------------------
*/
public function all() {
return $this->get();
}
-
/**
- * ---------------------------------------------------------------------------
* Returns the first result from a query.
*
* Examples:
*
* Db()->table('users')->first();
- * ---------------------------------------------------------------------------
*/
public function first()
{
@@ -298,39 +326,30 @@ public function first()
return false;
}
-
/**
- * ---------------------------------------------------------------------------
* Finds a single record based on an id or slug. If an int is passed its
* assumed to be an id. Anything not an int is considered a slug.
- * ---------------------------------------------------------------------------
*/
public function find($idOrSlug)
{
$type = (is_numeric($idOrSlug)) ? 'id' : 'slug'; // Need to use is_numeric because is_int wont work with strings (ex: "12")
return $this->where($type, '=', $idOrSlug)->first();
}
-
/**
- * ---------------------------------------------------------------------------
* Counts the number of records returned from the query
*
* Examples:
*
* Db()->get('users')->count(); // Count all records
* Db()->get('users')->where('age', '>', '23')->count()
- * ---------------------------------------------------------------------------
*/
public function count($column = '*') {
return $this->select(sprintf('COUNT(%s) AS count', $column))->first()->count;
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function orderBy($column, $direction = 'ASC')
{
@@ -343,11 +362,8 @@ public function orderBy($column, $direction = 'ASC')
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function limit($limit, $offset = null)
{
@@ -359,16 +375,13 @@ public function limit($limit, $offset = null)
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* Starts a where clause.
*
* Examples:
*
* Db::table('users')->where('name', 'LIKE', 'Bob')->get();
* Db::table('users')->where('email', '=', 'bob@example.com')->first();
- * ---------------------------------------------------------------------------
*/
public function where($column, $operator, $value, $modifier = null)
{
@@ -381,40 +394,31 @@ public function where($column, $operator, $value, $modifier = null)
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function andWhere($column, $operator, $value)
{
$this->where($column, $operator, $value, 'AND');
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function orWhere($column, $operator, $value)
{
$this->where($column, $operator, $value, 'OR');
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* Does an inner join on a table. The first param is the table to join, the
* last 3 params are used to create the ON statement.
*
* Examples:
*
* DB::table('users')->join('photos', 'photos.user_id', '=', 'user.id')->get();
- * ---------------------------------------------------------------------------
*/
public function join($table, $column1, $operator, $column2, $modifier = 'INNER')
{
@@ -424,11 +428,8 @@ public function join($table, $column1, $operator, $column2, $modifier = 'INNER')
return $this;
}
-
/**
- * ---------------------------------------------------------------------------
* TODO
- * ---------------------------------------------------------------------------
*/
public function leftJoin($table, $column1, $operator, $column2)
{
Oops, something went wrong.

0 comments on commit ec67e74

Please sign in to comment.