-
安装php扩展pdo_kdb(人大金仓对应php扩展)
在官网获取对应php版本的pdo_kdb扩展包3. PHP配置连接KingbaseES — KingbaseES产品手册https://help.kingbase.com.cn/v8/development/client-interfaces/php-pdo/php-pdo-2.html#linux-php
下载后,将pdo_kdb.so放到php包扩展目录
php -i | grep 'extension_dir' #查看扩展包位置
使用php -m命令查看扩展是否已经安装
如果报错缺失依赖libpq.so.5就把相关so文件也放到该目录下,然后创建软链接到/usr/lib64下
ln -s /usr/local/php74/lib/php/extensions/no-debug-non-zts-20190902/libssl.so.10 /usr/lib64/libssl.so.10
如果提示undefined symbol: file_globals_id,选择uzts
-
配置laravel框架
-
创建KingBaseConnector.php文件
在目录vendor/laravel/framework/src/llluminate/Database/Connectors下新建类KingBaseConnector.php
建议直接复制PostgresConnector类,直接修改类名为KingBaseConnector,同时修改165行 $dsn = "pgsql:{$host}dbname='{$database}'"为 $dsn = "kdb:{$host}dbname='{$database}'" 即可
<?php
namespace Illuminate\Database\Connectors;
use PDO;
class KingBaseConnector extends Connector implements ConnectorInterface
{
/**
* The default PDO connection options.
*
* @var array
*/
protected $options = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
];
/**
* Establish a database connection.
*
* @param array $config
* @return \PDO
*/
public function connect(array $config)
{
// First we'll create the basic DSN and connection instance connecting to the
// using the configuration option specified by the developer. We will also
// set the default character set on the connections to UTF-8 by default.
$connection = $this->createConnection(
$this->getDsn($config), $config, $this->getOptions($config)
);
$this->configureIsolationLevel($connection, $config);
$this->configureEncoding($connection, $config);
// Next, we will check to see if a timezone has been specified in this config
// and if it has we will issue a statement to modify the timezone with the
// database. Setting this DB timezone is an optional configuration item.
$this->configureTimezone($connection, $config);
$this->configureSchema($connection, $config);
// Postgres allows an application_name to be set by the user and this name is
// used to when monitoring the application with pg_stat_activity. So we'll
// determine if the option has been specified and run a statement if so.
$this->configureApplicationName($connection, $config);
$this->configureSynchronousCommit($connection, $config);
return $connection;
}
/**
* Set the connection transaction isolation level.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureIsolationLevel($connection, array $config)
{
if (isset($config['isolation_level'])) {
$connection->prepare("set session characteristics as transaction isolation level {$config['isolation_level']}")->execute();
}
}
/**
* Set the connection character set and collation.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureEncoding($connection, $config)
{
if (! isset($config['charset'])) {
return;
}
$connection->prepare("set names '{$config['charset']}'")->execute();
}
/**
* Set the timezone on the connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureTimezone($connection, array $config)
{
if (isset($config['timezone'])) {
$timezone = $config['timezone'];
$connection->prepare("set time zone '{$timezone}'")->execute();
}
}
/**
* Set the schema on the connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureSchema($connection, $config)
{
if (isset($config['schema'])) {
$schema = $this->formatSchema($config['schema']);
$connection->prepare("set search_path to {$schema}")->execute();
}
}
/**
* Format the schema for the DSN.
*
* @param array|string $schema
* @return string
*/
protected function formatSchema($schema)
{
if (is_array($schema)) {
return '"'.implode('", "', $schema).'"';
}
return '"'.$schema.'"';
}
/**
* Set the schema on the connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureApplicationName($connection, $config)
{
if (isset($config['application_name'])) {
$applicationName = $config['application_name'];
$connection->prepare("set application_name to '$applicationName'")->execute();
}
}
/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the PDO connections and return them back for use.
extract($config, EXTR_SKIP);
$host = isset($host) ? "host={$host};" : '';
$dsn = "kdb:{$host}dbname='{$database}'";
// If a port was specified, we will add it to this Postgres DSN connections
// format. Once we have done that we are ready to return this connection
// string back out for usage, as this has been fully constructed here.
if (isset($config['port'])) {
$dsn .= ";port={$port}";
}
return $this->addSslOptions($dsn, $config);
}
/**
* Add the SSL options to the DSN.
*
* @param string $dsn
* @param array $config
* @return string
*/
protected function addSslOptions($dsn, array $config)
{
foreach (['sslmode', 'sslcert', 'sslkey', 'sslrootcert'] as $option) {
if (isset($config[$option])) {
$dsn .= ";{$option}={$config[$option]}";
}
}
return $dsn;
}
/**
* Configure the synchronous_commit setting.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureSynchronousCommit($connection, array $config)
{
if (! isset($config['synchronous_commit'])) {
return;
}
$connection->prepare("set synchronous_commit to '{$config['synchronous_commit']}'")->execute();
}
}
-
创建KingBaseConnection.php文件
在目录vendor/laravel/framework/src/llluminate/Database下新建类KingBaseConnection.php
类似与PostgresConnection类
对protected function getDoctrineDriver()方法进行对应修改就行
<?php
namespace Illuminate\Database;
use Doctrine\DBAL\Driver\PDOPgSql\Driver as DoctrineDriver;
use Doctrine\DBAL\Version;
use Illuminate\Database\PDO\KingBaseDriver;
use Illuminate\Database\Query\Grammars\PostgresGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Schema\Grammars\PostgresGrammar as SchemaGrammar;
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Database\Schema\PostgresSchemaState;
use Illuminate\Filesystem\Filesystem;
use PDO;
class KingBaseConnection extends Connection
{
/**
* Bind values to their parameters in the given statement.
*
* @param \PDOStatement $statement
* @param array $bindings
* @return void
*/
public function bindValues($statement, $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($value)) {
$pdoParam = PDO::PARAM_INT;
} elseif (is_resource($value)) {
$pdoParam = PDO::PARAM_LOB;
} else {
$pdoParam = PDO::PARAM_STR;
}
$statement->bindValue(
is_string($key) ? $key : $key + 1,
$value,
$pdoParam
);
}
}
/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\PostgresGrammar
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new QueryGrammar);
}
/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\PostgresBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new PostgresBuilder($this);
}
/**
* Get the default schema grammar instance.
*
* @return \Illuminate\Database\Schema\Grammars\PostgresGrammar
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new SchemaGrammar);
}
/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
* @return \Illuminate\Database\Schema\PostgresSchemaState
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new PostgresSchemaState($this, $files, $processFactory);
}
/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\PostgresProcessor
*/
protected function getDefaultPostProcessor()
{
return new PostgresProcessor;
}
/**
* Get the Doctrine DBAL driver.
*
* @return \Doctrine\DBAL\Driver\PDOPgSql\Driver|\Illuminate\Database\PDO\KingBaseDriver
*/
protected function getDoctrineDriver()
{
return class_exists(Version::class) ? new DoctrineDriver : new KingBaseDriver;
}
}
-
修改ConnectionFactory类
在目录vendor/laravel/framework/src/llluminate/Database/Connectors下修改类ConnectionFactory,对方法public function createConnector()和方法protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])新增case人大金仓情况,同时不要忘记导入头文件 use Illuminate\Database\KingBaseConnection;
public function createConnector(array $config)
{
if (! isset($config['driver'])) {
throw new InvalidArgumentException('A driver must be specified.');
}
if ($this->container->bound($key = "db.connector.{$config['driver']}")) {
return $this->container->make($key);
}
switch ($config['driver']) {
case 'mysql':
return new MySqlConnector;
case 'pgsql':
return new PostgresConnector;
case 'sqlite':
return new SQLiteConnector;
case 'sqlsrv':
return new SqlServerConnector;
case 'kingbase':
return new KingBaseConnector;
}
throw new InvalidArgumentException("Unsupported driver [{$config['driver']}].");
}
/**
* Create a new connection instance.
*
* @param string $driver
* @param \PDO|\Closure $connection
* @param string $database
* @param string $prefix
* @param array $config
* @return \Illuminate\Database\Connection
*
* @throws \InvalidArgumentException
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{
if ($resolver = Connection::getResolver($driver)) {
return $resolver($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);
case 'kingbase':
return new KingBaseConnection($connection, $database, $prefix, $config);
}
throw new InvalidArgumentException("Unsupported driver [{$driver}].");
}
-
新建KingBaseDriver驱动类
在目录vendor/laravel/framework/src/llluminate/Database/PDO下新建文件KingBaseDriver.php
<?php
namespace Illuminate\Database\PDO;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Illuminate\Database\PDO\Concerns\ConnectsToDatabase;
class KingBaseDriver extends AbstractPostgreSQLDriver
{
use ConnectsToDatabase;
/**
* {@inheritdoc}
*/
public function getName()
{
return 'pdo_kdb';
}
}
-
修改database配置
目录config/database,新增kingbase
'kingbase' => [
'driver' => 'kingbase',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3301'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => env('DB_PREFIX', ''),
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
-
修改.env文件
DB_CONNECTION=kingbase
DB_HOST=127.0.0.1
DB_PORT=3301
DB_DATABASE=test
DB_USERNAME=system
DB_PASSWORD=00000