|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ActiveRecord for API. |
|
4
|
|
|
* |
|
5
|
|
|
* @see https://github.com/hiqdev/yii2-hiart |
|
6
|
|
|
* @package yii2-hiart |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\Component; |
|
15
|
|
|
use yii\caching\Cache; |
|
16
|
|
|
use yii\caching\Dependency; |
|
17
|
|
|
use yii\httpclient\Request; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The Command class implements execution of request. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Command extends Component |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Connection |
|
26
|
|
|
*/ |
|
27
|
|
|
public $db; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
2 |
|
* @var Request request object |
|
31
|
|
|
*/ |
|
32
|
2 |
|
protected $request; |
|
33
|
|
|
|
|
34
|
2 |
|
/** |
|
35
|
|
|
* @var int the default number of seconds that query results can remain valid in cache. |
|
36
|
|
|
* Use 0 to indicate that the cached data will never expire. And use a negative number to indicate |
|
37
|
|
|
* query cache should not be used. |
|
38
|
|
|
* @see cache() |
|
39
|
|
|
*/ |
|
40
|
|
|
public $queryCacheDuration; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Dependency the dependency to be associated with the cached query result for this command |
|
44
|
2 |
|
* @see cache() |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public $queryCacheDependency; |
|
47
|
|
|
|
|
48
|
2 |
|
public function getRequest() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->request; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function setRequest(Request $request) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->request = $request; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function queryInternal($default = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$rawRequest = $this->request->toString(); |
|
63
|
|
|
|
|
64
|
|
|
$info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency); |
|
65
|
|
|
if (is_array($info)) { |
|
66
|
|
|
/* @var $cache Cache */ |
|
67
|
|
|
$cache = $info[0]; |
|
68
|
|
|
$cacheKey = [ |
|
69
|
|
|
__CLASS__, |
|
70
|
|
|
$this->db->baseUrl, |
|
71
|
|
|
$rawRequest, |
|
72
|
|
|
]; |
|
73
|
|
|
$result = $cache->get($cacheKey); |
|
74
|
|
|
if (is_array($result) && isset($result[0])) { |
|
75
|
|
|
Yii::trace("Query result served from cache:\n$rawRequest", __METHOD__); |
|
76
|
|
|
return $result[0]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
Yii::beginProfile($rawRequest, __METHOD__); |
|
81
|
|
|
|
|
82
|
|
|
$response = $this->db->send($this->request); |
|
83
|
|
|
|
|
84
|
|
|
Yii::endProfile($rawRequest, __METHOD__); |
|
85
|
|
|
|
|
86
|
|
|
if ($response->isOk) { |
|
87
|
|
|
$result = $response->getData(); |
|
88
|
|
|
if (isset($cache, $cacheKey, $info)) { |
|
89
|
|
|
$cache->set($cacheKey, [$result], $info[1], $info[2]); |
|
90
|
|
|
Yii::trace('Saved query result in cache', __METHOD__); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $default; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function queryOne() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->queryInternal(null); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function queryAll() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->queryInternal([]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function count() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->queryInternal(0); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
2 |
|
* Enables query cache for this command. |
|
116
|
|
|
* @param int $duration the number of seconds that query result of this command can remain valid in the cache. |
|
117
|
2 |
|
* If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead. |
|
118
|
2 |
|
* Use 0 to indicate that the cached data will never expire. |
|
119
|
2 |
|
* @param Dependency $dependency the cache dependency associated with the cached query result |
|
120
|
2 |
|
* @return $this the command object itself |
|
121
|
2 |
|
*/ |
|
122
|
2 |
|
public function cache($duration = null, $dependency = null) |
|
123
|
|
|
{ |
|
124
|
2 |
|
$this->queryCacheDuration = $duration === null ? $this->db->queryCacheDuration : $duration; |
|
125
|
|
|
$this->queryCacheDependency = $dependency; |
|
126
|
|
|
return $this; |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
/** |
|
130
|
|
|
* Disables query cache for this command. |
|
131
|
|
|
* @return $this the command object itself |
|
132
|
|
|
*/ |
|
133
|
|
|
public function noCache() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->queryCacheDuration = -1; |
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|