Commit 7502dae2 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #420 from deeky666/DBAL-320

[DBAL-320] Add insert operation to QueryBuilder
parents 73106085 0262c512
...@@ -45,6 +45,7 @@ class QueryBuilder ...@@ -45,6 +45,7 @@ class QueryBuilder
const SELECT = 0; const SELECT = 0;
const DELETE = 1; const DELETE = 1;
const UPDATE = 2; const UPDATE = 2;
const INSERT = 3;
/* /*
* The builder states. * The builder states.
...@@ -70,7 +71,8 @@ class QueryBuilder ...@@ -70,7 +71,8 @@ class QueryBuilder
'where' => null, 'where' => null,
'groupBy' => array(), 'groupBy' => array(),
'having' => null, 'having' => null,
'orderBy' => array() 'orderBy' => array(),
'values' => array(),
); );
/** /**
...@@ -226,6 +228,9 @@ class QueryBuilder ...@@ -226,6 +228,9 @@ class QueryBuilder
} }
switch ($this->type) { switch ($this->type) {
case self::INSERT:
$sql = $this->getSQLForInsert();
break;
case self::DELETE: case self::DELETE:
$sql = $this->getSQLForDelete(); $sql = $this->getSQLForDelete();
break; break;
...@@ -536,6 +541,38 @@ class QueryBuilder ...@@ -536,6 +541,38 @@ class QueryBuilder
)); ));
} }
/**
* Turns the query being built into an insert query that inserts into
* a certain table
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->insert('users')
* ->values(
* array(
* 'name' => '?',
* 'password' => '?'
* )
* );
* </code>
*
* @param string $insert The table into which the rows should be inserted.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public function insert($insert = null)
{
$this->type = self::INSERT;
if ( ! $insert) {
return $this;
}
return $this->add('from', array(
'table' => $insert
));
}
/** /**
* Creates and adds a query root corresponding to the table identified by the * Creates and adds a query root corresponding to the table identified by the
* given alias, forming a cartesian product with any existing query roots. * given alias, forming a cartesian product with any existing query roots.
...@@ -842,6 +879,56 @@ class QueryBuilder ...@@ -842,6 +879,56 @@ class QueryBuilder
return $this->add('groupBy', $groupBy, true); return $this->add('groupBy', $groupBy, true);
} }
/**
* Sets a value for a column in an insert query.
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->insert('users')
* ->values(
* array(
* 'name' => '?'
* )
* )
* ->setValue('password', '?');
* </code>
*
* @param string $column The column into which the value should be inserted.
* @param string $value The value that should be inserted into the column.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public function setValue($column, $value)
{
$this->sqlParts['values'][$column] = $value;
return $this;
}
/**
* Specifies values for an insert query indexed by column names.
* Replaces any previous values, if any.
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->insert('users')
* ->values(
* array(
* 'name' => '?',
* 'password' => '?'
* )
* );
* </code>
*
* @param array $values The values to specify for the insert query indexed by column names.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public function values(array $values)
{
return $this->add('values', $values);
}
/** /**
* Specifies a restriction over the groups of the query. * Specifies a restriction over the groups of the query.
* Replaces any previous having restrictions, if any. * Replaces any previous having restrictions, if any.
...@@ -1029,6 +1116,18 @@ class QueryBuilder ...@@ -1029,6 +1116,18 @@ class QueryBuilder
: $this->connection->getDatabasePlatform()->modifyLimitQuery($query, $this->maxResults, $this->firstResult); : $this->connection->getDatabasePlatform()->modifyLimitQuery($query, $this->maxResults, $this->firstResult);
} }
/**
* Converts this instance into an INSERT string in SQL.
*
* @return string
*/
private function getSQLForInsert()
{
return 'INSERT INTO ' . $this->sqlParts['from']['table'] .
' (' . implode(', ', array_keys($this->sqlParts['values'])) . ')' .
' VALUES(' . implode(', ', $this->sqlParts['values']) . ')';
}
/** /**
* Converts this instance into an UPDATE string in SQL. * Converts this instance into an UPDATE string in SQL.
* *
......
...@@ -451,6 +451,78 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -451,6 +451,78 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertSame($qb2, $qb); $this->assertSame($qb2, $qb);
} }
public function testInsertValues()
{
$qb = new QueryBuilder($this->conn);
$qb->insert('users')
->values(
array(
'foo' => '?',
'bar' => '?'
)
);
$this->assertEquals(QueryBuilder::INSERT, $qb->getType());
$this->assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb);
}
public function testInsertReplaceValues()
{
$qb = new QueryBuilder($this->conn);
$qb->insert('users')
->values(
array(
'foo' => '?',
'bar' => '?'
)
)
->values(
array(
'bar' => '?',
'foo' => '?'
)
);
$this->assertEquals(QueryBuilder::INSERT, $qb->getType());
$this->assertEquals('INSERT INTO users (bar, foo) VALUES(?, ?)', (string) $qb);
}
public function testInsertSetValue()
{
$qb = new QueryBuilder($this->conn);
$qb->insert('users')
->setValue('foo', 'bar')
->setValue('bar', '?')
->setValue('foo', '?');
$this->assertEquals(QueryBuilder::INSERT, $qb->getType());
$this->assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb);
}
public function testInsertValuesSetValue()
{
$qb = new QueryBuilder($this->conn);
$qb->insert('users')
->values(
array(
'foo' => '?'
)
)
->setValue('bar', '?');
$this->assertEquals(QueryBuilder::INSERT, $qb->getType());
$this->assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb);
}
public function testEmptyInsert()
{
$qb = new QueryBuilder($this->conn);
$qb2 = $qb->insert();
$this->assertEquals(QueryBuilder::INSERT, $qb->getType());
$this->assertSame($qb2, $qb);
}
public function testGetConnection() public function testGetConnection()
{ {
$qb = new QueryBuilder($this->conn); $qb = new QueryBuilder($this->conn);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment