定制 uepg/laravel-sybase 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

uepg/laravel-sybase

Composer 安装命令:

composer require uepg/laravel-sybase

包简介

Sybase based Eloquent module extension for Laravel 13.x

关键字:

README 文档

README

Packagist Version PHP from Packagist Packagist GitHub contributors GitHub

  • Enables use of multiple kinds of fields.
  • Use default eloquent: works with odbc and dblib!

Install

Add the following in the require section of your composer.json:

Laravel 7 <=

"uepg/laravel-sybase": "~2"

Laravel 8 >= and <= 10

"uepg/laravel-sybase": "~4"

Laravel 11 and 12

"uepg/laravel-sybase": "~5"

Laravel 13 >=

"uepg/laravel-sybase": "~6"

Update the package dependencies executing:

composer update

Add the following entry to your providers array in config/app.php file, optional in Laravel 5.5 or above:

Uepg\LaravelSybase\SybaseServiceProvider::class,

Add the following entry to your aliases array in config/app.php file, optional in Laravel 5.5 or above:

'UepgBlueprint' => Uepg\LaravelSybase\Database\Schema\Blueprint::class,

Update your config/database.php's default driver with the settings for the sybase or your custom odbc. See the following example:

<?php

...

return [
    ...

    
    'connections' => [
        ...

        'sybase' => [
            'driver' => 'sybasease',
            'host' => env('DB_HOST', 'sybase.myserver.com'),
            'port' => env('DB_PORT', '5000'),
            'database' => env('DB_DATABASE', 'mydatabase'),
            'username' => env('DB_USERNAME', 'user'),
            'password' => env('DB_PASSWORD', 'password'),
            'charset' => 'utf8',
            'prefix' => '',
            'cache_tables' => true,
            'cache_time' => 3600,
             'application_encoding' => false,
             'application_charset' => '',
        ],

        ...
    ],

    ...
]

Update your .env with the settings for the sybase or your custom odbc. See the following example:

...

DB_CONNECTION=sybase
DB_HOST=sybase.myserver.com
DB_PORT=5000
DB_DATABASE=mydatabase
DB_USERNAME=user
DB_PASSWORD=password

...

Configuration of freetds driver

In Linux systems the driver version must be set in freetds.conf file to the right use of charset pages.

The file is usualy found in /etc/freetds/freetds.conf. Set the configuration at global section as the following example:

[global]
    # TDS protocol version
    tds version = 5.0

Configuring the charset conversion

This package offers to method to charset conversion, it can be converted in application layer or in database layer, we offered both methods because it can be useful for debugging, to config the application layer conversion you need to set up the following entries on the database.php file. You can view an example of the application encoding setup below:

To use the database layer conversion add the property charset to connection configuration on the sybase configuration array

```charset
     'charset' => 'utf8',
     'application_encoding' => false,
     'application_charset' => '',

Stored procedures (rpc())

Use rpc() on the Sybase connection to run stored procedures that return exactly one final result set (multiple trailing SELECT statements are not supported by this helper).

Result set convention (status columns)

Design procedures that participate in this flow so the only result set returned to the client always includes at least these columns (names matched case-insensitively):

Column Role
cd_retorno 0 = success; > 0 = application / business error code expected by PHP (throwOnError(), ProcedureExecutionException).
msg_retorno Human-readable message for errors (and optional context on success); surfaced on the exception when cd_retorno ≠ 0.

throwOnError() inspects the first row only. get() / first() return whatever columns the procedure selects, but callers and tooling should rely on the same contract so every call can distinguish OK vs application error without ad hoc parsing.

If a procedure omits cd_retorno (or returns no rows), throwOnError() cannot validate the outcome and will throw InvalidArgumentException as documented below.

use Illuminate\Support\Facades\DB;

$rows = DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with(['cd_pessoa_p' => $id]) // keys may include or omit a leading `@`
    ->get(); // same row shape as Connection::select()

// Positional arguments (values only), in the same order as the procedure parameters:
$rows = DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with([$id, $nome])
    ->get();

$first = DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with(['@cd_pessoa_p' => $id])
    ->first();

DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with($dto) // Illuminate\Contracts\Support\Arrayable or associative array
    ->throwOnError() // throws ProcedureExecutionException if cd_retorno != 0
    ->first();

Exceptions

Execution still goes through Connection::select(), so SQL and driver failures surface as Laravel’s usual Illuminate\Database\QueryException (and related PDO errors), like any other query.

Uepg\LaravelSybase\Database\ProcedureExecutionException (extends RuntimeException) is thrown by throwOnError() when the first row of the result set has cd_retorno ≠ 0 (application-level error returned by the procedure). Use it to branch on business errors without inspecting the row manually:

  • $e->getMessage() — prefers msg_retorno when present, otherwise a short default message.
  • $e->getCode() — the numeric cd_retorno (also available as $e->cdRetorno).
  • $e->msgRetorno — optional string from the msg_retorno column (may be null).

InvalidArgumentException is thrown by RpcCall for invalid usage before or during throwOnError(), including:

  • Procedure name passed to rpc() does not match the allowed pattern (empty or disallowed characters).
  • Named parameters: non-string keys, empty parameter name, or name not matching ^[a-zA-Z_][a-zA-Z0-9_]*$ (after stripping an optional leading @).
  • throwOnError(): result set is empty, or the first row has no cd_retorno column (case-insensitive match on column names).
use Illuminate\Database\QueryException;
use Uepg\LaravelSybase\Database\ProcedureExecutionException;

try {
    DB::connection('sybase')->rpc('dbo.sp_exemplo')->with(['id' => $id])->throwOnError();
} catch (ProcedureExecutionException $e) {
    // $e->cdRetorno, $e->msgRetorno, $e->getMessage()
} catch (QueryException $e) {
    // syntax, connectivity, Sybase errors, etc.
}

Hydrating results into DTOs

Implement Uepg\LaravelSybase\Contracts\RpcResultDto on a class with fromArray(array $row): static, then use firstAs() for a single row or getAs() for every row as an Illuminate\Support\Collection:

use Illuminate\Support\Facades\DB;
use Uepg\LaravelSybase\Contracts\RpcResultDto;

final class LoginRow implements RpcResultDto
{
    public function __construct(
        public readonly int $cdRetorno,
        public readonly ?string $msgRetorno,
    ) {}

    public static function fromArray(array $row): static
    {
        return new self(
            cdRetorno: (int) ($row['cd_retorno'] ?? 0),
            msgRetorno: isset($row['msg_retorno']) ? (string) $row['msg_retorno'] : null,
        );
    }
}

$row = DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with(['cd_pessoa_p' => $id])
    ->throwOnError()
    ->firstAs(LoginRow::class); // LoginRow|null

$rows = DB::connection('sybase')
    ->rpc('dbo.sp_lista')
    ->with(['p' => 1])
    ->getAs(LoginRow::class); // Collection<int, LoginRow>

Optional read path and fetch mode follow select():

DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with(['p' => 1])
    ->useReadPdo(false)
    ->fetchUsing([\PDO::FETCH_ASSOC])
    ->get();

toStatement() returns the built SQL (EXEC ... @name = ?, ... or EXEC ... ?, ?, ...) and bindings (for logging or tests) without executing the procedure. A new with() that switches between a list and an associative array replaces the previous argument style for that call chain.

This API does not cover OUTPUT parameters or procedures that return multiple result sets unless you handle them with a raw select() yourself.

Configuring the cache

As the library consults table information whenever it receives a request, caching can be used to avoid excessive queries

To use the cache, add the property cache_tables to the database.php connection configuration, you can customize the time of the cache with the property cache_time in the same configuration

        'cache_tables' => true,
        'cache_time' => 3600

Setting to use numeric data type

In the migration file you must replace use Illuminate\Database\Schema\Blueprint; with use Uepg\LaravelSybase\Database\Schema\Blueprint;. See the following example:

<?php

use Illuminate\Support\Facades\Schema;
// use Illuminate\Database\Schema\Blueprint;
use Uepg\LaravelSybase\Database\Schema\Blueprint; // or "use UepgBlueprint as Blueprint"
use Illuminate\Database\Migrations\Migration;

class CreateTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->numeric('column_name', length, autoIncrement);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('table_name');
    }
}

uepg/laravel-sybase 适用场景与选型建议

uepg/laravel-sybase 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.02k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2015 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sybase」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 uepg/laravel-sybase 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 uepg/laravel-sybase 我们能提供哪些服务?
定制开发 / 二次开发

基于 uepg/laravel-sybase 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 4
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-only
  • 更新时间: 2015-11-03