|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of laravel-quota |
|
5
|
|
|
* |
|
6
|
|
|
* (c) David Faith <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Full copyright and license information is available |
|
9
|
|
|
* in the LICENSE file distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Projectmentor\Quota\Console\Commands; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Console\Command; |
|
15
|
|
|
|
|
16
|
|
|
use DB; |
|
17
|
|
|
|
|
18
|
|
|
class ResetQuotaLog extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The name and signature of the console command. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $signature = 'quota:reset {date} {connection} {hits=0} {misses=0}'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The console command description. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $description = 'Reset the quotalog counters for a date and connection.'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new command instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
8 |
|
parent::__construct(); |
|
42
|
8 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Execute the console command. |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
8 |
|
public function handle() |
|
50
|
|
|
{ |
|
51
|
8 |
|
switch(DB::connection()->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME)) |
|
52
|
|
|
{ |
|
53
|
8 |
|
case 'mysql': |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$sql = 'INSERT INTO quotalog' . |
|
56
|
|
|
' (date, connection, hits, misses, created_at, updated_at)' . |
|
57
|
|
|
' VALUES' . |
|
58
|
|
|
' ( :date, :connection, :hits, :misses, :created_at, :updated_at)' . |
|
59
|
|
|
' ON DUPLICATE KEY UPDATE' . |
|
60
|
|
|
' hits = VALUES(hits),' . |
|
61
|
|
|
' misses = VALUES(misses),' . |
|
62
|
|
|
' updated_at = VALUES(updated_at)'; |
|
63
|
|
|
|
|
64
|
|
|
$this->doUpsertStatement($sql); |
|
65
|
|
|
|
|
66
|
|
|
break; |
|
67
|
8 |
|
case 'sqlite': |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
8 |
|
$date = $this->argument('date'); |
|
|
|
|
|
|
70
|
8 |
|
$connection = $this->argument('connection'); |
|
71
|
|
|
|
|
72
|
|
|
$sql = 'UPDATE quotalog' . |
|
73
|
8 |
|
' SET `date` = :date' . |
|
74
|
8 |
|
', `connection` = :connection' . |
|
75
|
8 |
|
', `hits` = :hits' . |
|
76
|
8 |
|
', `misses` = :misses' . |
|
77
|
8 |
|
', `updated_at` = :updated_at' . |
|
78
|
8 |
|
' WHERE `date` = ' . '\''. $date . '\'' . |
|
79
|
8 |
|
' AND `connection` = ' . '\'' . $connection . '\''; |
|
80
|
|
|
|
|
81
|
8 |
|
$this->doUpdateStatement($sql); |
|
82
|
|
|
|
|
83
|
8 |
|
$changes = DB::select('SELECT changes()'); |
|
84
|
8 |
|
if(! $this->sqliteUpdateDidChange($changes)) |
|
85
|
8 |
|
{ |
|
86
|
|
|
$sql = 'INSERT INTO quotalog' . |
|
87
|
8 |
|
' (' . |
|
88
|
8 |
|
' `date`' . |
|
89
|
8 |
|
', `connection`' . |
|
90
|
8 |
|
', `hits`' . |
|
91
|
8 |
|
', `misses`' . |
|
92
|
8 |
|
', `created_at`' . |
|
93
|
8 |
|
', `updated_at`' . |
|
94
|
8 |
|
')' . |
|
95
|
8 |
|
' VALUES' . |
|
96
|
8 |
|
' (' . |
|
97
|
8 |
|
' :date' . |
|
98
|
8 |
|
', :connection' . |
|
99
|
8 |
|
', :hits' . |
|
100
|
8 |
|
', :misses' . |
|
101
|
8 |
|
', :created_at' . |
|
102
|
8 |
|
', :updated_at' . |
|
103
|
8 |
|
')'; |
|
104
|
|
|
|
|
105
|
8 |
|
$this->doUpsertStatement($sql); |
|
106
|
8 |
|
} |
|
107
|
8 |
|
break; |
|
108
|
|
|
|
|
109
|
|
|
default: |
|
110
|
|
|
throw new \ErrorException( |
|
111
|
|
|
__CLASS__.'::'.__FUNCTION__. |
|
112
|
|
|
' Driver: ' . $driver . ' not supported.'); |
|
113
|
8 |
|
} |
|
114
|
8 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Helper. avoid duplicate code |
|
118
|
|
|
* Run sqlite3 update statement. |
|
119
|
|
|
* which must have same number of params |
|
120
|
|
|
* as sql bindings. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $sql |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
8 |
View Code Duplication |
protected function doUpdateStatement($sql) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
8 |
|
$now = \Carbon\Carbon::now()->toDateTimeString(); |
|
128
|
|
|
|
|
129
|
8 |
|
DB::statement($sql, [ |
|
130
|
8 |
|
'date' => $this->argument('date'), |
|
131
|
8 |
|
'connection' => $this->argument('connection'), |
|
132
|
8 |
|
'hits' => $this->argument('hits'), |
|
133
|
8 |
|
'misses' => $this->argument('misses'), |
|
134
|
|
|
'updated_at' => $now |
|
135
|
8 |
|
]); |
|
136
|
8 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Helper. avoid duplicate code |
|
140
|
|
|
* Run insert or update statement. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $sql |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
8 |
View Code Duplication |
protected function doUpsertStatement($sql) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
8 |
|
$now = \Carbon\Carbon::now()->toDateTimeString(); |
|
148
|
|
|
|
|
149
|
8 |
|
DB::statement($sql, [ |
|
150
|
8 |
|
'date' => $this->argument('date'), |
|
151
|
8 |
|
'connection' => $this->argument('connection'), |
|
152
|
8 |
|
'hits' => $this->argument('hits'), |
|
153
|
8 |
|
'misses' => $this->argument('misses'), |
|
154
|
8 |
|
'created_at' => $now, |
|
155
|
|
|
'updated_at' => $now |
|
156
|
8 |
|
]); |
|
157
|
8 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get the status of a sqlite3 UPDATE operation |
|
161
|
|
|
* from native stdClass |
|
162
|
|
|
* |
|
163
|
|
|
* |
|
164
|
|
|
* @param array $changes e.g: |
|
165
|
|
|
* [ 0 => { "changes()": 1 }] |
|
166
|
|
|
* @return boolean |
|
167
|
|
|
*/ |
|
168
|
8 |
|
protected function sqliteUpdateDidChange(array $changes) |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
8 |
|
$status = 'changes()'; |
|
171
|
8 |
|
return $changes[0]->$status; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
|
|
|
|
|
174
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.