我可以在 Laravel 中集成自定义 PDO 包装器吗

时间:2023-05-07
本文介绍了我可以在 Laravel 中集成自定义 PDO 包装器吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我和我的同事正在尝试使用 Laravel 和 Vertica 数据库开发 Web 应用程序.唯一的问题是,一旦您对这个特定数据库使用 bindValue 或 bindParam,PHP 就会因分段错误而崩溃.所以我编写了一个 PDO 包装器类,它将调用重定向到 PHP_ODBC 模块并且它确实有效.我现在想知道如果这样的事情可能的话,如何将它集成到 Laravel 中.

My fellows at work and I are trying to develop a web application using Laravel with a Vertica database. The only problem is that as soon as you use bindValue or bindParam with this specific database, PHP crashes with a segmentation fault. So I've written a PDO wrapper class that redirects calls to the PHP_ODBC module and that actually works. I was now wondering how to integrate it in Laravel if such a thing is even possible.

推荐答案

好的,经过大量的反复试验,我和我的同事设法使事情顺利进行.结果证明,最耗时的部分是构建包装器.假设你有这个,下面是你需要做的将它集成到 Laravel 中(顺便说一下,这些步骤适用于 Laravel 5.1).此外,我的包装器称为 PDOVertica,因此每当您看到这个术语时,您都必须将其替换为您自己的包装器的名称.

Okay so after a lot of trial and error, my co-workers and I managed to get things up and running. The most time-consuming part turned out to build the wrapper. Assuming you have that, here's what you need to do to integrate it in Laravel (these steps are for Laravel 5.1 by the way). Also, my wrapper's called PDOVertica so whenever you see this term, you have to substitute it for the name of your own wrapper.

1) 将您的包装文件复制到以下文件夹:

1) Copy your wrapper file to the following folder:

vendor/laravel/framework/src/Illuminate/Database/Connectors

2) 接下来,您需要修改几个文件:

2) Next, you need to modify a couple of files:

vendorlaravelframeworksrcIlluminateDatabaseConnection.php

vendorlaravelframeworksrcIlluminateDatabaseConnection.php

namespace IlluminateDatabase;

use PDO;
use PDOVertica; //Add this line
use Closure;
use DateTime;
...
//Change the type of the first parameter to PDOVertica as follow

public function __construct(PDOVertica $pdo, $database = '', $tablePrefix = '', array $config = [])

vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php

vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php

namespace IlluminateDatabaseConnectors;
include 'clsPDOVertica.php'; //Add this line
use PDO;
use PDOVertica; //Add this line
...

public function createConnection($dsn, array $config, array $options)
{
    $username = array_get($config, 'username');
    $password = array_get($config, 'password');

    //Modify the return value to return your wrapper
    return new PDOVertica($dsn, $username, $password, $options);
}

vendorlaravelframeworksrcIlluminateDatabaseConnectorsPostgresConnector.php

vendorlaravelframeworksrcIlluminateDatabaseConnectorsPostgresConnector.php

protected function getDsn(array $config)
{

    extract($config);

    $host = isset($host) ? "Server={$host};" : '';

    // Modify this line so that it creates the Vertica DSN. 
    // It should look something like this.
    $dsn = "Driver=/opt/vertica/lib64/libverticaodbc.so;{$host}Database={$database}";

    if (isset($config['port'])) {
        $dsn .= ";port={$port}";
    }

    if (isset($config['sslmode'])) {
        $dsn .= ";sslmode={$sslmode}";
    }

        return $dsn;
}

vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnectionFactory.php

vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnectionFactory.php

namespace IlluminateDatabaseConnectors;

use PDO;
use PDOVertica; //Add this line
use InvalidArgumentException;
...

// Modify the header of this function so that the $connection parameter
// is of type PDOVertica
protected function createConnection($driver, PDOVertica $connection, $database, $prefix = '', array $config = [])
{
    if ($this->container->bound($key = "db.connection.{$driver}")) {
        return $this->container->make($key, [$connection, $database, $prefix, $config]);
    }

    switch ($driver) {
        case 'mysql':
            return new MySqlConnection($connection, $database, $prefix, $config);

        case 'pgsql':
            return new PostgresConnection($connection, $database, $prefix, $config);

        case 'sqlite':
            return new SQLiteConnection($connection, $database, $prefix, $config);

        case 'sqlsrv':
            return new SqlServerConnection($connection, $database, $prefix, $config);
    }

    throw new InvalidArgumentException("Unsupported driver [$driver]");
}

3) 一旦文件被正确修改,您所要做的就是通过修改以下文件正确配置 Laravel 以使用您的自定义连接:

3) Once the files have been properly modified, all you have to do is properly configure Laravel to use your custom connection by modifying the following file:

config/database.php

config/database.php

/*   |--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => 'vertica',

...

'connections' => [

    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path('database.sqlite'),
        'prefix'   => '',
    ],

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', ''),
        'database'  => env('DB_DATABASE', ''),
        'username'  => env('DB_USERNAME', ''),
        'password'  => env('DB_PASSWORD', ''),
        'port'      => '5433h',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

    //This is our custom connection
    'vertica' => [
        'driver'    => 'pgsql',
        'host'      => env('DB_HOST', '192.168.1.1'),
        'database'  => env('DB_DATABASE', 'mydb'),
        'username'  => env('DB_USERNAME', 'myuser'),
        'password'  => env('DB_PASSWORD', 'mypassword'),
        'port'      => '5433',
        'charset'  => 'utf8',
        'schema'  => 'myschema',
    ],

    'pgsql' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'port'      => '5433',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ],

    'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'prefix'   => '',
    ],

],

据我所知,这是让 Laravel 连接到 Vertica 数据库而不会崩溃所需的所有步骤.我希望这会有所帮助.

So far as I could tell, this was all the steps needed to get Laravel to connect to a Vertica database without crashing. I hope this helps.

这篇关于我可以在 Laravel 中集成自定义 PDO 包装器吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用数组键和值创建 sql select 语句 下一篇:PDO UTF-8 编码问题?

相关文章