forked from usmanhalalit/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryObject.php
More file actions
96 lines (81 loc) · 2.07 KB
/
QueryObject.php
File metadata and controls
96 lines (81 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php namespace Pixie\QueryBuilder;
class QueryObject
{
/**
* @var string
*/
protected $sql;
/**
* @var array
*/
protected $bindings = array();
/**
* @var \PDO
*/
protected $pdo;
public function __construct($sql, array $bindings, \PDO $pdo)
{
$this->sql = (string)$sql;
$this->bindings = $bindings;
$this->pdo = $pdo;
}
/**
* @return string
*/
public function getSql()
{
return $this->sql;
}
/**
* @return array
*/
public function getBindings()
{
return $this->bindings;
}
/**
* Get the raw/bound sql
*
* @return string
*/
public function getRawSql()
{
return $this->interpolateQuery($this->sql, $this->bindings);
}
/**
* Replaces any parameter placeholders in a query with the value of that
* parameter. Useful for debugging. Assumes anonymous parameters from
* $params are are in the same order as specified in $query
*
* Reference: http://stackoverflow.com/a/1376838/656489
*
* @param string $query The sql query with parameter placeholders
* @param array $params The array of substitution parameters
*
* @return string The interpolated query
*/
protected function interpolateQuery($query, $params)
{
$keys = array();
$values = $params;
# build a regular expression for each parameter
foreach ($params as $key => $value) {
if (is_string($key)) {
$keys[] = '/:' . $key . '/';
} else {
$keys[] = '/[?]/';
}
if (is_string($value)) {
$values[$key] = $this->pdo->quote($value);
}
if (is_array($value)) {
$values[$key] = implode(',', $this->pdo->quote($value));
}
if (is_null($value)) {
$values[$key] = 'NULL';
}
}
$query = preg_replace($keys, $values, $query, 1, $count);
return $query;
}
}