Skip to content

Commit

Permalink
Table: accumulate INSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
vasa-c committed Oct 11, 2014
1 parent 1cd7070 commit c95873c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* #34: Fixed `?w` for PgSQL (argument of WHERE must be type boolean)
* #31, #32: Fix error messages for PgSQL
* Table: accumulate INSERT

## 2.2.1 (03.07.2014)

Expand Down
62 changes: 62 additions & 0 deletions goDB/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public function insert(array $set)
if (empty($set)) {
return null;
}
if ($this->accum !== null) {
$this->accum[] = $set;
if (\count($this->accum) >= $this->sizeAccum) {
$this->multiInsert($this->accum, true);
$this->accum = [];
}
$this->lastIdAccum++;
return $this->lastIdAccum;
}
if ($this->map) {
$set = $this->map->set($set);
}
Expand Down Expand Up @@ -302,6 +311,44 @@ public function getMap()
return $this->map;
}

/**
* Begins to accumulate INSERT queries (for multi insert)
*
* @param int $lastId [optional]
* @param int $size [optional]
* @return boolean
*/
public function startAccumInsert($lastId = 0, $size = 100)
{
$ok = ($this->accum === null) && ($size > 1);
if ($ok) {
$this->accum = array();
$this->lastIdAccum = $lastId;
$this->sizeAccum = $size;
}
return $ok;
}

/**
* Flushes the accumulated queries
*
* @return int
*/
public function flushAccumInsert()
{
if ($this->accum === null) {
return 0;
}
$count = \count($this->accum);
if ($count > 0) {
$this->multiInsert($this->accum, true);
}
$this->accum = null;
$this->lastIdAccum = null;
$this->sizeAccum = null;
return $count;
}

/**
* The table name
*
Expand All @@ -322,4 +369,19 @@ public function getMap()
* @var \go\DB\Helpers\MapFields
*/
private $map;

/**
* @var array
*/
private $accum;

/**
* @var int
*/
private $lastIdAccum;

/**
* @var int
*/
private $sizeAccum;
}
35 changes: 34 additions & 1 deletion tests/Real/Mysql/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,45 @@ public function testDB()
$this->assertEquals($expected, $actual);
$this->assertNull($actual[1][3]);
$this->assertNull($actual[3][3]);

$sql = 'SELECT COUNT(*) FROM `godbtest` WHERE ?w';
$this->assertTrue($db->query($sql, array(array()))->el() > 0);
$this->assertTrue($db->query($sql, array(null))->el() > 0);
$this->assertTrue($db->query($sql, array(true))->el() > 0);

$this->assertTrue($db->query($sql, array(false))->el() == 0);
}

/**
* @covers \go\DB\Table::startAccumInsert
* @covers \go\DB\Table::flustAccumInsert
*/
public function testAccumInsert()
{
$db = $this->createDB(__DIR__.'/accum.sql');
$table = $db->getTable('godbaccumtest');
$table->startAccumInsert(2, 3);
$this->assertSame(3, $table->insert(array('a' => 3, 'b' => 6)));
$this->assertSame(4, $table->insert(array('a' => 4, 'b' => 8)));
$this->assertEquals(0, $table->getCount());
$this->assertSame(5, $table->insert(array('a' => 5, 'b' => 10)));
$this->assertEquals(3, $table->getCount());
$this->assertSame(6, $table->insert(array('a' => 6, 'b' => 12)));
$this->assertEquals(3, $table->getCount());
$this->assertSame(1, $table->flushAccumInsert());
$this->assertSame(0, $table->flushAccumInsert());
$this->assertEquals(4, $table->getCount());
$this->assertSame(5, $table->insert(array('a' => 7, 'b' => 14)));
$this->assertSame(0, $table->flushAccumInsert());
$this->assertEquals(5, $table->getCount());
$actual = $table->select(array('id', 'a', 'b'), null, 'id')->numerics();
$expected = array(
array(1, 3, 6),
array(2, 4, 8),
array(3, 5, 10),
array(4, 6, 12),
array(5, 7, 14),
);
$this->assertEquals($expected, $actual);
}
}
6 changes: 6 additions & 0 deletions tests/Real/Mysql/accum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TEMPORARY TABLE `godbaccumtest` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`a` INT UNSIGNED NOT NULL,
`b` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
);

0 comments on commit c95873c

Please sign in to comment.