Php 如何从打开的连接确定SQLite文件名?
PHPSQlite3类不包含文件名信息,或者提供一种方法来获取SQLite引擎打开的主文件 如何确定文件名Php 如何从打开的连接确定SQLite文件名?,php,sqlite,unit-testing,Php,Sqlite,Unit Testing,PHPSQlite3类不包含文件名信息,或者提供一种方法来获取SQLite引擎打开的主文件 如何确定文件名 这在单元测试中可能会很有趣,以确保客户端使用的文件与配置中的预期文件匹配。如果SQLite3类不提供返回此信息的属性或方法,SQLite有一个PRAGMA语句来获取或设置内部数据或修改库行为 PRAGMA database_list; 它将返回一行,其中包含seq、name、file字段,分别包含序列id、数据库内部名称和文件路径: 0|main|/path/to/yourdatabas
这在单元测试中可能会很有趣,以确保客户端使用的文件与配置中的预期文件匹配。如果SQLite3类不提供返回此信息的属性或方法,SQLite有一个PRAGMA语句来获取或设置内部数据或修改库行为
PRAGMA database_list;
它将返回一行,其中包含seq、name、file字段,分别包含序列id、数据库内部名称和文件路径:
0|main|/path/to/yourdatabasefile.db
有些细节值得注意
- SQLite库可以使用多个文件
- 路径将是规范的,因此遵循符号链接
/**
* Tests the SQLite client connection
*/
function testClient () {
$client = ...
$config = ...
$row = $client->query("PRAGMA database_list")->fetchArray(SQLITE3_ASSOC);
$this->assertEquals(
[
'seq' => 0,
'name' => 'main',
'file' => realpath($config->databaseFilename)
],
$row,
"The query PRAGMA database_list didn't return what we expected: one database opened by the client, the file returned by the database matching our configuration file."
);
}
为了测试查询是否返回预期结果,一种有效的方法是比较两个数组,一个与预期结果比较,另一个与fetchArray返回的行比较
默认情况下,fetchArray将每个字段值存储两次,一个带有数字索引,一个带有AssociateKey。这里我们关注包含正确信息的字段,因此我们使用SQLITE3_ASSOC参数仅获取关联内容。如果要测试顺序,请使用fetchArray(SQLITE3_NUM):
realpath函数用于获取规范路径
参考文献:
$row = $client->query("PRAGMA database_list")->fetchArray(SQLITE3_NUM);
$this->assertEquals(
[0, 'main', realpath($config->databaseFilename)]
$row,
"The query PRAGMA database_list didn't return what we expected: one database opened by the client, the file returned by the database matching our configuration file."
);