Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
SET client_min_messages = warning;
-- SODA_DOCUMENT_T and SODA_CURSOR_T checks adapted from Oracle SODA behavior to
-- the public SQL API exposed by this implementation.
CREATE OR REPLACE FUNCTION pg_temp.soda_caught_exception(stmt text)
RETURNS bool
LANGUAGE plpgsql
AS $$
BEGIN
  EXECUTE stmt;
  RETURN false;
EXCEPTION
  WHEN OTHERS THEN
    RETURN true;
END;
$$;
SELECT typname,
       typtype,
       typbasetype::regtype::text AS base_type
FROM pg_type
WHERE typnamespace = 'pg_catalog'::regnamespace
  AND typname IN ('soda_document_t',
                  'soda_collection_t',
                  'soda_operation_t',
                  'soda_cursor_t')
ORDER BY typname;
      typname      | typtype | base_type 
-------------------+---------+-----------
 soda_collection_t | d       | integer
 soda_cursor_t     | d       | integer
 soda_document_t   | d       | integer
 soda_operation_t  | d       | integer
(4 rows)
 
SELECT soda_document_get_varchar2(
           soda_document_t(key => 'k1'::varchar2, v_content => '{"a": 1}'::varchar2)
       )::text AS varchar2_content;
 varchar2_content 
------------------
 {"a": 1}
(1 row)
 
SELECT soda_document_t_get_key(
           soda_document_t(key => 'alias_key'::varchar2, v_content => '{"a": 1}'::varchar2)
       )::text AS alias_get_key,
       soda_document_t_get_varchar2(
           soda_document_t(key => 'alias_key'::varchar2, v_content => '{"a": 1}'::varchar2)
       )::text AS alias_get_varchar2;
 alias_get_key | alias_get_varchar2 
---------------+--------------------
 alias_key     | {"a": 1}
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           $$SELECT soda_document_get_varchar2(
                    soda_document_t(key => 'k2'::varchar2,
                                    b_content => decode('7B2261223A317D', 'hex')::blob)
                  )$$
       ) AS get_varchar2_blob_throws;
 get_varchar2_blob_throws 
--------------------------
 t
(1 row)
 
SELECT length(
           soda_document_get_blob(
             soda_document_t(key => 'k3'::varchar2, b_content => decode('', 'hex')::blob)
           )
       ) AS empty_blob_length;
 empty_blob_length 
-------------------
                 0
(1 row)
 
SELECT soda_document_get_clob(
           soda_document_t(
             key => 'k4'::varchar2,
             c_content => '{
  "id": 100,
  "nodes": {
    "node_a": 1,
    "node_b": 2
  },
  "active": true
}'::clob
           )
       )::text = '{
  "id": 100,
  "nodes": {
    "node_a": 1,
    "node_b": 2
  },
  "active": true
}' AS clob_content_matches;
 clob_content_matches 
----------------------
 t
(1 row)
 
SELECT soda_document_get_data_type(
           soda_document_t(key => 'k5'::varchar2, v_content => '{"test": 123}'::varchar2)
       ) AS varchar2_data_type;
 varchar2_data_type 
--------------------
                  1
(1 row)
 
SELECT soda_document_get_data_type(
           soda_document_t(key => 'k18'::varchar2, b_content => decode('', 'hex')::blob)
       ) AS blob_data_type;
 blob_data_type 
----------------
              2
(1 row)
 
SELECT soda_document_get_data_type(
           soda_document_t(c_content => '{"type":"clob"}'::clob)
       ) AS clob_data_type;
 clob_data_type 
----------------
              3
(1 row)
 
SELECT soda_document_get_data_type(
           soda_document_t(key => 'k19'::varchar2, j_content => '{"a":1}'::json)
       ) AS json_data_type;
 json_data_type 
----------------
              4
(1 row)
 
SELECT soda_document_get_media_type(
           soda_document_t(key => 'k6'::varchar2, v_content => '{"test": 123}'::varchar2)
       )::text AS default_media_type;
 default_media_type 
--------------------
 application/json
(1 row)
 
SELECT soda_document_get_media_type(
           soda_document_t(key => 'k7'::varchar2,
                           v_content => 'plain text'::varchar2,
                           media_type => 'text/plain'::varchar2)
       )::text AS custom_media_type;
 custom_media_type 
-------------------
 text/plain
(1 row)
 
SELECT soda_document_get_version(
           soda_document_t(key => 'k8'::varchar2, v_content => '{"a":1}'::varchar2)
       ) IS NULL AS memory_document_version_is_null;
 memory_document_version_is_null 
---------------------------------
 t
(1 row)
 
SELECT soda_document_get_created_on(
           soda_document_t(key => 'k15'::varchar2, v_content => '{"a":1}'::varchar2)
       ) IS NULL AS memory_document_created_on_is_null;
 memory_document_created_on_is_null 
------------------------------------
 t
(1 row)
 
SELECT soda_document_get_last_modified(
           soda_document_t(key => 'k16'::varchar2, v_content => '{"a":1}'::varchar2)
       ) IS NULL AS memory_document_last_modified_is_null;
 memory_document_last_modified_is_null 
---------------------------------------
 t
(1 row)
 
SELECT soda_document_get_key(
           soda_document_t(key => 'my_super_key_123'::varchar2,
                           v_content => '{"a":1}'::varchar2)
       )::text AS explicit_key;
   explicit_key   
------------------
 my_super_key_123
(1 row)
 
SELECT soda_document_get_key(
           soda_document_t(v_content => '{"status":"no_key"}'::varchar2)
       ) IS NULL AS absent_key_is_null;
 absent_key_is_null 
--------------------
 t
(1 row)
 
SELECT soda_document_get_json(
           soda_document_t(j_content => '{"node":"test"}'::json)
       )::text AS json_content;
  json_content   
-----------------
 {"node":"test"}
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           $$SELECT soda_document_get_json(
                    soda_document_t(v_content => '{"node":"test"}'::varchar2)
                  )$$
       ) AS get_json_varchar2_throws;
 get_json_varchar2_throws 
--------------------------
 t
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           $$SELECT soda_document_get_clob(
                    soda_document_t(b_content => decode('7B2274797065223A2022626C6F62227D', 'hex')::blob)
                  )$$
       ) AS get_clob_blob_throws;
 get_clob_blob_throws 
----------------------
 t
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           $$SELECT soda_document_get_clob(
                    soda_document_t(v_content => '{"type":"varchar2"}'::varchar2)
                  )$$
       ) AS get_clob_varchar2_throws;
 get_clob_varchar2_throws 
--------------------------
 t
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           $$SELECT soda_document_get_varchar2(
                    soda_document_t(c_content => '{"type":"clob"}'::clob)
                  )$$
       ) AS get_varchar2_clob_throws;
 get_varchar2_clob_throws 
--------------------------
 t
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_CURSOR_EMPTY') AS cleanup_empty_cursor;
 cleanup_empty_cursor 
----------------------
 0
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_CURSOR_DATA') AS cleanup_data_cursor;
 cleanup_data_cursor 
---------------------
 0
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_METHOD_META') AS cleanup_doc_meta;
 cleanup_doc_meta 
------------------
 0
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_METHOD_META_TS') AS cleanup_doc_meta_ts;
 cleanup_doc_meta_ts 
---------------------
 0
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_PERSIST_JSON') AS cleanup_persist_json;
 cleanup_persist_json 
----------------------
 0
(1 row)
 
CREATE TEMP TABLE soda_empty_cursor_collection AS
SELECT dbms_soda_create_collection('DOC_CURSOR_EMPTY') AS collection;
CREATE TEMP TABLE soda_empty_cursor AS
SELECT soda_operation_get_cursor(soda_collection_find(collection)) AS cursor_obj
FROM soda_empty_cursor_collection;
SELECT soda_cursor_has_next(cursor_obj) AS empty_cursor_has_next
FROM soda_empty_cursor;
 empty_cursor_has_next 
-----------------------
 f
(1 row)
 
SELECT soda_cursor_next(cursor_obj) IS NULL AS empty_cursor_next_is_null
FROM soda_empty_cursor;
 empty_cursor_next_is_null 
---------------------------
 t
(1 row)
 
SELECT soda_cursor_close(cursor_obj) AS empty_cursor_close_result
FROM soda_empty_cursor;
 empty_cursor_close_result 
---------------------------
 t
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           'SELECT soda_cursor_has_next((SELECT cursor_obj FROM soda_empty_cursor))'
       ) AS has_next_after_close_throws;
 has_next_after_close_throws 
-----------------------------
 t
(1 row)
 
CREATE TEMP TABLE soda_data_cursor_collection AS
SELECT dbms_soda_create_collection('DOC_CURSOR_DATA') AS collection;
SELECT soda_collection_insert_one(
           collection,
           soda_document_t(j_content => '{"a":1}'::json)
       ) AS insert_status
FROM soda_data_cursor_collection;
 insert_status 
---------------
 1
(1 row)
 
SELECT soda_collection_insert_one(
           collection,
           soda_document_t(j_content => '{"a":2}'::json)
       ) AS insert_status
FROM soda_data_cursor_collection;
 insert_status 
---------------
 1
(1 row)
 
CREATE TEMP TABLE soda_data_cursor AS
SELECT soda_operation_get_cursor(soda_collection_find(collection)) AS cursor_obj
FROM soda_data_cursor_collection;
CREATE TEMP TABLE soda_data_cursor_docs AS
SELECT soda_cursor_next(cursor_obj) AS document
FROM soda_data_cursor
WHERE soda_cursor_has_next(cursor_obj);
INSERT INTO soda_data_cursor_docs
SELECT soda_cursor_next(cursor_obj) AS document
FROM soda_data_cursor
WHERE soda_cursor_has_next(cursor_obj);
SELECT count(*) AS cursor_document_count
FROM soda_data_cursor_docs;
 cursor_document_count 
-----------------------
                     2
(1 row)
 
SELECT soda_cursor_has_next(cursor_obj) AS exhausted_cursor_has_next
FROM soda_data_cursor;
 exhausted_cursor_has_next 
---------------------------
 f
(1 row)
 
SELECT soda_cursor_next(cursor_obj) IS NULL AS exhausted_cursor_next_is_null
FROM soda_data_cursor;
 exhausted_cursor_next_is_null 
-------------------------------
 t
(1 row)
 
SELECT soda_cursor_close(cursor_obj) AS data_cursor_close_result
FROM soda_data_cursor;
 data_cursor_close_result 
--------------------------
 t
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           'SELECT soda_cursor_next((SELECT cursor_obj FROM soda_data_cursor))'
       ) AS next_after_close_throws;
 next_after_close_throws 
-------------------------
 t
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           'SELECT soda_cursor_close((SELECT cursor_obj FROM soda_data_cursor))'
       ) AS close_after_close_throws;
 close_after_close_throws 
--------------------------
 t
(1 row)
 
CREATE TEMP TABLE soda_alias_chain_doc AS
SELECT soda_collection_insert_one_and_get(
           collection,
           soda_document_t(j_content => '{"alias":true}'::json)
       ) AS document
FROM soda_data_cursor_collection;
SELECT soda_document_t_get_json(
           soda_operation_t_get_one(
             soda_operation_t_key(
               soda_collection_t_find(collection),
               (SELECT soda_document_t_get_key(document) FROM soda_alias_chain_doc)
             )
           )
       ) IS NOT NULL AS migration_alias_chain_ok
FROM soda_data_cursor_collection;
 migration_alias_chain_ok 
--------------------------
 t
(1 row)
 
CREATE TEMP TABLE soda_doc_meta_collection AS
SELECT dbms_soda_create_collection('DOC_METHOD_META') AS collection;
CREATE TEMP TABLE soda_inserted_meta_doc AS
SELECT soda_collection_insert_one_and_get(
           collection,
           soda_document_t(j_content => '{"data":"meta"}'::json)
       ) AS document
FROM soda_doc_meta_collection;
SELECT soda_document_get_version(document) IS NOT NULL AS inserted_version_generated,
       soda_document_get_created_on(document) IS NULL AS inserted_created_on_is_null,
       soda_document_get_last_modified(document) IS NULL AS inserted_last_modified_is_null
FROM soda_inserted_meta_doc;
 inserted_version_generated | inserted_created_on_is_null | inserted_last_modified_is_null 
----------------------------+-----------------------------+--------------------------------
 t                          | t                           | t
(1 row)
 
CREATE TEMP TABLE soda_doc_meta_ts_collection AS
SELECT dbms_soda_create_collection(
           'DOC_METHOD_META_TS',
           '{"creationTimeColumn":{"name":"CREATED_ON"},"lastModifiedColumn":{"name":"LAST_MODIFIED"}}'
       ) AS collection;
CREATE TEMP TABLE soda_inserted_meta_ts_doc AS
SELECT soda_collection_insert_one_and_get(
           collection,
           soda_document_t(j_content => '{"data":"meta_ts"}'::json)
       ) AS document
FROM soda_doc_meta_ts_collection;
SELECT soda_document_get_version(document) IS NOT NULL AS inserted_ts_version_generated,
       soda_document_get_created_on(document) LIKE '____-__-__T__:__:__%Z' AS inserted_ts_created_on_iso,
       soda_document_get_last_modified(document) LIKE '____-__-__T__:__:__%Z' AS inserted_ts_last_modified_iso
FROM soda_inserted_meta_ts_doc;
 inserted_ts_version_generated | inserted_ts_created_on_iso | inserted_ts_last_modified_iso 
-------------------------------+----------------------------+-------------------------------
 t                             | t                          | t
(1 row)
 
CREATE TEMP TABLE soda_persist_json_collection AS
SELECT dbms_soda_create_collection('DOC_PERSIST_JSON') AS collection;
CREATE TEMP TABLE soda_persist_json_inserted AS
SELECT soda_collection_insert_one_and_get(
           collection,
           soda_document_t(j_content => '{"a":1}'::json)
       ) AS document
FROM soda_persist_json_collection;
CREATE TEMP TABLE soda_persist_json_found AS
SELECT soda_collection_find_one(
           collection,
           (SELECT soda_document_get_key(document) FROM soda_persist_json_inserted)
       ) AS document
FROM soda_persist_json_collection;
SELECT soda_document_get_data_type(document) AS persisted_json_data_type,
       soda_document_get_json(document) ->> 'a' AS persisted_json_value
FROM soda_persist_json_found;
 persisted_json_data_type | persisted_json_value 
--------------------------+----------------------
                        4 | 1
(1 row)
 
SELECT pg_temp.soda_caught_exception(
           'SELECT soda_document_get_blob((SELECT document FROM soda_persist_json_found))'
       ) AS persisted_json_get_blob_throws;
 persisted_json_get_blob_throws 
--------------------------------
 t
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_CURSOR_EMPTY') AS drop_empty_cursor;
 drop_empty_cursor 
-------------------
 1
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_CURSOR_DATA') AS drop_data_cursor;
 drop_data_cursor 
------------------
 1
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_METHOD_META') AS drop_doc_meta;
 drop_doc_meta 
---------------
 1
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_METHOD_META_TS') AS drop_doc_meta_ts;
 drop_doc_meta_ts 
------------------
 1
(1 row)
 
SELECT dbms_soda_drop_collection('DOC_PERSIST_JSON') AS drop_persist_json;
 drop_persist_json 
-------------------
 1
(1 row)
 
DROP TABLE IF EXISTS soda_empty_cursor_collection;
DROP TABLE IF EXISTS soda_empty_cursor;
DROP TABLE IF EXISTS soda_data_cursor_collection;
DROP TABLE IF EXISTS soda_data_cursor;
DROP TABLE IF EXISTS soda_data_cursor_docs;
DROP TABLE IF EXISTS soda_alias_chain_doc;
DROP TABLE IF EXISTS soda_doc_meta_collection;
DROP TABLE IF EXISTS soda_inserted_meta_doc;
DROP TABLE IF EXISTS soda_doc_meta_ts_collection;
DROP TABLE IF EXISTS soda_inserted_meta_ts_doc;
DROP TABLE IF EXISTS soda_persist_json_collection;
DROP TABLE IF EXISTS soda_persist_json_inserted;
DROP TABLE IF EXISTS soda_persist_json_found;