Skip to content

Commit 75bf24d

Browse files
committed
[hygiene] String reuse
1 parent c623c14 commit 75bf24d

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/persisters/sqlite/commands.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import {Id} from '../../types/common';
3535
export type Cmd = (sql: string, args?: any[]) => Promise<IdObj<any>[]>;
3636
type Schema = IdMap2<string>;
3737

38+
const TABLE = 'TABLE';
39+
const ALTER_TABLE = 'ALTER ' + TABLE;
40+
const DELETE_FROM = 'DELETE FROM';
3841
const SELECT_STAR_FROM = SELECT + '*FROM';
3942
const FROM_PRAGMA_TABLE = 'FROM pragma_table_';
4043
const WHERE = 'WHERE';
@@ -151,16 +154,20 @@ export const getCommandFunctions = (
151154
arrayIsEmpty(tableColumnNames) &&
152155
collHas(schemaMap, tableName)
153156
) {
154-
await cmd('DROP TABLE' + escapeId(tableName));
157+
await cmd('DROP ' + TABLE + escapeId(tableName));
155158
mapSet(schemaMap, tableName);
156159
return;
157160
}
158161

159162
// Create the table or alter or drop columns
160163
if (!arrayIsEmpty(tableColumnNames) && !collHas(schemaMap, tableName)) {
161164
await cmd(
162-
`CREATE TABLE${escapeId(tableName)}(${escapeId(rowIdColumnName)} ` +
163-
`PRIMARY KEY ON CONFLICT REPLACE${arrayJoin(
165+
`CREATE ` +
166+
TABLE +
167+
escapeId(tableName) +
168+
'(' +
169+
escapeId(rowIdColumnName) +
170+
` PRIMARY KEY ON CONFLICT REPLACE${arrayJoin(
164171
arrayMap(tableColumnNames, (cellId) => COMMA + escapeId(cellId)),
165172
)});`,
166173
);
@@ -182,7 +189,7 @@ export const getCommandFunctions = (
182189
...arrayMap(tableColumnNames, async (columnName) => {
183190
if (!collDel(columnNamesAccountedFor, columnName)) {
184191
await cmd(
185-
`ALTER TABLE${escapeId(tableName)}ADD${escapeId(columnName)}`,
192+
ALTER_TABLE + escapeId(tableName) + 'ADD' + escapeId(columnName),
186193
);
187194
mapSet(tableSchemaMap, columnName, EMPTY_STRING);
188195
}
@@ -193,9 +200,10 @@ export const getCommandFunctions = (
193200
async (columnName) => {
194201
if (columnName != rowIdColumnName) {
195202
await cmd(
196-
`ALTER TABLE${escapeId(tableName)}DROP${escapeId(
197-
columnName,
198-
)}`,
203+
ALTER_TABLE +
204+
escapeId(tableName) +
205+
'DROP' +
206+
escapeId(columnName),
199207
);
200208
mapSet(tableSchemaMap, columnName);
201209
}
@@ -208,13 +216,13 @@ export const getCommandFunctions = (
208216
// Insert or update or delete data
209217
if (partial) {
210218
if (isUndefined(table)) {
211-
await cmd('DELETE FROM' + escapeId(tableName) + 'WHERE 1');
219+
await cmd(DELETE_FROM + escapeId(tableName) + WHERE + ' 1');
212220
} else {
213221
await promiseAll(
214222
objMap(table, async (row, rowId) => {
215223
if (isUndefined(row)) {
216224
await cmd(
217-
'DELETE FROM' +
225+
DELETE_FROM +
218226
escapeId(tableName) +
219227
WHERE +
220228
escapeId(rowIdColumnName) +
@@ -259,7 +267,7 @@ export const getCommandFunctions = (
259267
useOnConflict,
260268
);
261269
await cmd(
262-
'DELETE FROM' +
270+
DELETE_FROM +
263271
escapeId(tableName) +
264272
WHERE +
265273
escapeId(rowIdColumnName) +
@@ -269,7 +277,7 @@ export const getCommandFunctions = (
269277
deleteRowIds,
270278
);
271279
} else if (collHas(schemaMap, tableName)) {
272-
await cmd('DELETE FROM' + escapeId(tableName) + 'WHERE 1');
280+
await cmd(DELETE_FROM + escapeId(tableName) + WHERE + ' 1');
273281
}
274282
}
275283
};

0 commit comments

Comments
 (0)