Ruby 'pg' gem 中的 `PG.connect` 和 `PG::Connection.open` 有什么区别?
What is the difference between `PG.connect` and `PG::Connection.open` in the Ruby 'pg' gem?
从 pg module doc 看来,连接到 PG 数据库的正确方法似乎是使用:
conn = PG::Connection.open(dbname: 'test')
但是,我在网上找到了 other examples,它使用了 PG.connect
方法:
conn = PG.connect(dbname: 'testdb', user: 'janbodnar', password: 'pswd37')
这两种连接postgresql数据库的方式有区别吗?如果是,那是什么?一种方法比另一种更好吗?每种方法的缺点/优点是什么?
从documentation for the PG module itself可以看出PG.connect
是PG::Connection.new
的"convenience alias":
def self::connect( *args )
return PG::Connection.new( *args )
end
从source code of PG::Connection
也可以看出PG::Connection.open
是PG::Connection.new
的别名:
void
init_pg_connection()
{
…
SINGLETON_ALIAS(rb_cPGconn, "open", "new");
…
}
因此,就连接数据库的方式而言,这三个实际上是相同的。 PG.connect
增加了一个额外方法调用的成本,因为它在内部调用 PG::Connection.new
.
从 pg module doc 看来,连接到 PG 数据库的正确方法似乎是使用:
conn = PG::Connection.open(dbname: 'test')
但是,我在网上找到了 other examples,它使用了 PG.connect
方法:
conn = PG.connect(dbname: 'testdb', user: 'janbodnar', password: 'pswd37')
这两种连接postgresql数据库的方式有区别吗?如果是,那是什么?一种方法比另一种更好吗?每种方法的缺点/优点是什么?
从documentation for the PG module itself可以看出PG.connect
是PG::Connection.new
的"convenience alias":
def self::connect( *args )
return PG::Connection.new( *args )
end
从source code of PG::Connection
也可以看出PG::Connection.open
是PG::Connection.new
的别名:
void
init_pg_connection()
{
…
SINGLETON_ALIAS(rb_cPGconn, "open", "new");
…
}
因此,就连接数据库的方式而言,这三个实际上是相同的。 PG.connect
增加了一个额外方法调用的成本,因为它在内部调用 PG::Connection.new
.