Skip to content

Commit

Permalink
Implement support for extended inserts (OnlineBuddies#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
iarna committed Mar 16, 2012
1 parent 341e19a commit 021fad8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Modyllic/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,15 @@ function cmd_INSERT_INTO() {
$this->assert_symbol();
$columns = $this->get_array();
$this->get_reserved('VALUES');
$this->get_symbol('(');
$values = $this->get_token_array();
if ( count($columns) != count($values) ) {
throw $this->error("INSERT INTO column count doesn't match value count" );
}
$row = array_combine( $columns, $values );
do {
$this->get_symbol('(');
$values = $this->get_token_array();
if ( count($columns) != count($values) ) {
throw $this->error("INSERT INTO column count doesn't match value count" );
}
$row = array_combine( $columns, $values );
$table->add_row( $row );
} while ($this->maybe(','));

This comment has been minimized.

Copy link
@aredridel

aredridel Mar 16, 2012

That's really elegant.

}
else if ( $this->maybe('SET') ) {
$this->assert_reserved();
Expand All @@ -224,11 +227,11 @@ function cmd_INSERT_INTO() {
$value = $this->next();
$row[$col] = $value;
}
$table->add_row( $row );
}
else {
throw $this->error( "Expected '(col_names) VALUES (values)' or 'SET col_name=value,...'" );
}
$table->add_row( $row );
}

function cmd_USE() {
Expand Down
55 changes: 55 additions & 0 deletions test/Static_Tables.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env php
<?php
/**
* Copyright © 2011 Online Buddies, Inc. - All Rights Reserved
*
* @package Modyllic
* @author [email protected]
*/

require_once dirname(__FILE__)."/../testlib/testmore.php";

plan(20);

require_ok("Modyllic/Parser.php");


$parser = new Modyllic_Parser();

$sql = <<<EOSQL
CREATE TABLE test ( id INT );
TRUNCATE TABLE test;
INSERT INTO test (id) VALUES (1);
EOSQL;

$schema = $parser->parse( $sql );
ok( isset($schema->tables['test']), "Test table created" );
$test = $schema->tables['test'];
is( $test->static, true, "Test table is flagged static" );
is( count($test->data), 1, "One row of test data was created" );
is( $test->data[0]['id'], 1, "The id column of the test data is set" );

$sql = <<<EOSQL
CREATE TABLE test ( id INT );
TRUNCATE TABLE test;
INSERT INTO test SET id=1;
EOSQL;

$schema = $parser->parse( $sql );
$test = $schema->tables['test'];
is( count($test->data), 1, "One row of test data was created using update style insert" );
is( $test->data[0]['id'], 1, "The id column of the test data is set using update style insert" );

$sql = <<<EOSQL
CREATE TABLE test ( id INT );
TRUNCATE TABLE test;
INSERT INTO test (id) VALUES (1),(2),(10);
EOSQL;

$schema = $parser->parse( $sql );
$test = $schema->tables['test'];
is( count($test->data), 3, "Three rows of test data were created using an extended insert" );
is( $test->data[0]['id'], 1, "The first row's id is set" );
is( $test->data[1]['id'], 2, "The second row's id is set" );
is( $test->data[2]['id'], 10, "The third row's id is set" );

0 comments on commit 021fad8

Please sign in to comment.