diff --git a/sql4array.class.php b/sql4array.class.php index 2a599a6..76c0ff4 100644 --- a/sql4array.class.php +++ b/sql4array.class.php @@ -1,6 +1,6 @@ @@ -9,8 +9,8 @@ * Date: 30/04/2007 * License: LGPL */ - -/* + +/** * Parameters available : * SELECT, DISTINCT, FROM, WHERE, ORDER BY, LIMIT, OFFSET * @@ -20,95 +20,247 @@ * Functions available in WHERE parameters : * LOWER(var), UPPER(var), TRIM(var) */ - + 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(); - - /* Query function - -------------------------------------------- */ + /** + * Init + */ + 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(); + + protected static $cache_patterns = array(); + protected static $cache_replacements = array(); + + /** + * sql4array setting + */ + protected $attr = array(); + + /** + * sql4array tables map + */ + protected $globals = array(); + + protected $cache_query = array(); + + public function __construct() + { + $this + ->destroy() + ->createFromGlobals(false) + ->cacheQuery(true) + ; + } + + /** + * set tables get from where + * and this func will reset $this->globals + * + * @return sql4array + */ + public function createFromGlobals($enabled = true) + { + $this->attr['createFromGlobals'] = $enabled; + + // reset $this->globals + $this->globals = array(); + + return $this; + } + + /** + * @return array + */ + function table($table) + { + if ($this->attr['createFromGlobals']) + { + return $GLOBALS[$table]; + } + else + { + return $this->globals[$table]; + } + } + + /** + * set tables map + * + * @return sql4array + */ + function asset($key, $value) { + $this->globals[$key] = $value; + + return $this; + } + + /** + * Query function + * + * @return array - return $this->return_response(); + */ 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(); - + + if (!$this->cacheQueryGet($this->query)) + { + + $this + ->parse_query() + ->parse_select() + ->parse_select_as() + ->parse_from() + ->parse_from_as() + ->parse_where() + ; + + $this->cacheQuerySet($this->query); + + } + + $this + ->exec_query() + ->parse_order() + ; + return $this->return_response(); } - - /* Destroy current values - -------------------------------------------- */ - private function destroy() + + public function cacheQuery($val = true, $clear = false) { - $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->attr['cacheQuery'] = $val; + + if ($clear) $this->cache_query = array(); + + return $this; } - - /* Parse SQL query - -------------------------------------------- */ - private function parse_query() + + /** + * @return bool + */ + protected function cacheQueryGet($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); + $key = md5($query); + + if ( + $this->attr['cacheQuery'] + && + 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']; + + $data['count_used'] += 1; + + return true; + } + + return false; } - - /* Parse SQL select parameters - -------------------------------------------- */ - private function parse_select() + + protected function cacheQuerySet($query) { - $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; + $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; } - - /* Parse again SQL select parameters with as keyword - -------------------------------------------- */ - private function parse_select_as() + + /** + * Destroy current values + */ + protected 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(); + + return $this; + } + + /** + * Parse SQL 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); + $this->parse_query = array_map('trim', $this->parse_query); + $this->parse_query_lower = array_map('strtolower', $this->parse_query); + + return $this; + } + + /** + * Parse SQL select parameters + */ + protected 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; + + return $this; + } + + /** + * Parse again SQL select parameters with as keyword + */ + 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); + $arrays = preg_split('#((\s)+AS(\s)+)#i', $select, -1, PREG_SPLIT_NO_EMPTY); $this->parse_select_as[$arrays[1]] = $arrays[0]; } else @@ -116,151 +268,188 @@ private function parse_select_as() $this->parse_select_as[$select] = $select; } } + + return $this; } - - /* Parse SQL from parameters - -------------------------------------------- */ - private function parse_from() + + /** + * Parse SQL from parameters + */ + protected 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; + $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; + + return $this; } - - /* Parse again SQL from parameters with as keyword - -------------------------------------------- */ - private function parse_from_as() + + /** + * Parse again SQL from parameters with as keyword + */ + 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); - + $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]] = $$table; + $from = $arrays[1]; } else { $table = $from; - global $$table; - $this->parse_from_as[$from] = $table; - $this->tables[$arrays[1]] = $$table; } + + $this->parse_from_as[$from] = $table; + /* + $this->tables[$from] = $this->table($table); + */ } + + return $this; } - - /* Parse SQL where parameters - -------------------------------------------- */ - private function parse_where() + + /** + * Parse SQL where parameters + */ + 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;"; - - $string = $this->parse_query[$key+1]; - - if (trim($string) == '') - return $this->parse_where = "return TRUE;"; - - - /* 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\").') '"; - - /* 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); - return '$row[$this->parse_select_as['.$col.']]'; + $this->parse_where = 'return TRUE;'; + + return $this; + } + + $string = $this->parse_query[$key + 1]; + + 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 { - return '$row[$this->parse_select_as['.$key.']]'; + + /** + * 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::$cache_patterns = $patterns; + self::$cache_replacements = $replacements; + + } + + /** + * 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'; + + // 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\").')'"; + $replacements[] = "'!eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'"; + + $this->parse_where = "return " . stripslashes(trim(preg_replace($patterns, $replacements, $string))) . ";"; + + return $this; + } + + /** + * return '$row[$this->parse_select_as[' . $key . ']]'; + * + * @return string + */ + protected function parse_where_key($key) + { + if (preg_match('#\.#', $key)) + { + list($table, $col) = explode('.', $key); + + $key = $col; } + + return '$row[$this->parse_select_as[' . $key . ']]'; } - - /* Format IN parameters for PHP - -------------------------------------------- */ - private function parse_in($string) + + /** + * Format IN parameters for PHP + * + * @return string + */ + protected function parse_in($string) { - $array = explode(',', $string); - $array = array_map('trim', $array); + $array = explode(',', $string); + $array = array_map('trim', $array); return implode(' || ', $array); } - - /* Execute query - -------------------------------------------- */ - private function exec_query() + + /** + * Execute query + */ + protected 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) + $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->parse_from_as as $from_name => $table_name) { - foreach ($table as $row) + + $this->tables[$from_name] = $this->table($table_name); + + foreach ($this->tables[$from_name] as $row) { // Offset if ($koffset !== FALSE && $irow < $offset) @@ -268,104 +457,100 @@ 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; - + 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; - + 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; + if ($klimit !== FALSE && count($this->response) == $limit) break; } - + $irow++; } } + + return $this; } - - /* Parse SQL order by parameters - -------------------------------------------- */ - private function parse_order() + + /** + * Parse SQL order by parameters + */ + protected 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("; - + $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, "; + 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); + + return $this; } - - /* Return response - -------------------------------------------- */ - private function return_response() + + /** + * Return response + */ + protected function return_response() { return $this->response; } - - /* 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 a column of an array + */ + protected 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 - -------------------------------------------- */ - private function entire_array_search($needle, $array) + + /** + * Entire array search + */ + protected function entire_array_search($needle, $array) { - foreach($array as $key => $value) - if ($value === $needle) - $return[] = $key; - - if (!is_array($return)) - $return = FALSE; - + foreach ($array as $key => $value) + if ($value === $needle) $return[] = $key; + + if (!is_array($return)) $return = FALSE; + return $return; } } diff --git a/sql4array.example.php b/sql4array.example.php index b6013d8..ce31f42 100644 --- a/sql4array.example.php +++ b/sql4array.example.php @@ -1,7 +1,6 @@ @@ -11,29 +10,53 @@ * License: LGPL */ -header("Content-type: text"); +//header("Content-type: text"); require "./sql4array.class.php"; +$array = array(); 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(); -$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"); +$sql = new sql4array(); + +$sql->createFromGlobals(); + +$r = array(); + +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%'", + "SELECT id AS iiiiii, foo AS fooooooooo FROM array WHERE array.iiiiii != 10", +) as $sqlstring) +{ + + $a = $sql->query($sqlstring); + + $r[] = array( + 'sql' => $sqlstring, + 'result' => $a, + ); + +} -var_dump($a); -var_dump($b); -var_dump($c); -var_dump($d); -var_dump($e); +var_dump($sql); + +foreach($r as $value) +{ + echo $value['sql']."\n"; + var_dump($value['result']); +} ?> \ No newline at end of file