-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathoracle-renderer.ts
More file actions
196 lines (151 loc) · 7.06 KB
/
oracle-renderer.ts
File metadata and controls
196 lines (151 loc) · 7.06 KB
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
import { FieldType } from "@/lib/schemas/field-schema";
import { TableType } from "@/lib/schemas/table-schema";
import { AST } from "node-sql-parser";
import BaseDatabaseRenderer, { ASTStatment } from "./base-database-renderer";
import { DEFAULT_LENGTH_PARAM, ForeignKeyActions, Modifiers } from "@/lib/field";
import { IndexType } from "@/lib/schemas/index-schema";
import { RelationshipType } from "@/lib/schemas/relationship-schema";
import { DataType } from "@/lib/schemas/data-type-schema";
import { DatabaseDialect } from "@/lib/database";
import { format } from 'sql-formatter';
export default class OracleRenderer extends BaseDatabaseRenderer {
public constructor(data_types: DataType[]) {
super(DatabaseDialect.ORACLE, data_types)
}
protected async astToSQL(ast: ASTStatment[]): Promise<string> {
let sql = ast.join("\n");
sql = format(sql, { language: 'sql' });
return sql;
}
protected createTableAst(table: TableType): ASTStatment[] | ASTStatment {
const ast: any = super.createTableAst(table);
let pk_constraint: string = ast.constraints?.pop();
if (pk_constraint)
pk_constraint = "," + pk_constraint
else
pk_constraint = "";
let indexes: string = ast.indices_ast.filter((index: string) => index != null).join("\n")
return `
CREATE TABLE ${table.name} (
${(ast as any).field_definitions.join(",\n")}
${pk_constraint}
);
${indexes}
` ;
}
protected getFieldDefinition(field: FieldType, table: TableType, ignorePkContraint: boolean = false, dropDefaultValue: boolean = false): ASTStatment {
const ast: any = super.getFieldDefinition(field, table, ignorePkContraint);
let primaryKey: string = ast.primary_key ? "PRIMARY KEY" : "";
let autoIncrement: string = ast.modifiers.includes(Modifiers.AUTO_INCREMENT) ? (ast.auto_increment ? "GENERATED BY DEFAULT AS IDENTITY" : "") : ""
let nullable: string = ""
let unique: string = "";
if (!ast.primary_key)
nullable = (field.nullable !== undefined) ? (field.nullable ? "NULL" : "NOT NULL") : "";
if (!ast.primary_key)
unique = field.unique ? "UNIQUE" : "";
let options: number[] | string = [];
if (ast.length)
options.push(ast.length);
else if (ast.modifiers.includes(Modifiers.LENGTH))
options.push(DEFAULT_LENGTH_PARAM);
if (ast.scale && ast.scale != "0")
options.push(ast.scale);
options = options.length > 0 ? `(${options.join(",")})` : "";
let defaultValue: any = "";
if (ast.default_value !== undefined && ast.default_value !== null) {
defaultValue = "DEFAULT " + ast.default_value
}
if (dropDefaultValue) {
defaultValue = "DEFAULT NULL"
}
let dataType = ast.dataType;
if (dataType === "TIMESTAMP WITH LOCAL TIME ZONE" && options) {
dataType = `TIMESTAMP${options} WITH LOCAL TIME ZONE`
}
else if (dataType === "TIMESTAMP WITH TIME ZONE" && options) {
dataType = `TIMESTAMP${options} WITH TIME ZONE`
}
else if (dataType === "INTERVAL DAY TO SECOND" && options) {
dataType = `INTERVAL DAY${options} TO SECOND`
}
else if (dataType === "INTERVAL YEAR TO MONTH" && options) {
dataType = `INTERVAL YEAR${options} TO MONTH`
}
else
dataType = dataType + options
//const dataType = field.type.name == "uuid" ? "RAW(16)" : ast.dataType;
return `${field.name} ${dataType} ${defaultValue} ${nullable} ${unique} ${autoIncrement} ${primaryKey}`;
}
protected processDefaultValue(field: FieldType): AST | null {
const ast: any = super.processDefaultValue(field);
if (ast && ast.default_value !== undefined && ast.default_value !== null) {
if (ast.default_value_type == "single_quote_string") {
ast.default_value = `'${ast.default_value}'`
}
else if (ast.default_value_type == "number" || ast.default_value_type == "bool")
ast.default_value = ast.default_value;
else if (ast.default_value_type == "function") {
if (ast.default_value == "CURRENT_TIMESTAMP") {
switch (field.type.name?.toUpperCase()) {
case "DATE":
ast.default_value = "SYSDATE"
break;
case "TIMESTAMP":
ast.default_value = "CURRENT_TIMESTAMP"
break;
case "TIMESTAMP WITH TIME ZONE":
ast.default_value = "SYSTIMESTAMP"
break;
case "TIMESTAMP WITH LOCAL TIME ZONE":
ast.default_value = "SYSTIMESTAMP"
break;
}
} else if (ast.default_value == "random") {
ast.default_value = "SYS_GUID()"
}
}
}
return ast;
}
protected createIndexAst(table: TableType, index: IndexType): ASTStatment {
const unique: string = index.unique ? " UNIQUE" : "";
let columns: string[] | string = index.fields.map((field: FieldType) => field.name);
if (columns.length == 0)
return null;
columns = `(${columns.join(",")})`
return `CREATE${unique} INDEX ${index.name} ON ${table.name} ${columns} ;`
}
protected createRelationshipAst(relationship: RelationshipType): ASTStatment {
const { primaryKey, foreignKey, sourceTable, targetTable } = super.createRelationshipAst(relationship) as any;
const constraintName: string = relationship.name ? " CONSTRAINT " + relationship.name : "";
const FKAction: string | null = this.foreignKeyActionToAst(relationship.onDelete as ForeignKeyActions);
const onDeleteAction: string = FKAction ? `ON DELETE ${FKAction}` : ""
return `
ALTER TABLE ${targetTable.name}
ADD ${constraintName} FOREIGN KEY (${foreignKey.name})
REFERENCES ${sourceTable.name}(${primaryKey.name}) ${onDeleteAction};
`
}
protected foreignKeyActionToAst(action: ForeignKeyActions): string | null {
switch (action) {
case ForeignKeyActions.CASCADE:
return "CASCADE";
case ForeignKeyActions.SET_NULL:
return "SET NULL";
}
return null;
}
protected getPrimaryKeyContraint(fields: FieldType[]): ASTStatment {
//throw new Error("Method not implemented.");
const pks: string[] = fields.map((field: FieldType) => field.name);
return `
PRIMARY KEY (${pks.join(",")})
`
}
protected startTransaction(): string {
throw new Error("Method not implemented.");
}
protected commit(): string {
throw new Error("Method not implemented.");
}
}