1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-beta.40 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Database; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Common trait for full text search manipulation. |
11
|
|
|
*/ |
12
|
|
|
trait FtsTrait |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Creates a new FTS configuration. |
17
|
|
|
* |
18
|
|
|
* @param string $cfgname The name of the FTS configuration to create |
19
|
|
|
* @param string $parser The parser to be used in new FTS configuration |
20
|
|
|
* @param string $template The existing FTS configuration to be used as template for the new one |
21
|
|
|
* @param string $comment If omitted, defaults to nothing |
22
|
|
|
* |
23
|
|
|
* @return bool|int 0 success |
24
|
|
|
* |
25
|
|
|
* @internal param string $locale Locale of the FTS configuration |
26
|
|
|
* @internal param string $withmap Should we copy whole map of existing FTS configuration to the new one |
27
|
|
|
* @internal param string $makeDefault Should this configuration be the default for locale given |
28
|
|
|
*/ |
29
|
|
|
public function createFtsConfiguration($cfgname, $parser = '', $template = '', $comment = '') |
30
|
|
|
{ |
31
|
|
|
$f_schema = $this->_schema; |
32
|
|
|
$this->fieldClean($f_schema); |
|
|
|
|
33
|
|
|
$this->fieldClean($cfgname); |
34
|
|
|
|
35
|
|
|
$sql = "CREATE TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$cfgname}\" ("; |
36
|
|
|
if ($parser != '') { |
37
|
|
|
$this->fieldClean($parser['schema']); |
38
|
|
|
$this->fieldClean($parser['parser']); |
39
|
|
|
$parser = "\"{$parser['schema']}\".\"{$parser['parser']}\""; |
40
|
|
|
$sql .= " PARSER = {$parser}"; |
41
|
|
|
} |
42
|
|
|
if ($template != '') { |
43
|
|
|
$this->fieldClean($template['schema']); |
44
|
|
|
$this->fieldClean($template['name']); |
45
|
|
|
$sql .= " COPY = \"{$template['schema']}\".\"{$template['name']}\""; |
46
|
|
|
} |
47
|
|
|
$sql .= ')'; |
48
|
|
|
|
49
|
|
|
if ($comment != '') { |
50
|
|
|
$status = $this->beginTransaction(); |
|
|
|
|
51
|
|
|
if ($status != 0) { |
52
|
|
|
return -1; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Create the FTS configuration |
57
|
|
|
$status = $this->execute($sql); |
|
|
|
|
58
|
|
|
if ($status != 0) { |
59
|
|
|
$this->rollbackTransaction(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return -1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Set the comment |
65
|
|
|
if ($comment != '') { |
66
|
|
|
$status = $this->setComment('TEXT SEARCH CONFIGURATION', $cfgname, '', $comment); |
|
|
|
|
67
|
|
|
if ($status != 0) { |
68
|
|
|
$this->rollbackTransaction(); |
69
|
|
|
|
70
|
|
|
return -1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->endTransaction(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Language functions |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns available FTS configurations. |
83
|
|
|
* |
84
|
|
|
* @param bool $all if false, returns schema qualified FTS confs |
85
|
|
|
* |
86
|
|
|
* @return \PHPPgAdmin\ADORecordSet A recordset |
87
|
|
|
*/ |
88
|
|
|
public function getFtsConfigurations($all = true) |
89
|
|
|
{ |
90
|
|
|
$c_schema = $this->_schema; |
91
|
|
|
$this->clean($c_schema); |
|
|
|
|
92
|
|
|
$sql = " |
93
|
|
|
SELECT |
94
|
|
|
n.nspname as schema, |
95
|
|
|
c.cfgname as name, |
96
|
|
|
pg_catalog.obj_description(c.oid, 'pg_ts_config') as comment |
97
|
|
|
FROM |
98
|
|
|
pg_catalog.pg_ts_config c |
99
|
|
|
JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace |
100
|
|
|
WHERE |
101
|
|
|
pg_catalog.pg_ts_config_is_visible(c.oid)"; |
102
|
|
|
|
103
|
|
|
if (!$all) { |
104
|
|
|
$sql .= " AND n.nspname='{$c_schema}'\n"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$sql .= 'ORDER BY name'; |
108
|
|
|
|
109
|
|
|
return $this->selectSet($sql); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Aggregate functions |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the map of FTS configuration given |
116
|
|
|
* (list of mappings (tokens) and their processing dictionaries). |
117
|
|
|
* |
118
|
|
|
* @param string $ftscfg Name of the FTS configuration |
119
|
|
|
* |
120
|
|
|
* @return \PHPPgAdmin\ADORecordSet recordset |
121
|
|
|
*/ |
122
|
|
|
public function getFtsConfigurationMap($ftscfg) |
123
|
|
|
{ |
124
|
|
|
$c_schema = $this->_schema; |
125
|
|
|
$this->clean($c_schema); |
126
|
|
|
$this->fieldClean($ftscfg); |
127
|
|
|
|
128
|
|
|
$oidSet = $this->selectSet("SELECT c.oid |
|
|
|
|
129
|
|
|
FROM pg_catalog.pg_ts_config AS c |
130
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.cfgnamespace) |
131
|
|
|
WHERE c.cfgname = '{$ftscfg}' |
132
|
|
|
AND n.nspname='{$c_schema}'"); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$oid = $oidSet->fields['oid']; |
135
|
|
|
|
136
|
|
|
$sql = " |
137
|
|
|
SELECT |
138
|
|
|
(SELECT t.alias FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS name, |
139
|
|
|
(SELECT t.description FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS description, |
140
|
|
|
c.cfgname AS cfgname, n.nspname ||'.'|| d.dictname as dictionaries |
141
|
|
|
FROM |
142
|
|
|
pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m, pg_catalog.pg_ts_dict d, |
143
|
|
|
pg_catalog.pg_namespace n |
144
|
|
|
WHERE |
145
|
|
|
c.oid = {$oid} |
146
|
|
|
AND m.mapcfg = c.oid |
147
|
|
|
AND m.mapdict = d.oid |
148
|
|
|
AND d.dictnamespace = n.oid |
149
|
|
|
ORDER BY name |
150
|
|
|
"; |
151
|
|
|
|
152
|
|
|
return $this->selectSet($sql); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns FTS parsers available. |
157
|
|
|
* |
158
|
|
|
* @param bool $all if false, return only Parsers from the current schema |
159
|
|
|
* |
160
|
|
|
* @return \PHPPgAdmin\ADORecordSet A recordset |
161
|
|
|
*/ |
162
|
|
|
public function getFtsParsers($all = true) |
163
|
|
|
{ |
164
|
|
|
$c_schema = $this->_schema; |
165
|
|
|
$this->clean($c_schema); |
166
|
|
|
$sql = " |
167
|
|
|
SELECT |
168
|
|
|
n.nspname as schema, |
169
|
|
|
p.prsname as name, |
170
|
|
|
pg_catalog.obj_description(p.oid, 'pg_ts_parser') as comment |
171
|
|
|
FROM pg_catalog.pg_ts_parser p |
172
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = p.prsnamespace) |
173
|
|
|
WHERE pg_catalog.pg_ts_parser_is_visible(p.oid)"; |
174
|
|
|
|
175
|
|
|
if (!$all) { |
176
|
|
|
$sql .= " AND n.nspname='{$c_schema}'\n"; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$sql .= 'ORDER BY name'; |
180
|
|
|
|
181
|
|
|
return $this->selectSet($sql); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns FTS dictionaries available. |
186
|
|
|
* |
187
|
|
|
* @param bool $all if false, return only Dics from the current schema |
188
|
|
|
* |
189
|
|
|
* @return \PHPPgAdmin\ADORecordSet A recordset |
190
|
|
|
*/ |
191
|
|
|
public function getFtsDictionaries($all = true) |
192
|
|
|
{ |
193
|
|
|
$c_schema = $this->_schema; |
194
|
|
|
$this->clean($c_schema); |
195
|
|
|
$sql = " |
196
|
|
|
SELECT |
197
|
|
|
n.nspname as schema, d.dictname as name, |
198
|
|
|
pg_catalog.obj_description(d.oid, 'pg_ts_dict') as comment |
199
|
|
|
FROM pg_catalog.pg_ts_dict d |
200
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace |
201
|
|
|
WHERE pg_catalog.pg_ts_dict_is_visible(d.oid)"; |
202
|
|
|
|
203
|
|
|
if (!$all) { |
204
|
|
|
$sql .= " AND n.nspname='{$c_schema}'\n"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$sql .= 'ORDER BY name;'; |
208
|
|
|
|
209
|
|
|
return $this->selectSet($sql); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns all FTS dictionary templates available. |
214
|
|
|
*/ |
215
|
|
|
public function getFtsDictionaryTemplates() |
216
|
|
|
{ |
217
|
|
|
$sql = " |
218
|
|
|
SELECT |
219
|
|
|
n.nspname as schema, |
220
|
|
|
t.tmplname as name, |
221
|
|
|
( SELECT COALESCE(np.nspname, '(null)')::pg_catalog.text || '.' || p.proname |
222
|
|
|
FROM pg_catalog.pg_proc p |
223
|
|
|
LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.pronamespace |
224
|
|
|
WHERE t.tmplinit = p.oid ) AS init, |
225
|
|
|
( SELECT COALESCE(np.nspname, '(null)')::pg_catalog.text || '.' || p.proname |
226
|
|
|
FROM pg_catalog.pg_proc p |
227
|
|
|
LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.pronamespace |
228
|
|
|
WHERE t.tmpllexize = p.oid ) AS lexize, |
229
|
|
|
pg_catalog.obj_description(t.oid, 'pg_ts_template') as comment |
230
|
|
|
FROM pg_catalog.pg_ts_template t |
231
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace |
232
|
|
|
WHERE pg_catalog.pg_ts_template_is_visible(t.oid) |
233
|
|
|
ORDER BY name;"; |
234
|
|
|
|
235
|
|
|
return $this->selectSet($sql); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Drops FTS coniguration. |
240
|
|
|
* |
241
|
|
|
* @param $ftscfg The configuration's name |
|
|
|
|
242
|
|
|
* @param $cascade Cascade to dependenced objects |
|
|
|
|
243
|
|
|
* |
244
|
|
|
* @return int 0 if operation was successful |
245
|
|
|
*/ |
246
|
|
|
public function dropFtsConfiguration($ftscfg, $cascade) |
247
|
|
|
{ |
248
|
|
|
$f_schema = $this->_schema; |
249
|
|
|
$this->fieldClean($f_schema); |
250
|
|
|
$this->fieldClean($ftscfg); |
251
|
|
|
|
252
|
|
|
$sql = "DROP TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$ftscfg}\""; |
253
|
|
|
if ($cascade) { |
254
|
|
|
$sql .= ' CASCADE'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->execute($sql); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Drops FTS dictionary. |
262
|
|
|
* |
263
|
|
|
* @param $ftsdict The dico's name |
264
|
|
|
* @param $cascade Cascade to dependenced objects |
265
|
|
|
* |
266
|
|
|
* @return int 0 if operation was successful |
267
|
|
|
* |
268
|
|
|
* @todo Support of dictionary templates dropping |
269
|
|
|
*/ |
270
|
|
|
public function dropFtsDictionary($ftsdict, $cascade) |
271
|
|
|
{ |
272
|
|
|
$f_schema = $this->_schema; |
273
|
|
|
$this->fieldClean($f_schema); |
274
|
|
|
$this->fieldClean($ftsdict); |
275
|
|
|
|
276
|
|
|
$sql = 'DROP TEXT SEARCH DICTIONARY'; |
277
|
|
|
$sql .= " \"{$f_schema}\".\"{$ftsdict}\""; |
278
|
|
|
if ($cascade) { |
279
|
|
|
$sql .= ' CASCADE'; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this->execute($sql); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Alters FTS configuration. |
287
|
|
|
* |
288
|
|
|
* @param $cfgname The conf's name |
289
|
|
|
* @param $comment A comment on for the conf |
290
|
|
|
* @param $name The new conf name |
291
|
|
|
* |
292
|
|
|
* @return bool|int 0 on success |
293
|
|
|
*/ |
294
|
|
|
public function updateFtsConfiguration($cfgname, $comment, $name) |
295
|
|
|
{ |
296
|
|
|
$status = $this->beginTransaction(); |
297
|
|
|
if ($status != 0) { |
298
|
|
|
$this->rollbackTransaction(); |
299
|
|
|
|
300
|
|
|
return -1; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$this->fieldClean($cfgname); |
304
|
|
|
|
305
|
|
|
$status = $this->setComment('TEXT SEARCH CONFIGURATION', $cfgname, '', $comment); |
306
|
|
|
if ($status != 0) { |
307
|
|
|
$this->rollbackTransaction(); |
308
|
|
|
|
309
|
|
|
return -1; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// Only if the name has changed |
313
|
|
|
if ($name != $cfgname) { |
314
|
|
|
$f_schema = $this->_schema; |
315
|
|
|
$this->fieldClean($f_schema); |
316
|
|
|
$this->fieldClean($name); |
317
|
|
|
|
318
|
|
|
$sql = "ALTER TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$cfgname}\" RENAME TO \"{$name}\""; |
319
|
|
|
$status = $this->execute($sql); |
320
|
|
|
if ($status != 0) { |
321
|
|
|
$this->rollbackTransaction(); |
322
|
|
|
|
323
|
|
|
return -1; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $this->endTransaction(); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Creates a new FTS dictionary or FTS dictionary template. |
332
|
|
|
* |
333
|
|
|
* @param string $dictname The name of the FTS dictionary to create |
334
|
|
|
* @param bool $isTemplate Flag whether we create usual dictionary or dictionary template |
335
|
|
|
* @param string $template The existing FTS dictionary to be used as template for the new one |
336
|
|
|
* @param string $lexize The name of the function, which does transformation of input word |
337
|
|
|
* @param string $init The name of the function, which initializes dictionary |
338
|
|
|
* @param string $option Usually, it stores various options required for the dictionary |
339
|
|
|
* @param string $comment If omitted, defaults to nothing |
340
|
|
|
* |
341
|
|
|
* @return bool|int 0 success |
342
|
|
|
*/ |
343
|
|
|
public function createFtsDictionary( |
344
|
|
|
$dictname, |
345
|
|
|
$isTemplate = false, |
346
|
|
|
$template = '', |
347
|
|
|
$lexize = '', |
348
|
|
|
$init = '', |
349
|
|
|
$option = '', |
350
|
|
|
$comment = '' |
351
|
|
|
) { |
352
|
|
|
$f_schema = $this->_schema; |
353
|
|
|
$this->fieldClean($f_schema); |
354
|
|
|
$this->fieldClean($dictname); |
355
|
|
|
$this->fieldClean($template); |
356
|
|
|
$this->fieldClean($lexize); |
357
|
|
|
$this->fieldClean($init); |
358
|
|
|
$this->fieldClean($option); |
359
|
|
|
|
360
|
|
|
$sql = 'CREATE TEXT SEARCH'; |
361
|
|
|
if ($isTemplate) { |
362
|
|
|
$sql .= " TEMPLATE \"{$f_schema}\".\"{$dictname}\" ("; |
363
|
|
|
if ($lexize != '') { |
364
|
|
|
$sql .= " LEXIZE = {$lexize}"; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if ($init != '') { |
368
|
|
|
$sql .= ", INIT = {$init}"; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
$sql .= ')'; |
372
|
|
|
$whatToComment = 'TEXT SEARCH TEMPLATE'; |
373
|
|
|
} else { |
374
|
|
|
$sql .= " DICTIONARY \"{$f_schema}\".\"{$dictname}\" ("; |
375
|
|
|
if ($template != '') { |
376
|
|
|
$this->fieldClean($template['schema']); |
377
|
|
|
$this->fieldClean($template['name']); |
378
|
|
|
$template = "\"{$template['schema']}\".\"{$template['name']}\""; |
379
|
|
|
|
380
|
|
|
$sql .= " TEMPLATE = {$template}"; |
381
|
|
|
} |
382
|
|
|
if ($option != '') { |
383
|
|
|
$sql .= ", {$option}"; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$sql .= ')'; |
387
|
|
|
$whatToComment = 'TEXT SEARCH DICTIONARY'; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/* if comment, begin a transaction to |
391
|
|
|
* run both commands */ |
392
|
|
|
if ($comment != '') { |
393
|
|
|
$status = $this->beginTransaction(); |
394
|
|
|
if ($status != 0) { |
395
|
|
|
return -1; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
// Create the FTS dictionary |
400
|
|
|
$status = $this->execute($sql); |
401
|
|
|
if ($status != 0) { |
402
|
|
|
$this->rollbackTransaction(); |
403
|
|
|
|
404
|
|
|
return -1; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
// Set the comment |
408
|
|
|
if ($comment != '') { |
409
|
|
|
$status = $this->setComment($whatToComment, $dictname, '', $comment); |
410
|
|
|
if ($status != 0) { |
411
|
|
|
$this->rollbackTransaction(); |
412
|
|
|
|
413
|
|
|
return -1; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $this->endTransaction(); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
// Role, User/Group functions |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Alters FTS dictionary or dictionary template. |
424
|
|
|
* |
425
|
|
|
* @param $dictname The dico's name |
426
|
|
|
* @param $comment The comment |
427
|
|
|
* @param $name The new dico's name |
428
|
|
|
* |
429
|
|
|
* @return bool|int 0 on success |
430
|
|
|
*/ |
431
|
|
|
public function updateFtsDictionary($dictname, $comment, $name) |
432
|
|
|
{ |
433
|
|
|
$status = $this->beginTransaction(); |
434
|
|
|
if ($status != 0) { |
435
|
|
|
$this->rollbackTransaction(); |
436
|
|
|
|
437
|
|
|
return -1; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
$this->fieldClean($dictname); |
441
|
|
|
$status = $this->setComment('TEXT SEARCH DICTIONARY', $dictname, '', $comment); |
442
|
|
|
if ($status != 0) { |
443
|
|
|
$this->rollbackTransaction(); |
444
|
|
|
|
445
|
|
|
return -1; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
// Only if the name has changed |
449
|
|
|
if ($name != $dictname) { |
450
|
|
|
$f_schema = $this->_schema; |
451
|
|
|
$this->fieldClean($f_schema); |
452
|
|
|
$this->fieldClean($name); |
453
|
|
|
|
454
|
|
|
$sql = "ALTER TEXT SEARCH DICTIONARY \"{$f_schema}\".\"{$dictname}\" RENAME TO \"{$name}\""; |
455
|
|
|
$status = $this->execute($sql); |
456
|
|
|
if ($status != 0) { |
457
|
|
|
$this->rollbackTransaction(); |
458
|
|
|
|
459
|
|
|
return -1; |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
return $this->endTransaction(); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Return all information relating to a FTS dictionary. |
468
|
|
|
* |
469
|
|
|
* @param $ftsdict The name of the FTS dictionary |
470
|
|
|
* |
471
|
|
|
* @return \PHPPgAdmin\ADORecordSet recordset of FTS dictionary information |
472
|
|
|
*/ |
473
|
|
|
public function getFtsDictionaryByName($ftsdict) |
474
|
|
|
{ |
475
|
|
|
$c_schema = $this->_schema; |
476
|
|
|
$this->clean($c_schema); |
477
|
|
|
$this->clean($ftsdict); |
478
|
|
|
|
479
|
|
|
$sql = "SELECT |
480
|
|
|
n.nspname as schema, |
481
|
|
|
d.dictname as name, |
482
|
|
|
( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM |
483
|
|
|
pg_catalog.pg_ts_template t |
484
|
|
|
LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace |
485
|
|
|
WHERE d.dicttemplate = t.oid ) AS template, |
486
|
|
|
d.dictinitoption as init, |
487
|
|
|
pg_catalog.obj_description(d.oid, 'pg_ts_dict') as comment |
488
|
|
|
FROM pg_catalog.pg_ts_dict d |
489
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace |
490
|
|
|
WHERE d.dictname = '{$ftsdict}' |
491
|
|
|
AND pg_catalog.pg_ts_dict_is_visible(d.oid) |
492
|
|
|
AND n.nspname='{$c_schema}' |
493
|
|
|
ORDER BY name"; |
494
|
|
|
|
495
|
|
|
return $this->selectSet($sql); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Creates/updates/deletes FTS mapping. |
500
|
|
|
* |
501
|
|
|
* @param $ftscfg |
502
|
|
|
* @param array $mapping Array of tokens' names |
503
|
|
|
* @param string $action What to do with the mapping: add, alter or drop |
504
|
|
|
* @param string $dictname Dictionary that will process tokens given or null in case of drop action |
505
|
|
|
* |
506
|
|
|
* @return int 0 if operation was successful |
507
|
|
|
* |
508
|
|
|
* @internal param string $cfgname The name of the FTS configuration to alter |
509
|
|
|
*/ |
510
|
|
|
public function changeFtsMapping($ftscfg, $mapping, $action, $dictname = null) |
511
|
|
|
{ |
512
|
|
|
if (count($mapping) > 0) { |
513
|
|
|
$f_schema = $this->_schema; |
514
|
|
|
$this->fieldClean($f_schema); |
515
|
|
|
$this->fieldClean($ftscfg); |
516
|
|
|
$this->fieldClean($dictname); |
517
|
|
|
$this->arrayClean($mapping); |
|
|
|
|
518
|
|
|
|
519
|
|
|
switch ($action) { |
520
|
|
|
case 'alter': |
521
|
|
|
$whatToDo = 'ALTER'; |
522
|
|
|
|
523
|
|
|
break; |
524
|
|
|
case 'drop': |
525
|
|
|
$whatToDo = 'DROP'; |
526
|
|
|
|
527
|
|
|
break; |
528
|
|
|
default: |
529
|
|
|
$whatToDo = 'ADD'; |
530
|
|
|
|
531
|
|
|
break; |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
$sql = "ALTER TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$ftscfg}\" {$whatToDo} MAPPING FOR "; |
535
|
|
|
$sql .= implode(',', $mapping); |
536
|
|
|
if ($action != 'drop' && !empty($dictname)) { |
537
|
|
|
$sql .= " WITH {$dictname}"; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
return $this->execute($sql); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return -1; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Return all information related to a given FTS configuration's mapping. |
548
|
|
|
* |
549
|
|
|
* @param $ftscfg The name of the FTS configuration |
550
|
|
|
* @param $mapping The name of the mapping |
551
|
|
|
* |
552
|
|
|
* @return FTS configuration information |
553
|
|
|
*/ |
554
|
|
|
public function getFtsMappingByName($ftscfg, $mapping) |
555
|
|
|
{ |
556
|
|
|
$c_schema = $this->_schema; |
557
|
|
|
$this->clean($c_schema); |
558
|
|
|
$this->clean($ftscfg); |
559
|
|
|
$this->clean($mapping); |
560
|
|
|
|
561
|
|
|
$oidSet = $this->selectSet("SELECT c.oid, cfgparser |
|
|
|
|
562
|
|
|
FROM pg_catalog.pg_ts_config AS c |
563
|
|
|
LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.cfgnamespace |
564
|
|
|
WHERE c.cfgname = '{$ftscfg}' |
565
|
|
|
AND n.nspname='{$c_schema}'"); |
|
|
|
|
566
|
|
|
|
567
|
|
|
$oid = $oidSet->fields['oid']; |
568
|
|
|
$cfgparser = $oidSet->fields['cfgparser']; |
569
|
|
|
|
570
|
|
|
$tokenIdSet = $this->selectSet("SELECT tokid |
|
|
|
|
571
|
|
|
FROM pg_catalog.ts_token_type({$cfgparser}) |
572
|
|
|
WHERE alias = '{$mapping}'"); |
|
|
|
|
573
|
|
|
|
574
|
|
|
$tokid = $tokenIdSet->fields['tokid']; |
575
|
|
|
|
576
|
|
|
$sql = "SELECT |
577
|
|
|
(SELECT t.alias FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS name, |
578
|
|
|
d.dictname as dictionaries |
579
|
|
|
FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m, pg_catalog.pg_ts_dict d |
580
|
|
|
WHERE c.oid = {$oid} AND m.mapcfg = c.oid AND m.maptokentype = {$tokid} AND m.mapdict = d.oid |
581
|
|
|
LIMIT 1;"; |
582
|
|
|
|
583
|
|
|
return $this->selectSet($sql); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Return list of FTS mappings possible for given parser |
588
|
|
|
* (specified by given configuration since configuration can only have 1 parser). |
589
|
|
|
* |
590
|
|
|
* @param $ftscfg The config's name that use the parser |
591
|
|
|
* |
592
|
|
|
* @return int 0 if operation was successful |
593
|
|
|
*/ |
594
|
|
|
public function getFtsMappings($ftscfg) |
595
|
|
|
{ |
596
|
|
|
$cfg = $this->getFtsConfigurationByName($ftscfg); |
597
|
|
|
|
598
|
|
|
$sql = "SELECT alias AS name, description |
599
|
|
|
FROM pg_catalog.ts_token_type({$cfg->fields['parser_id']}) |
600
|
|
|
ORDER BY name"; |
601
|
|
|
|
602
|
|
|
return $this->selectSet($sql); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Return all information related to a FTS configuration. |
607
|
|
|
* |
608
|
|
|
* @param $ftscfg The name of the FTS configuration |
609
|
|
|
* |
610
|
|
|
* @return FTS configuration information |
611
|
|
|
*/ |
612
|
|
|
public function getFtsConfigurationByName($ftscfg) |
613
|
|
|
{ |
614
|
|
|
$c_schema = $this->_schema; |
615
|
|
|
$this->clean($c_schema); |
616
|
|
|
$this->clean($ftscfg); |
617
|
|
|
$sql = " |
618
|
|
|
SELECT |
619
|
|
|
n.nspname as schema, |
620
|
|
|
c.cfgname as name, |
621
|
|
|
p.prsname as parser, |
622
|
|
|
c.cfgparser as parser_id, |
623
|
|
|
pg_catalog.obj_description(c.oid, 'pg_ts_config') as comment |
624
|
|
|
FROM pg_catalog.pg_ts_config c |
625
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace |
626
|
|
|
LEFT JOIN pg_catalog.pg_ts_parser p ON p.oid = c.cfgparser |
627
|
|
|
WHERE pg_catalog.pg_ts_config_is_visible(c.oid) |
628
|
|
|
AND c.cfgname = '{$ftscfg}' |
629
|
|
|
AND n.nspname='{$c_schema}'"; |
630
|
|
|
|
631
|
|
|
return $this->selectSet($sql); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
} |
635
|
|
|
|