diff --git a/ext/amalgalite/amalgalite3_database.c b/ext/amalgalite/amalgalite3_database.c index b5703a9..b82b8f6 100644 --- a/ext/amalgalite/amalgalite3_database.c +++ b/ext/amalgalite/amalgalite3_database.c @@ -300,6 +300,33 @@ VALUE am_sqlite3_database_prepare(VALUE self, VALUE rSQL) return stmt; } + +/** + * call-seqL + * database.execute_batch( sqls ) -> Boolean + * + * Execute the statements in a batch. + */ +VALUE am_sqlite3_database_exec(VALUE self, VALUE rSQL) +{ + VALUE sql = StringValue( rSQL ); + am_sqlite3 *am_db; + int rc; + + Data_Get_Struct(self, am_sqlite3, am_db); + + rc = sqlite3_exec( am_db->db, RSTRING_PTR(sql), NULL, NULL, NULL ); + + if ( SQLITE_OK != rc ){ + rb_raise( eAS_Error, "Failed to execute bulk statements: [SQLITE_ERROR %d] : %s\n", + rc, sqlite3_errmsg(am_db->db)); + } + + /* Presume that nobody will want to batch execute + more than a Fixnum's worth of statements */ + return Qtrue; +} + /** * This function is registered with a sqlite3 database using the sqlite3_trace * function. During the registration process a handle on a VALUE is also @@ -1137,6 +1164,7 @@ void Init_amalgalite3_database( ) rb_define_method(cAS_Database, "progress_handler", am_sqlite3_database_progress_handler, 2); /* in amalgalite3_database.c */ rb_define_method(cAS_Database, "interrupt!", am_sqlite3_database_interrupt_bang, 0); /* in amalgalite3_database.c */ rb_define_method(cAS_Database, "replicate_to", am_sqlite3_database_replicate_to, 1); /* in amalgalite3_database.c */ + rb_define_method(cAS_Database, "execute_batch", am_sqlite3_database_exec, 1); /* in amalgalite3_database.c */ /* diff --git a/lib/amalgalite/database.rb b/lib/amalgalite/database.rb index face4f1..e70e830 100644 --- a/lib/amalgalite/database.rb +++ b/lib/amalgalite/database.rb @@ -326,6 +326,14 @@ def execute_batch( sql, *bind_params) return count end + ## + # Execute a batch of statements via sqlite3_exec. This does the same as + # execute_batch, but doesn't update the statement statistics. + # + def import(sql) + @api.execute_batch(sql) + end + ## # clear all the current taps # diff --git a/spec/database_spec.rb b/spec/database_spec.rb index 53d7ff2..3112a11 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -502,4 +502,10 @@ def call( *args) "ftest5 called"; end lambda { @iso_db.replicate_to( false ) }.should raise_error( ArgumentError, /must be a String or a Database/ ) end + it "imports batch statements" do + db = Amalgalite::Database.new(":memory:") + db.import("CREATE TABLE things(stuff TEXT); INSERT INTO things (stuff) VALUES (\"foobar\");").should be_true + db.first_value_from("SELECT stuff FROM things").should == "foobar" + end + end