Commit 612dd120 authored by Alex Ne's avatar Alex Ne

New MySQL Driver Fix

parent c4fbe584
......@@ -5,6 +5,26 @@ class TableItem {
* @var mixed
*/
protected $table_name;
/**
* @var mixed
*/
protected $data_param;
/**
* @var mixed
*/
protected $data_where;
/**
* @var mixed
*/
protected $data_columns;
/**
* @var mixed
*/
protected $data_order;
/**
* @var mixed
*/
protected $data_limit;
/**
* @var string
*/
......@@ -43,8 +63,12 @@ class TableItem {
* @return mixed
*/
public function insert($Data) {
$this->sql = "INSERT INTO " . $this->build_insert($Data);
$this->sql_type = "insert";
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_param = $Data;
return $this;
}
$this->sql = "INSERT INTO " . $this->build_insert($Data);
return $this;
}
......@@ -53,8 +77,12 @@ class TableItem {
* @return mixed
*/
public function replace($Data) {
$this->sql = "REPLACE INTO " . $this->build_insert($Data);
$this->sql_type = "insert";
$this->sql_type = "replace";
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_param = $Data;
return $this;
}
$this->sql = "REPLACE INTO " . $this->build_insert($Data);
return $this;
}
......@@ -75,22 +103,35 @@ class TableItem {
* @param $Data
*/
public function update($Data) {
$this->sql_type = "update";
if ( ! is_array($Data) || count($Data) == 0) {
return false;
}
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_param = $Data;
return $this;
}
$params = "";
foreach ($Data as $key => $value) {
$params .= strlen($params) == 0 ? "" : ", ";
$params .= "`" . $key . "` = '" . $this->escape($value) . "'";
}
$this->sql = "UPDATE `{$this->table_name}` SET {$params}";
$this->sql_type = "update";
$this->sql = "UPDATE `{$this->table_name}` SET {$params}";
return $this;
}
/**
* @return mixed
*/
public function first() {
$columns = func_get_args();
$this->sql_type = "first";
$columns = func_get_args();
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_columns = $columns;
return $this;
}
if (count($columns) == 1 && is_array($columns[0])) {
$columns = "`" . implode("`,`", $columns[0]) . "`";
} else if (count($columns) > 1) {
......@@ -99,13 +140,22 @@ class TableItem {
$columns = "*";
}
$this->sql_type = "first";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
return $this;
}
/**
* @return mixed
*/
public function select() {
$columns = func_get_args();
$this->sql_type = "select";
$columns = func_get_args();
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_columns = $columns;
return $this;
}
if (count($columns) == 1 && is_array($columns[0])) {
$columns = "`" . implode("`,`", $columns[0]) . "`";
} else if (count($columns) > 1) {
......@@ -114,14 +164,17 @@ class TableItem {
$columns = "*";
}
$this->sql_type = "select";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
return $this;
}
/**
* @return mixed
*/
public function delete() {
$this->sql_type = "delete";
$this->sql = "DELETE FROM `{$this->table_name}`";
$this->sql = "DELETE FROM `{$this->table_name}`";
return $this;
}
......@@ -130,6 +183,10 @@ class TableItem {
* @return mixed
*/
public function where($data) {
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_where = $data;
return $this;
}
$this->sql_where = " WHERE " . $this->where_clause($data);
return $this;
}
......@@ -295,6 +352,10 @@ class TableItem {
* @param $type
*/
public function order($coll, $type) {
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_order = [$coll, $type];
return $this;
}
$this->sql_order = " ORDER BY `{$coll}` {$type}";
return $this;
}
......@@ -304,6 +365,11 @@ class TableItem {
* @param $op2
*/
public function limit($op1, $op2 = false) {
if ($this->driver instanceof \X\Database\Driver\PDO) {
$this->data_limit = [$op1];
if ($op2) {$this->data_limit[] = $op2;}
return $this;
}
if ($op2) {
$this->sql_limit = " LIMIT {$op1},{$op2}";
} else {
......@@ -321,6 +387,7 @@ class TableItem {
if ($this->driver instanceof \X_DB_MySQLi) {
return $this->driver->esc($data);
}
return $data;
}
/**
......@@ -340,11 +407,9 @@ class TableItem {
if ($this->sql_type == "update" && $this->driver instanceof \X_DB_MySQLi) {
return $this->driver->rq($this->sql . $this->sql_where);
}
if ($this->sql_type == "insert" && $this->driver instanceof \X_DB_MySQLi) {
return $this->driver->insert($this->sql);
}
if ($this->sql_type == "select" && $this->driver instanceof \X_DB_MySQLi) {
return $this->driver->get($this->sql . $this->sql_where . $this->sql_order . $this->sql_limit, $op1, $op2, $op3);
}
......@@ -355,6 +420,36 @@ class TableItem {
return $this->driver->rq($this->sql . $this->sql_where);
}
if ($this->sql_type == "update" && $this->driver instanceof \X\Database\Driver\PDO) {
return $this->driver->update($this->table_name, $this->data_param, $this->data_where);
}
if ($this->sql_type == "insert" && $this->driver instanceof \X\Database\Driver\PDO) {
return $this->driver->insert($this->table_name, $this->data_param);
}
if ($this->sql_type == "replace" && $this->driver instanceof \X\Database\Driver\PDO) {
return $this->driver->replace($this->table_name, $this->data_param);
}
if ($this->sql_type == "select" && $this->driver instanceof \X\Database\Driver\PDO) {
return $this->driver->select(
$this->table_name,
$this->data_where,
$this->data_order,
$this->data_limit,
$this->data_columns);
}
if ($this->sql_type == "first" && $this->driver instanceof \X\Database\Driver\PDO) {
return $this->driver->simple(
$this->table_name,
$this->data_where,
$this->data_order,
$this->data_limit,
$this->data_columns);
}
if ($this->sql_type == "delete" && $this->driver instanceof \X\Database\Driver\PDO) {
$whete_obj = new \X\Database\Driver\PDOWhereConstructor($this->data_where);
return $this->driver->exec($this->sql . $whete_obj->get_sql());
}
throw new \Exception("Internal error", 0);
}
}
......
......@@ -75,7 +75,7 @@ class Credetional {
}
public function get_PDO_MySQL_DSN() {
return "mysql:host={$this->hostname};port={$this->port};dbname={$this->database}";
return "mysql:host={$this->hostname};port={$this->port};dbname={$this->database};";
}
public function get_model() {
......
......@@ -61,6 +61,11 @@ class PDO {
}
}
public function set_charset() {
$charset = $this->Credetional->get_charset();
$this->exec("SET NAMES `{$charset}`");
}
protected function connect() {
try {
$this->PDO = new \PDO(
......@@ -68,6 +73,7 @@ class PDO {
$this->Credetional->get_username(),
$this->Credetional->get_password());
$this->PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->set_charset();
} catch (\PDOException $e) {
throw new PDO_ConnectionError("Connection to database error", 1, [
"message" => $e->getMessage(),
......@@ -145,7 +151,7 @@ class PDO {
* @param $table
* @param $data
*/
public function insert($table, $data, $replace) {
public function insert($table, $data, $replace = false) {
$keys = array_keys($data);
$pattern = array_map(function ($key) {return ":{$key}";}, $keys);
......@@ -154,7 +160,7 @@ class PDO {
} else {
$type = "INSERT";
}
$SQL = $type . " INTO `{$table}` (`" . implode('`,`', $keys) . "`) VALUES ( " . implode(", ", $keys) . " )";
$SQL = "{$type} INTO `{$table}` (`" . implode('`,`', $keys) . "`) VALUES ( " . implode(", ", $pattern) . " )";
$statement = $this->prepare($SQL);
foreach ($keys as $key) {
if (is_integer($data[$key])) {
......@@ -163,7 +169,12 @@ class PDO {
$statement->bindValue(":{$key}", $data[$key], \PDO::PARAM_STR);
}
}
return $statement->execute();
if ($statement->execute()) {
return $this->PDO->lastInsertId();
}
return false;
}
/**
......@@ -243,6 +254,7 @@ class PDO {
* @param null $columns
*/
public function select_statement($table, $where = [], $order = null, $limit = null, $columns = "*") {
$this->build_columns($columns);
$SQL = "SELECT {$columns} FROM `{$table}` ";
$whete_obj = new PDOWhereConstructor($where);
$SQL .= $whete_obj->get_sql();
......@@ -255,12 +267,24 @@ class PDO {
} else if (is_array($order)) {
$SQL .= " LIMIT {$limit[0]},{$limit[1]}";
}
$statement = $this->prepare($SQL);
$whete_obj->bind($statement);
$statement->execute();
return $statement;
}
/**
* @param $columns
*/
private function build_columns(&$columns) {
if (count($columns) == 1 && is_array($columns[0])) {
$columns = "`" . implode("`,`", $columns[0]) . "`";
} else if (count($columns) > 1) {
$columns = "`" . implode("`,`", $columns) . "`";
} else {
$columns = "*";
}
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment