Commit fb259e92 authored by Jens Segers's avatar Jens Segers

New batch insert detection, now detects batch inserts by looping all values and…

New batch insert detection, now detects batch inserts by looping all values and not only the first one
parent 06dacf1f
...@@ -106,11 +106,14 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -106,11 +106,14 @@ class Builder extends \Illuminate\Database\Query\Builder {
foreach ($this->aggregate['columns'] as $column) foreach ($this->aggregate['columns'] as $column)
{ {
// Translate count into sum
if ($function == 'count') if ($function == 'count')
{ {
$group[$column] = array('$sum' => 1); $group[$column] = array('$sum' => 1);
} }
else { // Pass other functions directly
else
{
$group[$column] = array('$' . $function => '$' . $column); $group[$column] = array('$' . $function => '$' . $column);
} }
} }
...@@ -160,7 +163,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -160,7 +163,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
} }
/** /**
* Generate the unique cache key for the query. * Generate the unique cache key for the current query.
* *
* @return string * @return string
*/ */
...@@ -260,13 +263,20 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -260,13 +263,20 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function insert(array $values) public function insert(array $values)
{ {
// Since every insert gets treated like a batch insert, we will make sure the // Since every insert gets treated like a batch insert, we will have to detect
// bindings are structured in a way that is convenient for building these // if the user is inserting a single document or an array of documents.
// inserts statements by verifying the elements are actually an array. $batch = true;
if ( ! is_array(reset($values))) foreach ($values as $value)
{
// As soon as we find a value that is not an array we assume the user is
// inserting a single document.
if (!is_array($value))
{ {
$values = array($values); $batch = false; break;
} }
}
if (!$batch) $values = array($values);
// Batch insert // Batch insert
return $this->collection->batchInsert($values); return $this->collection->batchInsert($values);
...@@ -502,7 +512,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -502,7 +512,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
// The new list of compiled wheres // The new list of compiled wheres
$wheres = array(); $wheres = array();
foreach ($this->wheres as $i=>&$where) foreach ($this->wheres as $i => &$where)
{ {
// Convert id's // Convert id's
if (isset($where['column']) && $where['column'] == '_id') if (isset($where['column']) && $where['column'] == '_id')
......
...@@ -20,14 +20,40 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -20,14 +20,40 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public function testInsert() public function testInsert()
{ {
$user = array('name' => 'John Doe'); $user = array(
'name' => 'John Doe',
'tags' => array('tag1', 'tag2')
);
DB::collection('users')->insert($user); DB::collection('users')->insert($user);
$users = DB::collection('users')->get(); $users = DB::collection('users')->get();
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$user = DB::collection('users')->first(); $user = $users[0];
$this->assertEquals('John Doe', $user['name']); $this->assertEquals('John Doe', $user['name']);
$this->assertTrue(is_array($user['tags']));
}
public function testBatchInsert()
{
$users = array(
array(
'name' => 'Jane Doe',
'tags' => array('tag1', 'tag2')
),
array(
'name' => 'John Doe',
'tags' => array('tag3')
),
);
DB::collection('users')->insert($users);
$users = DB::collection('users')->get();
$this->assertEquals(2, count($users));
$user = $users[0];
$this->assertEquals('Jane Doe', $user['name']);
$this->assertTrue(is_array($user['tags']));
} }
public function testFind() public function testFind()
......
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