spam-n-eggs/laravel-mysqlite

MySQLite是SQLite的Laravel连接器,可模拟MySQL功能。已移植的功能可在github网站上的README.md中查看。

1.3.0 2023-07-06 12:46 UTC

This package is auto-updated.

Last update: 2024-09-21 03:21:01 UTC


README

Build Status Coverage Status StyleCI Scrutinizer Code Quality Latest Stable Version Total Downloads License Dependabot Status

Laravel MySQLite旨在与Laravel一起使用。它是一种数据库连接,向SQLite添加MySQL的select功能。

用法

添加Composer资源

  1. 执行composer require spam-n-eggs/laravel-mysqlite或替代方案composer require --dev spam-n-eggs/laravel-mysqlite

注册为服务提供者

为了减少混乱,最好是创建一个单独的服务提供者

  1. 如果需要条件注册服务(例如,您仅在测试中使用它),请在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();
            }
        }
    }
  2. app/Providers/AppServiceProvider.php中的register()方法中添加一行

    $this->app->register(MySQLiteServiceProvider::class);

移植功能

常量

运算符

方法

聚合

日期和时间

流程

数字

字符串

杂项

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()使用PDO sqliteCreateFunction()在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