1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\App; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Variables\Variable; |
14
|
|
|
use Lechimp\Dicto\Analysis\Query; |
15
|
|
|
use Lechimp\Dicto\Analysis\CompilesVars; |
16
|
|
|
use Lechimp\Dicto\Indexer\Insert; |
17
|
|
|
use Lechimp\Dicto\Indexer\CachesReferences; |
18
|
|
|
use Doctrine\DBAL\Schema; |
19
|
|
|
use Doctrine\DBAL\Types\Type; |
20
|
|
|
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer; |
21
|
|
|
|
22
|
|
|
class IndexDB extends DB implements Insert, Query { |
23
|
|
|
use CachesReferences; |
24
|
|
|
|
25
|
|
|
// Implementation of Insert interface. |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
49 |
|
public function source_file($name, $content) { |
31
|
49 |
|
assert('is_string($name)'); |
32
|
49 |
|
assert('is_string($content)'); |
33
|
49 |
|
$stmt = $this->builder() |
34
|
49 |
|
->insert($this->source_file_table()) |
35
|
49 |
|
->values(array |
36
|
|
|
( "name" => "?" |
37
|
49 |
|
, "line" => "?" |
38
|
49 |
|
, "source" => "?" |
39
|
49 |
|
)) |
40
|
49 |
|
->setParameter(0, $name); |
41
|
49 |
|
$line = 1; |
42
|
49 |
|
foreach (explode("\n", $content) as $source) { |
43
|
|
|
$stmt |
44
|
49 |
|
->setParameter(1, $line) |
45
|
49 |
|
->setParameter(2, $source) |
46
|
49 |
|
->execute(); |
47
|
49 |
|
$line++; |
48
|
49 |
|
} |
49
|
49 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
52 |
|
public function entity($type, $name, $file, $start_line, $end_line) { |
55
|
52 |
|
assert('\\Lechimp\\Dicto\\Variables\\Variable::is_type($type)'); |
56
|
52 |
|
assert('is_string($name)'); |
57
|
52 |
|
assert('is_string($file)'); |
58
|
52 |
|
assert('is_int($start_line)'); |
59
|
52 |
|
assert('is_int($end_line)'); |
60
|
52 |
|
$this->builder() |
61
|
52 |
|
->insert($this->entity_table()) |
62
|
52 |
|
->values(array |
63
|
|
|
( "type" => "?" |
64
|
52 |
|
, "name" => "?" |
65
|
52 |
|
, "file" => "?" |
66
|
52 |
|
, "start_line" => "?" |
67
|
52 |
|
, "end_line" => "?" |
68
|
52 |
|
)) |
69
|
52 |
|
->setParameter(0, $type) |
70
|
52 |
|
->setParameter(1, $name) |
71
|
52 |
|
->setParameter(2, $file) |
72
|
52 |
|
->setParameter(3, $start_line) |
73
|
52 |
|
->setParameter(4, $end_line) |
74
|
52 |
|
->execute(); |
75
|
52 |
|
return (int)$this->connection->lastInsertId(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
41 |
|
public function reference($type, $name, $file, $line) { |
82
|
40 |
|
assert('\\Lechimp\\Dicto\\Variables\\Variable::is_type($type)'); |
83
|
40 |
|
assert('is_string($name)'); |
84
|
40 |
|
assert('is_string($file)'); |
85
|
40 |
|
assert('is_int($line)'); |
86
|
40 |
|
$this->builder() |
87
|
40 |
|
->insert($this->reference_table()) |
88
|
40 |
|
->values(array |
89
|
|
|
( "type" => "?" |
90
|
40 |
|
, "name" => "?" |
91
|
40 |
|
, "file" => "?" |
92
|
40 |
|
, "line" => "?" |
93
|
41 |
|
)) |
94
|
40 |
|
->setParameter(0, $type) |
95
|
40 |
|
->setParameter(1, $name) |
96
|
40 |
|
->setParameter(2, $file) |
97
|
40 |
|
->setParameter(3, $line) |
98
|
40 |
|
->execute(); |
99
|
40 |
|
return (int)$this->connection->lastInsertId(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
30 |
|
public function relation($name, $entity_id, $reference_id) { |
106
|
30 |
|
assert('is_string($name)'); |
107
|
30 |
|
assert('is_int($entity_id)'); |
108
|
30 |
|
assert('is_int($reference_id)'); |
109
|
30 |
|
$this->builder() |
110
|
30 |
|
->insert($this->relations_table()) |
111
|
30 |
|
->values(array |
112
|
|
|
( "name" => "?" |
113
|
30 |
|
, "entity_id" => "?" |
114
|
30 |
|
, "reference_id" => "?" |
115
|
30 |
|
)) |
116
|
30 |
|
->setParameter(0, $name) |
117
|
30 |
|
->setParameter(1, $entity_id) |
118
|
30 |
|
->setParameter(2, $reference_id) |
119
|
30 |
|
->execute(); |
120
|
30 |
|
} |
121
|
|
|
|
122
|
|
|
// Naming |
123
|
|
|
|
124
|
55 |
|
public function source_file_table() { |
125
|
55 |
|
return "files"; |
126
|
|
|
} |
127
|
|
|
|
128
|
55 |
|
public function entity_table() { |
129
|
55 |
|
return "entities"; |
130
|
|
|
} |
131
|
|
|
|
132
|
55 |
|
public function reference_table() { |
133
|
55 |
|
return "refs"; |
134
|
|
|
} |
135
|
|
|
|
136
|
55 |
|
public function relations_table() { |
137
|
55 |
|
return "relations"; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Creation of database. |
141
|
|
|
|
142
|
55 |
|
public function init_database_schema() { |
143
|
55 |
|
$schema = new Schema\Schema(); |
144
|
|
|
|
145
|
55 |
|
$file_table = $schema->createTable($this->source_file_table()); |
146
|
55 |
|
$file_table->addColumn |
147
|
55 |
|
( "name", "string" |
148
|
55 |
|
, array("notnull" => true) |
149
|
55 |
|
); |
150
|
55 |
|
$file_table->addColumn |
151
|
55 |
|
( "line", "integer" |
152
|
55 |
|
, array("notnull" => true, "unsigned" => true) |
153
|
55 |
|
); |
154
|
55 |
|
$file_table->addColumn |
155
|
55 |
|
( "source", "string" |
156
|
55 |
|
, array("notnull" => true) |
157
|
55 |
|
); |
158
|
55 |
|
$file_table->setPrimaryKey(array("name", "line")); |
159
|
|
|
|
160
|
55 |
|
$entity_table = $schema->createTable($this->entity_table()); |
161
|
55 |
|
$entity_table->addColumn |
162
|
55 |
|
("id", "integer" |
163
|
55 |
|
, array("notnull" => true, "unsigned" => true, "autoincrement" => true) |
164
|
55 |
|
); |
165
|
55 |
|
$entity_table->addColumn |
166
|
55 |
|
("type", "string" |
167
|
55 |
|
, array("notnull" => true) |
168
|
55 |
|
); |
169
|
55 |
|
$entity_table->addColumn |
170
|
55 |
|
("name", "string" |
171
|
55 |
|
, array("notnull" => true) |
172
|
55 |
|
); |
173
|
55 |
|
$entity_table->addColumn |
174
|
55 |
|
("file", "string" |
175
|
55 |
|
, array("notnull" => true) |
176
|
55 |
|
); |
177
|
55 |
|
$entity_table->addColumn |
178
|
55 |
|
("start_line", "integer" |
179
|
55 |
|
, array("notnull" => true, "unsigned" => true) |
180
|
55 |
|
); |
181
|
55 |
|
$entity_table->addColumn |
182
|
55 |
|
("end_line", "integer" |
183
|
55 |
|
, array("notnull" => true, "unsigned" => true) |
184
|
55 |
|
); |
185
|
55 |
|
$entity_table->setPrimaryKey(array("id")); |
186
|
|
|
|
187
|
55 |
|
$reference_table = $schema->createTable($this->reference_table()); |
188
|
55 |
|
$reference_table->addColumn |
189
|
55 |
|
( "id", "integer" |
190
|
55 |
|
, array("notnull" => true, "unsigned" => true, "autoincrement" => true) |
191
|
55 |
|
); |
192
|
55 |
|
$reference_table->addColumn |
193
|
55 |
|
("type", "string" |
194
|
55 |
|
, array("notnull" => true) |
195
|
55 |
|
); |
196
|
55 |
|
$reference_table->addColumn |
197
|
55 |
|
("name", "string" |
198
|
55 |
|
, array("notnull" => true) |
199
|
55 |
|
); |
200
|
55 |
|
$reference_table->addColumn |
201
|
55 |
|
("file", "string" |
202
|
55 |
|
, array("notnull" => true) |
203
|
55 |
|
); |
204
|
55 |
|
$reference_table->addColumn |
205
|
55 |
|
("line", "integer" |
206
|
55 |
|
, array("notnull" => true, "unsigned" => true) |
207
|
55 |
|
); |
208
|
55 |
|
$reference_table->setPrimaryKey(array("id")); |
209
|
|
|
|
210
|
55 |
|
$relations_table = $schema->createTable($this->relations_table()); |
211
|
55 |
|
$relations_table->addColumn |
212
|
55 |
|
("name", "string" |
213
|
55 |
|
, array("notnull" => true) |
214
|
55 |
|
); |
215
|
55 |
|
$relations_table->addColumn |
216
|
55 |
|
( "entity_id", "integer" |
217
|
55 |
|
, array("notnull" => true, "unsigned" => true) |
218
|
55 |
|
); |
219
|
55 |
|
$relations_table->addColumn |
220
|
55 |
|
( "reference_id", "integer" |
221
|
55 |
|
, array("notnull" => true, "unsigned" => true) |
222
|
55 |
|
); |
223
|
55 |
|
$relations_table->addForeignKeyConstraint |
224
|
55 |
|
( $entity_table |
225
|
55 |
|
, array("entity_id") |
226
|
55 |
|
, array("id") |
227
|
55 |
|
); |
228
|
55 |
|
$relations_table->addForeignKeyConstraint |
229
|
55 |
|
( $reference_table |
230
|
55 |
|
, array("reference_id") |
231
|
55 |
|
, array("id") |
232
|
55 |
|
); |
233
|
|
|
|
234
|
55 |
|
$sync = new SingleDatabaseSynchronizer($this->connection); |
235
|
55 |
|
$sync->createSchema($schema); |
236
|
55 |
|
} |
237
|
|
|
} |
238
|
|
|
|