@@ -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}
0 commit comments