This repository was archived by the owner on Jan 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbasedialect.js
More file actions
236 lines (217 loc) · 7.11 KB
/
basedialect.js
File metadata and controls
236 lines (217 loc) · 7.11 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
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
/**
* @module dialects/basedialect
* @typedef {Object} Dialect
*/
/**
* The SQL query string generator
* @property {SqlGenerator}
*/
exports.SqlGenerator = require("../query/sqlgenerator");
/**
* Contains the opening quote character used to quote table and column names
* @property {String}
*/
exports.openQuote = '"';
/**
* Contains the closing quote character used to quote table and column names
* @property {String}
*/
exports.closeQuote = '"';
/**
* Returns the SQL string to use for creating a column within a table
* @param {Mapping} mapping The mapping
* @returns {String} The SQL string
*/
exports.getColumnSql = function(mapping) {
if (!this.hasOwnProperty("dataTypes") || this.dataTypes === undefined) {
throw new Error("No data types defined for dialect");
}
const dataType = this.dataTypes[mapping.type];
if (typeof(dataType) !== "function") {
throw new Error("Missing or invalid dialect data type " + mapping.type);
}
return dataType(mapping);
};
/**
* Returns the storage engine type. This is only needed for MySQL databases.
* Defaults to null
* @returns {String} The storage engine type
*/
exports.getEngineType = function() {
return null;
};
/**
* Returns the string passed as argument enclosed in quotes
* @param {String} str The string to enclose in quotes
* @param {String} prefix Optional prefix which is also quoted
* @returns {String} The string enclosed in quotes
*/
exports.quote = function(str, prefix) {
const buf = [];
if (typeof(prefix) === "string" && prefix.length > 0) {
buf.push(this.openQuote, prefix, this.closeQuote, ".");
}
buf.push(this.openQuote, str, this.closeQuote);
return buf.join("");
};
/** @ignore */
exports.toString = function() {
return "[BaseDialect]";
};
/**
* True if the underlying database supports sequences. Dialect
* implementations should override this. Defaults to false.
* @property {Boolean}
*/
exports.hasSequenceSupport = false;
/**
* Returns the SQL statement for retrieving the next value of a sequence. Dialect
* implementations should override this.
* @param {String} sequenceName The name of the sequence
* @returns {String} The SQL statement
*/
exports.getSqlNextSequenceValue = function(sequenceName) {
throw new Error("No sequence support");
};
/**
* Returns the SQL statement for inserting a sequence value. Dialect
* implementations should override this.
* @param {String} sequenceName The name of the sequence
* @returns {String} The SQL statement
*/
exports.getInsertNextSequenceValue = function(sequenceName) {
throw new Error("No sequence support");
};
/**
* Returns the SQL statement for querying sequence names
* @returns {String} The SQL statement
*/
exports.getSqlQuerySequenceNames = function() {
throw new Error("No sequence support");
};
/**
* Extends the SQL statement passed as argument with a limit restriction. Dialect
* implementations should override this.
* @param {String} sql The SQL statement to add the limit restriction to
* @param {Number} limit The limit
* @returns {String} The SQL statement
*/
exports.addSqlLimit = function(sql, limit) {
throw new Error("Limit not implemented");
};
/**
* Extends the SQL statement passed as argument with an offset restriction. Dialect
* implementations should override this.
* @param {String} sql The SQL statement to add the offset restriction to
* @param {Number} offset The offset
* @returns {String} The SQL statement
*/
exports.addSqlOffset = function(sql, offset) {
throw new Error("Offset not implemented");
};
/**
* Extends the SQL statement passed as argument with a range restriction. Dialect
* implementations should override this.
* @param {String} sql The SQL statement to add the range restriction to
* @param {Number} offset The offset
* @param {Number} limit The limit
* @returns {String} The SQL statement
*/
exports.addSqlRange = function(sql, offset, limit) {
throw new Error("Range not implemented");
};
/**
* Returns the name of the default schema. Dialect implementations can override this.
* @param {java.sql.Connection} conn The connection to use
* @returns {String} The name of the default schema
*/
exports.getDefaultSchema = function(conn) {
return conn.getSchema();
};
/**
* Returns the SQL statement to use for updating a single record
* @param {Mapping} mapping The entity mapping to use
* @returns {String} The SQL statement
*/
exports.getUpdateSql = function(mapping) {
const buf = ["UPDATE ", this.quote(mapping.tableName, mapping.schemaName), " SET "];
const max = mapping.mappings.length;
// start at idx 1 to skip the id mapping
for (let idx=1; idx<max; idx+=1) {
let propMapping = mapping.mappings[idx];
if (idx > 1) {
buf.push(", ");
}
buf.push(this.quote(propMapping.column), " = ?");
}
buf.push(" WHERE ", this.quote(mapping.id.column), " = ?");
return buf.join("");
};
/**
* Returns the SQL statement to use for inserting a single record
* @param {Mapping} mapping The entity mapping to use
* @returns {String} The SQL statement
*/
exports.getInsertSql = function(mapping) {
const buf = ["INSERT INTO ", this.quote(mapping.tableName, mapping.schemaName),
" (", this.quote(mapping.id.column)];
const values = new Array(mapping.mappings);
if (mapping.id.sequence && this.hasSequenceSupport) {
values[0] = this.getInsertNextSequenceValue(mapping.id.sequence);
} else {
values[0] = "DEFAULT";
}
const max = mapping.mappings.length;
if (max > 0) {
buf.push(", ");
}
for (let idx = 1; idx < max; idx += 1) {
let propMapping = mapping.mappings[idx];
if (idx > 1) {
buf.push(", ");
}
buf.push(this.quote(propMapping.column));
values[idx] = "?";
}
buf.push(") VALUES (", values.join(", "), ")");
return buf.join("");
};
/**
* Returns the SQL statement to use for retrieving a single record
* @param {Mapping} mapping The entity mapping to use
* @returns {String} The SQL statement
*/
exports.getSelectSql = function(mapping) {
return [
"SELECT",
mapping.mappings.map(function(propMapping) {
return this.quote(propMapping.column);
}, this).join(", "),
"FROM",
this.quote(mapping.tableName, mapping.schemaName),
" WHERE",
this.quote(mapping.id.column), "= ?"
].join(" ");
};
/**
* Returns the SQL statement to use for checking the existence of a record
* @param {Mapping} mapping The entity mapping to use
* @returns {String} The SQL statement
*/
exports.getExistsSql = function(mapping) {
return [
"SELECT 1 FROM ", this.quote(mapping.tableName, mapping.schemaName),
" WHERE ", this.quote(mapping.id.column), " = ?"
].join(" ");
};
/**
* Returns the SQL statement to use for removing a single record
* @param {Mapping} mapping The entity mapping to use
* @returns {String} The SQL statement
*/
exports.getRemoveSql = function(mapping) {
return [
"DELETE FROM", this.quote(mapping.tableName, mapping.schemaName),
"WHERE", this.quote(mapping.id.column), "= ?"
].join(" ");
};