3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * This is a simple sql tokenizer / parser. * * It does NOT support multiline comments at this time. * * See the included example.php for usage. * * THIS CODE IS A PROTOTYPE/BETA * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @version 0.0.4 */ class SqlParser { var $handle = null; // statements public static $querysections = array('alter','create','drop', 'select', 'delete', 'insert', 'update', 'from', 'where', 'limit', 'order'); // operators public static $operators = array('=', '<>', '<', '<=', '>', '>=', 'like', 'clike', 'slike', 'not', 'is', 'in', 'between'); // types public static $types = array('character', 'char', 'varchar', 'nchar', 'bit', 'numeric', 'decimal', 'dec', 'integer', 'int', 'smallint', 'float', 'real', 'double', 'date', 'datetime', 'time', 'timestamp', 'interval', 'bool', 'boolean', 'set', 'enum', 'text'); // conjuctions public static $conjuctions = array('by', 'as', 'on', 'into', 'from', 'where', 'with'); // basic functions public static $funcitons = array('avg', 'count', 'max', 'min', 'sum', 'nextval', 'currval', 'concat', ); // reserved keywords public static $reserved = array('absolute', 'action', 'add', 'all', 'allocate', 'and', 'any', 'are', 'asc', 'ascending', 'assertion', 'at', 'authorization', 'begin', 'bit_length', 'both', 'cascade', 'cascaded', 'case', 'cast', 'catalog', 'char_length', 'character_length', 'check', 'close', 'coalesce', 'collate', 'collation', 'column', 'commit', 'connect', 'connection', 'constraint', 'constraints', 'continue', 'convert', 'corresponding', 'cross', 'current', 'current_date', 'current_time', 'current_timestamp', 'current_user', 'cursor', 'day', 'deallocate', 'declare', 'default', 'deferrable', 'deferred', 'desc', 'descending', 'describe', 'descriptor', 'diagnostics', 'disconnect', 'distinct', 'domain', 'else', 'end', 'end-exec', 'escape', 'except', 'exception', 'exec', 'execute', 'exists', 'external', 'extract', 'false', 'fetch', 'first', 'for', 'foreign', 'found', 'full', 'get', 'global', 'go', 'goto', 'grant', 'group', 'having', 'hour', 'identity', 'immediate', 'indicator', 'initially', 'inner', 'input', 'insensitive', 'intersect', 'isolation', 'join', 'key', 'language', 'last', 'leading', 'left', 'level', 'limit', 'local', 'lower', 'match', 'minute', 'module', 'month', 'names', 'national', 'natural', 'next', 'no', 'null', 'nullif', 'octet_length', 'of', 'only', 'open', 'option', 'or', 'order', 'outer', 'output', 'overlaps', 'pad', 'partial', 'position', 'precision', 'prepare', 'preserve', 'primary', 'prior', 'privileges', 'procedure', 'public', 'read', 'references', 'relative', 'restrict', 'revoke', 'right', 'rollback', 'rows', 'schema', 'scroll', 'second', 'section', 'session', 'session_user', 'size', 'some', 'space', 'sql', 'sqlcode', 'sqlerror', 'sqlstate', 'substring', 'system_user', 'table', 'temporary', 'then', 'timezone_hour', 'timezone_minute', 'to', 'trailing', 'transaction', 'translate', 'translation', 'trim', 'true', 'union', 'unique', 'unknown', 'upper', 'usage', 'user', 'using', 'value', 'values', 'varying', 'view', 'when', 'whenever', 'work', 'write', 'year', 'zone', 'eoc'); // open parens, tokens, and brackets public static $startparens = array('{', '('); public static $endparens = array('}', ')'); public static $tokens = array(',', ' '); private $query = ''; // constructor (placeholder only) public function __construct() { } /** * Simple SQL Tokenizer * * @author Justin Carlson <justin.carlson@gmail.com> * @license GPL * @param string $sqlQuery * @return token array */ public static function Tokenize($sqlQuery, $cleanWhitespace = true) { /** * Strip extra whitespace from the query */ if ($cleanWhitespace === true) { $sqlQuery = ltrim(preg_replace('/[\\s]{2,}/', ' ', $sqlQuery)); } /** * Regular expression parsing. * Inspired/Based on the Perl SQL::Tokenizer by Igor Sutton Lopes */ // begin group $regex = '('; // inline comments $regex .= '(?:--|\\#)[\\ \\t\\S]*'; // logical operators $regex .= '|(?:<>|<=>|>=|<=|==|=|!=|!|<<|>>|<|>|\\|\\||\\||&&|&|-'; $regex .= '|\\+|\\*(?!\/)|\/(?!\\*)|\\%|~|\\^|\\?)'; // empty quotes $regex .= '|[\\[\\]\\(\\),;`]|\\\'\\\'(?!\\\')|\\"\\"(?!\\"")'; // string quotes $regex .= '|".*?(?:(?:""){1,}"'; $regex .= '|(?<!["\\\\])"(?!")|\\\\"{2})'; $regex .= '|\'.*?(?:(?:\'\'){1,}\''; $regex .= '|(?<![\'\\\\])\'(?!\')'; $regex .= '|\\\\\'{2})'; // c comments $regex .= '|\/\\*[\\ \\t\\n\\S]*?\\*\/'; // wordds, column strings, params $regex .= '|(?:[\\w:@]+(?:\\.(?:\\w+|\\*)?)*)'; $regex .= '|[\t\ ]+'; // period and whitespace $regex .= '|[\.]'; $regex .= '|[\s]'; $regex .= ')'; # end group // perform a global match preg_match_all('/' . $regex . '/smx', $sqlQuery, $result); // return tokens return $result[0]; } /** * Simple SQL Parser * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @param string $sqlQuery * @param bool optional $cleanup * @return SqlParser Object */ public static function ParseString($sqlQuery, $cleanWhitespace = true) { // instantiate if called statically if (!isset($this)) { $handle = new SqlParser(); } else { $handle = $this; } // copy and tokenize the query $tokens = self::Tokenize($sqlQuery, $cleanWhitespace); $tokenCount = count($tokens); $queryParts = array(); if (isset($tokens[0])===true) { $section = $tokens[0]; } // parse the tokens for ($t = 0; $t < $tokenCount; $t++) { // if is paren if (in_array($tokens[$t], self::$startparens)) { // read until closed $sub = $handle->readsub($tokens, $t); $handle->query[$section].= $sub; } else { if (in_array(strtolower($tokens[$t]), self::$querysections) && !isset($handle->query[$tokens[$t]])) { $section = strtolower($tokens[$t]); } // rebuild the query in sections if (!isset($handle->query[$section])) $handle->query[$section] = ''; $handle->query[$section] .= $tokens[$t]; } } return $handle; } /** * Parses a sub-section of a query * * @param array $tokens * @param int $position * @return string section */ private function readsub($tokens, &$position) { $sub = $tokens[$position]; $tokenCount = count($tokens); $position++; while (!in_array($tokens[$position], self::$endparens) && $position < $tokenCount) { if (in_array($tokens[$position], self::$startparens)) { $sub.= $this->readsub($tokens, $position); $subs++; } else { $sub.= $tokens[$position]; } $position++; } $sub.= $tokens[$position]; return $sub; } /** * Returns manipulated sql to get the number of rows in the query. * Can be used for simple pagination, for example. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getCountQuery($optName = 'count') { // create temp copy of query $temp = $this->query; // create count() version of select and unset any limit statement $temp['select'] = 'select count(*) as `'.$optName.'` '; if (isset($temp['limit'])) { unset($temp['limit']); } return implode(null, $temp); } /** * Returns manipulated sql to get the unlimited number of rows in the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getLimitedCountQuery() { $this->query['select'] = 'select count(*) as `count` '; return implode('', $this->query); } /** * Returns the select section of the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getSelectStatement() { return $this->query['select']; } /** * Returns the from section of the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getFromStatement() { return $this->query['from']; } /** * Returns the where section of the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getWhereStatement() { return $this->query['where']; } /** * Returns the limit section of the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getLimitStatement() { return $this->query['limit']; } /** * Returns the specified section of the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function get($which) { if (!isset($this->query[$which])) return false; return $this->query[$which]; } /** * Returns the sections of the query. * * @author Justin Carlson <justin.carlson@gmail.com> * @license LGPL 3 * @return string sql */ public function getArray() { return $this->query; } } /** * Note: The closing tag of a PHP block at the end of a file is optional, * and in some cases omitting it is helpful when using include() or require(), * so unwanted whitespace will not occur at the end of files */ $totalCountSql = SqlParser::ParseString( 'SELECT name, rew from asdf' ); var_dump($totalCountSql);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.40.0130.01019.46
8.3.30.0110.00720.29
8.3.20.0000.00920.34
8.3.10.0080.00021.95
8.3.00.0030.00522.43
8.2.170.0040.01222.96
8.2.160.0070.00722.38
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0000.00826.16
8.2.120.0050.00321.00
8.2.110.0050.00519.49
8.2.100.0040.00718.41
8.2.90.0000.00819.55
8.2.80.0050.00319.46
8.2.70.0000.00917.88
8.2.60.0030.00618.05
8.2.50.0040.00418.07
8.2.40.0040.00418.44
8.2.30.0000.00819.61
8.2.20.0090.00018.18
8.2.10.0000.00818.25
8.2.00.0080.00018.31
8.1.270.0080.00023.99
8.1.260.0050.00326.35
8.1.250.0000.00828.09
8.1.240.0060.00322.78
8.1.230.0060.00721.29
8.1.220.0050.00318.77
8.1.210.0030.00619.35
8.1.200.0040.00817.85
8.1.190.0000.00917.80
8.1.180.0000.00818.10
8.1.170.0000.00918.81
8.1.160.0000.00819.23
8.1.150.0000.00819.04
8.1.140.0040.00417.90
8.1.130.0030.00618.11
8.1.120.0040.00717.70
8.1.110.0030.00517.66
8.1.100.0040.00417.79
8.1.90.0050.00317.69
8.1.80.0000.00717.72
8.1.70.0080.00017.78
8.1.60.0070.00317.94
8.1.50.0030.00617.89
8.1.40.0060.00617.90
8.1.30.0030.00517.80
8.1.20.0030.00717.92
8.1.10.0080.00017.82
8.1.00.0080.00017.71
8.0.300.0100.00020.27
8.0.290.0030.00517.25
8.0.280.0040.00418.54
8.0.270.0070.00017.44
8.0.260.0040.00417.26
8.0.250.0000.00717.40
8.0.240.0000.00817.32
8.0.230.0000.00817.45
8.0.220.0050.00317.40
8.0.210.0000.00717.25
8.0.200.0000.00817.35
8.0.190.0000.00717.32
8.0.180.0040.00417.36
8.0.170.0090.00017.28
8.0.160.0090.00017.29
8.0.150.0060.00317.10
8.0.140.0030.00617.17
8.0.130.0000.00613.68
8.0.120.0000.00817.43
8.0.110.0000.00817.38
8.0.100.0000.00917.34
8.0.90.0040.00417.22
8.0.80.0090.00617.33
8.0.70.0000.00817.14
8.0.60.0040.00417.40
8.0.50.0020.00517.17
8.0.30.0150.00517.32
8.0.20.0120.00917.49
8.0.10.0030.00517.54
8.0.00.0100.00917.17
7.4.330.0050.00016.77
7.4.320.0070.00016.97
7.4.300.0000.00617.03
7.4.290.0000.00817.07
7.4.280.0060.00316.93
7.4.270.0040.00417.04
7.4.260.0000.00713.85
7.4.250.0030.00516.84
7.4.240.0050.00316.96
7.4.230.0000.00817.08
7.4.220.0100.01016.84
7.4.210.0020.01317.07
7.4.200.0070.00017.09
7.4.190.0000.00817.18
7.4.160.0040.01416.88
7.4.150.0100.01017.40
7.4.140.0140.00717.86
7.4.130.0090.01116.94
7.4.120.0090.00916.87
7.4.110.0100.00717.14
7.4.100.0090.01616.95
7.4.90.0090.00916.93
7.4.80.0070.01119.39
7.4.70.0100.00616.93
7.4.60.0060.01117.07
7.4.50.0000.00916.71
7.4.40.0030.01222.77
7.4.30.0130.00617.20
7.4.00.0040.01415.18
7.3.330.0000.00613.51
7.3.320.0030.00313.66
7.3.310.0000.00716.77
7.3.300.0050.00316.72
7.3.290.0090.00616.77
7.3.280.0100.00816.75
7.3.270.0120.00617.40
7.3.260.0090.01016.79
7.3.250.0110.00916.75
7.3.240.0090.00916.94
7.3.230.0130.00716.99
7.3.210.0090.00916.97
7.3.200.0100.00719.39
7.3.190.0120.00616.98
7.3.180.0120.00916.92
7.3.170.0090.00916.98
7.3.160.0130.00616.78
7.3.120.0090.00915.11
7.2.330.0120.00617.05
7.2.320.0040.01616.96
7.2.310.0090.01017.06
7.2.300.0100.00716.82
7.2.290.0030.01516.94
7.2.60.0060.00916.91
7.2.00.0030.01019.67
7.1.200.0030.01316.25
7.1.100.0000.01218.61
7.1.70.0000.01417.57
7.1.60.0060.01319.17
7.1.50.0140.00317.14
7.1.00.0000.05322.48
7.0.200.0000.00716.68
7.0.140.0000.07722.14
7.0.80.0170.08320.27
7.0.70.0130.03720.11
7.0.60.0130.04720.16
7.0.50.0700.07320.44
7.0.40.0100.05320.16
7.0.30.0070.08319.97
7.0.20.0170.07720.06
7.0.10.0070.05020.16
7.0.00.0030.05320.16
5.6.280.0070.07321.17
5.6.230.0130.07720.84
5.6.220.0030.05320.76
5.6.210.0070.07720.71
5.6.200.0130.06321.06
5.6.190.0030.04321.27
5.6.180.0130.07321.13
5.6.170.0100.09021.12
5.6.160.0170.07721.11
5.6.150.0030.07321.03
5.6.140.0070.08321.02
5.6.130.0030.08021.20
5.6.120.0030.08321.16
5.6.110.0030.08321.22
5.6.100.0070.08321.19
5.6.90.0170.03021.08
5.6.80.0000.05720.41
5.6.70.0030.04020.56
5.6.60.0070.07320.60
5.6.50.0070.08320.50
5.6.40.0170.04020.48
5.6.30.0130.04720.52
5.6.20.0100.07020.40
5.6.10.0070.05020.38
5.6.00.0030.04320.52
5.5.370.0070.06020.60
5.5.360.0100.05320.41
5.5.350.0100.07320.45
5.5.340.0100.07720.91
5.5.330.0070.06720.96
5.5.320.0100.04720.86
5.5.310.0030.05320.91
5.5.300.0100.07320.86
5.5.290.0100.04720.86
5.5.280.0030.07320.91
5.5.270.0130.07320.96
5.5.260.0030.05720.91
5.5.250.0100.07020.72
5.5.240.0070.07020.24
5.5.230.0170.06020.30
5.5.220.0070.07320.06
5.5.210.0000.04720.19
5.5.200.0070.05320.22
5.5.190.0130.07320.36
5.5.180.0130.07020.29
5.5.160.0000.04320.21
5.5.150.0100.08020.20
5.5.140.0070.06020.24
5.5.130.0100.05020.16
5.5.120.0100.07720.20
5.5.110.0030.08320.04
5.5.100.0070.05320.19
5.5.90.0000.05020.04
5.5.80.0130.07720.16
5.5.70.0170.06720.14
5.5.60.0030.08020.19
5.5.50.0030.04720.23
5.5.40.0030.04720.16
5.5.30.0070.08320.17
5.5.20.0070.08720.16
5.5.10.0130.07020.15
5.5.00.0130.07020.04
5.4.450.0130.07319.20
5.4.440.0030.08319.29
5.4.430.0030.04719.59
5.4.420.0070.08019.39
5.4.410.0070.05019.42
5.4.400.0000.04719.07
5.4.390.0070.07018.90
5.4.380.0100.07019.13
5.4.370.0070.03719.13
5.4.360.0100.07719.20
5.4.350.0030.06019.04
5.4.340.0100.07319.07
5.4.320.0030.08019.09
5.4.310.0070.07718.91
5.4.300.0100.03718.98
5.4.290.0130.03019.13
5.4.280.0130.06019.14
5.4.270.0030.04718.98
5.4.260.0070.08318.97
5.4.250.0230.05319.05
5.4.240.0070.07019.22
5.4.230.0070.04719.04
5.4.220.0070.07318.89
5.4.210.0070.07718.86
5.4.200.0070.07319.08
5.4.190.0030.08019.05
5.4.180.0070.07019.02
5.4.170.0070.04319.02
5.4.160.0130.03718.86
5.4.150.0070.05019.25
5.4.140.0070.04316.47
5.4.130.0170.06016.26
5.4.120.0070.05316.39
5.4.110.0170.05716.54
5.4.100.0070.07316.42
5.4.90.0070.05016.56
5.4.80.0130.06716.43
5.4.70.0100.06016.43
5.4.60.0070.07016.39
5.4.50.0100.06716.51
5.4.40.0000.06316.53
5.4.30.0130.07016.47
5.4.20.0100.06316.39
5.4.10.0070.03716.38
5.4.00.0170.06715.94
5.3.290.0100.07314.74
5.3.280.0130.07014.80
5.3.270.0070.04314.75
5.3.260.0030.05314.75
5.3.250.0070.08014.58
5.3.240.0100.07314.82
5.3.230.0030.08014.74
5.3.220.0030.04014.78
5.3.210.0070.07014.72
5.3.200.0100.03314.67
5.3.190.0030.07314.76
5.3.180.0070.05014.71
5.3.170.0030.05314.64
5.3.160.0070.07014.68
5.3.150.0100.04014.71
5.3.140.0100.06314.75
5.3.130.0070.07014.70
5.3.120.0100.04714.56
5.3.110.0130.06714.68
5.3.100.0000.05714.23
5.3.90.0070.07714.16
5.3.80.0030.04314.24
5.3.70.0000.06314.16
5.3.60.0100.07014.15
5.3.50.0070.04314.14
5.3.40.0030.07014.09
5.3.30.0000.05714.12
5.3.20.0030.03013.85
5.3.10.0000.03313.73
5.3.00.0000.03713.83
5.2.170.0030.06011.78
5.2.160.0030.04011.78
5.2.150.0030.02711.78
5.2.140.0070.03711.78
5.2.130.0000.03011.78
5.2.120.0030.02711.78
5.2.110.0100.02011.78
5.2.100.0070.02711.78
5.2.90.0070.03311.78
5.2.80.0070.04011.78
5.2.70.0000.03011.78
5.2.60.0000.03011.78
5.2.50.0000.03011.78
5.2.40.0070.02311.78
5.2.30.0030.02711.78
5.2.20.0030.03011.78
5.2.10.0070.02311.78
5.2.00.0030.03011.78
5.1.60.0000.02711.78
5.1.50.0030.02311.78
5.1.40.0000.03011.78
5.1.30.0030.03011.78
5.1.20.0000.03011.78
5.1.10.0100.02011.78
5.1.00.0030.02311.78
5.0.50.0000.02011.78
5.0.40.0030.03711.78
5.0.30.0030.03311.78
5.0.20.0030.01311.78
5.0.10.0030.02011.78
5.0.00.0000.02711.78
4.4.90.0000.02011.78
4.4.80.0000.01711.78
4.4.70.0000.01711.78
4.4.60.0000.01311.78
4.4.50.0000.01711.78
4.4.40.0000.02311.78
4.4.30.0000.01711.78
4.4.20.0000.01711.78
4.4.10.0000.01311.78
4.4.00.0000.02311.78
4.3.110.0030.01311.78
4.3.100.0000.01311.78
4.3.90.0030.02011.78
4.3.80.0000.02311.78
4.3.70.0000.01311.78
4.3.60.0030.01311.78
4.3.50.0000.01711.78
4.3.40.0000.02311.78
4.3.30.0000.02011.78
4.3.20.0000.01711.78
4.3.10.0000.01711.78
4.3.00.0000.01311.78

preferences:
47 ms | 400 KiB | 5 Q