Browse Source

Removing parentheses from generated Query when converting to string.

Renan Gonçalves 12 years ago
parent
commit
6342328592

+ 1 - 2
src/Database/Driver/PDODriverTrait.php

@@ -79,8 +79,7 @@ trait PDODriverTrait {
  */
 	public function prepare($query) {
 		$this->connect();
-		$sql = $query instanceof Query ? $query->sql() : $query;
-		$statement = $this->_connection->prepare($sql);
+		$statement = $this->_connection->prepare((string)$query);
 		return new PDOStatement($statement, $this);
 	}
 

+ 1 - 2
src/Database/Driver/Sqlite.php

@@ -88,8 +88,7 @@ class Sqlite extends \Cake\Database\Driver {
  */
 	public function prepare($query) {
 		$this->connect();
-		$sql = $query instanceof Query ? $query->sql() : $query;
-		$statement = $this->_connection->prepare($sql);
+		$statement = $this->_connection->prepare((string)$query);
 		return new SqliteStatement(new PDOStatement($statement, $this), $this);
 	}
 

+ 1 - 1
src/Database/Query.php

@@ -1822,7 +1822,7 @@ class Query implements ExpressionInterface, IteratorAggregate {
  * @return string
  */
 	public function __toString() {
-		return sprintf('(%s)', $this->sql());
+		return $this->sql();
 	}
 
 /**

+ 14 - 0
tests/TestCase/Database/QueryTest.php

@@ -2397,6 +2397,20 @@ class QueryTest extends TestCase {
 	}
 
 /**
+ * Tests converting a query to a string
+ *
+ * @return void
+ */
+	public function testToString() {
+		$query = new Query($this->connection);
+		$query
+			->select(['title'])
+			->from('articles');
+		$result = (string)$query;
+		$this->assertQuotedQuery('SELECT <title> FROM <articles>', $result, true);
+	}
+
+/**
  * Tests __debugInfo
  *
  * @return void