spam-n-eggs / laravel-mysqlite
MySQLite是SQLite的Laravel连接器,可模拟MySQL功能。已移植的功能可在github网站上的README.md中查看。
Requires
- php: >=7.3.0
- illuminate/database: ^7.0 || ^8.0
- vectorface/mysqlite: ^0.1.4
Requires (Dev)
- codedungeon/phpunit-result-printer: dev-master
- php-coveralls/php-coveralls: ^2.4
- phpmd/phpmd: ^2.6
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.4
Suggests
- ext-intl: For locales other than en
- symfony/intl: If you just need the en locale
README
Laravel MySQLite旨在与Laravel一起使用。它是一种数据库连接,向SQLite添加MySQL的select功能。
用法
添加Composer资源
- 执行
composer require spam-n-eggs/laravel-mysqlite
或替代方案composer require --dev spam-n-eggs/laravel-mysqlite
注册为服务提供者
为了减少混乱,最好是创建一个单独的服务提供者
-
如果需要条件注册服务(例如,您仅在测试中使用它),请在
app/Providers
中创建一个新的类,该类扩展了Mhorninger\SQLite\MySQLiteServiceProvider
<?php namespace App\Providers; use Mhorninger\SQLite\MySQLiteServiceProvider as ServiceProvider; class MySQLiteServiceProvider extends ServiceProvider { public function register() { if ($shouldRegister) { parent::register(); } } }
-
在
app/Providers/AppServiceProvider.php
中的register()
方法中添加一行$this->app->register(MySQLiteServiceProvider::class);
移植功能
常量
运算符
方法
聚合
日期和时间
- convert_tz(date, fromTimezone, toTimezone)
- date_add(date, INTERVAL <number_of> <interval_specifier>)
- 移植间隔指定符
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- YEAR
- 差异
- 所有DATE_ADD调用都返回完整的日期时间,而不是简短日期。
- 移植间隔指定符
- date_format(date, format)
- 未移植格式字符串:
%U
、%V
、%X
- 其他限制:
%j
比实际日期晚一天。
- 未移植格式字符串:
- hour(time)
- minute(time)
- now()
- timestampdiff(timeUnit, startTimeStamp, endTimeStamp)
- time_to_sec(timeExpression)
- timediff(timeExpression1, timeExpression2)
- to_days(date)
- unix_timestamp(date = null)
- utc_timestamp()
- weekday(date)
流程
数字
- mod(number, divisor)
- 限制 - 仅支持标准
MOD(N,M)
和N % M
表示法。N MOD M
不受支持。
- 限制 - 仅支持标准
- rand()
- sqrt(value)
字符串
- concat(string ...)
- concat_ws(separator, string ...)
- format(number, decimals, locale = 'en_US')
- lpad(string, length, pad)
- rpad(string, length, pad)
- left(string, length)
- right(string, length)
杂项
Vectorface特定
比较
自定义功能
虽然此包旨在涵盖常见功能,但有时您需要快速支持某个功能或您应用程序独有的自定义功能。这很容易通过服务提供者类boot()
方法的两个方法来实现
<?php class MySQLiteServiceProvider extends ServiceProvider { ... public function boot() { $connection = $this->app->get('db')->connection(); if ($connection->getDriverName() === 'sqlite') { $connection ->addRewriteRule('/CURDATE\(\)/', "date('now')") ->addFunction('CURDATE', fn() => CarbonImmutable::today()->toDateString(), 0); } } }
addRewriteRule()
将使用正则表达式替换您的查询中的字符串,如果SQLite有一个可以1:1替换的本地函数。addFunction()
使用PDOsqliteCreateFunction()
在SQLite没有合适的替换或逻辑更复杂的情况下,在PHP中注册自定义函数。阅读更多。
贡献
想要提交一个错误报告、贡献一些代码、改进文档或请求一个功能?太棒了!请阅读我们关于贡献指南。所有贡献都必须遵循我们的行为准则。
问题
有问题吗?使用带有问题标签的问题跟踪。我们会及时回复你。
致谢
此库使用了其他开源组件。以下列出了它们的开源项目源代码和许可信息。我们承认并感谢这些开发者为开源社区做出的贡献。
项目:数据库 https://github.com/illuminate/database 版权 (c) Taylor Otwell 许可 (MIT) https://github.com/laravel/framework/blob/5.7/LICENSE.md
项目:MySQLite https://github.com/Vectorface/MySQLite 版权 (c) 2014 Vectorface, Inc. 许可:(MIT) https://github.com/Vectorface/MySQLite/blob/master/LICENSE