<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -22,20 +22,21 @@ class Expressions
 	private $values = array();
 	private $connection;
 
-	public function __construct($expressions=null /* [, $values ... ] */)
+	public function __construct($connection, $expressions=null /* [, $values ... ] */)
 	{
 		$values = null;
+		$this-&gt;connection = $connection;
 
 		if (is_array($expressions))
 		{
-			$glue = func_num_args() &gt; 1 ? func_get_arg(1) : ' AND ';
+			$glue = func_num_args() &gt; 2 ? func_get_arg(2) : ' AND ';
 			list($expressions,$values) = $this-&gt;build_sql_from_hash($expressions,$glue);
 		}
 
 		if ($expressions != '')
 		{
 			if (!$values)
-				$values = array_slice(func_get_args(),1);
+				$values = array_slice(func_get_args(),2);
 
 			$this-&gt;values = $values;
 			$this-&gt;expressions = $expressions;
@@ -89,7 +90,7 @@ class Expressions
 	public function to_s($substitute=false, &amp;$options=null)
 	{
 		if (!$options) $options = array();
-
+		
 		$values = array_key_exists('values',$options) ? $options['values'] : $this-&gt;values;
 
 		$ret = &quot;&quot;;
@@ -126,6 +127,9 @@ class Expressions
 
 		foreach ($hash as $name =&gt; $value)
 		{
+			if ($this-&gt;connection)
+				$name = $this-&gt;connection-&gt;quote_name($name);
+
 			if (is_array($value))
 				$sql .= &quot;$g$name IN(?)&quot;;
 			else</diff>
      <filename>lib/Expressions.php</filename>
    </modified>
    <modified>
      <diff>@@ -263,8 +263,7 @@ class SQLBuilder
 
 		if ($num_args == 1 &amp;&amp; is_hash($args[0]))
 		{
-			$e = new Expressions($args[0]);
-			$e-&gt;set_connection($this-&gt;connection);
+			$e = new Expressions($this-&gt;connection,$args[0]);
 			$this-&gt;where = $e-&gt;to_s();
 			$this-&gt;where_values = array_flatten($e-&gt;values());
 		}
@@ -277,8 +276,7 @@ class SQLBuilder
 			{
 				if (is_array($value))
 				{
-					$e = new Expressions($args[0]);
-					$e-&gt;set_connection($this-&gt;connection);
+					$e = new Expressions($this-&gt;connection,$args[0]);
 					$e-&gt;bind_values($values);
 					$this-&gt;where = $e-&gt;to_s();
 					$this-&gt;where_values = array_flatten($e-&gt;values());
@@ -304,9 +302,9 @@ class SQLBuilder
 
 	private function build_insert()
 	{
-		$keys = join(',',array_keys($this-&gt;data));
-		$e = new Expressions(&quot;INSERT INTO $this-&gt;table($keys) VALUES(?)&quot;,array_values($this-&gt;data));
-		$e-&gt;set_connection($this-&gt;connection);
+		$keys = join(',',$this-&gt;quoted_key_names());
+
+		$e = new Expressions($this-&gt;connection,&quot;INSERT INTO $this-&gt;table($keys) VALUES(?)&quot;,array_values($this-&gt;data));
 		return $e-&gt;to_s();
 	}
 
@@ -337,7 +335,7 @@ class SQLBuilder
 
 	private function build_update()
 	{
-		$fields = array_keys($this-&gt;data);
+		$fields = $this-&gt;quoted_key_names();
 		$sql = &quot;UPDATE $this-&gt;table SET &quot; . join('=?, ',$fields) . '=?';
 
 		if ($this-&gt;where)
@@ -345,5 +343,15 @@ class SQLBuilder
 
 		return $sql;
 	}
+
+	private function quoted_key_names()
+	{
+		$keys = array();
+
+		foreach ($this-&gt;data as $key =&gt; $value)
+			$keys[] = $this-&gt;connection-&gt;quote_name($key);
+
+		return $keys;
+	}
 }
 ?&gt;
\ No newline at end of file</diff>
      <filename>lib/SQLBuilder.php</filename>
    </modified>
    <modified>
      <diff>@@ -16,6 +16,13 @@ class ActiveRecordWriteTest extends DatabaseTest
 		$this-&gt;assert_not_null(Author::find($author-&gt;id));
 	}
 
+	public function test_insert_should_quote_keys()
+	{
+		$author = new Author(array('name' =&gt; 'Blah Blah'));
+		$author-&gt;save();
+		$this-&gt;assert_true(strpos($author-&gt;connection()-&gt;last_query,$author-&gt;connection()-&gt;quote_name('updated_at')) !== false);
+	}
+
 	public function test_save_auto_increment_id()
 	{
 		$venue = new Venue(array('name' =&gt; 'Bob'));
@@ -53,6 +60,14 @@ class ActiveRecordWriteTest extends DatabaseTest
 		$this-&gt;assert_same($new_name, $book-&gt;name, Book::find(1)-&gt;name);
 	}
 
+	public function test_update_should_quote_keys()
+	{
+		$book = Book::find(1);
+		$book-&gt;name = 'new name';
+		$book-&gt;save();
+		$this-&gt;assert_true(strpos($book-&gt;connection()-&gt;last_query,$book-&gt;connection()-&gt;quote_name('name')) !== false);
+	}
+
 	public function test_update_attributes()
 	{
 		$book = Book::find(1);
@@ -117,7 +132,8 @@ class ActiveRecordWriteTest extends DatabaseTest
 	public function test_dirty_attributes_cleared_after_saving()
 	{
 		$book = $this-&gt;make_new_book_and();
-		$this-&gt;assert_true(strpos($book-&gt;table()-&gt;last_sql,'(name,special)') !== false);
+		$this-&gt;assert_true(strpos($book-&gt;table()-&gt;last_sql,'name') !== false);
+		$this-&gt;assert_true(strpos($book-&gt;table()-&gt;last_sql,'special') !== false);
 		$this-&gt;assert_equals(null,$book-&gt;dirty_attributes());
 	}
 </diff>
      <filename>test/ActiveRecordWriteTest.php</filename>
    </modified>
    <modified>
      <diff>@@ -7,45 +7,45 @@ class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
 {
 	public function test_values()
 	{
-		$c = new Expressions('a=? and b=?',1,2);
+		$c = new Expressions(null,'a=? and b=?',1,2);
 		$this-&gt;assert_equals(array(1,2), $c-&gt;values());
 	}
 
 	public function test_one_variable()
 	{
-		$c = new Expressions('name=?','Tito');
+		$c = new Expressions(null,'name=?','Tito');
 		$this-&gt;assert_equals('name=?',$c-&gt;to_s());
 		$this-&gt;assert_equals(array('Tito'),$c-&gt;values());
 	}
 
 	public function test_array_variable()
 	{
-		$c = new Expressions('name IN(?) and id=?',array('Tito','George'),1);
+		$c = new Expressions(null,'name IN(?) and id=?',array('Tito','George'),1);
 		$this-&gt;assert_equals(array(array('Tito','George'),1),$c-&gt;values());
 	}
 
 	public function test_multiple_variables()
 	{
-		$c = new Expressions('name=? and book=?','Tito','Sharks');
+		$c = new Expressions(null,'name=? and book=?','Tito','Sharks');
 		$this-&gt;assert_equals('name=? and book=?',$c-&gt;to_s());
 		$this-&gt;assert_equals(array('Tito','Sharks'),$c-&gt;values());
 	}
 
 	public function test_to_string()
 	{
-		$c = new Expressions('name=? and book=?','Tito','Sharks');
+		$c = new Expressions(null,'name=? and book=?','Tito','Sharks');
 		$this-&gt;assert_equals('name=? and book=?',$c-&gt;to_s());
 	}
 
 	public function test_to_string_with_array_variable()
 	{
-		$c = new Expressions('name IN(?) and id=?',array('Tito','George'),1);
+		$c = new Expressions(null,'name IN(?) and id=?',array('Tito','George'),1);
 		$this-&gt;assert_equals('name IN(?,?) and id=?',$c-&gt;to_s());
 	}
 
 	public function test_to_string_with_null_options()
 	{
-		$c = new Expressions('name=? and book=?','Tito','Sharks');
+		$c = new Expressions(null,'name=? and book=?','Tito','Sharks');
 		$x = null;
 		$this-&gt;assert_equals('name=? and book=?',$c-&gt;to_s(false,$x));
 	}
@@ -55,83 +55,83 @@ class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
 	 */
 	public function test_insufficient_variables()
 	{
-		$c = new Expressions('name=? and id=?','Tito');
+		$c = new Expressions(null,'name=? and id=?','Tito');
 		$c-&gt;to_s();
 	}
 
 	public function test_no_values()
 	{
-		$c = new Expressions(&quot;name='Tito'&quot;);
+		$c = new Expressions(null,&quot;name='Tito'&quot;);
 		$this-&gt;assert_equals(&quot;name='Tito'&quot;,$c-&gt;to_s());
 		$this-&gt;assert_equals(0,count($c-&gt;values()));
 	}
 
 	public function test_null_variable()
 	{
-		$a = new Expressions('name=?',null);
+		$a = new Expressions(null,'name=?',null);
 		$this-&gt;assert_equals('name=?',$a-&gt;to_s());
 		$this-&gt;assert_equals(array(null),$a-&gt;values());
 	}
 
 	public function test_zero_variable()
 	{
-		$a = new Expressions('name=?',0);
+		$a = new Expressions(null,'name=?',0);
 		$this-&gt;assert_equals('name=?',$a-&gt;to_s());
 		$this-&gt;assert_equals(array(0),$a-&gt;values());
 	}
 
 	public function test_ignore_invalid_parameter_marker()
 	{
-		$a = new Expressions(&quot;question='Do you love backslashes?' and id in(?)&quot;,array(1,2));
+		$a = new Expressions(null,&quot;question='Do you love backslashes?' and id in(?)&quot;,array(1,2));
 		$this-&gt;assert_equals(&quot;question='Do you love backslashes?' and id in(?,?)&quot;,$a-&gt;to_s());
 	}
 
 	public function test_ignore_parameter_marker_with_escaped_quote()
 	{
-		$a = new Expressions(&quot;question='Do you love''s backslashes?' and id in(?)&quot;,array(1,2));
+		$a = new Expressions(null,&quot;question='Do you love''s backslashes?' and id in(?)&quot;,array(1,2));
 		$this-&gt;assert_equals(&quot;question='Do you love''s backslashes?' and id in(?,?)&quot;,$a-&gt;to_s());
 	}
 
 	public function test_ignore_parameter_marker_with_backspace_escaped_quote()
 	{
-		$a = new Expressions(&quot;question='Do you love\\'s backslashes?' and id in(?)&quot;,array(1,2));
+		$a = new Expressions(null,&quot;question='Do you love\\'s backslashes?' and id in(?)&quot;,array(1,2));
 		$this-&gt;assert_equals(&quot;question='Do you love\\'s backslashes?' and id in(?,?)&quot;,$a-&gt;to_s());
 	}
 
 	public function test_substitute()
 	{
-		$a = new Expressions('name=? and id=?','Tito',1);
+		$a = new Expressions(null,'name=? and id=?','Tito',1);
 		$this-&gt;assert_equals(&quot;name='Tito' and id=1&quot;,$a-&gt;to_s(true));
 	}
 
 	public function test_substitute_quotes_scalars_but_not_others()
 	{
-		$a = new Expressions('id in(?)',array(1,'2',3.5));
+		$a = new Expressions(null,'id in(?)',array(1,'2',3.5));
 		$this-&gt;assert_equals(&quot;id in(1,'2',3.5)&quot;,$a-&gt;to_s(true));
 	}
 
 	public function test_substitute_where_value_has_question_mark()
 	{
-		$a = new Expressions('name=? and id=?','??????',1);
+		$a = new Expressions(null,'name=? and id=?','??????',1);
 		$this-&gt;assert_equals(&quot;name='??????' and id=1&quot;,$a-&gt;to_s(true));
 	}
 
 	public function test_substitute_array_value()
 	{
-		$a = new Expressions('id in(?)',array(1,2));
+		$a = new Expressions(null,'id in(?)',array(1,2));
 		$this-&gt;assert_equals(&quot;id in(1,2)&quot;,$a-&gt;to_s(true));
 	}
 
 	public function test_substitute_escapes_quotes()
 	{
-		$a = new Expressions('name=? or name in(?)',&quot;Tito's Guild&quot;,array(1,&quot;Tito's Guild&quot;));
+		$a = new Expressions(null,'name=? or name in(?)',&quot;Tito's Guild&quot;,array(1,&quot;Tito's Guild&quot;));
 		$this-&gt;assert_equals(&quot;name='Tito''s Guild' or name in(1,'Tito''s Guild')&quot;,$a-&gt;to_s(true));
 	}
 
 	public function test_substitute_escape_quotes_with_connections_escape_method()
 	{
 		$conn = ActiveRecord\ConnectionManager::get_connection();
-		$a = new Expressions('name=?',&quot;Tito's Guild&quot;);
+		$a = new Expressions(null,'name=?',&quot;Tito's Guild&quot;);
 		$a-&gt;set_connection($conn);
 		$escaped = $conn-&gt;escape(&quot;Tito's Guild&quot;);
 		$this-&gt;assert_equals(&quot;name='$escaped'&quot;,$a-&gt;to_s(true));
@@ -139,14 +139,14 @@ class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
 
 	public function test_bind()
 	{
-		$a = new Expressions('name=? and id=?','Tito');
+		$a = new Expressions(null,'name=? and id=?','Tito');
 		$a-&gt;bind(2,1);
 		$this-&gt;assert_equals(array('Tito',1),$a-&gt;values());
 	}
 
 	public function test_bind_overwrite_existing()
 	{
-		$a = new Expressions('name=? and id=?','Tito',1);
+		$a = new Expressions(null,'name=? and id=?','Tito',1);
 		$a-&gt;bind(2,99);
 		$this-&gt;assert_equals(array('Tito',99),$a-&gt;values());
 	}
@@ -156,13 +156,13 @@ class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
 	 */
 	public function test_bind_invalid_parameter_number()
 	{
-		$a = new Expressions('name=?');
+		$a = new Expressions(null,'name=?');
 		$a-&gt;bind(0,99);
 	}
 
 	public function test_subsitute_using_alternate_values()
 	{
-		$a = new Expressions('name=?','Tito');
+		$a = new Expressions(null,'name=?','Tito');
 		$this-&gt;assert_equals(&quot;name='Tito'&quot;,$a-&gt;to_s(true));
 		$x = array('values' =&gt; array('Hocus'));
 		$this-&gt;assert_equals(&quot;name='Hocus'&quot;,$a-&gt;to_s(true,$x));
@@ -170,25 +170,25 @@ class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
 
 	public function test_null_value()
 	{
-		$a = new Expressions('name=?',null);
+		$a = new Expressions(null,'name=?',null);
 		$this-&gt;assert_equals('name=NULL',$a-&gt;to_s(true));
 	}
 
 	public function test_hash_with_default_glue()
 	{
-		$a = new Expressions(array('id' =&gt; 1, 'name' =&gt; 'Tito'));
+		$a = new Expressions(null,array('id' =&gt; 1, 'name' =&gt; 'Tito'));
 		$this-&gt;assert_equals('id=? AND name=?',$a-&gt;to_s());
 	}
 
 	public function test_hash_with_glue()
 	{
-		$a = new Expressions(array('id' =&gt; 1, 'name' =&gt; 'Tito'),', ');
+		$a = new Expressions(null,array('id' =&gt; 1, 'name' =&gt; 'Tito'),', ');
 		$this-&gt;assert_equals('id=?, name=?',$a-&gt;to_s());
 	}
 
 	public function test_hash_with_array()
 	{
-		$a = new Expressions(array('id' =&gt; 1, 'name' =&gt; array('Tito','Mexican')));
+		$a = new Expressions(null,array('id' =&gt; 1, 'name' =&gt; array('Tito','Mexican')));
 		$this-&gt;assert_equals('id=? AND name IN(?,?)',$a-&gt;to_s());
 	}
 }</diff>
      <filename>test/ExpressionsTest.php</filename>
    </modified>
    <modified>
      <diff>@@ -26,22 +26,22 @@ class SQLBuilderTest extends DatabaseTest
 
 	public function test_where_with_array()
 	{
-		$this-&gt;sql-&gt;where('id=? AND name IN(?)',1,array('Tito','Mexican'));
-		$this-&gt;assert_equals('SELECT * FROM authors WHERE id=? AND name IN(?,?)',(string)$this-&gt;sql);
+		$this-&gt;sql-&gt;where('`id`=? AND `name` IN(?)',1,array('Tito','Mexican'));
+		$this-&gt;assert_equals('SELECT * FROM authors WHERE `id`=? AND `name` IN(?,?)',(string)$this-&gt;sql);
 		$this-&gt;assert_equals(array(1,'Tito','Mexican'),$this-&gt;sql-&gt;get_where_values());
 	}
 
 	public function test_where_with_hash()
 	{
 		$this-&gt;sql-&gt;where(array('id' =&gt; 1, 'name' =&gt; 'Tito'));
-		$this-&gt;assert_equals('SELECT * FROM authors WHERE id=? AND name=?',(string)$this-&gt;sql);
+		$this-&gt;assert_equals('SELECT * FROM authors WHERE `id`=? AND `name`=?',(string)$this-&gt;sql);
 		$this-&gt;assert_equals(array(1,'Tito'),$this-&gt;sql-&gt;get_where_values());
 	}
 
 	public function test_where_with_hash_and_array()
 	{
 		$this-&gt;sql-&gt;where(array('id' =&gt; 1, 'name' =&gt; array('Tito','Mexican')));
-		$this-&gt;assert_equals('SELECT * FROM authors WHERE id=? AND name IN(?,?)',(string)$this-&gt;sql);
+		$this-&gt;assert_equals('SELECT * FROM authors WHERE `id`=? AND `name` IN(?,?)',(string)$this-&gt;sql);
 		$this-&gt;assert_equals(array(1,'Tito','Mexican'),$this-&gt;sql-&gt;get_where_values());
 	}
 
@@ -101,7 +101,7 @@ class SQLBuilderTest extends DatabaseTest
 		$this-&gt;sql-&gt;order('name');
 		$this-&gt;sql-&gt;group('name');
 		$this-&gt;sql-&gt;where(array('id' =&gt; 1));
-		$this-&gt;assert_equals(&quot;SELECT * FROM authors WHERE id=? GROUP BY name HAVING created_at &gt; '2009-01-01' ORDER BY name LIMIT 1,10&quot;, (string)$this-&gt;sql);
+		$this-&gt;assert_equals(&quot;SELECT * FROM authors WHERE `id`=? GROUP BY name HAVING created_at &gt; '2009-01-01' ORDER BY name LIMIT 1,10&quot;, (string)$this-&gt;sql);
 	}
 
 	/**
@@ -123,26 +123,26 @@ class SQLBuilderTest extends DatabaseTest
 	public function test_insert()
 	{
 		$this-&gt;sql-&gt;insert(array('id' =&gt; 1, 'name' =&gt; 'Tito'));
-		$this-&gt;assert_equals('INSERT INTO authors(id,name) VALUES(?,?)',(string)$this-&gt;sql);
+		$this-&gt;assert_equals('INSERT INTO authors(`id`,`name`) VALUES(?,?)',(string)$this-&gt;sql);
 	}
 
 	public function test_insert_with_null()
 	{
 		$this-&gt;sql-&gt;insert(array('id' =&gt; 1, 'name' =&gt; null));
-		$this-&gt;assert_equals('INSERT INTO authors(id,name) VALUES(?,?)',$this-&gt;sql-&gt;to_s());
+		$this-&gt;assert_equals('INSERT INTO authors(`id`,`name`) VALUES(?,?)',$this-&gt;sql-&gt;to_s());
 	}
 
 	public function test_update()
 	{
 		$this-&gt;sql-&gt;update(array('id' =&gt; 1, 'name' =&gt; 'Tito'))-&gt;where('id=1 AND name IN(?)',array('Tito','Mexican'));
- 		$this-&gt;assert_equals('UPDATE authors SET id=?, name=? WHERE id=1 AND name IN(?,?)',(string)$this-&gt;sql);
+ 		$this-&gt;assert_equals('UPDATE authors SET `id`=?, `name`=? WHERE id=1 AND name IN(?,?)',(string)$this-&gt;sql);
  		$this-&gt;assert_equals(array(1,'Tito','Tito','Mexican'),$this-&gt;sql-&gt;bind_values());
 	}
 
 	public function test_update_with_null()
 	{
 		$this-&gt;sql-&gt;update(array('id' =&gt; 1, 'name' =&gt; null))-&gt;where('id=1');
-		$this-&gt;assert_equals('UPDATE authors SET id=?, name=? WHERE id=1',$this-&gt;sql-&gt;to_s());
+		$this-&gt;assert_equals('UPDATE authors SET `id`=?, `name`=? WHERE id=1',$this-&gt;sql-&gt;to_s());
 	}
 
 	public function test_delete()
@@ -161,7 +161,7 @@ class SQLBuilderTest extends DatabaseTest
 	public function test_delete_with_hash()
 	{
 		$this-&gt;sql-&gt;delete(array('id' =&gt; 1, 'name' =&gt; array('Tito','Mexican')));
-		$this-&gt;assert_equals('DELETE FROM authors WHERE id=? AND name IN(?,?)',$this-&gt;sql-&gt;to_s());
+		$this-&gt;assert_equals('DELETE FROM authors WHERE `id`=? AND `name` IN(?,?)',$this-&gt;sql-&gt;to_s());
 		$this-&gt;assert_equals(array(1,'Tito','Mexican'),$this-&gt;sql-&gt;get_where_values());
 	}
 </diff>
      <filename>test/SQLBuilderTest.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b72094c767631ce678481b25c298ae8c5ba6e01e</id>
    </parent>
  </parents>
  <author>
    <name>kla</name>
    <email>la.kien@gmail.com</email>
  </author>
  <url>http://github.com/kla/php-activerecord/commit/7fb6a5433740a4fbd758ce5ab0abaa5d9c078914</url>
  <id>7fb6a5433740a4fbd758ce5ab0abaa5d9c078914</id>
  <committed-date>2009-09-18T14:50:02-07:00</committed-date>
  <authored-date>2009-09-18T14:50:02-07:00</authored-date>
  <message>quote field names when possible</message>
  <tree>f056475bfd7b580c7bf0ffa5332b26692575d39a</tree>
  <committer>
    <name>kla</name>
    <email>la.kien@gmail.com</email>
  </committer>
</commit>
