kkszymanowski/laravel-6-odbc
Composer 安装命令:
composer require kkszymanowski/laravel-6-odbc
包简介
ODBC Connector for Laravel 6
README 文档
README
Package inspired by tck/odbc but simplified, modernized and made compatible with Laravel 6, 7 and 8.
Installation
composer require kkszymanowski/laravel-6-odbc
Add you database configuration in config/database.php, for example:
'connections' => [ 'myOdbcConnection' => [ 'driver' => 'odbc', 'dsn' => env('DB_ODBC_CONNECTION_STRING'), 'host' => env('DB_ODBC_HOST'), 'database' => env('DB_ODBC_DATABASE'), 'username' => env('DB_ODBC_USERNAME'), 'password' => env('DB_ODBC_PASSWORD'), ], // ... ],
Add the environment variables referenced in the configuration to your .env file, for example:
DB_ODBC_CONNECTION_STRING="odbc:DRIVER=Pervasive;ServerName=192.168.0.1;DBQ=DATABASE;UID=User;PWD=Password"
DB_ODBC_HOST=192.168.0.1
DB_ODBC_DATABASE=DATABASE
DB_ODBC_USERNAME=User
DB_ODBC_PASSWORD=Password
If you would like to customize the schema grammar, query grammar, or the post processor used in the ODBC connection you can do that by extending \Odbc\OdbcSchemaGrammar, \Odbc\OdbcQueryGrammar and \Odbc\OdbcProcessor respectively.
Then add the following configuration entries:
'database.connections.odbc.grammar.query'
'database.connections.odbc.grammar.schema'
'database.connections.odbc.processor'
For example in config/database.php add:
'connections' => [ // ... 'odbc' => [ 'grammar' => [ 'query' => \App\Grammars\CustomQueryGrammar::class, 'schema' => \App\Grammars\CustomSchemaGrammar::class, ], 'processor' => \App\Processors\CustomProcessor::class, ], // ... ],
One of the more common cases would be to customize the compileLimit() method used in pagination and in the skip() method.
You can do this in the following way:
use Illuminate\Database\Query\Builder; use Odbc\OdbcQueryGrammar; class CustomQueryGrammar extends OdbcQueryGrammar { /** * Compile the "limit" portions of the query. * * @param Builder $query * @param int $limit * * @return string */ protected function compileLimit(Builder $query, $limit) { return 'select top ' . (int) $limit; } }
Note that the custom processor is not used when running raw queries, for example $connection->select('SELECT * FROM USERS').
To use it you must build the queries with the Eloquent query builder, for example:
User::get(); DB::connection('myOdbcConnection')->table('USERS')->get();
Usage
With Eloquent
To override your default database connection define $connection name in your Eloquent Model
/** * The connection name for the model. * * @var string */ protected $connection = 'myOdbcConnection';
After defining the connection name you perform all the standard Eloquent operations:
$user = User::where('id', 1)->get(); $users = User::all();
Without Eloquent
You can also perform queries without Eloquent models. Make sure you specify the connection name if it isn't your default one, for example:
$user = DB::connection('myOdbcConnection')->select('SELECT * FROM USERS WHERE id = :id', ['id' => 1]); $users = DB::connection('myOdbcConnection')->table('USERS')->where('id', 1)->get();
If you're running raw queries make sure to use parameter bindings wherever possible to avoid SQL Injection vulnerabilities.
kkszymanowski/laravel-6-odbc 适用场景与选型建议
kkszymanowski/laravel-6-odbc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.37k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kkszymanowski/laravel-6-odbc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kkszymanowski/laravel-6-odbc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 16.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-30