【问题标题】:Where are the initialize methods for SQLite3::Database and SQLite3::StatementSQLite3::Database 和 SQLite3::Statement 的初始化方法在哪里
【发布时间】:2012-04-13 20:05:45
【问题描述】:

我是一位经验丰富的程序员,正在学习 Ruby(并且非常喜欢它)。 我正在使用 SQLite3 设置数据库。 为了更好地学习 Ruby,我正在追踪 SQLite3。 我不明白的是,数据库和语句类的#new 代码在哪里。 实际上,我期望的不是#new 方法,而是#initialize 方法。

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

以上两个陈述来自文档。 但是在我的代码中,当我尝试追溯时

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

它只是走了过去。

然后当我尝试追踪时

#$db.execute 

我确实进入了 Database.rb 文件中的#execute 方法,但随后它调用了我尝试进入的#prepare 方法

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

但还是没有运气。它只是跨过它。

我已经搜索了源代码,完成了搜索等,但我找不到正在调用的初始化方法。他们在哪里?

感谢您考虑这个问题。

【问题讨论】:

    标签: ruby sqlite rubygems sqlite3-ruby


    【解决方案1】:

    initialize method for SQLite3::Database 是用 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)
    

    同样适用于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)
    

    Ruby 调试器不知道如何单步执行 C 函数(假设 SQLite3 扩展甚至已在调试支持下进行编译),因此它会跳过它们。

    【讨论】:

    • 谢谢你。但它提出了新的问题。首先,Ruby 如何解析 '#xxx.new' ?例如,它是否首先在 Ruby 文件中查找 #initialize 方法,然后在失败时查找“C”代码?其次,C代码是如何链接的?它是 SQLite3 的 gem 文件夹中的 obj 文件或库吗?
    • ps。 (我是 stackOverflow 的新手)-我的上述评论是否合适?还是应该是聊天,甚至是单独的问题?谢谢。 (根据我的观点工作!)
    • @WilliamSmith:如果您查看 C 文件的底部,您会看到 rb_define_method 调用将所有内容粘合在一起。 C 文件被编译成库并根据需要动态加载。您应该能够通过谷歌搜索找到一个 C 扩展教程。 cmets 很好,我们这里的大多数人都很友好(或者至少假装是 :)。
    • 感谢您的回复。与此同时,我相信我已经在实用程序员指南中的上述评论中找到了我自己问题的一些答案,在名为 Extending Ruby 的部分中:[ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html] - 感谢 stackOverflow,这是一个很棒的社区,也是一个非常酷的实现。我希望能够成为贡献者,而不仅仅是消费者。
    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2014-04-27
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    相关资源
    最近更新 更多