Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add execute_batch to Amalgalite::SQLite3::Database

  • Loading branch information...
commit 5a42f44faaac0d45836b86a06d57436432cc75ce 1 parent 2ee7d25
@bmalex bmalex authored committed
View
28 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 */
/*
View
8 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
#
View
6 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
Please sign in to comment.
Something went wrong with that request. Please try again.