Permalink
Browse files

Merge branch 'develop'

* develop:
  Add changelog
  Add is_dirty method
  Update Idiorm used in testing
  • Loading branch information...
2 parents 9f24362 + e9d4bb7 commit 149b88399e03fb9736a1622344e48ef72a323cba @j4mie j4mie committed Jan 24, 2011
Showing with 104 additions and 12 deletions.
  1. +15 −0 README.markdown
  2. +7 −0 paris.php
  3. +82 −12 test/idiorm.php
View
@@ -22,6 +22,17 @@ Features
* Uses [prepared statements](http://uk.php.net/manual/en/pdo.prepared-statements.php) throughout to protect against [SQL injection](http://en.wikipedia.org/wiki/SQL_injection) attacks.
* Database agnostic. Currently supports SQLite and MySQL. May support others, please give it a try!
+Changelog
+---------
+
+#### 1.0.0 - released 2010-12-01
+
+* Initial release
+
+#### 1.0.1 - released 2011-01-24
+
+* Add `is_dirty` method
+
Philosophy
----------
@@ -310,6 +321,10 @@ To create a new (empty) instance, use the `create` method:
$user->name = 'Paris';
$user->save();
+To check whether a property has been changed since the object was created (or last saved), call the `is_dirty` method:
+
+ $name_has_changed = $person->is_dirty('name'); // Returns true or false
+
Of course, because these objects are instances of your base model classes, you can also call methods that you have defined on them:
class User extends Model {
View
@@ -348,6 +348,13 @@ public function set($property, $value) {
$this->orm->set($property, $value);
}
+ /**
+ * Check whether the given field has changed since the object was created or saved
+ */
+ public function is_dirty($property) {
+ return $this->orm->is_dirty($property);
+ }
+
/**
* Wrapper for Idiorm's as_array method.
*/
View
@@ -61,6 +61,7 @@ class ORM {
'username' => null,
'password' => null,
'driver_options' => null,
+ 'identifier_quote_character' => null, // if this is null, will be autodetected
'logging' => false,
);
@@ -159,6 +160,7 @@ public static function configure($key, $value=null) {
* this will normally be the first method called in a chain.
*/
public static function for_table($table_name) {
+ self::_setup_db();
return new self($table_name);
}
@@ -171,18 +173,52 @@ protected static function _setup_db() {
$username = self::$_config['username'];
$password = self::$_config['password'];
$driver_options = self::$_config['driver_options'];
- self::$_db = new PDO($connection_string, $username, $password, $driver_options);
- self::$_db->setAttribute(PDO::ATTR_ERRMODE, self::$_config['error_mode']);
+ $db = new PDO($connection_string, $username, $password, $driver_options);
+ $db->setAttribute(PDO::ATTR_ERRMODE, self::$_config['error_mode']);
+ self::set_db($db);
}
}
/**
- * This can be called if the ORM should use a ready-instantiated
- * PDO object as its database connection. Won't be used in normal
- * operation, but it's here in case it's needed.
+ * Set the PDO object used by Idiorm to communicate with the database.
+ * This is public in case the ORM should use a ready-instantiated
+ * PDO object as its database connection.
*/
public static function set_db($db) {
self::$_db = $db;
+ self::_setup_identifier_quote_character();
+ }
+
+ /**
+ * Detect and initialise the character used to quote identifiers
+ * (table names, column names etc). If this has been specified
+ * manually using ORM::configure('identifier_quote_character', 'some-char'),
+ * this will do nothing.
+ */
+ public static function _setup_identifier_quote_character() {
+ if (is_null(self::$_config['identifier_quote_character'])) {
+ self::$_config['identifier_quote_character'] = self::_detect_identifier_quote_character();
+ }
+ }
+
+ /**
+ * Return the correct character used to quote identifiers (table
+ * names, column names etc) by looking at the driver being used by PDO.
+ */
+ protected static function _detect_identifier_quote_character() {
+ switch(self::$_db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
+ case 'pgsql':
+ case 'sqlsrv':
+ case 'dblib':
+ case 'mssql':
+ case 'sybase':
+ return '"';
+ case 'mysql':
+ case 'sqlite':
+ case 'sqlite2':
+ default:
+ return '`';
+ }
}
/**
@@ -191,7 +227,7 @@ public static function set_db($db) {
* required outside the class.
*/
public static function get_db() {
- self::_setup_db();
+ self::_setup_db(); // required in case this is called before Idiorm is instantiated
return self::$_db;
}
@@ -498,7 +534,7 @@ public function full_outer_join($table, $constraint, $table_alias=null) {
/**
* Internal method to add a WHERE condition to the query
*/
- protected function _add_where($fragment, $values) {
+ protected function _add_where($fragment, $values=array()) {
if (!is_array($values)) {
$values = array($values);
}
@@ -546,6 +582,13 @@ public function where_equal($column_name, $value) {
return $this->_add_simple_where($column_name, '=', $value);
}
+ /**
+ * Add a WHERE column != value clause to your query.
+ */
+ public function where_not_equal($column_name, $value) {
+ return $this->_add_simple_where($column_name, '!=', $value);
+ }
+
/**
* Special method to query the table by its primary key
*/
@@ -613,12 +656,28 @@ public function where_not_in($column_name, $values) {
return $this->_add_where("{$column_name} NOT IN ({$placeholders})", $values);
}
+ /**
+ * Add a WHERE column IS NULL clause to your query
+ */
+ public function where_null($column_name) {
+ $column_name = $this->_quote_identifier($column_name);
+ return $this->_add_where("{$column_name} IS NULL");
+ }
+
+ /**
+ * Add a WHERE column IS NOT NULL clause to your query
+ */
+ public function where_not_null($column_name) {
+ $column_name = $this->_quote_identifier($column_name);
+ return $this->_add_where("{$column_name} IS NOT NULL");
+ }
+
/**
* Add a raw WHERE clause to the query. The clause should
* contain question mark placeholders, which will be bound
* to the parameters supplied in the second argument.
*/
- public function where_raw($clause, $parameters) {
+ public function where_raw($clause, $parameters=array()) {
return $this->_add_where($clause, $parameters);
}
@@ -790,7 +849,8 @@ protected function _quote_identifier($identifier) {
* are compatible with (at least) MySQL and SQLite.
*/
protected function _quote_identifier_part($part) {
- return "`$part`";
+ $quote_character = self::$_config['identifier_quote_character'];
+ return $quote_character . $part . $quote_character;
}
/**
@@ -799,7 +859,6 @@ protected function _quote_identifier_part($part) {
*/
protected function _run() {
$query = $this->_build_select();
- self::_setup_db();
self::_log_query($query, $this->_values);
$statement = self::$_db->prepare($query);
$statement->execute($this->_values);
@@ -860,6 +919,14 @@ public function set($key, $value) {
$this->_dirty_fields[$key] = $value;
}
+ /**
+ * Check whether the given field has been changed since this
+ * object was saved.
+ */
+ public function is_dirty($key) {
+ return isset($this->_dirty_fields[$key]);
+ }
+
/**
* Save any fields which have been modified on this object
* to the database.
@@ -879,7 +946,6 @@ public function save() {
$query = $this->_build_insert();
}
- self::_setup_db();
self::_log_query($query, $values);
$statement = self::$_db->prepare($query);
$success = $statement->execute($values);
@@ -892,6 +958,7 @@ public function save() {
}
}
+ $this->_dirty_fields = array();
return $success;
}
@@ -940,7 +1007,6 @@ public function delete() {
"= ?",
));
$params = array($this->id());
- self::_setup_db();
self::_log_query($query, $params);
$statement = self::$_db->prepare($query);
return $statement->execute($params);
@@ -956,5 +1022,9 @@ public function __get($key) {
public function __set($key, $value) {
$this->set($key, $value);
}
+
+ public function __isset($key) {
+ return isset($this->_data[$key]);
+ }
}

0 comments on commit 149b883

Please sign in to comment.