From a7dc73adfabdaa966a05877ab7a382a5f551e7ae Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:08:49 +0800 Subject: [PATCH 01/76] change to phpdoc --- sql4array.class.php | 200 ++++++++++++++++++++++++-------------------- 1 file changed, 109 insertions(+), 91 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 2a599a6..f36c367 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -9,7 +9,7 @@ * Date: 30/04/2007 * License: LGPL */ - + /* * Parameters available : * SELECT, DISTINCT, FROM, WHERE, ORDER BY, LIMIT, OFFSET @@ -20,14 +20,15 @@ * Functions available in WHERE parameters : * LOWER(var), UPPER(var), TRIM(var) */ - + class sql4array { - /* Init - -------------------------------------------- */ + /** + * Init + **/ private $query = FALSE; private $parse_query = FALSE; - private $parse_query_lower = FALSE; + private $parse_query_lower = FALSE; private $parse_select = FALSE; private $parse_select_as = FALSE; private $parse_from = FALSE; @@ -37,8 +38,9 @@ class sql4array private $tables = array(); private $response = array(); - /* Query function - -------------------------------------------- */ + /** + * Query function + **/ public function query($query) { $this->destroy(); @@ -51,17 +53,18 @@ public function query($query) $this->parse_where(); $this->exec_query(); $this->parse_order(); - + return $this->return_response(); } - - /* Destroy current values - -------------------------------------------- */ + + /** + * Destroy current values + **/ private function destroy() { $this->query = FALSE; $this->parse_query = FALSE; - $this->parse_query_lower = FALSE; + $this->parse_query_lower = FALSE; $this->parse_select = FALSE; $this->parse_select_as = FALSE; $this->parse_from = FALSE; @@ -71,9 +74,10 @@ private function destroy() $this->tables = array(); $this->response = array(); } - - /* Parse SQL query - -------------------------------------------- */ + + /** + * Parse SQL query + **/ private function parse_query() { $this->parse_query = preg_replace('#ORDER(\s){2,}BY(\s+)(.*)(\s+)(ASC|DESC)#i', 'ORDER BY \\3 \\5', $this->query); @@ -81,27 +85,29 @@ private function parse_query() $this->parse_query = array_map('trim', $this->parse_query); $this->parse_query_lower = array_map('strtolower', $this->parse_query); } - - /* Parse SQL select parameters - -------------------------------------------- */ + + /** + * Parse SQL select parameters + **/ private function parse_select() { $key = array_search("distinct", $this->parse_query_lower); - + if ($key === FALSE) $key = array_search("select", $this->parse_query_lower); else $this->distinct_query = TRUE; - + $string = $this->parse_query[$key+1]; $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); - + foreach ($arrays as $array) $this->parse_select[] = $array; } - - /* Parse again SQL select parameters with as keyword - -------------------------------------------- */ + + /** + * Parse again SQL select parameters with as keyword + **/ private function parse_select_as() { foreach ($this->parse_select as $select) @@ -117,21 +123,23 @@ private function parse_select_as() } } } - - /* Parse SQL from parameters - -------------------------------------------- */ + + /** + * Parse SQL from parameters + **/ private function parse_from() { $key = array_search("from", $this->parse_query_lower); $string = $this->parse_query[$key+1]; $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); - + foreach ($arrays as $array) $this->parse_from[] = $array; } - - /* Parse again SQL from parameters with as keyword - -------------------------------------------- */ + + /** + * Parse again SQL from parameters with as keyword + **/ private function parse_from_as() { foreach ($this->parse_from as $from) @@ -139,7 +147,7 @@ private function parse_from_as() if (eregi('AS', $from)) { $arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY); - + $table = $arrays[0]; global $$table; $this->parse_from_as[$arrays[1]] = $table; @@ -154,34 +162,37 @@ private function parse_from_as() } } } - - /* Parse SQL where parameters - -------------------------------------------- */ + + /** + * Parse SQL where parameters + **/ private function parse_where() { $key = array_search("where", $this->parse_query_lower); if ($key == FALSE) return $this->parse_where = "return TRUE;"; - + $string = $this->parse_query[$key+1]; - + if (trim($string) == '') return $this->parse_where = "return TRUE;"; - - /* SQL Functions - -------------------------------------------- */ + + /** + * SQL Functions + **/ $patterns[] = '#LOWER\((.*)\)#ie'; $patterns[] = '#UPPER\((.*)\)#ie'; $patterns[] = '#TRIM\((.*)\)#ie'; - + $replacements[] = "'strtolower(\\1)'"; $replacements[] = "'strtoupper(\\1)'"; $replacements[] = "'trim(\\1)'"; - /* Basics SQL operators - -------------------------------------------- */ + /** + * Basics SQL operators + **/ $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie'; $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie'; @@ -190,7 +201,7 @@ private function parse_where() $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(NOT IN)(\s)+\((.*)\)#ie'; $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(IN)(\s)+\((.*)\)#ie'; - + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \\9 '"; $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \"\\10\" '"; $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; @@ -199,28 +210,29 @@ private function parse_where() $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \"\\10\" '"; $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; - - /* match SQL operators - -------------------------------------------- */ + + /** + * match SQL operators + **/ $ereg = array('%' => '(.*)', '_' => '(.)'); - + $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; - + $replacements[] = "'ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; $replacements[] = "'eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; $replacements[] = "'!ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; $replacements[] = "'!eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; - + $this->parse_where = "return ".stripslashes(trim(preg_replace($patterns, $replacements, $string))).";"; } - - /* + + /* -------------------------------------------- */ private function parse_where_key($key) - { + { if (ereg('\.', $key)) { list($table, $col) = explode('.', $key); @@ -231,9 +243,10 @@ private function parse_where_key($key) return '$row[$this->parse_select_as['.$key.']]'; } } - - /* Format IN parameters for PHP - -------------------------------------------- */ + + /** + * Format IN parameters for PHP + **/ private function parse_in($string) { $array = explode(',', $string); @@ -241,23 +254,24 @@ private function parse_in($string) return implode(' || ', $array); } - - /* Execute query - -------------------------------------------- */ + + /** + * Execute query + **/ private function exec_query() { $klimit = array_search("limit", $this->parse_query_lower); $koffset = array_search("offset", $this->parse_query_lower); - + if ($klimit !== FALSE) $limit = (int) $this->parse_query[$klimit+1]; - + if ($koffset !== FALSE) $offset = (int) $this->parse_query[$koffset+1]; - + $irow = 0; $distinct = array(); - + foreach ($this->tables as $table) { foreach ($table as $row) @@ -268,95 +282,99 @@ private function exec_query() $irow++; continue; } - + if (eval($this->parse_where)) { if ($this->parse_select_as[0] == '*') { foreach (array_keys($row) as $key) $temp[$key] = $row[$key]; - + if ($this->distinct_query && in_array($temp, $distinct)) continue; else $this->response[] = $temp; - + $distinct[] = $response; - } + } else { foreach ($this->parse_select_as as $key => $value) $temp[$key] = $row[$value]; - + if ($this->distinct_query && in_array($temp, $distinct)) continue; else $this->response[] = $temp; - + $distinct[] = $temp; } - + // Limit if ($klimit !== FALSE && count($this->response) == $limit) break; } - + $irow++; } } } - - /* Parse SQL order by parameters - -------------------------------------------- */ + + /** + * Parse SQL order by parameters + **/ private function parse_order() { $key = array_search("order by", $this->parse_query_lower); - + if ($key === FALSE) return; - + $string = $this->parse_query[$key+2]; $arrays = explode(',', $string); - + if (!is_array($arrays)) $arrays[] = $string; - + $arrays = array_map('trim', $arrays); - + $multisort = "array_multisort("; - + foreach ($arrays as $array) { list($col, $sort) = preg_split('#((\s)+)#', $array, -1, PREG_SPLIT_NO_EMPTY); $multisort .= "\$this->split_array(\$this->response, '$col'), SORT_".strtoupper($sort).", SORT_STRING, "; } - + $multisort .= "\$this->response);"; eval($multisort); } - - /* Return response - -------------------------------------------- */ + + /** + * Return response + **/ private function return_response() { return $this->response; } - - /* Return a column of an array - -------------------------------------------- */ + + /** + * Return a column of an array + **/ private function split_array($input_array, $column) - { + { $output_array = array(); - + foreach ($input_array as $key => $value) $output_array[] = $value[$column]; return $output_array; } - /* Entire array search - -------------------------------------------- */ + /** + * Entire array search + **/ private function entire_array_search($needle, $array) { foreach($array as $key => $value) From 5b4255e814b7077d4d853e122bfe6e100e4b856c Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:25:55 +0800 Subject: [PATCH 02/76] . --- sql4array.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index f36c367..89c7eef 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -1,6 +1,6 @@ @@ -10,7 +10,7 @@ * License: LGPL */ -/* +/** * Parameters available : * SELECT, DISTINCT, FROM, WHERE, ORDER BY, LIMIT, OFFSET * @@ -368,10 +368,10 @@ private function split_array($input_array, $column) foreach ($input_array as $key => $value) $output_array[] = $value[$column]; - + return $output_array; } - + /** * Entire array search **/ @@ -380,10 +380,10 @@ private function entire_array_search($needle, $array) foreach($array as $key => $value) if ($value === $needle) $return[] = $key; - + if (!is_array($return)) $return = FALSE; - + return $return; } } From 892c678742b52dee648ce7a979ad52999bb81435 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:26:36 +0800 Subject: [PATCH 03/76] . --- sql4array.class.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 89c7eef..e914bfb 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -25,7 +25,7 @@ class sql4array { /** * Init - **/ + */ private $query = FALSE; private $parse_query = FALSE; private $parse_query_lower = FALSE; @@ -40,7 +40,7 @@ class sql4array /** * Query function - **/ + */ public function query($query) { $this->destroy(); @@ -59,7 +59,7 @@ public function query($query) /** * Destroy current values - **/ + */ private function destroy() { $this->query = FALSE; @@ -77,7 +77,7 @@ private function destroy() /** * Parse SQL query - **/ + */ private function parse_query() { $this->parse_query = preg_replace('#ORDER(\s){2,}BY(\s+)(.*)(\s+)(ASC|DESC)#i', 'ORDER BY \\3 \\5', $this->query); @@ -88,7 +88,7 @@ private function parse_query() /** * Parse SQL select parameters - **/ + */ private function parse_select() { $key = array_search("distinct", $this->parse_query_lower); @@ -107,7 +107,7 @@ private function parse_select() /** * Parse again SQL select parameters with as keyword - **/ + */ private function parse_select_as() { foreach ($this->parse_select as $select) @@ -126,7 +126,7 @@ private function parse_select_as() /** * Parse SQL from parameters - **/ + */ private function parse_from() { $key = array_search("from", $this->parse_query_lower); @@ -139,7 +139,7 @@ private function parse_from() /** * Parse again SQL from parameters with as keyword - **/ + */ private function parse_from_as() { foreach ($this->parse_from as $from) @@ -165,7 +165,7 @@ private function parse_from_as() /** * Parse SQL where parameters - **/ + */ private function parse_where() { $key = array_search("where", $this->parse_query_lower); @@ -181,7 +181,7 @@ private function parse_where() /** * SQL Functions - **/ + */ $patterns[] = '#LOWER\((.*)\)#ie'; $patterns[] = '#UPPER\((.*)\)#ie'; $patterns[] = '#TRIM\((.*)\)#ie'; @@ -192,7 +192,7 @@ private function parse_where() /** * Basics SQL operators - **/ + */ $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie'; $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie'; @@ -213,7 +213,7 @@ private function parse_where() /** * match SQL operators - **/ + */ $ereg = array('%' => '(.*)', '_' => '(.)'); $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; @@ -246,7 +246,7 @@ private function parse_where_key($key) /** * Format IN parameters for PHP - **/ + */ private function parse_in($string) { $array = explode(',', $string); @@ -257,7 +257,7 @@ private function parse_in($string) /** * Execute query - **/ + */ private function exec_query() { $klimit = array_search("limit", $this->parse_query_lower); @@ -322,7 +322,7 @@ private function exec_query() /** * Parse SQL order by parameters - **/ + */ private function parse_order() { $key = array_search("order by", $this->parse_query_lower); @@ -353,7 +353,7 @@ private function parse_order() /** * Return response - **/ + */ private function return_response() { return $this->response; @@ -361,7 +361,7 @@ private function return_response() /** * Return a column of an array - **/ + */ private function split_array($input_array, $column) { $output_array = array(); @@ -374,7 +374,7 @@ private function split_array($input_array, $column) /** * Entire array search - **/ + */ private function entire_array_search($needle, $array) { foreach($array as $key => $value) From 0d9cd76e93d11552e5f69394ccda5931482520f2 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:27:42 +0800 Subject: [PATCH 04/76] change code format --- sql4array.class.php | 230 ++++++++++++++++++++------------------------ 1 file changed, 105 insertions(+), 125 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index e914bfb..932a257 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -26,17 +26,17 @@ class sql4array /** * Init */ - private $query = FALSE; - private $parse_query = FALSE; - private $parse_query_lower = FALSE; - private $parse_select = FALSE; - private $parse_select_as = FALSE; - private $parse_from = FALSE; - private $parse_from_as = FALSE; - private $parse_where = FALSE; - private $distinct_query = FALSE; - private $tables = array(); - private $response = array(); + private $query = FALSE; + private $parse_query = FALSE; + private $parse_query_lower = FALSE; + private $parse_select = FALSE; + private $parse_select_as = FALSE; + private $parse_from = FALSE; + private $parse_from_as = FALSE; + private $parse_where = FALSE; + private $distinct_query = FALSE; + private $tables = array(); + private $response = array(); /** * Query function @@ -62,17 +62,17 @@ public function query($query) */ private function destroy() { - $this->query = FALSE; - $this->parse_query = FALSE; - $this->parse_query_lower = FALSE; - $this->parse_select = FALSE; - $this->parse_select_as = FALSE; - $this->parse_from = FALSE; - $this->parse_from_as = FALSE; - $this->parse_where = FALSE; - $this->distinct_query = FALSE; - $this->tables = array(); - $this->response = array(); + $this->query = FALSE; + $this->parse_query = FALSE; + $this->parse_query_lower = FALSE; + $this->parse_select = FALSE; + $this->parse_select_as = FALSE; + $this->parse_from = FALSE; + $this->parse_from_as = FALSE; + $this->parse_where = FALSE; + $this->distinct_query = FALSE; + $this->tables = array(); + $this->response = array(); } /** @@ -80,10 +80,10 @@ private function destroy() */ private function parse_query() { - $this->parse_query = preg_replace('#ORDER(\s){2,}BY(\s+)(.*)(\s+)(ASC|DESC)#i', 'ORDER BY \\3 \\5', $this->query); - $this->parse_query = preg_split('#(SELECT|DISTINCT|FROM|JOIN|WHERE|ORDER(\s+)BY|LIMIT|OFFSET)+#i', $this->parse_query, -1, PREG_SPLIT_DELIM_CAPTURE); - $this->parse_query = array_map('trim', $this->parse_query); - $this->parse_query_lower = array_map('strtolower', $this->parse_query); + $this->parse_query = preg_replace('#ORDER(\s){2,}BY(\s+)(.*)(\s+)(ASC|DESC)#i', 'ORDER BY \\3 \\5', $this->query); + $this->parse_query = preg_split('#(SELECT|DISTINCT|FROM|JOIN|WHERE|ORDER(\s+)BY|LIMIT|OFFSET)+#i', $this->parse_query, -1, PREG_SPLIT_DELIM_CAPTURE); + $this->parse_query = array_map('trim', $this->parse_query); + $this->parse_query_lower = array_map('strtolower', $this->parse_query); } /** @@ -93,16 +93,13 @@ private function parse_select() { $key = array_search("distinct", $this->parse_query_lower); - if ($key === FALSE) - $key = array_search("select", $this->parse_query_lower); - else - $this->distinct_query = TRUE; + if ($key === FALSE) $key = array_search("select", $this->parse_query_lower); + else $this->distinct_query = TRUE; - $string = $this->parse_query[$key+1]; - $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); + $string = $this->parse_query[$key + 1]; + $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); - foreach ($arrays as $array) - $this->parse_select[] = $array; + foreach ($arrays as $array) $this->parse_select[] = $array; } /** @@ -114,7 +111,7 @@ private function parse_select_as() { if (eregi('as', $select)) { - $arrays = preg_split('#((\s)+AS(\s)+)#i', $select, -1, PREG_SPLIT_NO_EMPTY); + $arrays = preg_split('#((\s)+AS(\s)+)#i', $select, -1, PREG_SPLIT_NO_EMPTY); $this->parse_select_as[$arrays[1]] = $arrays[0]; } else @@ -129,12 +126,11 @@ private function parse_select_as() */ private function parse_from() { - $key = array_search("from", $this->parse_query_lower); - $string = $this->parse_query[$key+1]; - $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); + $key = array_search("from", $this->parse_query_lower); + $string = $this->parse_query[$key + 1]; + $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); - foreach ($arrays as $array) - $this->parse_from[] = $array; + foreach ($arrays as $array) $this->parse_from[] = $array; } /** @@ -146,7 +142,7 @@ private function parse_from_as() { if (eregi('AS', $from)) { - $arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY); + $arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY); $table = $arrays[0]; global $$table; @@ -168,65 +164,63 @@ private function parse_from_as() */ private function parse_where() { - $key = array_search("where", $this->parse_query_lower); + $key = array_search("where", $this->parse_query_lower); - if ($key == FALSE) - return $this->parse_where = "return TRUE;"; + if ($key == FALSE) return $this->parse_where = "return TRUE;"; - $string = $this->parse_query[$key+1]; + $string = $this->parse_query[$key + 1]; - if (trim($string) == '') - return $this->parse_where = "return TRUE;"; + if (trim($string) == '') return $this->parse_where = "return TRUE;"; /** * SQL Functions */ - $patterns[] = '#LOWER\((.*)\)#ie'; - $patterns[] = '#UPPER\((.*)\)#ie'; - $patterns[] = '#TRIM\((.*)\)#ie'; + $patterns[] = '#LOWER\((.*)\)#ie'; + $patterns[] = '#UPPER\((.*)\)#ie'; + $patterns[] = '#TRIM\((.*)\)#ie'; - $replacements[] = "'strtolower(\\1)'"; - $replacements[] = "'strtoupper(\\1)'"; - $replacements[] = "'trim(\\1)'"; + $replacements[] = "'strtolower(\\1)'"; + $replacements[] = "'strtoupper(\\1)'"; + $replacements[] = "'trim(\\1)'"; /** * Basics SQL operators */ - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<=|>=)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(NOT IN)(\s)+\((.*)\)#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(IN)(\s)+\((.*)\)#ie'; - - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \"\\10\" '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \"\\10\" '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<=|>=)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(NOT IN)(\s)+\((.*)\)#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(IN)(\s)+\((.*)\)#ie'; + + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \"\\10\" '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \"\\10\" '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; /** * match SQL operators */ $ereg = array('%' => '(.*)', '_' => '(.)'); - $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; - $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; - $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; - $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; + $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; + $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; + $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; + $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; - $replacements[] = "'ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; - $replacements[] = "'eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; - $replacements[] = "'!ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; - $replacements[] = "'!eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; + $replacements[] = "'ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; + $replacements[] = "'eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; + $replacements[] = "'!ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; + $replacements[] = "'!eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; - $this->parse_where = "return ".stripslashes(trim(preg_replace($patterns, $replacements, $string))).";"; + $this->parse_where = "return " . stripslashes(trim(preg_replace($patterns, $replacements, $string))) . ";"; } /* @@ -236,11 +230,11 @@ private function parse_where_key($key) if (ereg('\.', $key)) { list($table, $col) = explode('.', $key); - return '$row[$this->parse_select_as['.$col.']]'; + return '$row[$this->parse_select_as[' . $col . ']]'; } else { - return '$row[$this->parse_select_as['.$key.']]'; + return '$row[$this->parse_select_as[' . $key . ']]'; } } @@ -249,8 +243,8 @@ private function parse_where_key($key) */ private function parse_in($string) { - $array = explode(',', $string); - $array = array_map('trim', $array); + $array = explode(',', $string); + $array = array_map('trim', $array); return implode(' || ', $array); } @@ -260,17 +254,15 @@ private function parse_in($string) */ private function exec_query() { - $klimit = array_search("limit", $this->parse_query_lower); - $koffset = array_search("offset", $this->parse_query_lower); + $klimit = array_search("limit", $this->parse_query_lower); + $koffset = array_search("offset", $this->parse_query_lower); - if ($klimit !== FALSE) - $limit = (int) $this->parse_query[$klimit+1]; + if ($klimit !== FALSE) $limit = (int)$this->parse_query[$klimit + 1]; - if ($koffset !== FALSE) - $offset = (int) $this->parse_query[$koffset+1]; + if ($koffset !== FALSE) $offset = (int)$this->parse_query[$koffset + 1]; - $irow = 0; - $distinct = array(); + $irow = 0; + $distinct = array(); foreach ($this->tables as $table) { @@ -287,32 +279,25 @@ private function exec_query() { if ($this->parse_select_as[0] == '*') { - foreach (array_keys($row) as $key) - $temp[$key] = $row[$key]; + foreach (array_keys($row) as $key) $temp[$key] = $row[$key]; - if ($this->distinct_query && in_array($temp, $distinct)) - continue; - else - $this->response[] = $temp; + if ($this->distinct_query && in_array($temp, $distinct)) continue; + else $this->response[] = $temp; $distinct[] = $response; } else { - foreach ($this->parse_select_as as $key => $value) - $temp[$key] = $row[$value]; + foreach ($this->parse_select_as as $key => $value) $temp[$key] = $row[$value]; - if ($this->distinct_query && in_array($temp, $distinct)) - continue; - else - $this->response[] = $temp; + if ($this->distinct_query && in_array($temp, $distinct)) continue; + else $this->response[] = $temp; $distinct[] = $temp; } // Limit - if ($klimit !== FALSE && count($this->response) == $limit) - break; + if ($klimit !== FALSE && count($this->response) == $limit) break; } $irow++; @@ -325,28 +310,26 @@ private function exec_query() */ private function parse_order() { - $key = array_search("order by", $this->parse_query_lower); + $key = array_search("order by", $this->parse_query_lower); - if ($key === FALSE) - return; + if ($key === FALSE) return; - $string = $this->parse_query[$key+2]; - $arrays = explode(',', $string); + $string = $this->parse_query[$key + 2]; + $arrays = explode(',', $string); - if (!is_array($arrays)) - $arrays[] = $string; + if (!is_array($arrays)) $arrays[] = $string; - $arrays = array_map('trim', $arrays); + $arrays = array_map('trim', $arrays); - $multisort = "array_multisort("; + $multisort = "array_multisort("; foreach ($arrays as $array) { - list($col, $sort) = preg_split('#((\s)+)#', $array, -1, PREG_SPLIT_NO_EMPTY); - $multisort .= "\$this->split_array(\$this->response, '$col'), SORT_".strtoupper($sort).", SORT_STRING, "; + list($col, $sort) = preg_split('#((\s)+)#', $array, -1, PREG_SPLIT_NO_EMPTY); + $multisort .= "\$this->split_array(\$this->response, '$col'), SORT_" . strtoupper($sort) . ", SORT_STRING, "; } - $multisort .= "\$this->response);"; + $multisort .= "\$this->response);"; eval($multisort); } @@ -364,10 +347,9 @@ private function return_response() */ private function split_array($input_array, $column) { - $output_array = array(); + $output_array = array(); - foreach ($input_array as $key => $value) - $output_array[] = $value[$column]; + foreach ($input_array as $key => $value) $output_array[] = $value[$column]; return $output_array; } @@ -377,12 +359,10 @@ private function split_array($input_array, $column) */ private function entire_array_search($needle, $array) { - foreach($array as $key => $value) - if ($value === $needle) - $return[] = $key; + foreach ($array as $key => $value) + if ($value === $needle) $return[] = $key; - if (!is_array($return)) - $return = FALSE; + if (!is_array($return)) $return = FALSE; return $return; } From 52637b99cf722182da7c10a2652659af9d49c6d1 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:37:55 +0800 Subject: [PATCH 05/76] public function createFromGlobals($disable = false) --- sql4array.class.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 932a257..058c719 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -38,6 +38,18 @@ class sql4array private $tables = array(); private $response = array(); + /** + * sql4array setting + */ + protected $var = array(); + + public function createFromGlobals($disable = false) + { + $this->attr['createFromGlobals'] = !$disable; + + return $this; + } + /** * Query function */ From c1ac586c5740905608df512a3a75358ce594ad2a Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:41:28 +0800 Subject: [PATCH 06/76] function table() --- sql4array.class.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 058c719..1a8fc81 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -50,6 +50,18 @@ public function createFromGlobals($disable = false) return $this; } + function table() + { + if ($this->attr['createFromGlobals']) + { + + } + else + { + + } + } + /** * Query function */ From 4d3722aa56d0e6f641c9f897ba30093a921e447b Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:42:20 +0800 Subject: [PATCH 07/76] sql4array temp --- sql4array.class.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 1a8fc81..35cbaef 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -43,6 +43,11 @@ class sql4array */ protected $var = array(); + /** + * sql4array temp + */ + protected $temp = array(); + public function createFromGlobals($disable = false) { $this->attr['createFromGlobals'] = !$disable; From e30c85ce48fdf88ce82e935c72f4f8cf53d179ff Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:44:46 +0800 Subject: [PATCH 08/76] $this->temp['table'] = func_get_arg(0); --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 35cbaef..fdfdfa6 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -57,6 +57,8 @@ public function createFromGlobals($disable = false) function table() { + $this->temp['table'] = func_get_arg(0); + if ($this->attr['createFromGlobals']) { From 0b8292fe289475598c60c9e26715b6cb4777305a Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:48:04 +0800 Subject: [PATCH 09/76] global ${$this->temp['table']}; --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index fdfdfa6..6a915f4 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -61,7 +61,9 @@ function table() if ($this->attr['createFromGlobals']) { + global ${$this->temp['table']}; + return ${$this->temp['table']}; } else { From 855afeaf2351f88a19baa5f57ea8d3c8705fa09f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:48:55 +0800 Subject: [PATCH 10/76] $this->temp = array(); --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 6a915f4..3f35d9e 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -106,6 +106,8 @@ private function destroy() $this->distinct_query = FALSE; $this->tables = array(); $this->response = array(); + + $this->temp = array(); } /** From 4d3450ef509cec12f25094bc4e0808ab8a543346 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:52:00 +0800 Subject: [PATCH 11/76] return $GLOBALS[$this->temp['table']]; --- sql4array.class.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 3f35d9e..d07d8fc 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -61,9 +61,7 @@ function table() if ($this->attr['createFromGlobals']) { - global ${$this->temp['table']}; - - return ${$this->temp['table']}; + return $GLOBALS[$this->temp['table']]; } else { From 6f1a6671f3bc714190c9f355639023fc64552338 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:52:25 +0800 Subject: [PATCH 12/76] return (array)$GLOBALS[$this->temp['table']]; --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index d07d8fc..ba9ca7c 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -61,7 +61,7 @@ function table() if ($this->attr['createFromGlobals']) { - return $GLOBALS[$this->temp['table']]; + return (array)$GLOBALS[$this->temp['table']]; } else { From 5c35c8f13e279f128afdee9db2405c9f7352daf3 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:52:48 +0800 Subject: [PATCH 13/76] . --- sql4array.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index ba9ca7c..fb6ad74 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -55,6 +55,9 @@ public function createFromGlobals($disable = false) return $this; } + /** + * @return array + */ function table() { $this->temp['table'] = func_get_arg(0); From 29a278188a27a697df4ac446382e804d401ef5f5 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:53:41 +0800 Subject: [PATCH 14/76] return (array)$this->globals[$this->temp['table']]; --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index fb6ad74..df88f11 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -68,7 +68,7 @@ function table() } else { - + return (array)$this->globals[$this->temp['table']]; } } From 7620f72ac7865b7fca356f8db11ef78736d6b23f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 00:54:46 +0800 Subject: [PATCH 15/76] sql4array tables map --- sql4array.class.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index df88f11..9d7f126 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -48,6 +48,11 @@ class sql4array */ protected $temp = array(); + /** + * sql4array tables map + */ + protected $globals = array(); + public function createFromGlobals($disable = false) { $this->attr['createFromGlobals'] = !$disable; From 6cb21f987aba6153750b10dde4b223dacdc683a1 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:04:19 +0800 Subject: [PATCH 16/76] $this->tables[$arrays[1]] = $this->table($table); --- sql4array.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 9d7f126..c0960c6 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -188,14 +188,14 @@ private function parse_from_as() $table = $arrays[0]; global $$table; $this->parse_from_as[$arrays[1]] = $table; - $this->tables[$arrays[1]] = $$table; + $this->tables[$arrays[1]] = $this->table($table); } else { $table = $from; global $$table; $this->parse_from_as[$from] = $table; - $this->tables[$arrays[1]] = $$table; + $this->tables[$arrays[1]] = $this->table($table); } } } From e6129a334bbd6d9235a9f719fb0335ca6e9d1076 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:04:56 +0800 Subject: [PATCH 17/76] . --- sql4array.class.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index c0960c6..f663f97 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -186,14 +186,18 @@ private function parse_from_as() $arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY); $table = $arrays[0]; + /* global $$table; + */ $this->parse_from_as[$arrays[1]] = $table; $this->tables[$arrays[1]] = $this->table($table); } else { $table = $from; + /* global $$table; + */ $this->parse_from_as[$from] = $table; $this->tables[$arrays[1]] = $this->table($table); } From eed32eb1661433d9da3f6c840de25fa0bc13bcdd Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:09:23 +0800 Subject: [PATCH 18/76] $sql->createFromGlobals(); --- sql4array.example.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql4array.example.php b/sql4array.example.php index b6013d8..6d6ea5b 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -24,6 +24,9 @@ $sql = new sql4array(); + +$sql->createFromGlobals(); + $a = $sql->query("SELECT id, foo FROM array"); $b = $sql->query("SELECT id, foo FROM array WHERE id > 10"); $c = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10"); From 6ab414972a8df1c86d5192816740637ce3dc556f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:10:01 +0800 Subject: [PATCH 19/76] . --- sql4array.example.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/sql4array.example.php b/sql4array.example.php index 6d6ea5b..1ab8a68 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -1,7 +1,6 @@ @@ -18,20 +17,20 @@ for ($i = 0; $i < 20; $i++) { - $array[$i][id] = rand(0, 20); - $array[$i][foo] = md5(rand(0, 10000)); + $array[$i][id] = rand(0, 20); + $array[$i][foo] = md5(rand(0, 10000)); } -$sql = new sql4array(); +$sql = new sql4array(); $sql->createFromGlobals(); -$a = $sql->query("SELECT id, foo FROM array"); -$b = $sql->query("SELECT id, foo FROM array WHERE id > 10"); -$c = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10"); -$d = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10 AND f LIKE '%a%'"); -$e = $sql->query("SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE iiiiii != 10"); +$a = $sql->query("SELECT id, foo FROM array"); +$b = $sql->query("SELECT id, foo FROM array WHERE id > 10"); +$c = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10"); +$d = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10 AND f LIKE '%a%'"); +$e = $sql->query("SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE iiiiii != 10"); var_dump($a); var_dump($b); From 0bdc0aeac0fa41834b9a02337d5d385b69fdbf55 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:16:48 +0800 Subject: [PATCH 20/76] $this->globals[$key] = (array)$value; --- sql4array.class.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index f663f97..f7cc300 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -77,6 +77,10 @@ function table() } } + function asset($key, $value) { + $this->globals[$key] = (array)$value; + } + /** * Query function */ From a68f260286c9411baff6ea4d95511b761d562927 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:25:44 +0800 Subject: [PATCH 21/76] reset $this->globals --- sql4array.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index f7cc300..512a0bb 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -57,6 +57,9 @@ public function createFromGlobals($disable = false) { $this->attr['createFromGlobals'] = !$disable; + // reset $this->globals + $this->globals = array(); + return $this; } From 5a22981442f33557555adc687a0efb0441507f46 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:29:22 +0800 Subject: [PATCH 22/76] desc --- sql4array.class.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 512a0bb..0ba894d 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -53,6 +53,12 @@ class sql4array */ protected $globals = array(); + /** + * set tables get from where + * and this func will reset $this->globals + * + * @return sql4array + */ public function createFromGlobals($disable = false) { $this->attr['createFromGlobals'] = !$disable; From 3da5d99a027181b08d1f6c90fad1e4bb3ad3f039 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:31:29 +0800 Subject: [PATCH 23/76] $array = array(); --- sql4array.example.php | 1 + 1 file changed, 1 insertion(+) diff --git a/sql4array.example.php b/sql4array.example.php index 1ab8a68..7018091 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -14,6 +14,7 @@ require "./sql4array.class.php"; +$array = array(); for ($i = 0; $i < 20; $i++) { From a5c5c394ad4bd7ce0186bcf38cdad8ceff3714ab Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:33:43 +0800 Subject: [PATCH 24/76] private to protected --- sql4array.class.php | 50 ++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 0ba894d..5cc64a3 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -26,17 +26,17 @@ class sql4array /** * Init */ - private $query = FALSE; - private $parse_query = FALSE; - private $parse_query_lower = FALSE; - private $parse_select = FALSE; - private $parse_select_as = FALSE; - private $parse_from = FALSE; - private $parse_from_as = FALSE; - private $parse_where = FALSE; - private $distinct_query = FALSE; - private $tables = array(); - private $response = array(); + protected $query = FALSE; + protected $parse_query = FALSE; + protected $parse_query_lower = FALSE; + protected $parse_select = FALSE; + protected $parse_select_as = FALSE; + protected $parse_from = FALSE; + protected $parse_from_as = FALSE; + protected $parse_where = FALSE; + protected $distinct_query = FALSE; + protected $tables = array(); + protected $response = array(); /** * sql4array setting @@ -112,7 +112,7 @@ public function query($query) /** * Destroy current values */ - private function destroy() + protected function destroy() { $this->query = FALSE; $this->parse_query = FALSE; @@ -132,7 +132,7 @@ private function destroy() /** * Parse SQL query */ - private function parse_query() + protected function parse_query() { $this->parse_query = preg_replace('#ORDER(\s){2,}BY(\s+)(.*)(\s+)(ASC|DESC)#i', 'ORDER BY \\3 \\5', $this->query); $this->parse_query = preg_split('#(SELECT|DISTINCT|FROM|JOIN|WHERE|ORDER(\s+)BY|LIMIT|OFFSET)+#i', $this->parse_query, -1, PREG_SPLIT_DELIM_CAPTURE); @@ -143,7 +143,7 @@ private function parse_query() /** * Parse SQL select parameters */ - private function parse_select() + protected function parse_select() { $key = array_search("distinct", $this->parse_query_lower); @@ -159,7 +159,7 @@ private function parse_select() /** * Parse again SQL select parameters with as keyword */ - private function parse_select_as() + protected function parse_select_as() { foreach ($this->parse_select as $select) { @@ -178,7 +178,7 @@ private function parse_select_as() /** * Parse SQL from parameters */ - private function parse_from() + protected function parse_from() { $key = array_search("from", $this->parse_query_lower); $string = $this->parse_query[$key + 1]; @@ -190,7 +190,7 @@ private function parse_from() /** * Parse again SQL from parameters with as keyword */ - private function parse_from_as() + protected function parse_from_as() { foreach ($this->parse_from as $from) { @@ -220,7 +220,7 @@ private function parse_from_as() /** * Parse SQL where parameters */ - private function parse_where() + protected function parse_where() { $key = array_search("where", $this->parse_query_lower); @@ -283,7 +283,7 @@ private function parse_where() /* -------------------------------------------- */ - private function parse_where_key($key) + protected function parse_where_key($key) { if (ereg('\.', $key)) { @@ -299,7 +299,7 @@ private function parse_where_key($key) /** * Format IN parameters for PHP */ - private function parse_in($string) + protected function parse_in($string) { $array = explode(',', $string); $array = array_map('trim', $array); @@ -310,7 +310,7 @@ private function parse_in($string) /** * Execute query */ - private function exec_query() + protected function exec_query() { $klimit = array_search("limit", $this->parse_query_lower); $koffset = array_search("offset", $this->parse_query_lower); @@ -366,7 +366,7 @@ private function exec_query() /** * Parse SQL order by parameters */ - private function parse_order() + protected function parse_order() { $key = array_search("order by", $this->parse_query_lower); @@ -395,7 +395,7 @@ private function parse_order() /** * Return response */ - private function return_response() + protected function return_response() { return $this->response; } @@ -403,7 +403,7 @@ private function return_response() /** * Return a column of an array */ - private function split_array($input_array, $column) + protected function split_array($input_array, $column) { $output_array = array(); @@ -415,7 +415,7 @@ private function split_array($input_array, $column) /** * Entire array search */ - private function entire_array_search($needle, $array) + protected function entire_array_search($needle, $array) { foreach ($array as $key => $value) if ($value === $needle) $return[] = $key; From 1afc9ff03518555ffccea1794310b4a614f25be9 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:49:35 +0800 Subject: [PATCH 25/76] $table --- sql4array.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 5cc64a3..5868786 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -72,17 +72,17 @@ public function createFromGlobals($disable = false) /** * @return array */ - function table() + function table($table) { - $this->temp['table'] = func_get_arg(0); + $table = func_get_arg(0); if ($this->attr['createFromGlobals']) { - return (array)$GLOBALS[$this->temp['table']]; + return (array)$GLOBALS[$table]; } else { - return (array)$this->globals[$this->temp['table']]; + return (array)$this->globals[$table]; } } From b938686977325ca66ba1b528def926707a8711eb Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 01:49:47 +0800 Subject: [PATCH 26/76] . --- sql4array.class.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 5868786..0137375 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -74,8 +74,6 @@ public function createFromGlobals($disable = false) */ function table($table) { - $table = func_get_arg(0); - if ($this->attr['createFromGlobals']) { return (array)$GLOBALS[$table]; From a2e53ef3f0382aa9c2668a74554d05f6b84f98e0 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 02:04:06 +0800 Subject: [PATCH 27/76] @return sql4array --- sql4array.class.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 0137375..fc22ffd 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -84,8 +84,15 @@ function table($table) } } + /** + * set tables map + * + * @return sql4array + */ function asset($key, $value) { $this->globals[$key] = (array)$value; + + return $this; } /** From 427a420fa2338cf02b23c39c34f662cc39ed5658 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 02:06:20 +0800 Subject: [PATCH 28/76] return $this; --- sql4array.class.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index fc22ffd..bc4eeef 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -132,6 +132,8 @@ protected function destroy() $this->response = array(); $this->temp = array(); + + return $this; } /** @@ -143,6 +145,8 @@ protected function parse_query() $this->parse_query = preg_split('#(SELECT|DISTINCT|FROM|JOIN|WHERE|ORDER(\s+)BY|LIMIT|OFFSET)+#i', $this->parse_query, -1, PREG_SPLIT_DELIM_CAPTURE); $this->parse_query = array_map('trim', $this->parse_query); $this->parse_query_lower = array_map('strtolower', $this->parse_query); + + return $this; } /** @@ -159,6 +163,8 @@ protected function parse_select() $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); foreach ($arrays as $array) $this->parse_select[] = $array; + + return $this; } /** @@ -178,6 +184,8 @@ protected function parse_select_as() $this->parse_select_as[$select] = $select; } } + + return $this; } /** @@ -190,6 +198,8 @@ protected function parse_from() $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); foreach ($arrays as $array) $this->parse_from[] = $array; + + return $this; } /** @@ -220,6 +230,8 @@ protected function parse_from_as() $this->tables[$arrays[1]] = $this->table($table); } } + + return $this; } /** @@ -284,6 +296,8 @@ protected function parse_where() $replacements[] = "'!eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; $this->parse_where = "return " . stripslashes(trim(preg_replace($patterns, $replacements, $string))) . ";"; + + return $this; } /* @@ -366,6 +380,8 @@ protected function exec_query() $irow++; } } + + return $this; } /** @@ -395,6 +411,8 @@ protected function parse_order() $multisort .= "\$this->response);"; eval($multisort); + + return $this; } /** From 7da4cf808c0e2f0941f258c5ec51ecb2471bf7f7 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 02:08:43 +0800 Subject: [PATCH 29/76] . --- sql4array.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/sql4array.class.php b/sql4array.class.php index bc4eeef..bd9db41 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -102,6 +102,7 @@ public function query($query) { $this->destroy(); $this->query = $query; + $this->parse_query(); $this->parse_select(); $this->parse_select_as(); From 2e12eb232535c54ce6beb592080f65ae7f85d4e1 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 02:10:47 +0800 Subject: [PATCH 30/76] . --- sql4array.class.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index bd9db41..dff2941 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -103,14 +103,16 @@ public function query($query) $this->destroy(); $this->query = $query; - $this->parse_query(); - $this->parse_select(); - $this->parse_select_as(); - $this->parse_from(); - $this->parse_from_as(); - $this->parse_where(); - $this->exec_query(); - $this->parse_order(); + $this + ->parse_query() + ->parse_select() + ->parse_select_as() + ->parse_from() + ->parse_from_as() + ->parse_where() + ->exec_query() + ->parse_order() + ; return $this->return_response(); } From 51cbd79a7c67985bcac765aad0e05408c1650c0f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 02:12:05 +0800 Subject: [PATCH 31/76] . --- sql4array.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index dff2941..f17ae6f 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -76,11 +76,11 @@ function table($table) { if ($this->attr['createFromGlobals']) { - return (array)$GLOBALS[$table]; + return $GLOBALS[$table]; } else { - return (array)$this->globals[$table]; + return $this->globals[$table]; } } @@ -90,7 +90,7 @@ function table($table) * @return sql4array */ function asset($key, $value) { - $this->globals[$key] = (array)$value; + $this->globals[$key] = $value; return $this; } From df56cbbd654e56a2e4f9288078cb47d5aa76e8fb Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:21:27 +0800 Subject: [PATCH 32/76] bugfix --- sql4array.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index f17ae6f..bae47a3 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -110,6 +110,8 @@ public function query($query) ->parse_from() ->parse_from_as() ->parse_where() + ; + $this ->exec_query() ->parse_order() ; @@ -230,7 +232,7 @@ protected function parse_from_as() global $$table; */ $this->parse_from_as[$from] = $table; - $this->tables[$arrays[1]] = $this->table($table); + $this->tables[$from] = $this->table($table); } } From 4519fcedd06d942691d93bae5fb4c3fa259dd2cb Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:21:44 +0800 Subject: [PATCH 33/76] . --- sql4array.example.php | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/sql4array.example.php b/sql4array.example.php index 7018091..7ae679c 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -10,7 +10,7 @@ * License: LGPL */ -header("Content-type: text"); +//header("Content-type: text"); require "./sql4array.class.php"; @@ -27,16 +27,33 @@ $sql->createFromGlobals(); -$a = $sql->query("SELECT id, foo FROM array"); -$b = $sql->query("SELECT id, foo FROM array WHERE id > 10"); -$c = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10"); -$d = $sql->query("SELECT id AS i, foo AS f FROM array WHERE i > 10 AND f LIKE '%a%'"); -$e = $sql->query("SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE iiiiii != 10"); - -var_dump($a); -var_dump($b); -var_dump($c); -var_dump($d); -var_dump($e); +$r = array(); + +foreach(array( + "SELECT id, foo FROM array", + "SELECT id, foo FROM array WHERE id > 10", + "SELECT id, foo FROM array as arr WHERE id > 10", + "SELECT id AS i, foo AS f FROM array WHERE i > 10", + "SELECT id AS i, foo AS f FROM array WHERE i > 10 AND f LIKE '%a%'", + "SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE iiiiii != 10", +) as $sqlstring) +{ + + $a = $sql->query($sqlstring); + + $r[] = array( + 'sql' => $sqlstring, + 'result' => $a, + ); + +} + +var_dump($sql); + +foreach($r as $value) +{ + echo $value['sql']."\n"; + var_dump($value['result']); +} ?> \ No newline at end of file From 0da8afcabd345b871f1103fd207623ab9c5c15bf Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:23:21 +0800 Subject: [PATCH 34/76] $from --- sql4array.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index bae47a3..c54da4d 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -219,11 +219,12 @@ protected function parse_from_as() $arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY); $table = $arrays[0]; + $from = $arrays[1]; /* global $$table; */ - $this->parse_from_as[$arrays[1]] = $table; - $this->tables[$arrays[1]] = $this->table($table); + $this->parse_from_as[$from] = $table; + $this->tables[$from] = $this->table($table); } else { From 5718cc4c73cf05b52ac9caf3284a0033d1d51549 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:24:07 +0800 Subject: [PATCH 35/76] . --- sql4array.class.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index c54da4d..f7fbc78 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -220,21 +220,14 @@ protected function parse_from_as() $table = $arrays[0]; $from = $arrays[1]; - /* - global $$table; - */ - $this->parse_from_as[$from] = $table; - $this->tables[$from] = $this->table($table); } else { $table = $from; - /* - global $$table; - */ - $this->parse_from_as[$from] = $table; - $this->tables[$from] = $this->table($table); } + + $this->parse_from_as[$from] = $table; + $this->tables[$from] = $this->table($table); } return $this; From c74fa7ba95c5c14a343e7e0431958dc5d5b8eb20 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:32:08 +0800 Subject: [PATCH 36/76] =?UTF-8?q?=E6=94=B9=E9=80=B2=20table=20map=20?= =?UTF-8?q?=E5=AD=98=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql4array.class.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index f7fbc78..a3fa667 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -340,8 +340,11 @@ protected function exec_query() $irow = 0; $distinct = array(); - foreach ($this->tables as $table) + foreach ($this->parse_from_as as $table_name) { + + $table = $this->table($table_name); + foreach ($table as $row) { // Offset From 445a465a8e1984de18c2eaa5821e0a8879016646 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:33:12 +0800 Subject: [PATCH 37/76] =?UTF-8?q?=E5=B0=87=E4=B8=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A2=BC=E8=A8=BB=E8=A7=A3=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index a3fa667..be286bf 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -227,7 +227,9 @@ protected function parse_from_as() } $this->parse_from_as[$from] = $table; + /* $this->tables[$from] = $this->table($table); + */ } return $this; From 16cd45831cf4ec91efe0e379a3f812273ac8ed6d Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:34:47 +0800 Subject: [PATCH 38/76] $table = $this->tables[$table_name] = $this->table($table_name); --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index be286bf..b15729f 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -345,7 +345,7 @@ protected function exec_query() foreach ($this->parse_from_as as $table_name) { - $table = $this->table($table_name); + $table = $this->tables[$table_name] = $this->table($table_name); foreach ($table as $row) { From b17dfc4df317944307350964795afd91eddec767 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:36:46 +0800 Subject: [PATCH 39/76] foreach ($this->parse_from_as as $from_name => $table_name) --- sql4array.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index b15729f..3f3c3e8 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -342,10 +342,10 @@ protected function exec_query() $irow = 0; $distinct = array(); - foreach ($this->parse_from_as as $table_name) + foreach ($this->parse_from_as as $from_name => $table_name) { - $table = $this->tables[$table_name] = $this->table($table_name); + $table = $this->tables[$from_name] = $this->table($table_name); foreach ($table as $row) { From 522a3a7e1bea0aa7b0d550ee7b82f985b6e55701 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 03:38:00 +0800 Subject: [PATCH 40/76] foreach ($this->tables[$from_name] as $row) --- sql4array.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 3f3c3e8..db34e48 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -345,9 +345,9 @@ protected function exec_query() foreach ($this->parse_from_as as $from_name => $table_name) { - $table = $this->tables[$from_name] = $this->table($table_name); + $this->tables[$from_name] = $this->table($table_name); - foreach ($table as $row) + foreach ($this->tables[$from_name] as $row) { // Offset if ($koffset !== FALSE && $irow < $offset) From 44dcb13f8327116c69d0b82c26c6a6914a29a8f6 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:42:01 +0800 Subject: [PATCH 41/76] $patterns = self::$cache_patterns; $replacements = self::$cache_replacements; --- sql4array.class.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index db34e48..408063a 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -38,6 +38,9 @@ class sql4array protected $tables = array(); protected $response = array(); + protected static $cache_patterns = array(); + protected static $cache_replacements = array(); + /** * sql4array setting */ @@ -248,6 +251,13 @@ protected function parse_where() if (trim($string) == '') return $this->parse_where = "return TRUE;"; + if (self::$cache_patterns && self::$cache_replacements) + { + $patterns = self::$cache_patterns; + $replacements = self::$cache_replacements; + } + else + { /** * SQL Functions @@ -281,6 +291,11 @@ protected function parse_where() $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; + self::$patterns = $cache_patterns; + self::$replacements = $cache_replacements; + + } + /** * match SQL operators */ From eda2a1f8bb66e0d39c4d2c877a63ca508a92ad60 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:42:17 +0800 Subject: [PATCH 42/76] . --- sql4array.class.php | 62 ++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 408063a..50bb5a1 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -259,37 +259,37 @@ protected function parse_where() else { - /** - * SQL Functions - */ - $patterns[] = '#LOWER\((.*)\)#ie'; - $patterns[] = '#UPPER\((.*)\)#ie'; - $patterns[] = '#TRIM\((.*)\)#ie'; - - $replacements[] = "'strtolower(\\1)'"; - $replacements[] = "'strtoupper(\\1)'"; - $replacements[] = "'trim(\\1)'"; - - /** - * Basics SQL operators - */ - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<=|>=)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+([[:digit:]]+)(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(NOT IN)(\s)+\((.*)\)#ie'; - $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(IN)(\s)+\((.*)\)#ie'; - - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \"\\10\" '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \\9 '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \"\\10\" '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; - $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; + /** + * SQL Functions + */ + $patterns[] = '#LOWER\((.*)\)#ie'; + $patterns[] = '#UPPER\((.*)\)#ie'; + $patterns[] = '#TRIM\((.*)\)#ie'; + + $replacements[] = "'strtolower(\\1)'"; + $replacements[] = "'strtoupper(\\1)'"; + $replacements[] = "'trim(\\1)'"; + + /** + * Basics SQL operators + */ + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<=|>=)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+([[:digit:]]+)(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(NOT IN)(\s)+\((.*)\)#ie'; + $patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(IN)(\s)+\((.*)\)#ie'; + + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \"\\10\" '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \\9 '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \"\\10\" '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; + $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; self::$patterns = $cache_patterns; self::$replacements = $cache_replacements; From 4dc958fbbc5d9d923807851fbf653529fe91de01 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:44:18 +0800 Subject: [PATCH 43/76] [bugfix] Fatal error: Access to undeclared static property: sql4array::$patterns in sql4array.class.php on line 294 --- sql4array.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 50bb5a1..a3c6dc9 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -291,8 +291,8 @@ protected function parse_where() $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '"; $replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '"; - self::$patterns = $cache_patterns; - self::$replacements = $cache_replacements; + self::$cache_patterns = $patterns; + self::$cache_replacements = $replacements; } From b165e016a95e4cedd4f9db5e00e386ed853f8c61 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:49:53 +0800 Subject: [PATCH 44/76] [desc] parse_where_key --- sql4array.class.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index a3c6dc9..e4b4b6e 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -316,8 +316,11 @@ protected function parse_where() return $this; } - /* - -------------------------------------------- */ + /** + * return '$row[$this->parse_select_as[' . $key . ']]'; + * + * @return string + */ protected function parse_where_key($key) { if (ereg('\.', $key)) From d21dec940d500d49511a5b2ad337816028360fd6 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:50:22 +0800 Subject: [PATCH 45/76] @return string --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index e4b4b6e..5ba279b 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -336,6 +336,8 @@ protected function parse_where_key($key) /** * Format IN parameters for PHP + * + * @return string */ protected function parse_in($string) { From 061330a04de35cb7f5e8b65e19e55ba0d2187a9f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:52:12 +0800 Subject: [PATCH 46/76] . --- sql4array.class.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 5ba279b..d78a993 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -326,12 +326,10 @@ protected function parse_where_key($key) if (ereg('\.', $key)) { list($table, $col) = explode('.', $key); - return '$row[$this->parse_select_as[' . $col . ']]'; + + $key = $col; } - else - { return '$row[$this->parse_select_as[' . $key . ']]'; - } } /** From 831ee8910c419ada72eb4432920fd7bfb3e49c43 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:52:24 +0800 Subject: [PATCH 47/76] . --- sql4array.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index d78a993..e8c10f5 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -329,7 +329,8 @@ protected function parse_where_key($key) $key = $col; } - return '$row[$this->parse_select_as[' . $key . ']]'; + + return '$row[$this->parse_select_as[' . $key . ']]'; } /** From 05370f48d6aad1616bc2c3c194de0d3d16d54ac8 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:55:29 +0800 Subject: [PATCH 48/76] . --- sql4array.example.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.example.php b/sql4array.example.php index 7ae679c..ba579e2 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -35,7 +35,7 @@ "SELECT id, foo FROM array as arr WHERE id > 10", "SELECT id AS i, foo AS f FROM array WHERE i > 10", "SELECT id AS i, foo AS f FROM array WHERE i > 10 AND f LIKE '%a%'", - "SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE iiiiii != 10", + "SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE array.iiiiii != 10", ) as $sqlstring) { From b6a15db19ff94588cdb1dac63effc1465976cd48 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:56:27 +0800 Subject: [PATCH 49/76] ereg('\.', $key) => preg_match('#\.#', $key) --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index e8c10f5..807f17a 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -323,7 +323,7 @@ protected function parse_where() */ protected function parse_where_key($key) { - if (ereg('\.', $key)) + if (preg_match('#\.#', $key)) { list($table, $col) = explode('.', $key); From fb50d759551b0cd3b486950e96bf81690153ffad Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 04:59:05 +0800 Subject: [PATCH 50/76] eregi('as', $select) => preg_match('#as#i', $select) --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index 807f17a..ff1af4b 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -182,7 +182,7 @@ protected function parse_select_as() { foreach ($this->parse_select as $select) { - if (eregi('as', $select)) + if (preg_match('#as#i', $select)) { $arrays = preg_split('#((\s)+AS(\s)+)#i', $select, -1, PREG_SPLIT_NO_EMPTY); $this->parse_select_as[$arrays[1]] = $arrays[0]; From d88b95e1a05129984d68f14080cb715f740e8dda Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:03:19 +0800 Subject: [PATCH 51/76] preg_match('#as#i', $from) --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index ff1af4b..6645ef0 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -217,7 +217,7 @@ protected function parse_from_as() { foreach ($this->parse_from as $from) { - if (eregi('AS', $from)) + if (preg_match('#as#i', $from)) { $arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY); From 950a280feed7bee6cc655f6bf03f9eb7a8cf8ef3 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:06:51 +0800 Subject: [PATCH 52/76] TODO: use preg to replace ereg --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 6645ef0..668f971 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -306,6 +306,8 @@ protected function parse_where() $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT LIKE(\s)*(\'|\")(.*)(\'|\")#ie'; $patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT ILIKE(\s)*(\'|\")(.*)(\'|\")#ie'; + // TODO: use preg to replace ereg + $replacements[] = "'ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; $replacements[] = "'eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; $replacements[] = "'!ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; From 1f7fddcebe70f11d9d748faf26f300e097ed5fa2 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:13:11 +0800 Subject: [PATCH 53/76] . --- sql4array.class.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 668f971..4a34b1e 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -162,7 +162,7 @@ protected function parse_query() */ protected function parse_select() { - $key = array_search("distinct", $this->parse_query_lower); + $key = array_search('distinct', $this->parse_query_lower); if ($key === FALSE) $key = array_search("select", $this->parse_query_lower); else $this->distinct_query = TRUE; @@ -201,7 +201,7 @@ protected function parse_select_as() */ protected function parse_from() { - $key = array_search("from", $this->parse_query_lower); + $key = array_search('from', $this->parse_query_lower); $string = $this->parse_query[$key + 1]; $arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY); @@ -243,13 +243,13 @@ protected function parse_from_as() */ protected function parse_where() { - $key = array_search("where", $this->parse_query_lower); + $key = array_search('where', $this->parse_query_lower); - if ($key == FALSE) return $this->parse_where = "return TRUE;"; + if ($key == FALSE) return $this->parse_where = 'return TRUE;'; $string = $this->parse_query[$key + 1]; - if (trim($string) == '') return $this->parse_where = "return TRUE;"; + if (trim($string) == '') return $this->parse_where = 'return TRUE;'; if (self::$cache_patterns && self::$cache_replacements) { @@ -353,8 +353,8 @@ protected function parse_in($string) */ protected function exec_query() { - $klimit = array_search("limit", $this->parse_query_lower); - $koffset = array_search("offset", $this->parse_query_lower); + $klimit = array_search('limit', $this->parse_query_lower); + $koffset = array_search('offset', $this->parse_query_lower); if ($klimit !== FALSE) $limit = (int)$this->parse_query[$klimit + 1]; @@ -414,7 +414,7 @@ protected function exec_query() */ protected function parse_order() { - $key = array_search("order by", $this->parse_query_lower); + $key = array_search('order by', $this->parse_query_lower); if ($key === FALSE) return; @@ -425,15 +425,15 @@ protected function parse_order() $arrays = array_map('trim', $arrays); - $multisort = "array_multisort("; + $multisort = 'array_multisort('; foreach ($arrays as $array) { list($col, $sort) = preg_split('#((\s)+)#', $array, -1, PREG_SPLIT_NO_EMPTY); - $multisort .= "\$this->split_array(\$this->response, '$col'), SORT_" . strtoupper($sort) . ", SORT_STRING, "; + $multisort .= "\$this->split_array(\$this->response, '$col'), SORT_" . strtoupper($sort) . ', SORT_STRING, '; } - $multisort .= "\$this->response);"; + $multisort .= '$this->response);'; eval($multisort); From 09ad2baf5962dac6b70e3f6ba391b4a15a8d501c Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:17:42 +0800 Subject: [PATCH 54/76] return $this; --- sql4array.class.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index 4a34b1e..ee76409 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -245,7 +245,12 @@ protected function parse_where() { $key = array_search('where', $this->parse_query_lower); - if ($key == FALSE) return $this->parse_where = 'return TRUE;'; + if ($key == FALSE) + { + $this->parse_where = 'return TRUE;'; + + return $this; + } $string = $this->parse_query[$key + 1]; From 1dbf5eca0f1f7def26e1ce2afcc84b3889f860b7 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:19:27 +0800 Subject: [PATCH 55/76] . --- sql4array.class.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index ee76409..704fdfd 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -113,8 +113,6 @@ public function query($query) ->parse_from() ->parse_from_as() ->parse_where() - ; - $this ->exec_query() ->parse_order() ; From d1a3224d822cbc41e872a587b16834335a2acf74 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:20:22 +0800 Subject: [PATCH 56/76] . --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 704fdfd..a7898ff 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -100,6 +100,8 @@ function asset($key, $value) { /** * Query function + * + * @return array - return $this->return_response(); */ public function query($query) { From 09061ef79ea497f60065281d74749d2f6af162ed Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:24:56 +0800 Subject: [PATCH 57/76] $enabled = true --- sql4array.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index a7898ff..ba34ae3 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -56,15 +56,20 @@ class sql4array */ protected $globals = array(); + public function __construct() + { + $this->createFromGlobals(false); + } + /** * set tables get from where * and this func will reset $this->globals * * @return sql4array */ - public function createFromGlobals($disable = false) + public function createFromGlobals($enabled = true) { - $this->attr['createFromGlobals'] = !$disable; + $this->attr['createFromGlobals'] = $enabled; // reset $this->globals $this->globals = array(); From fabb880b4e922cbb102c269b1fd118034e973d80 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:27:29 +0800 Subject: [PATCH 58/76] . --- sql4array.class.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index ba34ae3..d27854b 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -58,7 +58,10 @@ class sql4array public function __construct() { - $this->createFromGlobals(false); + $this + ->createFromGlobals(false) + ->destroy() + ; } /** From 34861a721d1c2e2a4c8f9705fb92b5ce008b9b0e Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:43:51 +0800 Subject: [PATCH 59/76] protected $cache_query = array(); --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index d27854b..ecc8f6f 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -56,6 +56,8 @@ class sql4array */ protected $globals = array(); + protected $cache_query = array(); + public function __construct() { $this From f681c4e2cea7c3674ebb87b5bb0c1d9d8d0f5939 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:44:48 +0800 Subject: [PATCH 60/76] . --- sql4array.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index ecc8f6f..f5e56be 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -125,6 +125,9 @@ public function query($query) ->parse_from() ->parse_from_as() ->parse_where() + ; + + $this ->exec_query() ->parse_order() ; From ba516e37711541e1136809f41717935e31c3c50a Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:48:39 +0800 Subject: [PATCH 61/76] $_skip = $this->cacheQueryGet($this->query); --- sql4array.class.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index f5e56be..5e52105 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -118,6 +118,14 @@ public function query($query) $this->destroy(); $this->query = $query; + if ($this->attr['cacheQuery']) + { + $_skip = $this->cacheQueryGet($this->query); + } + + if (!$_skip) + { + $this ->parse_query() ->parse_select() @@ -127,6 +135,8 @@ public function query($query) ->parse_where() ; + } + $this ->exec_query() ->parse_order() From 7753d74470521d0ee4d1ec0e52ed52962dc991fe Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:49:24 +0800 Subject: [PATCH 62/76] . --- sql4array.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index 5e52105..77306ec 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -61,9 +61,11 @@ class sql4array public function __construct() { $this - ->createFromGlobals(false) ->destroy() + ->createFromGlobals(false) ; + + $this->attr['cacheQuery'] = true; } /** From d91d1a037562dc154fcf5909b07235000da8c785 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:55:53 +0800 Subject: [PATCH 63/76] protected function cacheQueryGet($query) --- sql4array.class.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 77306ec..5e5b011 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -147,6 +147,31 @@ public function query($query) return $this->return_response(); } + protected function cacheQueryGet($query) + { + $key = md5($query); + + if ( + array_key_exists($key, $this->cache_query) + && $data = $this->cache_query[$key] + ) + { + $this->query = $data['query']; + $this->parse_query = $data['parse_query']; + $this->parse_query_lower = $data['parse_query_lower']; + $this->parse_select = $data['parse_select']; + $this->parse_select_as = $data['parse_select_as']; + $this->parse_from = $data['parse_from']; + $this->parse_from_as = $data['parse_from_as']; + $this->parse_where = $data['parse_where']; + $this->distinct_query = $data['distinct_query']; + + return true; + } + + return false; + } + /** * Destroy current values */ From 9aafbef818dff14594358b06ad8ef064ad9ae641 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:58:49 +0800 Subject: [PATCH 64/76] protected function cacheQuerySet($query) --- sql4array.class.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 5e5b011..028dac9 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -172,6 +172,27 @@ protected function cacheQueryGet($query) return false; } + protected function cacheQuerySet($query) + { + $key = md5($query); + + $data = array(); + + $data['query'] = $this->query; + $data['parse_query'] = $this->parse_query; + $data['parse_query_lower'] = $this->parse_query_lower; + $data['parse_select'] = $this->parse_select; + $data['parse_select_as'] = $this->parse_select_as; + $data['parse_from'] = $this->parse_from; + $data['parse_from_as'] = $this->parse_from_as; + $data['parse_where'] = $this->parse_where; + $data['distinct_query'] = $this->distinct_query; + + $this->cache_query[$key] = $data; + + return $this; + } + /** * Destroy current values */ From 521b59b36727fa513e62550c924612a9c3820a41 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:59:29 +0800 Subject: [PATCH 65/76] $this->cacheQuerySet($this->query); --- sql4array.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 028dac9..d9a608c 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -137,6 +137,8 @@ public function query($query) ->parse_where() ; + $this->cacheQuerySet($this->query); + } $this From 7f3c84f1c214b965ca6f4ddd477825092900a59f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 05:59:37 +0800 Subject: [PATCH 66/76] . --- sql4array.class.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index d9a608c..367f39d 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -128,14 +128,14 @@ public function query($query) if (!$_skip) { - $this - ->parse_query() - ->parse_select() - ->parse_select_as() - ->parse_from() - ->parse_from_as() - ->parse_where() - ; + $this + ->parse_query() + ->parse_select() + ->parse_select_as() + ->parse_from() + ->parse_from_as() + ->parse_where() + ; $this->cacheQuerySet($this->query); From 5be70ee339b4d09e59e7d5631d545af3d99a6df4 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:01:30 +0800 Subject: [PATCH 67/76] . --- sql4array.example.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql4array.example.php b/sql4array.example.php index ba579e2..ce31f42 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -32,6 +32,9 @@ foreach(array( "SELECT id, foo FROM array", "SELECT id, foo FROM array WHERE id > 10", + + "SELECT id, foo FROM array WHERE id > 10", + "SELECT id, foo FROM array as arr WHERE id > 10", "SELECT id AS i, foo AS f FROM array WHERE i > 10", "SELECT id AS i, foo AS f FROM array WHERE i > 10 AND f LIKE '%a%'", From e9a07ea57441a78c54739d4e0d512287268790bd Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:02:25 +0800 Subject: [PATCH 68/76] . --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index 367f39d..8d66e0b 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -44,7 +44,7 @@ class sql4array /** * sql4array setting */ - protected $var = array(); + protected $attr = array(); /** * sql4array temp From 796ba754d6e7ccb3bf86a3f01f2b36008b50d595 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:05:16 +0800 Subject: [PATCH 69/76] . --- sql4array.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 8d66e0b..72f3051 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -120,12 +120,7 @@ public function query($query) $this->destroy(); $this->query = $query; - if ($this->attr['cacheQuery']) - { - $_skip = $this->cacheQueryGet($this->query); - } - - if (!$_skip) + if (!$this->cacheQuery($this->query)) { $this @@ -149,6 +144,11 @@ public function query($query) return $this->return_response(); } + protected function cacheQuery($query) + { + return ($this->attr['cacheQuery'] && $this->cacheQueryGet($query)); + } + protected function cacheQueryGet($query) { $key = md5($query); From bff0420271d8ea181b6a1125919cc1dc6c592018 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:07:16 +0800 Subject: [PATCH 70/76] @return bool --- sql4array.class.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sql4array.class.php b/sql4array.class.php index 72f3051..bcd344d 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -144,11 +144,17 @@ public function query($query) return $this->return_response(); } + /** + * @return bool + */ protected function cacheQuery($query) { return ($this->attr['cacheQuery'] && $this->cacheQueryGet($query)); } + /** + * @return bool + */ protected function cacheQueryGet($query) { $key = md5($query); From 5eed53b3956665366f9dc10ab04967ec8d87b95b Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:14:14 +0800 Subject: [PATCH 71/76] remove var temp --- sql4array.class.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index bcd344d..ee7ed66 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -46,11 +46,6 @@ class sql4array */ protected $attr = array(); - /** - * sql4array temp - */ - protected $temp = array(); - /** * sql4array tables map */ @@ -218,8 +213,6 @@ protected function destroy() $this->tables = array(); $this->response = array(); - $this->temp = array(); - return $this; } From 9509302a4eca2f90adbdff4fd079d87660b94bc9 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:22:00 +0800 Subject: [PATCH 72/76] . --- sql4array.class.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index ee7ed66..1d23065 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -115,7 +115,7 @@ public function query($query) $this->destroy(); $this->query = $query; - if (!$this->cacheQuery($this->query)) + if (!$this->cacheQueryGet($this->query)) { $this @@ -142,9 +142,11 @@ public function query($query) /** * @return bool */ - protected function cacheQuery($query) + protected function cacheQuery($val = true) { - return ($this->attr['cacheQuery'] && $this->cacheQueryGet($query)); + $this->attr['cacheQuery'] = $val; + + return $this; } /** @@ -155,6 +157,8 @@ protected function cacheQueryGet($query) $key = md5($query); if ( + $this->attr['cacheQuery'] + && array_key_exists($key, $this->cache_query) && $data = $this->cache_query[$key] ) From 4f7b1e7c5985302d53e14ff44d372d4a83c922a6 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:22:18 +0800 Subject: [PATCH 73/76] public function cacheQuery($val = true) --- sql4array.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index 1d23065..6ea065f 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -142,7 +142,7 @@ public function query($query) /** * @return bool */ - protected function cacheQuery($val = true) + public function cacheQuery($val = true) { $this->attr['cacheQuery'] = $val; From bb9a591017d739e600b9c5cc3dd19121c3f64bfb Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:24:25 +0800 Subject: [PATCH 74/76] if ($clear) $this->cache_query = array(); --- sql4array.class.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index 6ea065f..a822cb4 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -58,9 +58,8 @@ public function __construct() $this ->destroy() ->createFromGlobals(false) + ->cacheQuery(true) ; - - $this->attr['cacheQuery'] = true; } /** @@ -142,10 +141,12 @@ public function query($query) /** * @return bool */ - public function cacheQuery($val = true) + public function cacheQuery($val = true, $clear = false) { $this->attr['cacheQuery'] = $val; + if ($clear) $this->cache_query = array(); + return $this; } From c4c6b1807948649f019d1551cc940f601d81a57c Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:25:29 +0800 Subject: [PATCH 75/76] . --- sql4array.class.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/sql4array.class.php b/sql4array.class.php index a822cb4..d163609 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -138,9 +138,6 @@ public function query($query) return $this->return_response(); } - /** - * @return bool - */ public function cacheQuery($val = true, $clear = false) { $this->attr['cacheQuery'] = $val; From 0892f42706a1424684b5d66bc4902ddf9f07012f Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 28 Feb 2012 06:48:05 +0800 Subject: [PATCH 76/76] $data['count_used'] += 1; --- sql4array.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sql4array.class.php b/sql4array.class.php index d163609..76c0ff4 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -158,7 +158,7 @@ protected function cacheQueryGet($query) $this->attr['cacheQuery'] && array_key_exists($key, $this->cache_query) - && $data = $this->cache_query[$key] + && $data = &$this->cache_query[$key] ) { $this->query = $data['query']; @@ -171,6 +171,8 @@ protected function cacheQueryGet($query) $this->parse_where = $data['parse_where']; $this->distinct_query = $data['distinct_query']; + $data['count_used'] += 1; + return true; }