Skip to content

Commit 0ddf6c2

Browse files
authored
Merge pull request #50 from MiguelSilvaR/index_parser_bug_keys
Fix in regular index generation paths
2 parents e3917da + 929251e commit 0ddf6c2

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

MigrationTools/IndexParser/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The purpose of this package is to help migrating indexes from MongoDB to Oracle
1010
See: [Oracle Database API for MongoDB](https://docs.oracle.com/en/database/oracle/mongodb-api/mgapi/oracle-database-api-mongodb.pdf)
1111
* This package will scan all your data and suggest SQL indexes to create based on a combination of your MongoDB index definitions and the actual data types used.
1212
* If your data is large, running this package may take some time as it must scan all of your collection data.
13+
* Package must be used in the same schema as the collection where indexes are going to be used.
1314

1415
## Installation
1516

@@ -72,7 +73,7 @@ If your Object Store bucket is not public, you must also specify a credential.
7273
from "INDEXES", json_table(
7374
json_document, '$' columns (
7475
collectionName,
75-
nested '$.indexes[*]' columns(doc format json path '$')
76+
nested '$.indexes[*]' columns(doc clob format json path '$')
7677
)
7778
);
7879
```
@@ -81,7 +82,7 @@ If your Object Store bucket is not public, you must also specify a credential.
8182
select ora_idx_parser.getSQLIndexes(collectionName, indexes) as SQL_Idx
8283
from "INDEXES", json_table(
8384
json_document, '$' columns (
84-
collectionName, indexes format json
85+
collectionName, indexes clob format json
8586
)
8687
);
8788
```
@@ -93,8 +94,8 @@ Example result:
9394
```
9495
alter session enable parallel ddl;
9596
96-
create index "$ora:shows.summary_1" on shows(
97-
json_value(data, '$.summary.stringOnly()' error on error null on empty) asc
97+
create index "$ora:shows.summary_1" on SHOWS(
98+
json_value(DATA, q'[$."summary".stringOnly()]' error on error null on empty) asc
9899
, 1) parallel;
99100
100101
alter session disable parallel ddl;

MigrationTools/IndexParser/ora_idx_parser.body.sql

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,42 @@ create or replace package body ora_idx_parser as
532532
return true;
533533
end;
534534

535+
/*
536+
*
537+
* Method used to sanitize keys used in regular idx, so json_value has the
538+
* correct input
539+
* E.g.
540+
* $.a".abc => json_value(DATA, q'[$."a\""."abc".stringOnly()]')
541+
*/
542+
function sanitize_key_string(v_key clob)
543+
return clob
544+
is
545+
path_cursor sys_refcursor;
546+
arr_elem json_object_t;
547+
v_path clob;
548+
stmt clob;
549+
path clob;
550+
begin
551+
552+
stmt := 'select regexp_substr(path, ''[^.]+'', 1, level) from (select ';
553+
stmt := stmt || q'[q'[]' || v_key;
554+
stmt := stmt || ']''';
555+
stmt := stmt || ' path from dual) connect by level <= length(path)-length(replace(path, ''.''))+1';
556+
557+
open path_cursor for stmt;
558+
loop
559+
fetch path_cursor into v_path;
560+
exit when path_cursor%NOTFOUND;
561+
562+
path := path || '"' || escapeKeyChars(v_path) || '"';
563+
path := path || '.';
564+
565+
end loop;
566+
567+
568+
return path;
569+
end;
570+
535571
/*
536572
* Method that returns the SQL form of a simple index
537573
* i.e. not a TTL or multivalue one
@@ -565,22 +601,21 @@ create or replace package body ora_idx_parser as
565601

566602
jtype := key_types(v_key);
567603

568-
v_key := escapeKeyChars(v_key);
569-
out_stmt := out_stmt || chr(10) || chr(9) || 'json_value(' || json_data_column || ', ' || q'[q'[$."]' || v_key;
570-
out_stmt := out_stmt || q'["]';
604+
v_key := sanitize_key_string(v_key);
605+
out_stmt := out_stmt || chr(10) || chr(9) || 'json_value(' || json_data_column || ', ' || q'[q'[$.]' || v_key;
571606
case jtype
572607
when 'string' then
573-
out_stmt := out_stmt || '.stringOnly()';
608+
out_stmt := out_stmt || 'stringOnly()';
574609
when 'number' then
575-
out_stmt := out_stmt || '.numberOnly()';
610+
out_stmt := out_stmt || 'numberOnly()';
576611
when 'double' then
577-
out_stmt := out_stmt || '.numberOnly()';
612+
out_stmt := out_stmt || 'numberOnly()';
578613
when 'timestamp' then
579-
out_stmt := out_stmt || '.dateTimeOnly()';
614+
out_stmt := out_stmt || 'dateTimeOnly()';
580615
when 'boolean' then
581-
out_stmt := out_stmt || '.booleanOnly()';
616+
out_stmt := out_stmt || 'booleanOnly()';
582617
when 'binary' then
583-
out_stmt := out_stmt || '.binaryOnly()';
618+
out_stmt := out_stmt || 'binaryOnly()';
584619
else
585620
err_count := err_count + 1;
586621
return '/* Unsupported type ''' || jtype || ''', found in ''' || idx_spec || ''' index spec */';
@@ -912,4 +947,4 @@ create or replace package body ora_idx_parser as
912947

913948

914949
end ora_idx_parser;
915-
/
950+
/

0 commit comments

Comments
 (0)