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 {
foreach ($this->aggregate['columns'] as $column)
{
// Translate count into sum
if ($function == 'count')
{
$group[$column] = array('$sum' => 1);
}
else {
// Pass other functions directly
else
{
$group[$column] = array('$' . $function => '$' . $column);
}
}
......@@ -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
*/
......@@ -260,13 +263,20 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
public function insert(array $values)
{
// Since every insert gets treated like a batch insert, we will make sure the
// bindings are structured in a way that is convenient for building these
// inserts statements by verifying the elements are actually an array.
if ( ! is_array(reset($values)))
// Since every insert gets treated like a batch insert, we will have to detect
// if the user is inserting a single document or an array of documents.
$batch = true;
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
return $this->collection->batchInsert($values);
......@@ -502,7 +512,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
// The new list of compiled wheres
$wheres = array();
foreach ($this->wheres as $i=>&$where)
foreach ($this->wheres as $i => &$where)
{
// Convert id's
if (isset($where['column']) && $where['column'] == '_id')
......
......@@ -20,14 +20,40 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public function testInsert()
{
$user = array('name' => 'John Doe');
$user = array(
'name' => 'John Doe',
'tags' => array('tag1', 'tag2')
);
DB::collection('users')->insert($user);
$users = DB::collection('users')->get();
$this->assertEquals(1, count($users));
$user = DB::collection('users')->first();
$user = $users[0];
$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()
......
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