Class: Amalgalite::Requires::Bootstrap
- Inherits:
-
Object
- Object
- Amalgalite::Requires::Bootstrap
- Defined in:
- ext/amalgalite/c/amalgalite_requires_bootstrap.c
Defined Under Namespace
Classes: Error
Constant Summary collapse
- DEFAULT_DB =
constants for default db, table, column, rowid, contents
rb_str_new2( "lib.db" )
- DEFAULT_TABLE =
rb_str_new2( "rubylibs" )
- DEFAULT_BOOTSTRAP_TABLE =
rb_str_new2( "bootstrap" )
- DEFAULT_ROWID_COLUMN =
rb_str_new2( "id" )
- DEFAULT_FILENAME_COLUMN =
rb_str_new2( "filename" )
- DEFAULT_CONTENTS_COLUMN =
rb_str_new2( "contents" )
- DEFAULT_COMPRESSED_COLUMN =
rb_str_new2( "compressed" )
Class Method Summary collapse
-
.Amalgalite::Requires::Bootstrap.lift('dbfile') ⇒ Object
WARNING WARNING WARNING WARNING WARNING WARNING WARNING.
-
.Amalgalite::Requires::Bootstrap.lift('dbfile') ⇒ Object
WARNING WARNING WARNING WARNING WARNING WARNING WARNING.
Class Method Details
.Amalgalite::Requires::Bootstrap.lift('dbfile') ⇒ Object
WARNING WARNING WARNING WARNING WARNING WARNING WARNING
This is a boostrap mechanism to eval all the code in a particular column in a specially formatted table in an sqlite database. It should only be used for a specific purpose, mainly loading the Amalgalite ruby code directly from an sqlite table.
Amalgalite::Requires adds in the ability to require code that is in an sqlite database. Since Amalgalite::Requires is itself ruby code, if Amalgalite::Requires was in an sqlite database, it could not require itself. Therefore this method is made available. It is a pure C extension method that directly calls the sqlite3 C functions directly and uses the ruby C api to eval the data in the table.
This method attaches to an sqlite3 database (filename) and then does:
SELECT filename_column_name, content_column_name
FROM table_name
ORDER BY rowid_column_name
For each row returned it does an eval on the code in the content_column_name and then updates _$LOADED_FEATURES_ directly with the value from filename_column_name.
The database to be opened by lift must be an sqlite3 UTF-8 database.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'ext/amalgalite/c/amalgalite_requires_bootstrap.c', line 155
VALUE am_bootstrap_lift( VALUE self, VALUE args )
{
sqlite3 *db = NULL;
int rc;
char raise_msg[BUFSIZ];
VALUE tmp = Qnil;
VALUE am_db_c = rb_const_get( cARB, rb_intern("DEFAULT_DB") );
char *dbfile = NULL;
if ( Qnil == args ) {
args = rb_hash_new();
} else {
args = rb_ary_shift( args );
}
Check_Type( args, T_HASH );
/* get the arguments */
dbfile = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "dbfile" ) ) ) ) ? StringValuePtr( am_db_c ) : StringValuePtr( tmp );
/* open the database */
rc = sqlite3_open_v2( dbfile , &db, SQLITE_OPEN_READONLY, NULL);
if ( SQLITE_OK != rc ) {
memset( raise_msg, 0, BUFSIZ );
snprintf(raise_msg, BUFSIZ, "Failure to open database %s for bootload: [SQLITE_ERROR %d] : %s", dbfile, rc, sqlite3_errmsg( db ) );
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
}
am_bootstrap_from_db( db, args );
/* close the database */
rc = sqlite3_close( db );
if ( SQLITE_OK != rc ) {
memset( raise_msg, 0, BUFSIZ );
snprintf( raise_msg, BUFSIZ, "Failure to close database : [SQLITE_ERROR %d] : %s\n", rc, sqlite3_errmsg( db )),
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
}
return Qnil;
}
|
.Amalgalite::Requires::Bootstrap.lift('dbfile') ⇒ Object
WARNING WARNING WARNING WARNING WARNING WARNING WARNING
This is a boostrap mechanism to eval all the code in a particular column in a specially formatted table in an sqlite database. It should only be used for a specific purpose, mainly loading the Amalgalite ruby code directly from an sqlite table.
Amalgalite::Requires adds in the ability to require code that is in an sqlite database. Since Amalgalite::Requires is itself ruby code, if Amalgalite::Requires was in an sqlite database, it could not require itself. Therefore this method is made available. It is a pure C extension method that directly calls the sqlite3 C functions directly and uses the ruby C api to eval the data in the table.
This method attaches to an sqlite3 database (filename) and then does:
SELECT filename_column_name, content_column_name
FROM table_name
ORDER BY rowid_column_name
For each row returned it does an eval on the code in the content_column_name and then updates _$LOADED_FEATURES_ directly with the value from filename_column_name.
The database to be opened by lift must be an sqlite3 UTF-8 database.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'ext/amalgalite/c/amalgalite_requires_bootstrap.c', line 155
VALUE am_bootstrap_lift( VALUE self, VALUE args )
{
sqlite3 *db = NULL;
int rc;
char raise_msg[BUFSIZ];
VALUE tmp = Qnil;
VALUE am_db_c = rb_const_get( cARB, rb_intern("DEFAULT_DB") );
char *dbfile = NULL;
if ( Qnil == args ) {
args = rb_hash_new();
} else {
args = rb_ary_shift( args );
}
Check_Type( args, T_HASH );
/* get the arguments */
dbfile = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "dbfile" ) ) ) ) ? StringValuePtr( am_db_c ) : StringValuePtr( tmp );
/* open the database */
rc = sqlite3_open_v2( dbfile , &db, SQLITE_OPEN_READONLY, NULL);
if ( SQLITE_OK != rc ) {
memset( raise_msg, 0, BUFSIZ );
snprintf(raise_msg, BUFSIZ, "Failure to open database %s for bootload: [SQLITE_ERROR %d] : %s", dbfile, rc, sqlite3_errmsg( db ) );
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
}
am_bootstrap_from_db( db, args );
/* close the database */
rc = sqlite3_close( db );
if ( SQLITE_OK != rc ) {
memset( raise_msg, 0, BUFSIZ );
snprintf( raise_msg, BUFSIZ, "Failure to close database : [SQLITE_ERROR %d] : %s\n", rc, sqlite3_errmsg( db )),
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
}
return Qnil;
}
|