vectorface/mysqlite

选择PDO的SQLite驱动程序的MySQL兼容性函数

v0.2.0 2024-05-29 18:43 UTC

This package is auto-updated.

Last update: 2024-09-21 19:07:21 UTC


README

Build Status Code Coverage

MySQLite是一种简单的方法,可以将MySQL函数添加到通过PDO访问的SQLite。这在可能比真实MySQL数据库更实用的测试和开发中非常有用。

用法

使用MySQLite的目的是简单易行,无需配置

use Vectorface\MySQLite\MySQLite;

// Create a PDO instance on an SQLite database
$pdo = new PDO('sqlite::memory:');

// Create compatibility functions for use within that database connection.
MySQLite::createFunctions($pdo);

// Use it.
$three = $pdo->query("SELECT BIT_OR(1, 2)")->fetch(PDO::FETCH_COLUMN);
// Wait... That works now?!? What the what?!?

也可以将其用作一行代码

$pdo = MySQLite::createFunctions(new PDO('sqlite::memory:'));

您可以使用MySQLite::getFunctionList()获取支持的函数列表。

限制

MySQLite仅提供MySQL函数的有限子集。MySQL函数众多,因此添加所有函数都会产生开发和性能的代价。

如果需要添加更多函数,很简单。只需在src/Vectorface/MySQLite/MySQL中的一个特质中添加一个名为mysql_[function name]的函数即可。

例如,在String.php中创建一个具有愚蠢行为的函数FOO()

	...
	public static function mysql_foo($foo)
	{
		if ($foo == "foo") {
			return "bar";
		}
		return $foo;
	}
	...

有了上述定义,String::mysql_foo将自动在MySQLite::createFunctions($pdo)使用时注册。