mirror of
https://github.com/processwire/processwire.git
synced 2025-08-16 11:44:42 +02:00
Update DatabaseQuery class to support a getDebugQuery() method that returns the query (string) with all bind variables populated into the SQL
This commit is contained in:
@@ -77,7 +77,8 @@ abstract class DatabaseQuery extends WireData {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected $bindOptions = array(
|
protected $bindOptions = array(
|
||||||
'prefix' => 'pw', // prefix for auto-generated keys
|
'prefix' => 'pw', // prefix for auto-generated global keys
|
||||||
|
'suffix' => 'X', // 1-character suffix for auto-generated keys
|
||||||
'global' => false // globally unique among all bind keys in all instances?
|
'global' => false // globally unique among all bind keys in all instances?
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -249,12 +250,11 @@ abstract class DatabaseQuery extends WireData {
|
|||||||
*/
|
*/
|
||||||
public function getUniqueBindKey(array $options = array()) {
|
public function getUniqueBindKey(array $options = array()) {
|
||||||
|
|
||||||
static $alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
||||||
|
|
||||||
if(empty($options['key'])) {
|
if(empty($options['key'])) {
|
||||||
// auto-generate key
|
// auto-generate key
|
||||||
$key = ':';
|
$key = ':';
|
||||||
$prefix = (isset($options['prefix']) ? $options['prefix'] : $this->bindOptions['prefix']);
|
$prefix = (isset($options['prefix']) ? $options['prefix'] : $this->bindOptions['prefix']);
|
||||||
|
$suffix = isset($option['suffix']) && $options['suffix'] ? $options['suffix'] : $this->bindOptions['suffix'];
|
||||||
$value = isset($options['value']) ? $options['value'] : null;
|
$value = isset($options['value']) ? $options['value'] : null;
|
||||||
$global = isset($options['global']) ? $options['global'] : $this->bindOptions['global'];
|
$global = isset($options['global']) ? $options['global'] : $this->bindOptions['global'];
|
||||||
|
|
||||||
@@ -262,30 +262,33 @@ abstract class DatabaseQuery extends WireData {
|
|||||||
|
|
||||||
if($value !== null) {
|
if($value !== null) {
|
||||||
if(is_int($value)) {
|
if(is_int($value)) {
|
||||||
$key .= "int";
|
$key .= "i";
|
||||||
} else if(is_string($value)) {
|
} else if(is_string($value)) {
|
||||||
$key .= "str";
|
$key .= "s";
|
||||||
} else if(is_array($value)) {
|
} else if(is_array($value)) {
|
||||||
$key .= "arr";
|
$key .= "a";
|
||||||
} else {
|
} else {
|
||||||
$key .= "oth";
|
$key .= "o";
|
||||||
}
|
}
|
||||||
} else if($prefix && !$global) {
|
} else if($prefix && !$global) {
|
||||||
$key .= $prefix;
|
$key .= $prefix;
|
||||||
|
} else {
|
||||||
|
$key .= "v";
|
||||||
}
|
}
|
||||||
|
|
||||||
$k = $key;
|
|
||||||
$n = 0;
|
$n = 0;
|
||||||
while(isset($this->bindKeys[$key])) {
|
$k = $key;
|
||||||
$key = $k . (isset($alpha[$n]) ? $alpha[$n] : $n);
|
$key = $k . '0' . $suffix;
|
||||||
$n++;
|
|
||||||
|
while(isset($this->bindKeys[$key]) && ++$n) {
|
||||||
|
$key = $k . $n . $suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// provided key, make sure it is valid and unique
|
// provided key, make sure it is valid and unique (this part is not typically used)
|
||||||
$key = ltrim($options['key'], ':');
|
$key = ltrim($options['key'], ':') . 'X';
|
||||||
if(!ctype_alnum(str_replace('_', '', $key))) $key = $this->wire('database')->escapeCol($key);
|
if(!ctype_alnum(str_replace('_', '', $key))) $key = $this->wire('database')->escapeCol($key);
|
||||||
if(empty($key) || ctype_digit($key) || isset($this->bindKeys[":$key"])) {
|
if(empty($key) || ctype_digit($key[0]) || isset($this->bindKeys[":$key"])) {
|
||||||
// if key is not valid, then auto-generate one instead
|
// if key is not valid, then auto-generate one instead
|
||||||
unset($options['key']);
|
unset($options['key']);
|
||||||
$key = $this->getUniqueBindKey($options);
|
$key = $this->getUniqueBindKey($options);
|
||||||
@@ -331,7 +334,9 @@ abstract class DatabaseQuery extends WireData {
|
|||||||
|
|
||||||
if(!empty($options['inSQL'])) {
|
if(!empty($options['inSQL'])) {
|
||||||
foreach(array_keys($bindValues) as $bindKey) {
|
foreach(array_keys($bindValues) as $bindKey) {
|
||||||
if(!preg_match('/' . $bindKey . '\b/', $options['inSQL'])) {
|
if(strpos($options['inSQL'], $bindKey) === false) {
|
||||||
|
unset($bindValues[$bindKey]);
|
||||||
|
} else if(!preg_match('/' . $bindKey . '\b/', $options['inSQL'])) {
|
||||||
unset($bindValues[$bindKey]);
|
unset($bindValues[$bindKey]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -577,10 +582,31 @@ abstract class DatabaseQuery extends WireData {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the SQL query based on everything set in this DatabaseQuery object
|
* Generate the SQL query based on everything set in this DatabaseQuery object
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
abstract public function getQuery();
|
abstract public function getQuery();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get SQL query with bind params populated for debugging purposes (not to be used as actual query)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function getDebugQuery() {
|
||||||
|
$sql = $this->getQuery();
|
||||||
|
$suffix = $this->bindOptions['suffix'];
|
||||||
|
foreach($this->bindValues as $bindKey => $bindValue) {
|
||||||
|
if($bindKey[strlen($bindKey)-1] === $suffix) {
|
||||||
|
$sql = str_replace($bindKey, $bindValue);
|
||||||
|
} else {
|
||||||
|
$sql = preg_replace('/' . $bindKey . '\b/', $bindValue, $sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return generated SQL for entire query or specific method
|
* Return generated SQL for entire query or specific method
|
||||||
*
|
*
|
||||||
|
@@ -126,8 +126,9 @@ class FieldsTableTools extends Wire {
|
|||||||
// remove requested
|
// remove requested
|
||||||
if($uniqueIndexName === $requireIndexName) {
|
if($uniqueIndexName === $requireIndexName) {
|
||||||
// remove the unique index
|
// remove the unique index
|
||||||
|
$sql = "ALTER TABLE $table DROP INDEX `$requireIndexName`";
|
||||||
try {
|
try {
|
||||||
$result = $database->exec("ALTER TABLE $table DROP INDEX `$requireIndexName`");
|
$result = $database->exec($sql) !== false;
|
||||||
if($result) $action = 'remove';
|
if($result) $action = 'remove';
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
$result = false;
|
$result = false;
|
||||||
@@ -144,7 +145,7 @@ class FieldsTableTools extends Wire {
|
|||||||
$col = $database->escapeCol($col);
|
$col = $database->escapeCol($col);
|
||||||
$sql = "ALTER TABLE $table ADD UNIQUE `$requireIndexName` (`$col`)";
|
$sql = "ALTER TABLE $table ADD UNIQUE `$requireIndexName` (`$col`)";
|
||||||
try {
|
try {
|
||||||
$result = $database->exec($sql);
|
$result = $database->exec($sql) !== false;
|
||||||
if($result) $action = 'add';
|
if($result) $action = 'add';
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
$action = 'remove';
|
$action = 'remove';
|
||||||
|
Reference in New Issue
Block a user