PHP code example of uepg / laravel-sybase

1. Go to this page and download the library: Download uepg/laravel-sybase library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uepg / laravel-sybase example snippets


Uepg\LaravelSybase\SybaseServiceProvider::class,

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



...

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' => '',
        ],

        ...
    ],

    ...
]

use Illuminate\Support\Facades\DB;

$rows = DB::connection('sybase')
    ->rpc('dbo.sp_exemplo')
    ->with(['cd_pessoa_p' => $id]) // keys may 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();

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.
}

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>

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



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');
    }
}
text
[global]
    # TDS protocol version
    tds version = 5.0