SQLite3::Database和SQLite3::Statement的初始化方法在哪里?

4

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

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 )

但是,我还是没有运气。它只是跳过了它。

我已经查看了源代码,进行了搜索等操作,但是我无法找到被调用的初始化方法。它们在哪里?

谢谢您考虑这个问题。

1个回答

2

SQLite3::Databaseinitialize 方法 是用 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文件或库吗? - Will
ps.(我是StackOverflow的新手)- 我上面的评论是否合适?还是应该在聊天中讨论,甚至提出一个单独的问题?谢谢。(正在努力获得积分!) - Will
@WilliamSmith:如果你看一下 C 文件的底部,你会看到 rb_define_method 调用将各个部分粘合在一起。这些 C 文件编译成库,并根据需要动态加载。只要稍微搜索一下,你应该就能找到一个 C 扩展教程。评论很好,我们这里的大多数人都很友好(或者至少假装是 :) 。 - mu is too short
感谢您的回复。同时,我相信在《实用程序员指南》中的上述评论部分中,我已经找到了自己问题的一些答案,该部分名为扩展Ruby:[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html] - 感谢stackOverflow,这是一个伟大的社区和非常酷的实现。我希望能够成为贡献者而不仅仅是消费者。 - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接