Skip to content

Commit

Permalink
MySQL, PostgreSQL: Fix queries splitting and string constants
Browse files Browse the repository at this point in the history
Thanks to alxivnov (vrana#490).
  • Loading branch information
peterpp committed Sep 20, 2024
1 parent aee800e commit 53df486
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions adminer/drivers/mssql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,10 @@ function is_strict_mode() {
return false;
}

function is_c_style_escapes() {
return true;
}

function show_status() {
return array();
}
Expand Down
10 changes: 10 additions & 0 deletions adminer/drivers/mysql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,16 @@ function is_strict_mode() {
return $strictMode;
}

function is_c_style_escapes() {
static $c_style = null;

if ($c_style === null) {
$c_style = strpos(get_key_vals("SHOW VARIABLES LIKE 'sql_mode'")["sql_mode"], 'NO_BACKSLASH_ESCAPES') === false;
}

return $c_style;
}

/** Get process list
* @return array ($row)
*/
Expand Down
4 changes: 4 additions & 0 deletions adminer/drivers/oracle.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ function is_strict_mode() {
return false;
}

function is_c_style_escapes() {
return true;
}

function process_list() {
return get_rows('SELECT sess.process AS "process", sess.username AS "user", sess.schemaname AS "schema", sess.status AS "status", sess.wait_class AS "wait_class", sess.seconds_in_wait AS "seconds_in_wait", sql.sql_text AS "sql_text", sess.machine AS "machine", sess.port AS "port"
FROM v$session sess LEFT OUTER JOIN v$sql sql
Expand Down
11 changes: 11 additions & 0 deletions adminer/drivers/pgsql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,16 @@ function is_strict_mode() {
return false;
}

function is_c_style_escapes() {
static $c_style = null;

if ($c_style === null) {
$c_style = get_vals("SHOW standard_conforming_strings")[0] == "off";
}

return $c_style;
}

function process_list() {
return get_rows("SELECT * FROM pg_stat_activity ORDER BY " . (min_version(9.2) ? "pid" : "procpid"));
}
Expand Down Expand Up @@ -949,6 +959,7 @@ function driver_config() {
"char|text" => "||",
)
),
'c_style_escapes' => true,
);
}
}
4 changes: 4 additions & 0 deletions adminer/drivers/sqlite.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,10 @@ function is_strict_mode() {
return false;
}

function is_c_style_escapes() {
return true;
}

function show_status() {
$return = array();
foreach (get_vals("PRAGMA compile_options") as $option) {
Expand Down
11 changes: 10 additions & 1 deletion adminer/sql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@
$offset = $pos + strlen($found);

if ($found && rtrim($found) != $delimiter) { // find matching quote or comment end
while (preg_match('(' . ($found == '/*' ? '\*/' : ($found == '[' ? ']' : (preg_match('~^-- |^#~', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES
$c_style_escape = is_c_style_escapes() || ($jush == "pgsql" && ($pos > 0 && strtolower($query[$pos - 1]) == "e"));

$pattern = '(' .
($found == '/*' ? '\*/' :
($found == '[' ? ']' :
(preg_match('~^-- |^#~', $found) ? "\n" :
(preg_quote($found) . ($c_style_escape ? "|\\\\." : ""))))) .
'|$)s';

while (preg_match($pattern, $query, $match, PREG_OFFSET_CAPTURE, $offset)) {
$s = $match[0][0];
if (!$s && $fp && !feof($fp)) {
$query .= fread($fp, 1e5);
Expand Down

0 comments on commit 53df486

Please sign in to comment.