您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20161231|thinkphp5-Db.php

2016-11-28 08:35 375 查看
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;
// 开启了神器的数据库,刘先生,不要让我失望哦
use think\App;// 继承应用
use think\Collection;// 收集系统
use think\db\Query;// 搜索语句
use think\paginator\Collection as PaginatorCollection;// 感觉像是魔术分区、分页

/**
* Class Db
* @package think
* @method Query table(string $table) static 指定数据表(含前缀)  表设置 静态
* @method Query name(string $name) static 指定数据表(不含前缀)   表设置2 静态
* @method Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件 where 查询
* @method Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询  join 查询
* @method Query union(mixed $union, boolean $all = false) static UNION查询 UNION 查询
* @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT limit 查询
* @method Query order(mixed $field, string $order = null) static 查询ORDER order 查询
* @method Query cache(mixed $key = true , integer $expire = null) static 设置查询缓存  缓存数据
* @method mixed value(string $field) static 获取某个字段的值 获取某个字段的值
* @method array column(string $field, string $key = '') static 获取某个列的值 获取某列的值
* @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询  视图级别查询
* @method mixed find(mixed $data = []) static 查询单个记录 单条记录查询
* @method mixed select(mixed $data = []) static 查询多个记录 多条记录查询
* @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录 插入一条记录
* @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID 插入一条记录 并返回Id
* @method integer insertAll(array $dataSet) static 插入多条记录  插入多条记录
* @method integer update(array $data) static 更新记录 更新记录
* @method integer delete(mixed $data = []) static 删除记录 删除记录
* @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据  这个有意思,分块获取数据
* @method mixed query(string $sql, array $bind = [], boolean $fetch = false, boolean $master = false, mixed $class = false) static SQL查询  普通的 sql 语句查询
* @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行  sql 执行 事务执行
* @method PaginatorCollection paginate(integer $listRows = 15, boolean $simple = false, array $config = []) static 分页查询  分页查询
* @method mixed transaction(callable $callback) static 执行数据库事务  执行事务
* @method boolean batchQuery(array $sqlArray) static 批处理执行SQL语句 批量执行 sql 语句 传入参数 数组
*/
class Db
{
//  数据库连接实例
private static $instance = [];// 实例化
// 查询次数
public static $queryTimes = 0;// 查询次数
// 执行次数
public static $executeTimes = 0;// 执行次数

/**
* 数据库初始化 并取得数据库类实例
* @static
* @access public
* @param mixed         $config 连接配置
* @param bool|string   $name 连接标识 true 强制重新连接
* @return \think\db\Connection
* @throws Exception
*/
public static function connect($config = [], $name = false)
{// 配置项 跟 连接标识符
if (false === $name) {
$name = md5(serialize($config));// 不同的配置 不同的初始化
}
if (true === $name || !isset(self::$instance[$name])) {// 强制重新 连接 或者 没有当前配置的设置
// 解析连接参数 支持数组和字符串
$options = self::parseConfig($config);// 解析连接资源
if (empty($options['type'])) {// 类型为空,抛出异常
throw new \InvalidArgumentException('Underfined db type');
}// 获取 类信息
$class = false !== strpos($options['type'], '\\') ? $options['type'] : '\\think\\db\\connector\\' . ucwords($options['type']);
// 记录初始化信息
if (App::$debug) {// 日志记录
Log::record('[ DB ] INIT ' . $options['type'], 'info');
}
if (true === $name) {// 返回新的 实例化类,并且不存储
return new $class($options);
} else {//返回并保存
self::$instance[$name] = new $class($options);
}
}
return self::$instance[$name];//返回仓库 句柄
}

/**
* 数据库连接参数解析
* @static
* @access private
* @param mixed $config
* @return array
*/
private static function parseConfig($config)
{// 解析参数
if (empty($config)) {// 为空
$config = Config::get('database');// 获取配置
} elseif (is_string($config) && false === strpos($config, '/')) {// 字符串,并且 有相应个格式
// 支持读取配置参数
$config = Config::get($config);// 获取配置 位置的配置参数
}
if (is_string($config)) {// 如果是 普通的字符串
return self::parseDsn($config);// 按照 dsn 解析
} else {
return $config;// 否自 直接返回
}
}

/**
* DSN解析
* 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1¶m2=val2#utf8
* @static
* @access private
* @param string $dsnStr
* @return array
*/
private static function parseDsn($dsnStr)
{
$info = parse_url($dsnStr);// 按照 url 格式解析
if (!$info) {
return [];
}// 有问题 返回空
$dsn = [
'type'     => $info['scheme'],
'username' => isset($info['user']) ? $info['user'] : '',
'password' => isset($info['pass']) ? $info['pass'] : '',
'hostname' => isset($info['host']) ? $info['host'] : '',
'hostport' => isset($info['port']) ? $info['port'] : '',
'database' => !empty($info['path']) ? ltrim($info['path'], '/') : '',
'charset'  => isset($info['fragment']) ? $info['fragment'] : 'utf8',
];// 配置 默认系那个名

if (isset($info['query'])) {
parse_str($info['query'], $dsn['params']);// 解析字符串
} else {
$dsn['params'] = [];
}
return $dsn;// 返回解析数据
}

// 调用驱动类的方法
public static function __callStatic($method, $params)
{// 全部变成了 静态化调用
// 自动初始化数据库
return call_user_func_array([self::connect(), $method], $params);
}
}
// 这个类简单了,就是 实例化 连接,估计会是个父类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp php