|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace duxet\Rethinkdb; |
|
4
|
|
|
|
|
5
|
|
|
use r; |
|
6
|
|
|
|
|
7
|
|
|
class Query |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The connection instance. |
|
11
|
|
|
* |
|
12
|
|
|
* @var Connection |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $connection; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The \r\ValuedQuery instance. |
|
18
|
|
|
* |
|
19
|
|
|
* @var \r\ValuedQuery |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $query; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Connection $connection |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Connection $connection) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->connection = $connection; |
|
31
|
|
|
$this->query = r\Db($connection->getDatabaseName()); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Handle dynamic method calls. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $method |
|
38
|
|
|
* @param array $parameters |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __call($method, $parameters) |
|
43
|
|
|
{ |
|
44
|
|
|
$autoRun = ['count', 'sum', 'insert']; |
|
45
|
|
|
|
|
46
|
|
|
$query = call_user_func_array([$this->query, $method], $parameters); |
|
47
|
|
|
|
|
48
|
|
|
if (in_array($method, $autoRun)) { |
|
49
|
|
|
return $this->run($query); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->query = $query; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function run($query = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$start = microtime(true); |
|
60
|
|
|
$query = $query ?: $this->query; |
|
61
|
|
|
|
|
62
|
|
|
$connection = $this->connection->getConnection(); |
|
63
|
|
|
$result = $query->run($connection); |
|
64
|
|
|
|
|
65
|
|
|
$query = strval($this->query); |
|
66
|
|
|
$time = $this->connection->getElapsedTime($start); |
|
67
|
|
|
$this->connection->logQuery($query, [], $time); |
|
68
|
|
|
|
|
69
|
|
|
return $this->nativeArray($result); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function nativeArray($val) |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_array($val)) { |
|
75
|
|
|
foreach ($val as $k => $v) { |
|
76
|
|
|
$val[$k] = $this->nativeArray($v); |
|
77
|
|
|
} |
|
78
|
|
|
return $val; |
|
79
|
|
|
} elseif (is_object($val) && $val instanceof \ArrayObject) { |
|
80
|
|
|
return $val->getArrayCopy(); |
|
81
|
|
|
} else { |
|
82
|
|
|
return $val; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..