a tiny mvc framework for php using php-activerecord
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Allow LIMIT/ORDER clauses for UPDATE/DELETE operations when using mysql or sqlite adapters

+50 -2
+8
lib/php-activerecord/lib/Connection.php
··· 487 487 * Executes query to specify the character set for this connection. 488 488 */ 489 489 abstract function set_encoding($charset); 490 + 491 + /** 492 + * Specifies whether or not adapter can use LIMIT/ORDER clauses with DELETE & UPDATE operations 493 + * 494 + * @internal 495 + * @returns boolean (FALSE by default) 496 + */ 497 + public function accepts_limit_and_order_for_update_and_delete() { return false; } 490 498 }; 491 499 ?>
+18
lib/php-activerecord/lib/SQLBuilder.php
··· 331 331 if ($this->where) 332 332 $sql .= " WHERE $this->where"; 333 333 334 + if ($this->connection->accepts_limit_and_order_for_update_and_delete()) 335 + { 336 + if ($this->order) 337 + $sql .= " ORDER BY $this->order"; 338 + 339 + if ($this->limit) 340 + $sql = $this->connection->limit($sql,null,$this->limit); 341 + } 342 + 334 343 return $sql; 335 344 } 336 345 ··· 388 397 389 398 if ($this->where) 390 399 $sql .= " WHERE $this->where"; 400 + 401 + if ($this->connection->accepts_limit_and_order_for_update_and_delete()) 402 + { 403 + if ($this->order) 404 + $sql .= " ORDER BY $this->order"; 405 + 406 + if ($this->limit) 407 + $sql = $this->connection->limit($sql,null,$this->limit); 408 + } 391 409 392 410 return $sql; 393 411 }
+2
lib/php-activerecord/lib/adapters/MysqlAdapter.php
··· 75 75 $params = array($charset); 76 76 $this->query('SET NAMES ?',$params); 77 77 } 78 + 79 + public function accepts_limit_and_order_for_update_and_delete() { return true; } 78 80 } 79 81 ?>
+2
lib/php-activerecord/lib/adapters/SqliteAdapter.php
··· 82 82 { 83 83 throw new ActiveRecordException("SqliteAdapter::set_charset not supported."); 84 84 } 85 + 86 + public function accepts_limit_and_order_for_update_and_delete() { return true; } 85 87 }; 86 88 ?>
+1 -1
lib/php-activerecord/test/ActiveRecordFindTest.php
··· 346 346 { 347 347 $venues = Venue::all(array('select' => 'state', 'group' => 'state', 'having' => 'length(state) = 2', 'order' => 'state', 'limit' => 2)); 348 348 $this->assert_true(count($venues) > 0); 349 - $this->assert_sql_has($this->conn->limit('SELECT state FROM venues GROUP BY state HAVING length(state) = 2 ORDER BY state',0,2),Venue::table()->last_sql); 349 + $this->assert_sql_has($this->conn->limit('SELECT state FROM venues GROUP BY state HAVING length(state) = 2 ORDER BY state',null,2),Venue::table()->last_sql); 350 350 } 351 351 352 352 public function test_escape_quotes()
+19 -1
lib/php-activerecord/test/SQLBuilderTest.php
··· 150 150 { 151 151 $this->sql->update(array('id' => 1, 'name' => 'Tito'))->where('id=1 AND name IN(?)',array('Tito','Mexican')); 152 152 $this->assert_sql_has("UPDATE authors SET id=?, name=? WHERE id=1 AND name IN(?,?)",(string)$this->sql); 153 - $this->assert_equals(array(1,'Tito','Tito','Mexican'),$this->sql->bind_values()); 153 + $this->assert_equals(array(1,'Tito','Tito','Mexican'),$this->sql->bind_values()); 154 + } 155 + 156 + public function test_update_with_limit_and_order() 157 + { 158 + if (!$this->conn->accepts_limit_and_order_for_update_and_delete()) 159 + $this->mark_test_skipped('Only MySQL & Sqlite accept limit/order with UPDATE operation'); 160 + 161 + $this->sql->update(array('id' => 1))->order('name asc')->limit(1); 162 + $this->assert_sql_has("UPDATE authors SET id=? ORDER BY name asc LIMIT 1", $this->sql->to_s()); 154 163 } 155 164 156 165 public function test_update_with_string() ··· 183 192 $this->sql->delete(array('id' => 1, 'name' => array('Tito','Mexican'))); 184 193 $this->assert_sql_has("DELETE FROM authors WHERE id=? AND name IN(?,?)",$this->sql->to_s()); 185 194 $this->assert_equals(array(1,'Tito','Mexican'),$this->sql->get_where_values()); 195 + } 196 + 197 + public function test_delete_with_limit_and_order() 198 + { 199 + if (!$this->conn->accepts_limit_and_order_for_update_and_delete()) 200 + $this->mark_test_skipped('Only MySQL & Sqlite accept limit/order with DELETE operation'); 201 + 202 + $this->sql->delete(array('id' => 1))->order('name asc')->limit(1); 203 + $this->assert_sql_has("DELETE FROM authors WHERE id=? ORDER BY name asc LIMIT 1",$this->sql->to_s()); 186 204 } 187 205 188 206 public function test_reverse_order()