123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php namespace Dever\Store;
- use Dever;
- use Dever\Debug;
- use Dever\Output;
- use Dever\String\Sql;
- class Pdo extends Base
- {
- protected function connect($setting, $partition)
- {
- if (isset($partition['database'])) {
- $setting['name'] .= '_' . $partition['database'];
- }
- $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'], array(\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_EMULATE_PREPARES, false);
- //handle->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
- Debug::add('db ' . $setting['host'] . ' connected', $setting['type']);
- return $handle;
- } catch (\PDOException $e) {
- if (strstr($e->getMessage(), 'Unknown database')) {
- $this->create($setting);
- $this->connect($setting, $partition);
- } else {
- Output::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) {
- $this->query(Sql::alter($config['table'], $config['struct'], $this->query(Sql::desc($config['table']))));
- } else {
- $this->query(Sql::create($config));
- }
- if (isset($config['default']) && $config['default']) {
- $count = $this->count($config['table'], array());
- if (!$count) {
- $this->query(Sql::inserts($config['table'], $config['default']));
- }
- }
- }
- public function index($config, $state = 0)
- {
- $this->query(Sql::index($config['table'], $config['index'], $this->query(Sql::showIndex($config['table']))));
- }
- public function partition($config, $partition)
- {
- $this->query(Sql::partition($config['table'], $partition, $this->query(Sql::showIndex($config['table']))));
- }
- public function query($sql, $bind = array(), $method = 'read')
- {
- try {
- if ($bind) {
- $handle = $this->$method->prepare($sql);
- $handle->execute($bind);
- } else {
- $handle = $this->$method->query($sql);
- }
- } catch (\PDOException $exception) {
- $this->error($exception->getMessage(), $sql);
- }
- if (Debug::$shell) {
- $this->sql($sql, $bind);
- $this->log(array('sql' => $sql, 'count' => $handle->rowCount()));
- }
- return $handle;
- }
- public function select($table, $param, $set, $field)
- {
- $bind = array();
- $sql = Sql::select($table, $param, $bind, $set, $field);
- return $this->query($sql, $bind);
- }
- public function count($table, $param, $field)
- {
- return $this->select($table, $param, array('col'=>'count(*)'), $field)->fetch(\PDO::FETCH_NUM)[0];
- }
- public function insert($table, $data, $field)
- {
- $bind = array();
- $sql = Sql::insert($table, $data, $bind, $field);
- $this->query($sql, $bind, 'update');
- return $this->update->lastInsertId();
- }
- public function update($table, $param, $data, $field)
- {
- $bind = array();
- $sql = Sql::update($table, $param, $data, $bind, $field);
- return $this->query($sql, $bind, 'update')->rowCount();
- }
- public function delete($table, $param, $field)
- {
- $bind = array();
- $sql = Sql::delete($table, $param, $bind, $field);
- return $this->query($sql, $bind, 'update')->rowCount();
- }
- public function begin()
- {
- $this->update->beginTransaction();
- }
- public function commit()
- {
- $this->update->commit();
- }
- public function rollback()
- {
- $this->update->rollback();
- }
- }
|