Permalink
Browse files

[DoctrineBridge] Refactored the query sanitization in the collector

The original parameters are kept whenever possible to allow using them
again to explain the query.
  • Loading branch information...
1 parent 318b454 commit fb009c834dd9445de57760073040b4590e5e8a43 @stof stof committed Jan 23, 2012
Showing with 49 additions and 23 deletions.
  1. +49 −23 DataCollector/DoctrineDataCollector.php
@@ -13,6 +13,7 @@
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Logging\DebugStack;
+use Doctrine\DBAL\Types\Type;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -54,7 +55,7 @@ public function collect(Request $request, Response $response, \Exception $except
{
$queries = array();
foreach ($this->loggers as $name => $logger) {
- $queries[$name] = $this->sanitizeQueries($logger->queries);
+ $queries[$name] = $this->sanitizeQueries($name, $logger->queries);
}
$this->data = array(
@@ -104,48 +105,73 @@ public function getName()
return 'db';
}
- private function sanitizeQueries($queries)
+ private function sanitizeQueries($connectionName, $queries)
{
foreach ($queries as $i => $query) {
- foreach ((array) $query['params'] as $j => $param) {
- $queries[$i]['params'][$j] = $this->varToString($param);
- }
+ $queries[$i] = $this->sanitizeQuery($connectionName, $query);
}
return $queries;
}
- private function varToString($var)
+ private function sanitizeQuery($connectionName, $query)
+ {
+ $query['explainable'] = true;
+ $query['params'] = (array) $query['params'];
+ foreach ($query['params'] as $j => &$param) {
+ if (isset($query['types'][$j])) {
+ // Transform the param according to the type
+ $type = $query['types'][$j];
+ if (is_string($type)) {
+ $type = Type::getType($type);
+ }
+ if ($type instanceof Type) {
+ $query['types'][$j] = $type->getBindingType();
+ $param = $type->convertToDatabaseValue($param, $this->registry->getConnection($connectionName)->getDatabasePlatform());
+ }
+ }
+
+ list($param, $explainable) = $this->sanitizeParam($param);
+ if (!$explainable) {
+ $query['explainable'] = false;
+ }
+ }
+
+ return $query;
+ }
+
+ /**
+ * Sanitizes a param.
+ *
+ * The return value is an array with the sanitized value and a boolean
+ * indicating if the original value was kept (allowing to use the sanitized
+ * value to explain the query).
+ *
+ * @param mixed $var
+ * @return array
+ */
+ private function sanitizeParam($var)
{
if (is_object($var)) {
- return sprintf('Object(%s)', get_class($var));
+ return array(sprintf('Object(%s)', get_class($var)), false);
}
if (is_array($var)) {
$a = array();
+ $original = true;
foreach ($var as $k => $v) {
- $a[] = sprintf('%s => %s', $k, $this->varToString($v));
+ list($value, $orig) = $this->sanitizeParam($v);
+ $original = $original && $orig;
+ $a[$k] = $value;
}
- return sprintf("Array(%s)", implode(', ', $a));
+ return array($a, $original);
}
if (is_resource($var)) {
- return sprintf('Resource(%s)', get_resource_type($var));
- }
-
- if (null === $var) {
- return 'null';
- }
-
- if (false === $var) {
- return 'false';
- }
-
- if (true === $var) {
- return 'true';
+ return array(sprintf('Resource(%s)', get_resource_type($var)), false);
}
- return (string) $var;
+ return array($var, true);
}
}

0 comments on commit fb009c8

Please sign in to comment.