Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions src/db/builder/Dm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace think\db\builder;

use think\db\Builder;
use think\db\Query;
use think\db\exception\DbException as Exception;
use think\db\Raw;

/**
* Oracle数据库驱动
*/
class Dm extends Builder
{
// protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';

/**
* limit分析
* @access protected
* @param Query $query 查询对象
* @param mixed $limit
* @return string
*/
protected function parseLimit(Query $query, string $limit): string
{
return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';

}

/**
* 设置锁机制
* @access protected
* @param Query $query 查询对象
* @param bool|false $lock
* @return string
*/
protected function parseLock(Query $query, $lock = false): string
{
if (!$lock) {
return '';
}

return ' FOR UPDATE NOWAIT ';
}

/**
* 字段和表名处理
* @access public
* @param Query $query 查询对象
* @param string $key
* @param bool $strict
* @return string
* @throws Exception
*/
public function parseKey(Query $query, $key, bool $strict = false): string
{
if (is_int($key)) {
return (string) $key;
} elseif ($key instanceof Raw) {
return $this->parseRaw($query, $key);
}

$key = trim($key);

if (strpos($key, '->') && false === strpos($key, '(')) {
// JSON字段支持
[$field, $name] = explode($key, '->');
$key = $field . '."' . $name . '"';
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
[$table, $key] = explode('.', $key, 2);

$alias = $query->getOptions('alias');

if ('__TABLE__' == $table) {
$table = $query->getOptions('table');
$table = is_array($table) ? array_shift($table) : $table;
}

if (isset($alias[$table])) {
$table = $alias[$table];
}
}

if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
throw new Exception('not support data:' . $key);
}

if ('*' != $key && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
$key = '"' . $key . '"';
}

if (isset($table)) {
$key = '"' . $table . '".' . $key;
}

return $key;
}

/**
* 随机排序
* @access protected
* @param Query $query 查询对象
* @return string
*/
protected function parseRand(Query $query): string
{
return 'DBMS_RANDOM.value';
}
}
133 changes: 133 additions & 0 deletions src/db/connector/Dm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace think\db\connector;

use PDO;
use think\db\exception\PDOException;
use think\db\PDOConnection;

/**
* mysql数据库驱动
*/
class Dm extends PDOConnection
{

/**
* 解析pdo连接的dsn信息
* @access protected
* @param array $config 连接信息
* @return string
*/
protected function parseDsn(array $config): string
{
$dsn = sprintf("dm:host=%s;port=%s;dbname=%s", $config['hostname'], $config['hostport'], $config['database']);
return $dsn;
}
/**
* 连接数据库方法
* @access public
* @param array $config 连接参数
* @param integer $linkNum 连接序号
* @param array|bool $autoConnection 是否自动连接主数据库(用于分布式)
* @return PDO
* @throws PDOException
*/
public function connect(array $config = [], $linkNum = 0, $autoConnection = false): PDO {
if (empty($config)) {
$config = $this->config;
} else {
$config = array_merge($this->config, $config);
}

$PDO = parent::connect($config, $linkNum, $autoConnection);

$PDO->query(sprintf("SET SCHEMA %s", $config['database']));
return $PDO;

}

/**
* 取得数据表的字段信息
* @access public
* @param string $tableName
* @return array
*/
public function getFields(string $tableName): array
{
$tableName = str_replace("`", "", $tableName);

$sql = $sql = sprintf("
select a.column_name,data_type,decode(nullable,'Y',0,1) notnull,data_default,decode(a.column_name,b.column_name,1,0) pk from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='%s') b where table_name='%s' and a.column_name=b.column_name(+)
", $tableName, $tableName);

$pdo = $this->getPDOStatement($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = [];

if (!empty($result)) {
foreach ($result as $key => $val) {
$val = array_change_key_case($val);

$info[$val['column_name']] = [
'name' => $val['column_name'],
'type' => $val['data_type'],
'notnull' => 1 == $val['notnull'],
'default' => $val['data_default'],
'primary' => $val['pk'] == 1,
'autoinc' => $val['pk'] == 1,
'comment' => '',
];
}
}

return $this->fieldCase($info);
}

/**
* 取得数据库的表信息
* @access public
* @param string $dbName
* @return array
*/
public function getTables(string $dbName = ''): array
{
$sql = " SELECT table_name FROM USER_TABLES where TABLESPACE_NAME='MAIN'";
$pdo = $this->getPDOStatement($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = [];

foreach ($result as $key => $val) {
$info[$key] = current($val);
}

return $info;
}

protected function supportSavepoint(): bool
{
return true;
}

/**
* 启动XA事务
* @access public
* @param string $xid XA事务id
* @return void
*/
public function startTransXa(string $xid): void
{
$this->initConnect(true);
$this->linkID->exec("XA START '$xid'");
}

}
14 changes: 7 additions & 7 deletions src/model/relation/OneToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public function eagerly(Query $query, string $relation, $field = true, string $j
}

// 预载入封装
$joinTable = $this->query->getTable();
$joinAlias = $relation;
$joinType = $joinType ?: $this->joinType;
$joinTable = $this->query->getTable();
$joinAlias = $relation;
$joinType = $joinType ?: $this->joinType;

$query->via($joinAlias);

Expand Down Expand Up @@ -274,8 +274,8 @@ protected function match(string $model, string $relation, Model $result): void
foreach ($result->getData() as $key => $val) {
if (str_contains($key, '__')) {
[$name, $attr] = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
if ($name === strtolower($relation)) {
$list[$relation][$attr] = $val;
unset($result->$key);
}
}
Expand Down Expand Up @@ -315,8 +315,8 @@ protected function match(string $model, string $relation, Model $result): void
protected function bindAttr(Model $result, Model $model = null): void
{
foreach ($this->bindAttr as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
$value = $result->getOrigin($key);
$key = is_numeric($key) ? $attr : $key;
$value = $result->getOrigin($key);

if (!is_null($value)) {
throw new Exception('bind attr has exists:' . $key);
Expand Down