Created
June 14, 2023 22:11
-
-
Save Soremwar/fa3736ef5e27a6e75835d253700f126a to your computer and use it in GitHub Desktop.
Showcase Oracle Data Modeler capabilities for programatic creation of primary key and index creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var console = {log: function(msg){ model.getAppView().logMessage(msg); }} | |
function getColumn(elements, name) { | |
for (var x in elements) { | |
var column = elements[x]; | |
if (column.getName() == name) { | |
return column; | |
} | |
} | |
return null; | |
} | |
// TABLE, INDEX NAME BASE, PRIMARY KEY, COLUMN | |
// INDEX NAME BASE will be apended to the table name, since indexes can't have the same name | |
var INDEXES = [ | |
["TABLE_1", "idx1", true, "col_1"], | |
["TABLE_1", "idx1", true, "col_2"], | |
["TABLE_1", "idx2", true, "col_2"], | |
["TABLE_1", "idx2", true, "col_3"], | |
["TABLE_2", "idx1", true, "col_1"], | |
["TABLE_2", "idx1", true, "col_2"], | |
]; | |
// Cleanup existing indexes | |
for (var x in INDEXES) { | |
var index_item = INDEXES[x]; | |
var table_name = index_item[0]; | |
var table = model.getTableSet().getByName(table_name); | |
if (table !== null) { | |
var existing_indexes = table.getKeys(); | |
for (var y in existing_indexes) { | |
existing_indexes[y].remove(); | |
} | |
} | |
} | |
for (var x in INDEXES) { | |
var index_item = INDEXES[x]; | |
var table_name = index_item[0]; | |
var base_index_name = index_item[1]; | |
var index_name = table_name + "_" + base_index_name; | |
var primary_key = index_item[2]; | |
var column_name = index_item[3]; | |
var table = model.getTableSet().getByName(table_name); | |
if (table === null) { | |
throw new Error("Can't create index on non-existing table" + table_name); | |
} | |
var column = getColumn(table.getElements(), column_name); | |
if (column === null) { | |
throw new Error("Index "+ index_name + " can't use non-existing column " + column_name); | |
} | |
var index = table.getKeySet().getByName(index_name); | |
if (index === null) { | |
index = table.createIndex(); | |
index.setDirty(true); | |
index.setName(index_name); | |
index.setPK(primary_key); | |
} | |
index.add(column); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment