Skip to content

Commit

Permalink
Add execute_batch to Amalgalite::SQLite3::Database
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalex authored and copiousfreetime committed Sep 12, 2011
1 parent 2ee7d25 commit 5a42f44
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ext/amalgalite/amalgalite3_database.c
Expand Up @@ -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
Expand Down Expand Up @@ -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 */


/*
Expand Down
8 changes: 8 additions & 0 deletions lib/amalgalite/database.rb
Expand Up @@ -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
#
Expand Down
6 changes: 6 additions & 0 deletions spec/database_spec.rb
Expand Up @@ -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

0 comments on commit 5a42f44

Please sign in to comment.