$type, 'data' => $data , 'time' => $t); $timer = microtime(); } // end of the "PMA_sqlParser_ArrayAdd_Timer()" function function PMA_sqlParser_ArrayAdd(&$arr, $type, $data) { if ($GLOBALS['timing']) { PMA_sqlParser_ArrayAdd_Timer($arr, $type, $data); } else { $arr[] = array('type' => $type, 'data' => $data); } } // end of the "PMA_sqlParser_ArrayAdd()" function function PMA_sqlParser_strInStr($needle, $haystack) { return (strpos(' ' . $haystack, $needle) > 0); } // end of the "PMA_sqlParser_strInStr()" function // Checks if a given character position in the string is escaped or not function PMA_sqlParser_isEscaped($string, $pos, $start = 0) { $len = strlen($string); // Base case // Check for string length or invalid input or special case of input // (pos == $start) if ($pos == $start || $len <= $pos) { return FALSE; } $p = $pos-1; $escaped = FALSE; while (($p >= $start) && ($string[$p] == '\\')) { $escaped = !$escaped; $p--; } if ($pos < $start) { // Throw error about strings } return $escaped; } // end of the "PMA_sqlParser_isEscaped()" function function PMA_numberInRange($num, $lower, $upper) { return (($num >= $lower) && ($num <= $upper)); } // end of the "PMA_numberInRange()" function function PMA_strIsDigit($c) { $ord_zero = 48; //ord('0'); $ord_nine = 57; //ord('9'); $ord_c = ord($c); return PMA_numberInRange($ord_c, $ord_zero, $ord_nine); } // end of the "PMA_strIsDigit()" function function PMA_strHexIsDigit($c) { $ord_Aupper = 65; //ord('A'); $ord_Fupper = 70; //ord('F'); $ord_Alower = 97; //ord('a'); $ord_Flower = 102; //ord('f'); $ord_zero = 48; //ord('0'); $ord_nine = 57; //ord('9'); $ord_c = ord($c); return (PMA_numberInRange($ord_c, $ord_zero, $ord_nine) || PMA_numberInRange($ord_c, $ord_Aupper, $ord_Fupper) || PMA_numberInRange($ord_c, $ord_Alower, $ord_Flower)); } // end of the "PMA_strHexIsDigit()" function function PMA_strIsUpper($c) { $ord_zero = 65; //ord('A'); $ord_nine = 90; //ord('Z'); $ord_c = ord($c); return PMA_numberInRange($ord_c, $ord_zero, $ord_nine); } // end of the "PMA_strIsUpper()" function function PMA_strIsLower($c) { $ord_zero = 97; //ord('a'); $ord_nine = 122; //ord('z'); $ord_c = ord($c); return PMA_numberInRange($ord_c, $ord_zero, $ord_nine); } // end of the "PMA_strIsLower()" function function PMA_strIsAlpha($c) { return (PMA_strIsUpper($c) || PMA_strIsLower($c)); } // end of the "PMA_strIsAlpha()" function function PMA_strIsAlnum($c) { return (PMA_strIsUpper($c) || PMA_strIsLower($c) || PMA_strIsDigit($c)); } // end of the "PMA_strIsAlnum()" function function PMA_strIsSpace($c) { $ord_tab = 9; $ord_CR = 13; $ord_c = ord($c); return (($ord_c == 32) || PMA_numberInRange($ord_c, $ord_tab, $ord_CR)); } // end of the "PMA_strIsSpace()" function function PMA_str_isSqlIdentifier($c, $dotIsValid = FALSE) { return (PMA_strIsAlnum($c) || ($c == '_') || ($c == '$') || (($dotIsValid != FALSE) && ($c == '.'))); } // end of the "PMA_str_isSqlIdentifier()" function function PMA_strInArray($str, $arr, $arrsize) { // $arr NUST be sorted, due to binary search $top = $arrsize - 1; $bottom = 0; $found = FALSE; while (($top >= $bottom) && ($found === FALSE)) { $mid = intval(($top + $bottom) / 2); $res = strcmp($str, $arr[$mid]); if ($res == 0) { $found = TRUE; } else if ($res < 0) { $top = $mid-1; } else { $bottom = $mid + 1; } } // end while return $found; } // end of the "PMA_strInArray()" function function PMA_sqlParser_tokenizer($sql) { global $syntax_columnAttrib, $syntax_reservedWord, $syntax_columnType, $syntax_functionName; $len = strlen($sql); if ($len == 0) { return array(); } $sql_array = array(); $sql_array['raw'] = $sql; $count1 = 0; $count2 = 0; $punct_queryend = ';'; $punct_qualifier = '.'; $punct_listsep = ','; $punct_level_plus = '('; $punct_level_minus = ')'; $digit_floatdecimal = '.'; $digit_hexset = 'x'; $bracket_list = '()[]{}'; $allpunct_list = '-,;:!?/.^~\*&%+<=>|'; $allpunct_list_pair = array ( 0 => '!=', 1 => '&&', 2 => ':=', 3 => '<<', 4 => '<=', 5 => '<=>', 6 => '<>', 7 => '>=', 8 => '>>', 9 => '||'); $allpunct_list_pair_size = 10; //count($allpunct_list_pair); $quote_list = "\'\"\`"; $arraysize = 0; while ($count2 < $len) { $c = $sql[$count2]; $count1 = $count2; // Checks for white space if (PMA_strIsSpace($c)) { $count2++; continue; } // Checks for comment lines. // MySQL style # // C style /* */ // ANSI style -- if (($c == '#') || (($count2+1 < $len) && ($c == '/') && ($sql[$count2 + 1] == '*')) || (($c == '-') && ($count2 + 2 < $len) && ($sql[$count2 + 1] == '-') && ($sql[$count2 + 2] == ' '))) { $count2++; $pos = strpos($sql, "\n", $count2); $count2 = ($pos < $count2) ? $len : $pos; $str = substr($sql, $count1, $count2 - $count1); switch ($c) { case '#': $type = 'mysql'; break; case '/': $type = 'c'; break; case '-': $type = 'ansi'; break; default: $type = 'bad'; break; } // end switch PMA_sqlParser_ArrayAdd ($sql_array, 'comment_' . $type, $str); $arraysize++; continue; } // end if (checks for comment) // Checks for something inside quotation marks if (PMA_sqlParser_strInStr($c, $quote_list)) { $startquotepos = $count2; $quotetype = $c; $count2++; $escaped = FALSE; $escaped_escaped = FALSE; $pos = $count2; $oldpos = 0; do { $oldpos = $pos; $pos = strpos(' ' . $sql, $quotetype, $oldpos); if ($pos == 0) { trigger_error('Syntax: Unclosed quote (' . $quotetype . ') at ' . $startquotepos); return; } // If the quote is the first character, it can't be escaped, so don't // do the rest of the code else if ($pos == 1) { break; } else if (PMA_sqlParser_isEscaped($sql, $pos - 1)) { $pos ++; continue; } else { break; } } while ($len > $pos); // end do $count2 = $pos; $count2++; $type = 'quote_'; switch ($quotetype) { case '\'': $type .= 'single'; break; case '"': $type .= 'double'; break; case '`': $type .= 'backtick'; break; default : $type .= 'unknown'; break; } // end switch $data = substr($sql, $count1, $count2 - $count1); PMA_sqlParser_ArrayAdd($sql_array, $type, $data); $arraysize++; continue; } // end if (checks for something inside quotation marks) // Checks for brackets if (PMA_sqlParser_strInStr($c, $bracket_list)) { // All bracket tokens are only one item long $count2++; $type_type = ''; if (PMA_sqlParser_strInStr($c, '([{')) { $type_type = 'open'; } else { $type_type = 'close'; } $type_style = ''; if (PMA_sqlParser_strInStr($c, '()')) { $type_style = 'round'; } else if (PMA_sqlParser_strInStr($c, '[]')) { $type_style = 'square'; } else { $type_style = 'curly'; } $type = 'punct_bracket_' . $type_type . '_' . $type_style; PMA_sqlParser_ArrayAdd ($sql_array, $type, $c); $arraysize++; continue; } // end if (checks for brackets) // Checks for punct if (PMA_sqlParser_strInStr($c, $allpunct_list)) { while (($count2 < $len) && PMA_sqlParser_strInStr($sql[$count2], $allpunct_list)) { $count2++; } $l = $count2 - $count1; if ($l == 1) { $punct_data = $c; } else { $punct_data = substr($sql, $count1, $l); } // Special case, sometimes, althought two characters are adjectent directly, // they ACTUALLY need to be seperate if ($l == 1) { $t_suffix = ''; switch ($punct_data) { case $punct_queryend : $t_suffix = '_queryend'; break; case $punct_qualifier : $t_suffix = '_qualifier'; break; case $punct_listsep : $t_suffix = '_listsep'; break; } // end switch PMA_sqlParser_ArrayAdd($sql_array, 'punct' . $t_suffix, $punct_data); $arraysize++; } else if (PMA_strInArray($punct_data, $allpunct_list_pair, $allpunct_list_pair_size)) { // Ok, we have one of the valid combined punct expressions PMA_sqlParser_ArrayAdd($sql_array, 'punct', $punct_data); $arraysize++; } else { // Bad luck, lets split it up more $first = $punct_data[0]; $first2 = $punct_data[0] . $punct_data[1]; $last2 = $punct_data[$l - 2] . $punct_data[$l - 1]; $last = $punct_data[$l - 1]; if (($first == ',') || ($first == ';') || ($first == '.') || $first = '*') { $count2 = $count1 + 1; $punct_data = $first; } else if (($last2 == '/*') || ($last2 == '--')) { $count2 -= 2; $punct_data = substr($sql, $count1, $count2 - $count1); } else if (($last == '-') || ($last == '+') || ($last == '!')) { $count2--; $punct_data = substr($sql, $count1, $count2 - $count1); } else { trigger_error('Syntax: Unknown punctation string (' . $punct_data . ') at ' . $count1); return; } // $punct_data = substr($sql, $count1, $count2 - $count1); PMA_sqlParser_ArrayAdd($sql_array, 'punct', $punct_data); $arraysize++; continue; } continue; } // end if (checks for punct) // Checks for alpha if (PMA_str_isSqlIdentifier($c, FALSE) || ($c == '@')) { $count2++; $is_sql_variable = ($c == '@'); $is_digit = (!$is_sql_variable) && PMA_strIsDigit($c); $is_hex_digit = ($is_digit) && ($c == '0') && ($sql[$count2] == 'x'); $is_float_digit = FALSE; $is_float_digit_exponent = FALSE; if ($is_hex_digit) { $count2++; } while (($count2 < $len) && PMA_str_isSqlIdentifier($sql[$count2], $is_sql_variable || $is_digit)) { $c2 = $sql[$count2]; if ($is_sql_variable && ($c2 == '.')) { $count2++; continue; } if ($is_digit && (!$is_hex_digit) && ($c2 == '.')) { $count2++; if (!$is_float_digit) { $is_float_digit = TRUE; continue; } else { trigger_error('Syntax: Invalid Identifer (' . substr($sql, $count1, $count2 - $count1) . ') at ' . $count1); return; } } if ($is_digit && (!$is_hex_digit) && (($c2 == 'e') || ($c2 == 'E'))) { if (!$is_float_digit_exponent) { $is_float_digit_exponent = TRUE; $is_float_digit = TRUE; $count2++; continue; } else { $is_digit = FALSE; $is_float_digit = FALSE; } } if (($is_hex_digit && PMA_strHexIsDigit($c2)) || ($is_digit && PMA_strIsDigit($c2))) { $count2++; continue; } else { $is_digit = FALSE; $is_hex_digit = FALSE; } $count2++; } // end while $l = $count2 - $count1; $str = substr($sql, $count1, $l); $type = ''; if ($is_digit) { $type = 'digit'; if ($is_float_digit) { $type .= '_float'; } else if ($is_hex_digit) { $type .= '_hex'; } else { $type .= '_integer'; } } else if ($is_sql_variable != FALSE) { $type = 'alpha_variable'; } else { $type = 'alpha'; } PMA_sqlParser_ArrayAdd($sql_array, $type, $str); $arraysize++; continue; } // end if (checks for alpha) // DEBUG $count2++; echo 'Why did we get here? '.$count1.' '.$count2.' '.$len.'
'."\n"; echo 'Leftover: '.substr($sql,$count1,$count2-$count1).'
'."\n"; echo 'A: '.$count1.' '.$count2.'
'."\n"; flush(); ob_flush(); } // end while $len_column_attrib = count($syntax_columnAttrib); $len_reserved_word = count($syntax_reservedWord); $len_column_type = count($syntax_columnType); $len_function_name = count($syntax_functionName); if ($arraysize > 0) { $t_next = $sql_array[0]['type']; $t_prev = NULL; } for ($i = 0; $i < $arraysize; $i++) { $t_prev = $t_cur; $t_cur = $t_next; if (($i + 1) < $arraysize) { $t_next = $sql_array[$i+1]['type']; } else { $t_next = NULL; } if ($t_cur == 'alpha') { $t_suffix = '_identifier'; $d_cur_upper = strtoupper($sql_array[$i]['data']); if (($t_next == 'punct_qualifier') || ($t_prev == 'punct_qualifier')) { // $t_suffix = '_identifier'; } else if (($t_next == 'punct_bracket_open_round') && PMA_strInArray($d_cur_upper, $syntax_functionName, $len_function_name)) { $t_suffix = '_functionName'; } else if (PMA_strInArray($d_cur_upper, $syntax_reservedWord, $len_reserved_word)) { $t_suffix = '_reservedWord'; } else if (PMA_strInArray($d_cur_upper, $syntax_columnType, $len_column_type)) { $t_suffix = '_columnType'; } else if (PMA_strInArray($d_cur_upper, $syntax_columnAttrib, $len_column_attrib)) { $t_suffix = '_columnAttrib'; } $sql_array[$i]['type'] .= $t_suffix; } // end if } // end for $sql_array['len'] = $arraysize; return $sql_array; } // end of the "PMA_sqlParser_tokenizer()" function function PMA_sqlParser_extractData($arr) { $result = array('list_db' => array(), 'list_tbl' => array(), 'list_tbl_alias' => array(), 'list_col' => array(), 'list_col_alias' => array(), '' => array() ); $i = 0; } // end of the "PMA_sqlParser_extractData()" function function PMA_sqlParser_format_colorize($arr) { $i = strpos($arr['type'], '_'); $class = ''; if ($i > 0) { $class = 'syntax_' . substr($arr['type'], 0, $i) . ' '; } $class .= 'syntax_' . $arr['type']; return '' . htmlspecialchars($arr['data']) . ''; } // end of the "PMA_sqlParser_format_colorize()" function function PMA_sqlParser_format($arr) { $str = ''; $indent = 0; $bracket_level = 0; $function_level = 0; $in_function = FALSE; $space_punct_listsep = ' '; $space_punct_listsep_function_name = ' '; $space_alpha_reserved_word = '
' . "\n"; $keywords_with_brackets = array('INDEX', 'INTO', 'KEY', 'PRIMARY', 'REFERENCES', 'UNIQUE'); $keywords_with_brackets_size = count($keywords_with_brackets); $arraysize = $arr['len']; $typearr = array(); if ($arraysize >= 11111111111) { /* array_push($typearr,NULL); array_push($typearr,NULL); array_push($typearr,NULL); array_push($typearr,$arr[0]['type']); array_push($typearr,$arr[1]['type']); */ $typearr[0] = NULL; $typearr[1] = NULL; $typearr[2] = NULL; $typearr[3] = $arr[0]['type']; } for ($i = 0; $i < $arraysize; $i++) { $before = ''; $after = ''; $indent = 0; // array_shift($typearr); /* 0 prev2 1 prev 2 current 3 next */ if (($i + 1) < $arraysize) { // array_push($typearr, $arr[$i + 1]['type']); $typearr[4] = $arr[$i + 1]['type']; } else { // array_push($typearr,NULL); $typearr[4] = NULL; } for ($j = 0; $j < 4; $j++) { $typearr[$j] = $typearr[$j + 1]; } switch ($typearr[2]) { case 'punct_bracket_open_round': $bracket_level++; $in_function = FALSE; // Make sure this array is sorted! if (($typearr[1] == 'alpha_functionName') || ($typearr[1] == 'alpha_columnType') || ($typearr[1] == 'punct') || ($typearr[3] == 'digit_integer') || ($typearr[3] == 'digit_hex') || ($typearr[3] == 'digit_float') || (($typearr[0] == 'alpha_reservedWord' ) && (PMA_strInArray(strtoupper($arr[$i - 2]['data']), $keywords_with_brackets, $keywords_with_brackets_size)))) { $function_level++; $in_function = TRUE; $after .= ' '; } else { $indent++; $after .= '
' . "\n"; } break; case 'punct_qualifier': break; case 'punct_listsep': if ($in_function == TRUE) { $after .= $space_punct_listsep_function_name; } else { $after .= $space_punct_listsep; } break; case 'punct_queryend': $after .= '
' . "\n"; break; case 'comment': if ($t_next == 'comment') { $after .= '
' . "\n"; } break; case 'punct_bracket_close_round': $bracket_level--; if ($in_function == TRUE) { $function_level--; $after .= ' '; } else { $indent--; $before .= '
'; } $in_function = ($function_level > 0) ? TRUE : FALSE; break; case 'alpha_reservedWord': if (($typearr[1] != 'alpha_reservedWord') && ($typearr[1] != 'punct_level_plus')) { $before .= $space_alpha_reserved_word; } switch (strtoupper($arr[$i]['data'])) { case 'CREATE': case 'UPDATE': $space_punct_listsep = '
' . "\n"; $space_alpha_reserved_word = ' '; break; case 'INSERT': $space_punct_listsep = '
' . "\n"; $space_alpha_reserved_word = '
' . "\n"; break; case 'VALUES': $space_punct_listsep = ' '; case 'SELECT': $space_punct_listsep = ' '; $space_alpha_reserved_word = '
' . "\n"; break; } // end switch (strtoupper($arr[$i]['data'])) $after .= ' '; break; default: if ($typearr[3] != 'punct_qualifier') { $after .= ' '; } } // end switch ($typearr[2]) { $str .= $before . PMA_sqlParser_format_colorize($arr[$i]) . $after; } // end for return $str; } // end of the "PMA_sqlParser_format()" function /* ---------------------------------------------------------------------------------------- * * Test harness beyond this point * * ---------------------------------------------------------------------------------------- */ function microtime2double($m) { $frap = substr($m, 0, 10); $intp = substr($m, 11); return doubleval($intp) + doubleval($frap); } // end of the "microtime2double()" function function timing($sql) { $s1 = microtime(); $arr = PMA_sqlParser_tokenizer($sql); $e1 = microtime(); $s2 = microtime(); $str = PMA_sqlParser_format($arr); $e2 = microtime(); $s1 = microtime2double($s1); $e1 = microtime2double($e1); $s2 = microtime2double($s2); $e2 = microtime2double($e2); echo 'Explode: ' . ($e1 - $s1) . '
' . "\n"; echo 'Format: ' . ($e2 - $s2) . '
' . "\n"; $t = microtime2double($arr[0]['time']); $threshold = 0.0005; $totalcount = array ( 'alpha' => 0, 'alpha_columnAttrib' => 0, 'alpha_columnType' => 0, 'alpha_functionName' => 0, 'alpha_identifier' => 0, 'alpha_reservedWord' => 0, 'alpha_variable' => 0, 'comment_bad' => 0, 'comment_mysql' => 0, 'comment_c' => 0, 'comment_ansi' => 0, 'digit_float' => 0, 'digit_hex' => 0, 'digit_integer' => 0, 'punct' => 0, 'punct_bracket_open_round' => 0, 'punct_bracket_close_round' => 0, 'punct_listsep' => 0, 'punct_qualifier' => 0, 'punct_queryend' => 0, 'quote_backtick' => 0, 'quote_double' => 0, 'quote_single' => 0 ); ksort($totalcount); $threscount = $totalcount; $totaltime = $totalcount; $arraysize = $arr['len']; for ($i = 0; $i < $arraysize; $i++) { $t2 = microtime2double($arr[$i]['time']); if ($arr[$i]['type'] == '') { echo "BAD! $i\n
\n"; } $elaps = ($t2 - $t); $totaltime[$arr[$i]['type']] += $elaps; if ($elaps >= $threshold) { $threscount[$arr[$i]['type']]++; } $totalcount[$arr[$i]['type']]++; $t = $t2; } // end for echo '' . "\n"; $timetotal = 0; while (list($key, $data) = each($totalcount)) { echo "\n" . " \n"; $timetotal += $totaltime[$key]; echo " \n"; echo " \n"; if ($data > 0) { echo ' ' . "\n"; } echo ' ' . "\n"; if ($data > 0) { echo ' ' . "\n"; } echo '' . "\n"; echo '
$key$data$threscount[$key]' . ($threscount[$key] / $data) . '' . (($totaltime[$key]) * 1000) . 'ms' . ($totaltime[$key]/$data*1000) . 'ms
' . "\n"; echo 'Total time to parse: ' . $timetotal . 's
' . "\n"; echo "Parsed:
\n" . $str; } // end while } // end of the "timing()" function function validateSQL($sql) { $srv = new SQLValidator(); $srv->start(); return $srv->ValidationString($sql); } // end of the "validateSQL()" function function test($sql,$type) { echo '
' . "\n"; switch ($type) { case 'explode' : echo "
\n";
            print_r(PMA_sqlParser_tokenizer($sql)) . "\n";					
            echo "
\n"; break; case 'format' : echo "Parsed:
\n" . PMA_sqlParser_format(PMA_sqlParser_tokenizer($sql)); break; case 'size' : echo 'Size: ' . strlen($sql) . "
\n"; break; case 'raw' : echo "Raw:
\n
\n$sql\n
\n"; break; case 'timing' : timing($sql); break; case 'mimer' : echo '
' . "\n"; echo ' ' . validateSQL($sql); echo '
' . "\n"; break; } // end switch } // end of the "PMA_sqlParser_format()" function function getDirList($dirName) { $d = dir('query'); $arr = array( ); while (FALSE != ($entry = $d->read())) { if ($entry != '.' && $entry != '..') { $arr[] = $entry; } } $d->close(); sort($arr); return $arr; } // end of the "getDirList()" function $arr = getDirList('query'); $q = isset($_GET['q']) ? $_GET['q'] : $arr[0]; $t = isset($_GET['t']) ? $_GET['t'] : 'f'; $textbox = isset($_GET['textinputbox']) ? $_GET['textinputbox'] : ''; ?> View Source of sqlparse.php
View Source of data.php
Query :
Type:
Custom Query: