Search code examples
rubysqliterubygemssqlite3-ruby

Where are the initialize methods for SQLite3::Database and SQLite3::Statement


I am an experienced programmer learning Ruby (and liking it a lot). I'm working on setting up a database using SQLite3. In order to better learn Ruby, I'm tracing into SQLite3. What I don't understand is, where is the code for #new for the Database and Statement classes. Actually, I expect not a #new method, but a #initialize method.

SQLite3::Database.new(file, options = {})
SQLite3::Statement.new(db, sql)

The above two statements are from the documentation. But in my code when I try to trace into this

$db = SQLite3::Database.new"MyDBfile"

it just steps over.

Then later on when I try to trace into

#$db.execute 

I do get into the #execute method in the Database.rb file, but then it calls the #prepare method where I try to step into

stmt = SQLite3::Statement.new( self, sql )

but again no luck. It just steps over it.

I've scoured the source code, done searches etc but I cannot locate the initialize methods that are being called. Where are they ?

Thank you for considering this question.


Solution

  • The initialize method for SQLite3::Database is implemented in C:

    /* call-seq: SQLite3::Database.new(file, options = {})
     *
     * Create a new Database object that opens the given file. If utf16
     * is +true+, the filename is interpreted as a UTF-16 encoded string.
     *
     * By default, the new database will return result rows as arrays
     * (#results_as_hash) and has type translation disabled (#type_translation=).
     */
    static VALUE initialize(int argc, VALUE *argv, VALUE self)
    

    Similarly for SQLite3::Statement:

    /* call-seq: SQLite3::Statement.new(db, sql)
     *
     * Create a new statement attached to the given Database instance, and which
     * encapsulates the given SQL text. If the text contains more than one
     * statement (i.e., separated by semicolons), then the #remainder property
     * will be set to the trailing text.
     */
    static VALUE initialize(VALUE self, VALUE db, VALUE sql)
    

    The Ruby debugger doesn't know how to step into C functions (assuming the SQLite3 extensions have even been compiled with debugging support) so it skips over them.