Commit 55c60cd4 authored by Steve Müller's avatar Steve Müller

add insert query

parent 25a1cced
......@@ -45,6 +45,7 @@ class QueryBuilder
const SELECT = 0;
const DELETE = 1;
const UPDATE = 2;
const INSERT = 3;
/*
* The builder states.
......@@ -70,7 +71,8 @@ class QueryBuilder
'where' => null,
'groupBy' => array(),
'having' => null,
'orderBy' => array()
'orderBy' => array(),
'values' => array(),
);
/**
......@@ -226,6 +228,9 @@ class QueryBuilder
}
switch ($this->type) {
case self::INSERT:
$sql = $this->getSQLForInsert();
break;
case self::DELETE:
$sql = $this->getSQLForDelete();
break;
......@@ -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' => 'username',
* 'password' => md5('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
* given alias, forming a cartesian product with any existing query roots.
......@@ -842,6 +879,56 @@ class QueryBuilder
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' => 'username'
* )
* )
* ->setValue('password', md5('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' => 'username',
* 'password' => md5('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.
* Replaces any previous having restrictions, if any.
......@@ -1029,6 +1116,18 @@ class QueryBuilder
: $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.
*
......
......@@ -451,6 +451,78 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$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()
{
$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