Skip to content

Instantly share code, notes, and snippets.

@NKame
Created October 9, 2012 21:55
Show Gist options
  • Select an option

  • Save NKame/3861698 to your computer and use it in GitHub Desktop.

Select an option

Save NKame/3861698 to your computer and use it in GitHub Desktop.
Simple interpolation des paramètres dans une requête SQL, requête paramétrée et paramètres étant récupérés dans les traces Hibernate (org.hibernate.SQL et org.hibernate.type). Ne gère pas grand chose...
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/hml;charset=UTF-8">
<title>Reconstitution requête Hibernate</title>
</head>
<body>
<form method="POST" action="?">
<textarea name="content" cols="130" rows="50">
<?=isset($_POST['content'])?$_POST['content']:''?>
</textarea>
<input type="submit">
</form>
<pre>
<?php
if(isset($_POST['content'])) {
$arr = array();
preg_match("/^.*binding '.*$/m", $_POST['content'], $arr, PREG_OFFSET_CAPTURE);
$query = substr($_POST['content'], 0, $arr[0][1]);
$bindingsDecl = substr($_POST['content'], $arr[0][1]);
$bindings = array();
preg_match_all("/([A-Za-z]+)] binding '([^']+)' to parameter: ([0-9]+)/m", $bindingsDecl, $arr, PREG_SET_ORDER);
foreach($arr as $match) {
$value = $match[2];
if($match[1] === 'StringType') {
$value = "'$value'";
}
$bindings[intval($match[3], 10)] = $value;
}
ksort($bindings);
reset($bindings);
$newQuery = preg_replace_callback('/\?/', function ($match) use (&$bindings) {
list($k,$v) = each ($bindings);
return $v;
}, $query);
echo $newQuery;
}
?>
</pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment