|
| 1 | +/* eslint-disable prettier/prettier */ |
| 2 | +import { |
| 3 | + SQLiteManagerType, |
| 4 | + SQLiteManagerColumn, |
| 5 | + SQLiteManagerTable, |
| 6 | + SQLiteManagerConstraints, |
| 7 | + SQLiteManagerDefault, |
| 8 | + SQLiteManagerCollate, |
| 9 | + SQLiteManagerForeignKeyOptions, |
| 10 | + SQLiteManagerForeignKeyOn |
| 11 | +} from './types' |
| 12 | + |
| 13 | +export class SQLiteManager { |
| 14 | + private table: SQLiteManagerTable |
| 15 | + private create: boolean |
| 16 | + |
| 17 | + constructor(table?: SQLiteManagerTable) { |
| 18 | + if (typeof table === 'undefined') { |
| 19 | + this.create = true |
| 20 | + this.table = {} as SQLiteManagerTable |
| 21 | + } else { |
| 22 | + if (table.name) { |
| 23 | + this.create = true |
| 24 | + } else { |
| 25 | + this.create = false |
| 26 | + } |
| 27 | + this.table = table |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + reset(): void { |
| 32 | + this.table = {} as SQLiteManagerTable |
| 33 | + } |
| 34 | + |
| 35 | + set name(name: string) { |
| 36 | + this.table.name = name |
| 37 | + } |
| 38 | + |
| 39 | + queryBuilder(): string { |
| 40 | + let query = '' |
| 41 | + |
| 42 | + if (this.create && this.table.columns) { |
| 43 | + query += 'CREATE TABLE "' + this.table.name + '" (' |
| 44 | + |
| 45 | + for (let j = 0; j < this.table.columns.length; j++) { |
| 46 | + const column: SQLiteManagerColumn = this.table.columns[j] |
| 47 | + |
| 48 | + query += '"' + column.name + '" ' + SQLiteManagerType[column.type] |
| 49 | + |
| 50 | + if (column.constraints) { |
| 51 | + const constraints: string[] = Object.keys(column.constraints).filter(key => { |
| 52 | + if (column.constraints) { |
| 53 | + return column.constraints[key as keyof SQLiteManagerConstraints] |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + constraints.forEach(constraint => { |
| 58 | + query += ' ' + constraint.replace('_', ' ') |
| 59 | + }) |
| 60 | + |
| 61 | + if (column.constraints.Check) { |
| 62 | + query += ' CHECK (' + column.constraints.Check + ')' |
| 63 | + } |
| 64 | + |
| 65 | + if (column.constraints.Default) { |
| 66 | + query += ' DEFAULT ' |
| 67 | + if (typeof column.constraints.Default === 'string') { |
| 68 | + query += column.constraints.Default |
| 69 | + } else { |
| 70 | + query += SQLiteManagerDefault[column.constraints.Default] |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (column.constraints.Collate) { |
| 75 | + query += ' COLLATE ' |
| 76 | + if (typeof column.constraints.Collate === 'string') { |
| 77 | + query += column.constraints.Collate |
| 78 | + } else { |
| 79 | + query += SQLiteManagerCollate[column.constraints.Collate] |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (column.constraints.ForeignKey) { |
| 84 | + if (column.constraints.ForeignKey.enabled) { |
| 85 | + query += ' REFERENCES ' + column.constraints.ForeignKey.table + '(' + column.constraints.ForeignKey.column + ')' |
| 86 | + if (column.constraints.ForeignKey.options) { |
| 87 | + query += ' ' + SQLiteManagerForeignKeyOptions[column.constraints.ForeignKey.options] |
| 88 | + } |
| 89 | + |
| 90 | + if (column.constraints.ForeignKey.onDelete) { |
| 91 | + query += ' ON DELETE ' + SQLiteManagerForeignKeyOn[column.constraints.ForeignKey.onDelete] |
| 92 | + } |
| 93 | + |
| 94 | + if (column.constraints.ForeignKey.onUpdate) { |
| 95 | + query += ' ON UPDATE ' + SQLiteManagerForeignKeyOn[column.constraints.ForeignKey.onUpdate] |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (j < this.table.columns.length - 1) { |
| 102 | + query += ', ' |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + query += ');' |
| 107 | + } |
| 108 | + |
| 109 | + return query |
| 110 | + } |
| 111 | + |
| 112 | + mixTables(tables: SQLiteManagerTable, newTables: Partial<SQLiteManagerTable>): SQLiteManagerTable { |
| 113 | + return { ...tables, ...newTables } |
| 114 | + } |
| 115 | + |
| 116 | + addColumn(column: SQLiteManagerColumn): string { |
| 117 | + this.table = this.mixTables(this.table, { name: this.table.name, columns: [Object.create(column)] }) |
| 118 | + return this.queryBuilder() |
| 119 | + } |
| 120 | + |
| 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 | + } |
| 127 | + } |
| 128 | + //this.tables[index].columns = this.tables[index].columns.filter(column => column.name !== columnName) it's slower |
| 129 | + |
| 130 | + return this.queryBuilder() |
| 131 | + } |
| 132 | + |
| 133 | + 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 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return this.queryBuilder() |
| 142 | + } |
| 143 | + |
| 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 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return this.queryBuilder() |
| 153 | + } |
| 154 | + |
| 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 | + } */ |
| 162 | + |
| 163 | + this.table = this.mixTables(this.table, { |
| 164 | + columns: [{ name: name, type: type, constraints: constraints }] |
| 165 | + }) |
| 166 | + |
| 167 | + return this.queryBuilder() |
| 168 | + } |
| 169 | +} |
0 commit comments