Permalink
Browse files

progress made on database and statement

Database:
  * autocommit
  * row_chanes
  * close
  * last row id
  * total changes

Statement
  * close
  * sql


20080514070354-2f820-5fd587d70ed5190fe7bed1614ec65829a32146d7.gz
  • Loading branch information...
copiousfreetime authored and git-darcs-import committed May 14, 2008
1 parent 12f47f1 commit 827e38e9b0b56328d156048cc5a577e0d7e6dd36
View
@@ -185,12 +185,17 @@ void Init_amalgalite3()
rb_define_singleton_method(cAS_Database, "open", am_sqlite3_database_open, -1); /* in amalgalite3_database.c */
rb_define_singleton_method(cAS_Database, "open16", am_sqlite3_database_open16, 1); /* in amalgalite3_database.c */
rb_define_method(cAS_Database, "prepare", am_sqlite3_database_prepare, 1); /* in amalgalite3_database.c */
rb_define_method(cAS_Database, "close", am_sqlite3_database_close, 0); /* in amalgalite3_database.c */
rb_define_method(cAS_Database, "last_insert_rowid", am_sqlite3_database_last_insert_rowid, 0); /* in amalgalite3_database.c */
rb_define_method(cAS_Database, "autocommit?", am_sqlite3_database_is_autocommit, 0); /* in amalgalite3_database.c */
/*
* class Statement
*/
cAS_Statement = rb_define_class_under(mAS, "Statement", rb_cObject);
rb_define_alloc_func(cAS_Statement, am_sqlite3_statement_alloc); /* in amalgalite3_statement.c */
rb_define_method(cAS_Statement, "sql", am_sqlite3_statement_sql, 0); /* in amalgalite3_statement.c */
rb_define_method(cAS_Statement, "close", am_sqlite3_statement_close, 0); /* in amalgalite3_statement.c */
}
View
@@ -25,15 +25,28 @@ typedef struct am_sqlite3_stmt {
/***********************************************************************
* Prototypes
**********************************************************************/
/*----------------------------------------------------------------------
* Database functions
*---------------------------------------------------------------------*/
extern void am_define_constants_under(VALUE);
extern VALUE am_sqlite3_database_alloc(VALUE klass);
extern void am_sqlite3_database_free(am_sqlite3*);
extern VALUE am_sqlite3_database_open(int argc, VALUE* argv, VALUE self);
extern VALUE am_sqlite3_database_close(VALUE self);
extern VALUE am_sqlite3_database_open16(VALUE self, VALUE rFilename);
extern VALUE am_sqlite3_database_last_insert_rowid(VALUE self);
extern VALUE am_sqlite3_database_is_autocommit(VALUE self);
extern VALUE am_sqlite3_database_prepare(VALUE self, VALUE rSQL);
/*----------------------------------------------------------------------
* Statement functions
*---------------------------------------------------------------------*/
extern VALUE am_sqlite3_statement_alloc(VALUE klass);
extern void am_sqlite3_statement_free(am_sqlite3_stmt* );
extern VALUE am_sqlite3_statement_sql(VALUE self);
extern VALUE am_sqlite3_statement_close(VALUE self);
/***********************************************************************
View
@@ -43,7 +43,6 @@ VALUE am_sqlite3_database_open(int argc, VALUE *argv, VALUE class)
if ( SQLITE_OK != rc ) {
rb_raise(eAS_Error, "Failure to set extended result codes %s : [SQLITE_ERROR %d] : %s\n",
filename, rc, sqlite3_errmsg(am_db->db));
sqlite3_close( am_db->db );
}
return self;
@@ -75,12 +74,108 @@ VALUE am_sqlite3_database_open16(VALUE class, VALUE rFilename)
if ( SQLITE_OK != rc ) {
rb_raise(eAS_Error, "Failure to set extended result codes on UTF-16 database %s : [SQLITE_ERROR %d] : %s\n",
filename, rc, sqlite3_errmsg16(am_db->db));
sqlite3_close( am_db->db );
}
return self;
}
/**
* :call-seq:
* database.close
*
* Close the database
*/
VALUE am_sqlite3_database_close(VALUE self)
{
am_sqlite3 *am_db;
int rc = 0;
Data_Get_Struct(self, am_sqlite3, am_db);
rc = sqlite3_close( am_db->db );
if ( SQLITE_OK != rc ) {
rb_raise(eAS_Error, "Failure to close database : [SQLITE_ERROR %d] : %s\n",
rc, sqlite3_errmsg( am_db->db ));
}
return self;
}
/**
* :call-seq:
* database.last_insert_rowid -> Integer
*
* Return the rowid of the last row inserted into the database from this
* database connection.
*/
VALUE am_sqlite3_database_last_insert_rowid(VALUE self)
{
am_sqlite3 *am_db;
sqlite3_int64 last_id;
Data_Get_Struct(self, am_sqlite3, am_db);
last_id = sqlite3_last_insert_rowid( am_db->db );
return SQLINT64_2NUM( last_id );
}
/**
* :call-seq:
* database.autocommit? -> true or false
*
* return true if the database is in autocommit mode, otherwise return false
*
*/
VALUE am_sqlite3_database_is_autocommit(VALUE self)
{
am_sqlite3 *am_db;
int rc;
Data_Get_Struct(self, am_sqlite3, am_db);
rc = sqlite3_get_autocommit( am_db->db );
return ( 0 == rc ) ? Qfalse : Qtrue ;
}
/**
* :call-seq:
* database.row_changes -> Integer
*
* return the number of rows changed with the most recent INSERT, UPDATE or
* DELETE statement.
*
*/
VALUE am_sqlite3_database_row_changes(VALUE self)
{
am_sqlite3 *am_db;
int rc;
Data_Get_Struct(self, am_sqlite3, am_db);
rc = sqlite3_changes( am_db->db );
return INT2FIX(rc);
}
/**
* :call-seq:
* database.total_changes -> Integer
*
* return the number of rows changed by INSERT, UPDATE or DELETE statements
* in the database connection since the connection was opened.
*
*/
VALUE am_sqlite3_database_total_changes(VALUE self)
{
am_sqlite3 *am_db;
int rc;
Data_Get_Struct(self, am_sqlite3, am_db);
rc = sqlite3_total_changes( am_db->db );
return INT2FIX(rc);
}
/**
* :call-seq:
* database.prepare( sql ) -> SQLite3::Statement
@@ -6,16 +6,54 @@
* vim: shiftwidth=4
*/
/**
* :call-seq:
* stmt.sql -> String
*
* Return a copy of the original string used to create the prepared statement.
*/
VALUE am_sqlite3_statement_sql(VALUE self)
{
am_sqlite3_stmt *am_stmt;
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
return rb_str_new2( sqlite3_sql( am_stmt->stmt ) );
}
/**
* :call-seq:
* stmt.close -> nil
*
* Closes the statement. If there is a problem closing the statement then an
* error is raised.
*/
VALUE am_sqlite3_statement_close( VALUE self )
{
am_sqlite3_stmt *am_stmt;
int rc;
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
rc = sqlite3_finalize( am_stmt->stmt );
if ( SQLITE_OK != rc ) {
rb_raise(eAS_Error, "Failure to close statment : [SQLITE_ERROR %d] : %s\n",
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
}
}
/***********************************************************************
* Ruby life cycle methods
***********************************************************************/
/*
* garbage collector free method for the am_sqlite3_statement tructure
* garbage collector free method for the am_sqlite3_statement structure
*/
void am_sqlite3_statement_free(am_sqlite3_stmt* wrapper)
{
free(wrapper);
return;
}
View
@@ -37,17 +37,15 @@ Core 'Objects'
*skipped* const char *sqlite3_errmsg(sqlite3*);
*skipped* const void *sqlite3_errmsg16(sqlite3*);
int sqlite3_close(sqlite3 *)
int sqlite3_exec -> don't use, wrap in ruby exec
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*)
*done* int sqlite3_close(sqlite3 *)
TODO: int sqlite3_exec -> don't use, wrap in ruby exec
*done* sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*)
int sqlite3_get_autocommit(sqlite3*)
*done* int sqlite3_get_autocommit(sqlite3*)
int sqlite3_changes(sqlite3*)
int sqlite3_total_changes(sqlite3*)
int sqlite3_busy_timeout(sqlite3*)
void sqlite3_progress_hander(sqlite3*, int, int(*)(void*), void*)
int sqlite3_errcode(sqlite3 *db)
*done* int sqlite3_changes(sqlite3*)
*done* int sqlite3_total_changes(sqlite3*)
*skipped* int sqlite3_errcode(sqlite3 *db)
int sqlite3_prepare_v2(sqlite3* db, sql, nbyte, sqlite3_stmt**, const char **tail)
int sqlite3_prepare16_v2(sqlite3* db, sql, nbyte, sqlite3_stmt**, const char **tail)
@@ -56,29 +54,37 @@ Core 'Objects'
int sqlite3_key(sqlite3* db, const void *pKey, int nKey);
int sqlite3_rekey(sqlite3* db, const void *pKey, int nKey);
int sqlite3_table_column_metadata();
// BLOB
int sqlite3_blog_open(sqlite3*, *db, *table, *column, rowid, flags, **blob);
int sqlite3_blob_open(sqlite3*, *db, *table, *column, rowid, flags, **blob);
TODO: int sqlite3_busy_timeout(sqlite3*)
TODO: void sqlite3_progress_hander(sqlite3*, int, int(*)(void*), void*)
TODO : tracing should be an IO object, if it is true then send it to STDERR
TODO : tracing should also yield a block like a transaction so that during the block the db has tracing turned on.
sqlite3_trace
TODO: create Table and Column classes
TODO: int sqlite3_table_column_metadata();
for later implementation
________________________
sqlite3_enable_load_extensions
sqlite3_load_extension
sqlite3_interrupt
sqlite3_busy_handler(sqlite3*, function pointer, void *)
sqlite3_trace
sqlite3_profile
sqlite3_limit(sqlite3*, int id, int newVal) -- maybe implement
sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
sqlite3_update_hook(sqlite3*, function ponter, void*);
*skip - experimental* sqlite3_profile
*Skip - experimental* sqlite3_limit(sqlite3*, int id, int newVal) -- maybe implement
*skip - experimental* sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
*skip - experimental* sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
*skip - experimental* sqlite3_update_hook(sqlite3*, function ponter, void*);
sqlite3_stmt (typedef struct sqlite3_stmt) -> handle for statements
------------
const char *sqlite3_sql(sqlite3_stmt *pStmt)
*done* const char *sqlite3_sql(sqlite3_stmt *pStmt)
int sqlite3_bind_parameter_count(sqlite3_stmt*);
# binding by name or index, if passed in an array to the bind() method then
@@ -90,8 +96,8 @@ Core 'Objects'
sqlite3_bind_null(sqlite3_stmt*, int);
sqlite3_bind_text(sqlite3_stmt*, int, constc char*, int n, void(*)(void*));
sqlite3_bind_text16(sqlite3_stmt*, int, constc char*, int, void(*)(void*));
sqlite3_bind_valud(sqlite3_stmt*, int, const sqlite3_value*);
sqlite3_bind_zeroblog(sqlite3_stmt*, int, int n);
sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
// returns a pointer to the name of nth sql paramtere in the statemetn
const char* sqlite3_bind_parameter_name(sqlite3_stmt*, int )
@@ -105,7 +111,7 @@ Core 'Objects'
sqlite3_column_name(sqlite3_stmt*, int N);
sqlite3_column_name16(sqlite3_stmt*, int N);
sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
*skip - used internally* sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
sqlite3_column_database_name(sqlite3_stmt*, int);
sqlite3_column_database_name16(sqlite3_stmt*, int);
@@ -118,7 +124,7 @@ Core 'Objects'
sqlite3_column_decltype16(sqlite3_stmt*, int);
sqlite3_step(sqlite3_stmt*);
int sqlite3_finalize(sqlite3_stmt *pStmt);
*done - as 'close' *int sqlite3_finalize(sqlite3_stmt *pStmt);
int sqlite3_reset(sqlite3_stmt *pStmt);
sqlite3_data_count(sqlite3_stmt *pStmt);
@@ -129,7 +135,7 @@ Core 'Objects'
// dont' use these, they are just implemented in terms of the the
// sqlite3_value methods
sqlite3_column_blog(sqlite3_stmt*, int iCol);
sqlite3_column_blob(sqlite3_stmt*, int iCol);
sqlite3_column_bytes(sqlite3_stmt*, int iCol);
sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
sqlite3_column_double(sqlite3_stmt*, int iCol);
View
@@ -47,6 +47,7 @@ class InvalidModeError < ::Amalgalite::Error; end
# Create a new database
#
def initialize( filename, mode = "w+", opts = {})
@open = false
unless VALID_MODES.keys.include?( mode )
raise InvalidModeError, "#{mode} is invalid, must be one of #{VALID_MODES.keys.join(', ')}"
end
@@ -56,6 +57,37 @@ def initialize( filename, mode = "w+", opts = {})
else
@api = Amalgalite::SQLite3::Database.open( filename, VALID_MODES[mode] )
end
@open = true
end
##
# Is the database open or not
#
def open?
@open
end
##
# Close the database
#
def close
if open? then
@api.close
end
end
##
# Is the database in autocommit mode or not
#
def autocommit?
@api.autocommit?
end
##
# Return the rowid of the last inserted row
#
def last_insert_rowid
@api.last_insert_rowid
end
##
@@ -12,11 +12,18 @@ class Statement
def initialize( db, sql )
@db = db
@sql = sql
prepare_method = @db.utf16? ? :prepare16 : :prepare
@api = @db.api.send( prepare_method, sql )
end
def sql
@api.sql
end
def close
@api.close
end
end
end
Oops, something went wrong.

0 comments on commit 827e38e

Please sign in to comment.