Skip to content

Commit 59052ba

Browse files
committed
create table fixes, types fixes
1 parent 3c575de commit 59052ba

4 files changed

Lines changed: 61 additions & 55 deletions

File tree

src/manager.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,61 +109,62 @@ export class SQLiteManager {
109109
return query
110110
}
111111

112-
mixTables(tables: SQLiteManagerTable, newTables: Partial<SQLiteManagerTable>): SQLiteManagerTable {
113-
return { ...tables, ...newTables }
114-
}
115-
116112
addColumn(column: SQLiteManagerColumn): string {
117-
this.table = this.mixTables(this.table, { name: this.table.name, columns: [Object.create(column)] })
113+
if (this.table.columns) {
114+
this.table.columns.push(column)
115+
} else {
116+
this.table.columns = [column]
117+
}
118+
118119
return this.queryBuilder()
119120
}
120121

121-
deleteColumn(columnName: string): string {
122-
if (this.table.columns) {
123-
const i = this.table.columns.findIndex(column => column.name === columnName)
124-
if (i > -1) {
125-
this.table.columns.splice(i, 1)
126-
}
122+
deleteColumn(name: string): string {
123+
const i = this.findColumn(name)
124+
125+
if (typeof i != 'undefined' && this.table.columns) {
126+
this.table.columns.splice(i, 1)
127127
}
128-
//this.tables[index].columns = this.tables[index].columns.filter(column => column.name !== columnName) it's slower
129128

130129
return this.queryBuilder()
131130
}
132131

133132
renameColumn(oldColumnName: string, newColumnName: string): string {
134-
if (this.table.columns) {
135-
const i = this.table.columns.findIndex(column => column.name === oldColumnName)
136-
if (i > -1) {
137-
this.table.columns[i].name = newColumnName
138-
}
133+
const i = this.findColumn(oldColumnName)
134+
135+
if (typeof i != 'undefined' && this.table.columns) {
136+
this.table.columns[i].name = newColumnName
139137
}
140138

141139
return this.queryBuilder()
142140
}
143141

144-
changeColumnType(columnName: string, type: SQLiteManagerType): string {
145-
if (this.table.columns) {
146-
const i = this.table.columns.findIndex(column => column.name === columnName)
147-
if (i > -1) {
148-
this.table.columns[i].type = type
149-
}
142+
changeColumnType(name: string, type: SQLiteManagerType): string {
143+
const i = this.findColumn(name)
144+
145+
if (typeof i != 'undefined' && this.table.columns) {
146+
this.table.columns[i].type = type
150147
}
151148

152149
return this.queryBuilder()
153150
}
154151

155-
changeColumnConstraints(name: string, type: SQLiteManagerType, constraints: SQLiteManagerConstraints): string {
156-
/* if (typeof this.table.columns != 'undefined') {
157-
const i = this.table.columns.findIndex(column => column.name === columnName)
158-
if (i > -1) {
159-
this.table.columns[i].constraints = constraints
160-
}
161-
} */
152+
changeColumnConstraints(name: string, constraints: SQLiteManagerConstraints): string {
153+
const i = this.findColumn(name)
162154

163-
this.table = this.mixTables(this.table, {
164-
columns: [{ name: name, type: type, constraints: constraints }]
165-
})
155+
if (typeof i != 'undefined' && this.table.columns) {
156+
this.table.columns[i].constraints = constraints
157+
}
166158

167159
return this.queryBuilder()
168160
}
161+
162+
private findColumn(name: string): number | undefined {
163+
if (this.table.columns) {
164+
const i = this.table.columns.findIndex(column => column.name === name)
165+
if (i > -1) {
166+
return i
167+
}
168+
}
169+
}
169170
}

src/types.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,24 @@ export enum SQLiteCloudArrayType {
130130
ARRAY_TYPE_SQLITE_STATUS = 50 // used in sqlite_status
131131
}
132132

133-
/** SQLite column types*/
133+
/** SQLite Datatypes*/
134134
export enum SQLiteManagerType {
135-
TEXT,
135+
NULL,
136136
INTEGER,
137137
REAL,
138-
BLOB,
139-
VARCHAR,
140-
SMALLINT,
141-
FLOAT,
142-
DOUBLE,
143-
BOOLEAN,
144-
CURRENCY,
145-
DATE,
146-
TIME,
147-
TIMESTAMP,
148-
BINARY
138+
TEXT,
139+
BLOB
149140
}
150141

151-
/** SQLite column defaults */
142+
/** SQLite Default clause */
152143
export enum SQLiteManagerDefault {
153144
NULL,
154145
CURRENT_TIME,
155146
CURRENT_DATE,
156147
CURRENT_TIMESTAMP
157148
}
158149

159-
/** SQLite column collates */
150+
/** SQLite Collate clause */
160151
export enum SQLiteManagerCollate {
161152
BINARY,
162153
NOCASE,
@@ -174,7 +165,7 @@ export enum SQLiteManagerForeignKeyOptions {
174165
NOT_DEFERRABLE_INITIALLY_IMMEDIATE
175166
}
176167

177-
/** SQLite column foreign key on delete or on update cases */
168+
/** SQLite ON DELETE and ON UPDATE Actions */
178169
export enum SQLiteManagerForeignKeyOn {
179170
NO_ACTION,
180171
RESTRICT,
@@ -191,17 +182,19 @@ class SQLiteManagerForeignKey {
191182
options: SQLiteManagerForeignKeyOptions = SQLiteManagerForeignKeyOptions.NONE
192183
onDelete: SQLiteManagerForeignKeyOn = SQLiteManagerForeignKeyOn.NO_ACTION
193184
onUpdate: SQLiteManagerForeignKeyOn = SQLiteManagerForeignKeyOn.NO_ACTION
185+
match = ''
194186

195187
constructor(
196188
enabled: boolean,
197189
table: string,
198190
column: string,
199191
options?: SQLiteManagerForeignKeyOptions,
200192
onDelete?: SQLiteManagerForeignKeyOn,
201-
onUpdate?: SQLiteManagerForeignKeyOn
193+
onUpdate?: SQLiteManagerForeignKeyOn,
194+
match?: string
202195
) {
203196
if (enabled) {
204-
this.enable(table, column, options, onDelete, onUpdate)
197+
this.enable(table, column, options, onDelete, onUpdate, match)
205198
} else {
206199
this.disable()
207200
}
@@ -215,9 +208,17 @@ class SQLiteManagerForeignKey {
215208
this.options = SQLiteManagerForeignKeyOptions.NONE
216209
this.onDelete = SQLiteManagerForeignKeyOn.NO_ACTION
217210
this.onUpdate = SQLiteManagerForeignKeyOn.NO_ACTION
211+
this.match = ''
218212
}
219213

220-
enable(table: string, column: string, options?: SQLiteManagerForeignKeyOptions, onDelete?: SQLiteManagerForeignKeyOn, onUpdate?: SQLiteManagerForeignKeyOn) {
214+
enable(
215+
table: string,
216+
column: string,
217+
options?: SQLiteManagerForeignKeyOptions,
218+
onDelete?: SQLiteManagerForeignKeyOn,
219+
onUpdate?: SQLiteManagerForeignKeyOn,
220+
match?: string
221+
) {
221222
this.enabled = true
222223
this.table = table
223224
this.column = column
@@ -233,6 +234,10 @@ class SQLiteManagerForeignKey {
233234
if (onUpdate) {
234235
this.onUpdate = onUpdate
235236
}
237+
238+
if (match) {
239+
this.match = match
240+
}
236241
}
237242
}
238243

test/assets/manager-test-tables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const testTable = {
1717
},
1818
{
1919
name: 'column2',
20-
type: types.SQLiteManagerType.TEXT,
20+
type: types.SQLiteManagerType.REAL,
2121
constraints: {
2222
NOT_NULL: true,
2323
UNIQUE: true

test/manager.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Create a table', () => {
1313
'CREATE TABLE "' + testTable.name + '" ("' + testTable.columns[0].name + '" ' + SQLiteManagerType[testTable.columns[0].type]
1414
)
1515

16-
manager.addColumn(testTable.columns[1])
16+
manager.addColumn(JSON.parse(JSON.stringify(testTable.columns[1])))
1717
expect(manager.deleteColumn(testTable.columns[0].name)).not.toContain(testTable.columns[0].name)
1818

1919
const risRen: string = manager.renameColumn(testTable.columns[1].name, testTable.columns[2].name)
@@ -26,7 +26,7 @@ describe('Create a table', () => {
2626
expect(risCh).toContain(SQLiteManagerType[SQLiteManagerType.TEXT])
2727
expect(risCh).not.toContain(SQLiteManagerType[SQLiteManagerType.INTEGER])
2828

29-
const risCnstr: string = manager.changeColumnConstraints(testTable.columns[2].name, testTable.columns[2].type, testTable.columns[2].constraints)
29+
const risCnstr: string = manager.changeColumnConstraints(testTable.columns[2].name, testTable.columns[2].constraints)
3030

3131
expect(risCnstr).toContain('NOT NULL UNIQUE')
3232
})

0 commit comments

Comments
 (0)