From 15838405912f2db0da79c6004f8bafbc6fce0054 Mon Sep 17 00:00:00 2001 From: Catfan Date: Sun, 10 Apr 2016 19:35:58 +0800 Subject: [PATCH 01/12] [feature] Advanced order syntax #181 --- medoo.php | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/medoo.php b/medoo.php index 4871d23f..ec81a4a3 100644 --- a/medoo.php +++ b/medoo.php @@ -461,37 +461,33 @@ protected function where_clause($where) if (isset($where[ 'ORDER' ])) { - $rsort = '/(^[a-zA-Z0-9_\-\.]*)(\s*(DESC|ASC))?/'; $ORDER = $where[ 'ORDER' ]; if (is_array($ORDER)) { - if ( - isset($ORDER[ 1 ]) && - is_array($ORDER[ 1 ]) - ) - { - $where_clause .= ' ORDER BY FIELD(' . $this->column_quote($ORDER[ 0 ]) . ', ' . $this->array_quote($ORDER[ 1 ]) . ')'; - } - else - { - $stack = array(); + $stack = array(); - foreach ($ORDER as $column) + foreach ($ORDER as $column => $value) + { + if (is_array($value)) { - preg_match($rsort, $column, $order_match); - - array_push($stack, '"' . str_replace('.', '"."', $order_match[ 1 ]) . '"' . (isset($order_match[ 3 ]) ? ' ' . $order_match[ 3 ] : '')); + array_push($stack, 'FIELD(' . $this->column_quote($column) . ', ' . $this->array_quote($value) . ')'); + } + else if ($value === 'ASC' || $value === 'DESC') + { + array_push($stack, '"' . str_replace('.', '"."', $column) . ' ' . $value . '"'); + } + else if (is_int($column)) + { + array_push($stack, '"' . str_replace('.', '"."', $value) . '"'); } - - $where_clause .= ' ORDER BY ' . implode($stack, ','); } + + $where_clause .= ' ORDER BY ' . implode($stack, ','); } else { - preg_match($rsort, $ORDER, $order_match); - - $where_clause .= ' ORDER BY "' . str_replace('.', '"."', $order_match[ 1 ]) . '"' . (isset($order_match[ 3 ]) ? ' ' . $order_match[ 3 ] : ''); + $where_clause .= ' ORDER BY "' . str_replace('.', '"."', $ORDER) . '"'; } } From ae0d13c06bc568b2913aba869bc0ca687acad756 Mon Sep 17 00:00:00 2001 From: Catfan Date: Mon, 18 Apr 2016 16:48:54 +0800 Subject: [PATCH 02/12] [update] Improve column quote --- medoo.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/medoo.php b/medoo.php index ec81a4a3..3709d759 100644 --- a/medoo.php +++ b/medoo.php @@ -190,7 +190,7 @@ public function quote($string) protected function column_quote($string) { - return '"' . str_replace('.', '"."', preg_replace('/(^#|\(JSON\)\s*)/', '', $string)) . '"'; + return preg_replace('/(\(JSON\)\s*|^#)?([a-zA-Z0-9_]*)\.([a-zA-Z0-9_]*)/', '"$2"."$3"', $string); } protected function column_push($columns) @@ -479,7 +479,7 @@ protected function where_clause($where) } else if (is_int($column)) { - array_push($stack, '"' . str_replace('.', '"."', $value) . '"'); + array_push($stack, $this->column_quote($value); } } @@ -487,7 +487,7 @@ protected function where_clause($where) } else { - $where_clause .= ' ORDER BY "' . str_replace('.', '"."', $ORDER) . '"'; + $where_clause .= ' ORDER BY ' . $this->column_quote($ORDER); } } @@ -574,7 +574,7 @@ protected function select_context($table, $join, &$columns = null, $where = null $joins[] = $this->prefix . ( strpos($key, '.') > 0 ? // For ['tableB.column' => 'column'] - '"' . str_replace('.', '"."', $key) . '"' : + $this->column_quote($key) : // For ['column1' => 'column2'] $table . '."' . $key . '"' From e3a60cb488c7de389d90e366bc7eb8e4242ee85a Mon Sep 17 00:00:00 2001 From: Catfan Date: Mon, 18 Apr 2016 16:49:11 +0800 Subject: [PATCH 03/12] [update] Update version --- medoo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/medoo.php b/medoo.php index 3709d759..e11121f0 100644 --- a/medoo.php +++ b/medoo.php @@ -2,7 +2,7 @@ /*! * Medoo database framework * http://medoo.in - * Version 1.0.2 + * Version 1.1 * * Copyright 2016, Angel Lai * Released under the MIT license From 0ba8db301efd7997412b2bdcf6a18fec4d34474c Mon Sep 17 00:00:00 2001 From: Catfan Date: Sun, 22 May 2016 13:34:28 +0800 Subject: [PATCH 04/12] [Fix] Quote fix --- medoo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/medoo.php b/medoo.php index e11121f0..6a16a473 100644 --- a/medoo.php +++ b/medoo.php @@ -479,7 +479,7 @@ protected function where_clause($where) } else if (is_int($column)) { - array_push($stack, $this->column_quote($value); + array_push($stack, $this->column_quote($value)); } } From 6ab6038060d849721cc5bab79f71525db8d1737c Mon Sep 17 00:00:00 2001 From: Catfan Date: Sun, 22 May 2016 23:29:45 +0800 Subject: [PATCH 05/12] [feature] Add data mapping for select --- medoo.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/medoo.php b/medoo.php index 6a16a473..2a639095 100644 --- a/medoo.php +++ b/medoo.php @@ -209,15 +209,22 @@ protected function column_push($columns) foreach ($columns as $key => $value) { - preg_match('/([a-zA-Z0-9_\-\.]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $value, $match); - - if (isset($match[ 1 ], $match[ 2 ])) + if (is_array($value)) { - array_push($stack, $this->column_quote( $match[ 1 ] ) . ' AS ' . $this->column_quote( $match[ 2 ] )); + array_push($stack, $this->column_push($value)); } else { - array_push($stack, $this->column_quote( $value )); + preg_match('/([a-zA-Z0-9_\-\.]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $value, $match); + + if (isset($match[ 1 ], $match[ 2 ])) + { + array_push($stack, $this->column_quote( $match[ 1 ] ) . ' AS ' . $this->column_quote( $match[ 2 ] )); + } + else + { + array_push($stack, $this->column_quote( $value )); + } } } @@ -656,13 +663,74 @@ protected function select_context($table, $join, &$columns = null, $where = null return 'SELECT ' . $column . ' FROM ' . $table . $this->where_clause($where); } + protected function data_map($index, $key, $value, $data, &$stack) + { + if (is_array($value)) + { + $vstack = []; + + foreach ($value as $sub_key => $sub_value) + { + if (is_array($sub_value)) + { + $dstack = $stack[ $index ][ $key ]; + + $this->data_map(false, $sub_key, $sub_value, $data, $dstack); + + $stack[ $index ][ $key ][ $sub_key ] = $dstack[0][ $sub_key ]; + } + else + { + $this->data_map(false, preg_replace('/^[\w]*\./i', "", $sub_value), $sub_key, $data, $vstack); + + $stack[ $index ][ $key ] = $vstack; + } + } + } + else + { + if ($index !== false) + { + $stack[ $index ][ $value ] = $data[ $value ]; + } + else + { + $stack[ $key ] = $data[ $key ]; + } + } + } + public function select($table, $join, $columns = null, $where = null) { $query = $this->query($this->select_context($table, $join, $columns, $where)); - return $query ? $query->fetchAll( - (is_string($columns) && $columns != '*') ? PDO::FETCH_COLUMN : PDO::FETCH_ASSOC - ) : false; + $stack = array(); + + $index = 0; + + if (!$query) + { + return false; + } + + while ($row = $query->fetch(PDO::FETCH_ASSOC)) + { + foreach ($columns as $key => $value) + { + if (is_array($value)) + { + $this->data_map($index, $key, $value, $row, $stack); + } + else + { + $this->data_map($index, $key, preg_replace('/^[\w]*\./i', "", $value), $row, $stack); + } + } + + $index++; + } + + return $stack; } public function insert($table, $datas) From 6ae9eb5229f71076e2930a8ba1db135dc0e4b5a6 Mon Sep 17 00:00:00 2001 From: Catfan Date: Sat, 4 Jun 2016 13:18:09 +0800 Subject: [PATCH 06/12] [update] Code improve --- medoo.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/medoo.php b/medoo.php index 2a639095..67c0f85b 100644 --- a/medoo.php +++ b/medoo.php @@ -162,7 +162,7 @@ public function query($query) return false; } - array_push($this->logs, $query); + $this->logs[] = $query; return $this->pdo->query($query); } @@ -178,7 +178,7 @@ public function exec($query) return false; } - array_push($this->logs, $query); + $this->logs[] = $query; return $this->pdo->exec($query); } @@ -211,7 +211,7 @@ protected function column_push($columns) { if (is_array($value)) { - array_push($stack, $this->column_push($value)); + $stack[] = $this->column_push($value); } else { @@ -219,11 +219,11 @@ protected function column_push($columns) if (isset($match[ 1 ], $match[ 2 ])) { - array_push($stack, $this->column_quote( $match[ 1 ] ) . ' AS ' . $this->column_quote( $match[ 2 ] )); + $stack[] = $this->column_quote( $match[ 1 ] ) . ' AS ' . $this->column_quote( $match[ 2 ] ); } else { - array_push($stack, $this->column_quote( $value )); + $stack[] = $this->column_quote( $value ); } } } @@ -478,15 +478,15 @@ protected function where_clause($where) { if (is_array($value)) { - array_push($stack, 'FIELD(' . $this->column_quote($column) . ', ' . $this->array_quote($value) . ')'); + $stack[] = 'FIELD(' . $this->column_quote($column) . ', ' . $this->array_quote($value) . ')'; } else if ($value === 'ASC' || $value === 'DESC') { - array_push($stack, '"' . str_replace('.', '"."', $column) . ' ' . $value . '"'); + $stack[] = '"' . str_replace('.', '"."', $column) . ' ' . $value . '"'; } else if (is_int($column)) { - array_push($stack, $this->column_quote($value)); + $stack[] = $this->column_quote($value); } } @@ -667,23 +667,23 @@ protected function data_map($index, $key, $value, $data, &$stack) { if (is_array($value)) { - $vstack = []; + $sub_stack = array(); foreach ($value as $sub_key => $sub_value) { if (is_array($sub_value)) { - $dstack = $stack[ $index ][ $key ]; + $current_stack = $stack[ $index ][ $key ]; - $this->data_map(false, $sub_key, $sub_value, $data, $dstack); + $this->data_map(false, $sub_key, $sub_value, $data, $current_stack); - $stack[ $index ][ $key ][ $sub_key ] = $dstack[0][ $sub_key ]; + $stack[ $index ][ $key ][ $sub_key ] = $current_stack[ 0 ][ $sub_key ]; } else { - $this->data_map(false, preg_replace('/^[\w]*\./i', "", $sub_value), $sub_key, $data, $vstack); + $this->data_map(false, preg_replace('/^[\w]*\./i', "", $sub_value), $sub_key, $data, $sub_stack); - $stack[ $index ][ $key ] = $vstack; + $stack[ $index ][ $key ] = $sub_stack; } } } @@ -750,7 +750,7 @@ public function insert($table, $datas) foreach ($data as $key => $value) { - array_push($columns, $this->column_quote($key)); + $columns[] = $this->column_quote($key); switch (gettype($value)) { From cd389edf8589016c1c0142fe2300e677b0a33f7e Mon Sep 17 00:00:00 2001 From: Catfan Date: Thu, 9 Jun 2016 00:25:16 +0800 Subject: [PATCH 07/12] [fix] Fix table prefix bug --- medoo.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/medoo.php b/medoo.php index 67c0f85b..73666a95 100644 --- a/medoo.php +++ b/medoo.php @@ -190,7 +190,7 @@ public function quote($string) protected function column_quote($string) { - return preg_replace('/(\(JSON\)\s*|^#)?([a-zA-Z0-9_]*)\.([a-zA-Z0-9_]*)/', '"$2"."$3"', $string); + return preg_replace('/(\(JSON\)\s*|^#)?([a-zA-Z0-9_]*)\.([a-zA-Z0-9_]*)/', '"' . $this->prefix . '$2"."$3"', $string); } protected function column_push($columns) @@ -578,7 +578,7 @@ protected function select_context($table, $join, &$columns = null, $where = null foreach ($relation as $key => $value) { - $joins[] = $this->prefix . ( + $joins[] = ( strpos($key, '.') > 0 ? // For ['tableB.column' => 'column'] $this->column_quote($key) : @@ -594,7 +594,14 @@ protected function select_context($table, $join, &$columns = null, $where = null } } - $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN "' . $this->prefix . $match[ 3 ] . '" ' . (isset($match[ 5 ]) ? 'AS "' . $match[ 5 ] . '" ' : '') . $relation; + $table_name = $this->prefix . $match[ 3 ] . '" '; + + if (isset($match[ 5 ])) + { + $table_name .= 'AS "' . $match[ 5 ] . '" '; + } + + $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN "' . $table_name . $relation; } } From 7b8ac3bd0a968ade3068e3cf98f3a7797a59284f Mon Sep 17 00:00:00 2001 From: Catfan Date: Thu, 9 Jun 2016 00:52:41 +0800 Subject: [PATCH 08/12] [fix] Fix table alias prefix for columns --- medoo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/medoo.php b/medoo.php index 73666a95..d437b938 100644 --- a/medoo.php +++ b/medoo.php @@ -598,7 +598,7 @@ protected function select_context($table, $join, &$columns = null, $where = null if (isset($match[ 5 ])) { - $table_name .= 'AS "' . $match[ 5 ] . '" '; + $table_name .= 'AS "' . $this->prefix . $match[ 5 ] . '" '; } $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN "' . $table_name . $relation; From a8128222e57cbb760b8e5c177e563136261572e9 Mon Sep 17 00:00:00 2001 From: Catfan Date: Thu, 9 Jun 2016 14:01:06 +0800 Subject: [PATCH 09/12] [feature] Improve table quote and add table alias feature --- medoo.php | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/medoo.php b/medoo.php index d437b938..040734a5 100644 --- a/medoo.php +++ b/medoo.php @@ -188,6 +188,11 @@ public function quote($string) return $this->pdo->quote($string); } + protected function table_quote($table) + { + return '"' . $this->prefix . $table . '"'; + } + protected function column_quote($string) { return preg_replace('/(\(JSON\)\s*|^#)?([a-zA-Z0-9_]*)\.([a-zA-Z0-9_]*)/', '"' . $this->prefix . '$2"."$3"', $string); @@ -537,7 +542,21 @@ protected function where_clause($where) protected function select_context($table, $join, &$columns = null, $where = null, $column_fn = null) { - $table = '"' . $this->prefix . $table . '"'; + preg_match('/([a-zA-Z0-9_\-]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $table, $table_match); + + if (isset($table_match[ 1 ], $table_match[ 2 ])) + { + $table = $this->table_quote($table_match[ 1 ]); + + $table_query = $this->table_quote($table_match[ 1 ]) . ' AS ' . $this->table_quote($table_match[ 2 ]); + } + else + { + $table = $this->table_quote($table); + + $table_query = $table; + } + $join_key = is_array($join) ? array_keys($join) : null; if ( @@ -605,7 +624,7 @@ protected function select_context($table, $join, &$columns = null, $where = null } } - $table .= ' ' . implode($table_join, ' '); + $table_query .= ' ' . implode($table_join, ' '); } else { @@ -667,7 +686,7 @@ protected function select_context($table, $join, &$columns = null, $where = null $column = $this->column_push($columns); } - return 'SELECT ' . $column . ' FROM ' . $table . $this->where_clause($where); + return 'SELECT ' . $column . ' FROM ' . $table_query . $this->where_clause($where); } protected function data_map($index, $key, $value, $data, &$stack) @@ -785,7 +804,7 @@ public function insert($table, $datas) } } - $this->exec('INSERT INTO "' . $this->prefix . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')'); + $this->exec('INSERT INTO ' . $this->table_quote($table) . ' (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')'); $lastId[] = $this->pdo->lastInsertId(); } @@ -839,12 +858,12 @@ public function update($table, $data, $where = null) } } - return $this->exec('UPDATE "' . $this->prefix . $table . '" SET ' . implode(', ', $fields) . $this->where_clause($where)); + return $this->exec('UPDATE ' . $this->table_quote($table) . ' SET ' . implode(', ', $fields) . $this->where_clause($where)); } public function delete($table, $where) { - return $this->exec('DELETE FROM "' . $this->prefix . $table . '"' . $this->where_clause($where)); + return $this->exec('DELETE FROM ' . $this->table_quote($table) . $this->where_clause($where)); } public function replace($table, $columns, $search = null, $replace = null, $where = null) From 048c08393c1d9eb284aad9a12b14ea44a3961c0f Mon Sep 17 00:00:00 2001 From: Catfan Date: Thu, 9 Jun 2016 14:38:47 +0800 Subject: [PATCH 10/12] [fix] Column alias bug --- medoo.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/medoo.php b/medoo.php index 040734a5..a74a95ed 100644 --- a/medoo.php +++ b/medoo.php @@ -198,7 +198,7 @@ protected function column_quote($string) return preg_replace('/(\(JSON\)\s*|^#)?([a-zA-Z0-9_]*)\.([a-zA-Z0-9_]*)/', '"' . $this->prefix . '$2"."$3"', $string); } - protected function column_push($columns) + protected function column_push(&$columns) { if ($columns == '*') { @@ -225,6 +225,8 @@ protected function column_push($columns) if (isset($match[ 1 ], $match[ 2 ])) { $stack[] = $this->column_quote( $match[ 1 ] ) . ' AS ' . $this->column_quote( $match[ 2 ] ); + + $columns[ $key ] = $match[ 2 ]; } else { From b1ecd96f302f66125682c561606edb8b36ba1ad6 Mon Sep 17 00:00:00 2001 From: Catfan Date: Thu, 9 Jun 2016 16:20:55 +0800 Subject: [PATCH 11/12] [update] Code improve --- medoo.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/medoo.php b/medoo.php index a74a95ed..93802049 100644 --- a/medoo.php +++ b/medoo.php @@ -127,7 +127,7 @@ public function __construct($options = null) } if ( - in_array($type, explode(' ', 'mariadb mysql pgsql sybase mssql')) && + in_array($type, array('mariadb', 'mysql', 'pgsql', 'sybase', 'mssql')) && $this->charset ) { @@ -428,7 +428,7 @@ protected function where_clause($where) $where_OR = preg_grep("/^OR\s*#?$/i", $where_keys); $single_condition = array_diff_key($where, array_flip( - explode(' ', 'AND OR GROUP ORDER HAVING LIMIT LIKE MATCH') + array('AND', 'OR', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'LIKE', 'MATCH') )); if ($single_condition != array()) @@ -608,21 +608,21 @@ protected function select_context($table, $join, &$columns = null, $where = null $table . '."' . $key . '"' ) . ' = ' . - '"' . (isset($match[ 5 ]) ? $match[ 5 ] : $match[ 3 ]) . '"."' . $value . '"'; + $this->table_quote(isset($match[ 5 ]) ? $match[ 5 ] : $match[ 3 ]) . '."' . $value . '"'; } $relation = 'ON ' . implode($joins, ' AND '); } } - $table_name = $this->prefix . $match[ 3 ] . '" '; + $table_name = $this->table_quote($match[ 3 ]) . ' '; if (isset($match[ 5 ])) { - $table_name .= 'AS "' . $this->prefix . $match[ 5 ] . '" '; + $table_name .= 'AS ' . $this->table_quote($match[ 5 ]) . ' '; } - $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN "' . $table_name . $relation; + $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN ' . $table_name . $relation; } } From 05f96ca366cd0592daf9dc7c65536d22088ff523 Mon Sep 17 00:00:00 2001 From: Catfan Date: Fri, 24 Jun 2016 15:46:27 +0800 Subject: [PATCH 12/12] [update] Code improve --- medoo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/medoo.php b/medoo.php index 93802049..b5bbe09b 100644 --- a/medoo.php +++ b/medoo.php @@ -905,7 +905,7 @@ public function replace($table, $columns, $search = null, $replace = null, $wher } } - return $this->exec('UPDATE "' . $this->prefix . $table . '" SET ' . $replace_query . $this->where_clause($where)); + return $this->exec('UPDATE ' . $this->table_quote($table) . ' SET ' . $replace_query . $this->where_clause($where)); } public function get($table, $join = null, $column = null, $where = null)