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(); } }