Db.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\db\Connection;
  13. use think\db\Query;
  14. /**
  15. * Class Db
  16. * @package think
  17. * @method Query table(string $table) static 指定数据表(含前缀)
  18. * @method Query name(string $name) static 指定数据表(不含前缀)
  19. * @method Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件
  20. * @method Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询
  21. * @method Query union(mixed $union, boolean $all = false) static UNION查询
  22. * @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT
  23. * @method Query order(mixed $field, string $order = null) static 查询ORDER
  24. * @method Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存
  25. * @method mixed value(string $field) static 获取某个字段的值
  26. * @method array column(string $field, string $key = '') static 获取某个列的值
  27. * @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
  28. * @method mixed find(mixed $data = null) static 查询单个记录
  29. * @method mixed select(mixed $data = null) static 查询多个记录
  30. * @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录
  31. * @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID
  32. * @method integer insertAll(array $dataSet) static 插入多条记录
  33. * @method integer update(array $data) static 更新记录
  34. * @method integer delete(mixed $data = null) static 删除记录
  35. * @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据
  36. * @method mixed query(string $sql, array $bind = [], boolean $master = false, bool $pdo = false) static SQL查询
  37. * @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行
  38. * @method Paginator paginate(integer $listRows = 15, mixed $simple = null, array $config = []) static 分页查询
  39. * @method mixed transaction(callable $callback) static 执行数据库事务
  40. * @method void startTrans() static 启动事务
  41. * @method void commit() static 用于非自动提交状态下面的查询提交
  42. * @method void rollback() static 事务回滚
  43. * @method boolean batchQuery(array $sqlArray) static 批处理执行SQL语句
  44. * @method string quote(string $str) static SQL指令安全过滤
  45. * @method string getLastInsID($sequence = null) static 获取最近插入的ID
  46. */
  47. class Db
  48. {
  49. // 数据库连接实例
  50. private static $instance = [];
  51. // 查询次数
  52. public static $queryTimes = 0;
  53. // 执行次数
  54. public static $executeTimes = 0;
  55. /**
  56. * 数据库初始化 并取得数据库类实例
  57. * @static
  58. * @access public
  59. * @param mixed $config 连接配置
  60. * @param bool|string $name 连接标识 true 强制重新连接
  61. * @return Connection
  62. * @throws Exception
  63. */
  64. public static function connect($config = [], $name = false)
  65. {
  66. if (false === $name) {
  67. $name = md5(serialize($config));
  68. }
  69. if (true === $name || !isset(self::$instance[$name])) {
  70. // 解析连接参数 支持数组和字符串
  71. $options = self::parseConfig($config);
  72. if (empty($options['type'])) {
  73. throw new \InvalidArgumentException('Undefined db type');
  74. }
  75. $class = false !== strpos($options['type'], '\\') ? $options['type'] : '\\think\\db\\connector\\' . ucwords($options['type']);
  76. // 记录初始化信息
  77. if (App::$debug) {
  78. Log::record('[ DB ] INIT ' . $options['type'], 'info');
  79. }
  80. if (true === $name) {
  81. $name = md5(serialize($config));
  82. }
  83. self::$instance[$name] = new $class($options);
  84. }
  85. return self::$instance[$name];
  86. }
  87. public static function clear() {
  88. self::$instance = null;
  89. }
  90. /**
  91. * 数据库连接参数解析
  92. * @static
  93. * @access private
  94. * @param mixed $config
  95. * @return array
  96. */
  97. private static function parseConfig($config)
  98. {
  99. if (empty($config)) {
  100. $config = Config::get('database');
  101. } elseif (is_string($config) && false === strpos($config, '/')) {
  102. // 支持读取配置参数
  103. $config = Config::get($config);
  104. }
  105. if (is_string($config)) {
  106. return self::parseDsn($config);
  107. } else {
  108. return $config;
  109. }
  110. }
  111. /**
  112. * DSN解析
  113. * 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1&param2=val2#utf8
  114. * @static
  115. * @access private
  116. * @param string $dsnStr
  117. * @return array
  118. */
  119. private static function parseDsn($dsnStr)
  120. {
  121. $info = parse_url($dsnStr);
  122. if (!$info) {
  123. return [];
  124. }
  125. $dsn = [
  126. 'type' => $info['scheme'],
  127. 'username' => isset($info['user']) ? $info['user'] : '',
  128. 'password' => isset($info['pass']) ? $info['pass'] : '',
  129. 'hostname' => isset($info['host']) ? $info['host'] : '',
  130. 'hostport' => isset($info['port']) ? $info['port'] : '',
  131. 'database' => !empty($info['path']) ? ltrim($info['path'], '/') : '',
  132. 'charset' => isset($info['fragment']) ? $info['fragment'] : 'utf8',
  133. ];
  134. if (isset($info['query'])) {
  135. parse_str($info['query'], $dsn['params']);
  136. } else {
  137. $dsn['params'] = [];
  138. }
  139. return $dsn;
  140. }
  141. // 调用驱动类的方法
  142. public static function __callStatic($method, $params)
  143. {
  144. // 自动初始化数据库
  145. return call_user_func_array([self::connect(), $method], $params);
  146. }
  147. }