123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php namespace Dever\Store;
- use Dever;
- use Dever\Debug;
- use Dever\Sql;
- class Pdo extends Base
- {
- protected $tool;
- protected function connect($setting)
- {
- $this->tool = Dever::get(Sql::class);
- $this->type = $setting['type'];
- $handle = false;
- if (strpos($setting['host'], ':') !== false) {
- list($setting['host'], $setting['port']) = explode(':', $setting['host']);
- }
- if (empty($setting['pdo_type'])) {
- $setting['pdo_type'] = 'mysql';
- }
- if (empty($setting['charset'])) {
- $setting['charset'] = 'utf8mb4';
- }
- if (empty($setting['collation'])) {
- $setting['collation'] = 'utf8mb4_general_ci';
- }
- $dsn = $setting['pdo_type'] . ':type='.$setting['type'].';host='.$setting['host'].';port='.$setting['port'].';dbname='.$setting['name'].';collation='.$setting['collation'];
- try {
- if (empty($setting['persistent'])) {
- $persistent = false;
- }
- $handle = new \PDO($dsn, $setting['user'], $setting['pwd'], [\PDO::ATTR_PERSISTENT => $persistent]);
- $handle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- $handle->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_NATURAL);
- $handle->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
- $handle->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
- $handle->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
- //$handle->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
- //handle->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
- Dever::debug('db ' . $setting['host'] . ' connected', $setting['type']);
- return $handle;
- } catch (\PDOException $e) {
- if (strstr($e->getMessage(), 'Unknown database')) {
- $this->create($setting);
- return $this->connect($setting);
- } else {
- Dever::out()->error($e->getMessage());
- }
- }
- }
- private function create($setting)
- {
- $method = 'mysql';
- if (function_exists('mysqli_connect')) {
- $method = 'mysqli';
- }
- $connect = $method . '_connect';
- $query = $method . '_query';
- $close = $method . '_close';
- $link = $connect($setting['host'] . ':' . $setting['port'], $setting['user'], $setting['pwd']);
- if ($link) {
- $sql = 'CREATE DATABASE `' . $setting['name'] . '` DEFAULT CHARACTER SET ' . $setting['charset'] . ' COLLATE ' . $setting['collation'];
- if ($method == 'mysql') {
- $query($sql, $link);
- } else {
- $query($link, $sql);
- }
- $close($link);
- }
- }
- public function struct($config, $state = 0)
- {
- if ($state) {
- $sql = $this->tool->alter($config['table'], $config['struct'], $this->query($this->tool->desc($config['table'])));
- if ($sql) {
- $this->query($sql);
- }
- } else {
- $this->query($this->tool->create($config));
- }
- if (isset($config['default']) && $config['default']) {
- $count = $this->count($config['table'], [], $config['struct']);
- if (!$count) {
- $this->query($this->tool->inserts($config['table'], $config['default']));
- }
- }
- }
- public function index($config, $state = 0)
- {
- $this->query($this->tool->index($config['table'], $config['index'], $this->query($this->tool->showIndex($config['table']))));
- }
- public function partition($config, $partition)
- {
- $this->query($this->tool->partition($config['table'], $partition, $this->query($this->tool->showIndex($config['table']))));
- }
- public function query($sql, $bind = [], $method = 'read')
- {
- try {
- if ($bind) {
- $handle = $this->$method->prepare($sql);
- $handle->execute($bind);
- } else {
- if (strpos($sql, ';')) {
- $this->$method->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, true);
- $this->$method->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
- }
- $handle = $this->$method->query($sql);
- }
- } catch (\PDOException $exception) {
- $this->error(array('sql' => $sql, 'msg' => $exception->getMessage()));
- }
- if (Dever::get(Debug::class)->shell) {
- $this->bsql($sql, $bind);
- $this->log(array('sql' => $sql, 'count' => $handle->rowCount()));
- }
- return $handle;
- }
- public function load($table, $param, $set, $field, $version)
- {
- $bind = [];
- $sql = $this->tool->select($table, $param, $bind, $set, $field, $version);
- return $this->query($sql, $bind);
- }
- public function sql($table, $param, $set, $field, $version)
- {
- $bind = '';
- return $this->tool->select($table, $param, $bind, $set, $field, $version);
- }
- public function select($table, $param, $set, $field, $version)
- {
- return $this->load($table, $param, $set, $field, $version)->fetchAll();
- }
- public function find($table, $param, $set, $field, $version)
- {
- return $this->load($table, $param, $set, $field, $version)->fetch();
- }
- public function column($table, $param, $set, $field, $version)
- {
- return $this->load($table, $param, $set, $field, $version)->fetchColumn();
- }
- public function columns($table, $param, $set, $field, $version)
- {
- return $this->load($table, $param, $set, $field, $version)->fetchAll(\PDO::FETCH_COLUMN);
- }
- public function count($table, $param, $field)
- {
- return $this->load($table, $param, array('col'=>'count(*)'), $field, false)->fetch(\PDO::FETCH_NUM)[0];
- }
- public function explain($table, $param, $field)
- {
- $bind = [];
- $sql = $this->tool->explain($this->tool->select($table, $param, $bind, array('col'=>'count(*)'), $field, false));
- return $this->query($sql, $bind)->fetch();
- }
- public function insert($table, $data, $field)
- {
- $bind = [];
- $sql = $this->tool->insert($table, $data, $bind, $field);
- $this->query($sql, $bind, 'update');
- return $this->update->lastInsertId();
- }
- public function inserts($table, $data, $field)
- {
- $sql = $this->tool->inserts($table, $data);
- $this->query($sql, [], 'update');
- return $this->update->lastInsertId();
- }
- public function update($table, $param, $data, $field)
- {
- $bind = [];
- $sql = $this->tool->update($table, $param, $data, $bind, $field);
- return $this->query($sql, $bind, 'update')->rowCount();
- }
- public function delete($table, $param, $field)
- {
- $bind = [];
- $sql = $this->tool->delete($table, $param, $bind, $field);
- return $this->query($sql, $bind, 'update')->rowCount();
- }
- public function copy($table, $dest, $param, $field)
- {
- $bind = [];
- $sql = $this->tool->copy($table, $dest, $param, $bind, $field);
- return $this->query($sql, $bind, 'update')->rowCount();
- }
- public function optimize($table)
- {
- $sql = $this->tool->optimize($table) . ';' . $this->tool->analyze($table);
- return $this->query($sql, [], 'update');
- }
- public function inTransaction()
- {
- return $this->update->inTransaction();
- }
- public function begin()
- {
- $this->update->beginTransaction();
- }
- public function commit()
- {
- $this->update->commit();
- }
- public function rollback()
- {
- $this->update->rollback();
- }
- public function transaction($class, $param, $msg)
- {
- if (Dever::getCommit()) {
- try {
- Dever::setCommit();
- $this->begin();
- $result = call_user_func_array($class, $param);
- $this->commit();
- return $result;
- } catch (\Exception $e) {
- $this->rollback();
- Dever::out()->error($msg);
- }
- } else {
- return call_user_func_array($class, $param);
- }
- }
- }
|