|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace StorageTests\Database\Connect; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Builder; |
|
7
|
|
|
use CommonTestClass; |
|
8
|
|
|
use kalanis\kw_mapper\Interfaces\IDriverSources; |
|
9
|
|
|
use kalanis\kw_mapper\Interfaces\IEntryType; |
|
10
|
|
|
use kalanis\kw_mapper\Interfaces\IQueryBuilder; |
|
11
|
|
|
use kalanis\kw_mapper\MapperException; |
|
12
|
|
|
use kalanis\kw_mapper\Mappers\Database\ADatabase; |
|
13
|
|
|
use kalanis\kw_mapper\Records\ASimpleRecord; |
|
14
|
|
|
use kalanis\kw_mapper\Search\Search; |
|
15
|
|
|
use kalanis\kw_mapper\Storage\Database\Config; |
|
16
|
|
|
use kalanis\kw_mapper\Storage\Database\ConfigStorage; |
|
17
|
|
|
use kalanis\kw_mapper\Storage\Database\DatabaseSingleton; |
|
18
|
|
|
use kalanis\kw_mapper\Storage\Database\Dialects; |
|
19
|
|
|
use kalanis\kw_mapper\Storage\Database\PDO\MSSQL; |
|
20
|
|
|
use PDO; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class MySqlTest |
|
25
|
|
|
* @package StorageTests\Database\Connect |
|
26
|
|
|
* @requires extension PDO |
|
27
|
|
|
* @requires extension pdo_mssql |
|
28
|
|
|
* @link https://sqliteonline.com/ |
|
29
|
|
|
*/ |
|
30
|
|
|
class MsSqlTest extends CommonTestClass |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var null|MSSQL */ |
|
33
|
|
|
protected $database = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws MapperException |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function setUp(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$location = getenv('MSSERVER'); |
|
41
|
|
|
$location = false !== $location ? strval($location) : '127.0.0.1' ; |
|
42
|
|
|
|
|
43
|
|
|
$port = getenv('MSPORT'); |
|
44
|
|
|
$port = false !== $port ? intval($port) : 3306 ; |
|
45
|
|
|
|
|
46
|
|
|
$user = getenv('MSUSER'); |
|
47
|
|
|
$user = false !== $user ? strval($user) : 'testing' ; |
|
48
|
|
|
|
|
49
|
|
|
$pass = getenv('MSPASS'); |
|
50
|
|
|
$pass = false !== $pass ? strval($pass) : 'testing' ; |
|
51
|
|
|
|
|
52
|
|
|
$db = getenv('MSDB'); |
|
53
|
|
|
$db = false !== $db ? strval($db) : 'testing' ; |
|
54
|
|
|
|
|
55
|
|
|
$conf = Config::init()->setTarget( |
|
56
|
|
|
IDriverSources::TYPE_PDO_MSSQL, |
|
57
|
|
|
'test_mssql_local', |
|
58
|
|
|
$location, |
|
59
|
|
|
$port, |
|
60
|
|
|
$user, |
|
61
|
|
|
$pass, |
|
62
|
|
|
$db |
|
63
|
|
|
); |
|
64
|
|
|
$conf->setParams(2400, true); |
|
65
|
|
|
ConfigStorage::getInstance()->addConfig($conf); |
|
66
|
|
|
$this->database = DatabaseSingleton::getInstance()->getDatabase($conf); |
|
67
|
|
|
$this->database->addAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @throws MapperException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testProcess(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->database->reconnect(); |
|
76
|
|
|
$this->assertFalse($this->database->exec('', [])); |
|
77
|
|
|
$this->database->reconnect(); |
|
78
|
|
|
$this->assertEmpty($this->database->query('', [])); |
|
79
|
|
|
|
|
80
|
|
|
$this->dataRefill(); |
|
81
|
|
|
|
|
82
|
|
|
$query = new Builder(); |
|
83
|
|
|
$query->setBaseTable('d_queued_commands'); |
|
84
|
|
|
$sql = new Dialects\TransactSQL(); |
|
85
|
|
|
$result = $this->database->query($sql->describe($query), []); |
|
86
|
|
|
//var_dump($result); |
|
87
|
|
|
$this->assertNotEmpty($result, 'There MUST be table from file!'); |
|
88
|
|
|
|
|
89
|
|
|
$query->addColumn('d_queued_commands', 'qc_id'); |
|
90
|
|
|
$query->addColumn('d_queued_commands', 'qc_status'); |
|
91
|
|
|
$lines = $this->database->query($sql->select($query), $query->getParams()); |
|
92
|
|
|
$this->assertEquals(6, count($lines)); |
|
93
|
|
|
//var_dump(['full dump' => $lines]); |
|
94
|
|
|
$query->addCondition('d_queued_commands', 'qc_time_start', IQueryBuilder::OPERATION_EQ, 123456); |
|
95
|
|
|
//var_dump(['query dump' => str_split($sql->select($query), 120)]); |
|
96
|
|
|
$lines = $this->database->query($sql->select($query), $query->getParams()); |
|
97
|
|
|
$this->assertEquals(5, count($lines)); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertTrue($this->database->beginTransaction()); |
|
100
|
|
|
$this->database->exec('INSERT INTO "d_queued_commands" ("qc_id", "qc_time_start", "qc_time_end", "qc_status", "qc_command") VALUES (11, 123456, 123456789, 13, \'ls -laf\');', []); |
|
101
|
|
|
$this->assertTrue($this->database->commit()); |
|
102
|
|
|
$this->assertNotEmpty($this->database->lastInsertId(), 'There must be last id!'); |
|
103
|
|
|
$this->assertEquals(1, $this->database->rowCount()); |
|
104
|
|
|
$this->assertTrue($this->database->beginTransaction()); |
|
105
|
|
|
$this->database->exec('INSERT INTO "d_queued_commands" ("qc_id", "qc_time_start", "qc_time_end", "qc_status", "qc_command") VALUES (12, 1234567, 123456789, 13, \'ls -laf\');', []); |
|
106
|
|
|
$this->assertTrue($this->database->rollBack()); |
|
107
|
|
|
|
|
108
|
|
|
$lines = $this->database->query($sql->select($query), $query->getParams()); |
|
109
|
|
|
$this->assertEquals(6, count($lines)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @throws MapperException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testMapped(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->dataRefill(); |
|
118
|
|
|
|
|
119
|
|
|
// now queries - search |
|
120
|
|
|
$search = new Search(new MsSqlTestRecord()); |
|
121
|
|
|
$search->like('command', '%laf%'); |
|
122
|
|
|
$this->assertEquals(4, $search->getCount()); |
|
123
|
|
|
|
|
124
|
|
|
$records = $search->getResults(); |
|
125
|
|
|
$this->assertEquals(4, count($records)); |
|
126
|
|
|
|
|
127
|
|
|
/** @var MsSqlTestRecord $record */ |
|
128
|
|
|
$record = reset($records); |
|
129
|
|
|
$this->assertEquals(5, $record->id); |
|
130
|
|
|
$this->assertEquals(123456, $record->timeStart); |
|
131
|
|
|
$this->assertEquals(12345678, $record->timeEnd); |
|
132
|
|
|
$this->assertEquals(5, $record->status); |
|
133
|
|
|
|
|
134
|
|
|
$search2 = new Search(new MsSqlTestRecord()); |
|
135
|
|
|
$search2->exact('status', 55); |
|
136
|
|
|
$this->assertEquals(0, $search2->getCount()); |
|
137
|
|
|
$this->assertEquals(0, count($search2->getResults())); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @throws MapperException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testCrud(): void |
|
144
|
|
|
{ |
|
145
|
|
|
$this->dataRefill(); |
|
146
|
|
|
|
|
147
|
|
|
// create |
|
148
|
|
|
$rec1 = new MsSqlTestRecord(); |
|
149
|
|
|
$rec1->id = 14; |
|
150
|
|
|
$rec1->timeStart = 12345; |
|
151
|
|
|
$rec1->timeEnd = 1234567; |
|
152
|
|
|
$rec1->status = 8; |
|
153
|
|
|
$this->assertTrue($rec1->save(true)); |
|
154
|
|
|
|
|
155
|
|
|
// read |
|
156
|
|
|
$rec2 = new MsSqlTestRecord(); |
|
157
|
|
|
$rec2->status = 8; |
|
158
|
|
|
$this->assertEquals(1, count($rec2->loadMultiple())); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertTrue($rec2->load()); |
|
161
|
|
|
$this->assertEquals(12345, $rec2->timeStart); |
|
162
|
|
|
$this->assertEquals(1234567, $rec2->timeEnd); |
|
163
|
|
|
// update |
|
164
|
|
|
$rec2->status = 9; |
|
165
|
|
|
$this->assertTrue($rec2->save()); |
|
166
|
|
|
|
|
167
|
|
|
$rec3 = new MsSqlTestRecord(); |
|
168
|
|
|
$rec3->status = 8; |
|
169
|
|
|
$this->assertEquals(0, $rec3->count()); |
|
170
|
|
|
|
|
171
|
|
|
$rec4 = new MsSqlTestRecord(); |
|
172
|
|
|
$rec4->id = 6; |
|
173
|
|
|
$this->assertTrue($rec4->load()); |
|
174
|
|
|
$this->assertEquals(1234567, $rec4->timeStart); |
|
175
|
|
|
$this->assertEquals(12345678, $rec4->timeEnd); |
|
176
|
|
|
|
|
177
|
|
|
// delete |
|
178
|
|
|
$rec5 = new MsSqlTestRecord(); |
|
179
|
|
|
$rec5->status = 9; |
|
180
|
|
|
$this->assertTrue($rec5->delete()); |
|
181
|
|
|
|
|
182
|
|
|
// bulk update - for now via ugly hack |
|
183
|
|
|
$rec6 = new MsSqlTestRecord(); |
|
184
|
|
|
$rec6->getEntry('status')->setData(5, true); // hack to set condition |
|
185
|
|
|
$rec6->timeEnd = 123; // this will be updated |
|
186
|
|
|
$rec6->getMapper()->update($rec6); // todo: another hack, change rules for insert/update in future |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @throws MapperException |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function dataRefill(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$this->assertTrue($this->database->exec($this->dropTable(), [])); |
|
195
|
|
|
$this->assertTrue($this->database->exec($this->basicTable(), [])); |
|
196
|
|
|
$this->assertTrue($this->database->exec($this->fillTable(), [])); |
|
197
|
|
|
$this->assertEquals(6, $this->database->rowCount()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
protected function dropTable(): string |
|
201
|
|
|
{ |
|
202
|
|
|
return 'DROP TABLE IF EXISTS "d_queued_commands"'; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
protected function basicTable(): string |
|
206
|
|
|
{ |
|
207
|
|
|
return 'CREATE TABLE "d_queued_commands" ( |
|
208
|
|
|
"qc_id" INT PRIMARY KEY NOT NULL, |
|
209
|
|
|
"qc_time_start" VARCHAR(20) NULL, |
|
210
|
|
|
"qc_time_end" VARCHAR(20) NULL, |
|
211
|
|
|
"qc_status" INT NULL, |
|
212
|
|
|
"qc_command" VARCHAR(256) NULL |
|
213
|
|
|
)'; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
protected function fillTable(): string |
|
217
|
|
|
{ |
|
218
|
|
|
return 'INSERT INTO "d_queued_commands" ("qc_id", "qc_time_start", "qc_time_end", "qc_status", "qc_command") VALUES |
|
219
|
|
|
( 5, 123456, 12345678, 5, \'ls -laf\'), |
|
220
|
|
|
( 6, 1234567, 12345678, 5, \'ls -laf\'), |
|
221
|
|
|
( 7, 123456, 12345678, 11, \'ls -laf\'), |
|
222
|
|
|
( 8, 123456, 12345678, 11, \'ls -laf\'), |
|
223
|
|
|
( 9, 123456, 12345678, 12, \'ls -alF\'), |
|
224
|
|
|
(10, 123456, 12345678, 14, null) |
|
225
|
|
|
'; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Class MsSqlTestRecord |
|
232
|
|
|
* @property int id |
|
233
|
|
|
* @property int timeStart |
|
234
|
|
|
* @property int timeEnd |
|
235
|
|
|
* @property int status |
|
236
|
|
|
* @property string command |
|
237
|
|
|
*/ |
|
238
|
|
|
class MsSqlTestRecord extends ASimpleRecord |
|
239
|
|
|
{ |
|
240
|
|
|
protected function addEntries(): void |
|
241
|
|
|
{ |
|
242
|
|
|
$this->addEntry('id', IEntryType::TYPE_INTEGER, 64); |
|
243
|
|
|
$this->addEntry('timeStart', IEntryType::TYPE_INTEGER, 99999999); |
|
244
|
|
|
$this->addEntry('timeEnd', IEntryType::TYPE_INTEGER, 99999999); |
|
245
|
|
|
$this->addEntry('status', IEntryType::TYPE_INTEGER, 64); |
|
246
|
|
|
$this->addEntry('command', IEntryType::TYPE_STRING, 250); |
|
247
|
|
|
$this->setMapper('\StorageTests\Database\Connect\MsSqlTestMapper'); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
class MsSqlTestMapper extends ADatabase |
|
253
|
|
|
{ |
|
254
|
|
|
protected function setMap(): void |
|
255
|
|
|
{ |
|
256
|
|
|
$this->setSource('test_mssql_local'); |
|
257
|
|
|
$this->setTable('d_queued_commands'); |
|
258
|
|
|
$this->setRelation('id', 'qc_id'); |
|
259
|
|
|
$this->setRelation('timeStart', 'qc_time_start'); |
|
260
|
|
|
$this->setRelation('timeEnd', 'qc_time_end'); |
|
261
|
|
|
$this->setRelation('status', 'qc_status'); |
|
262
|
|
|
$this->setRelation('command', 'qc_command'); |
|
263
|
|
|
$this->addPrimaryKey('id'); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|