77 SQLiteManagerDefault ,
88 SQLiteManagerCollate ,
99 SQLiteManagerForeignKeyOptions ,
10- SQLiteManagerForeignKeyOn
10+ SQLiteManagerForeignKeyOn ,
11+ keywords
1112} from './types'
1213
1314enum AT {
@@ -40,9 +41,14 @@ export class SQLiteManager {
4041 this . create = true
4142 if ( table . columns ) {
4243 this . create = false
44+ this . table = table
45+ } else {
46+ this . table = { name : this . escape ( table . name ) } as SQLiteManagerTable
4347 }
48+ } else {
49+ this . create = true
50+ this . table = { } as SQLiteManagerTable
4451 }
45- this . table = table
4652 }
4753 }
4854
@@ -60,10 +66,10 @@ export class SQLiteManager {
6066 /** If changing name in altertable you need to manually call the queryBuilder() */
6167 set name ( name : string ) {
6268 if ( this . create ) {
63- this . table . name = name
69+ this . table . name = this . escape ( name )
6470 } else {
65- this . table . name = name
66- this . queryBuilder ( AT . RENAME_TABLE , { name : name } as SQLiteManagerColumn )
71+ this . table . name = this . escape ( name )
72+ this . queryBuilder ( AT . RENAME_TABLE , { name : this . table . name } as SQLiteManagerColumn )
6773 }
6874 }
6975
@@ -241,6 +247,7 @@ export class SQLiteManager {
241247 * sql[]: SELECT sql FROM sqlite_schema WHERE tbl_name='X'; where X is the name of the table you're using
242248 */
243249 addColumn ( column : SQLiteManagerColumn , sql ?: string [ ] ) : string {
250+ column . name = this . escape ( column . name )
244251 if ( this . table . columns ) {
245252 if ( typeof this . findColumn ( column . name ) == 'undefined' ) {
246253 this . table . columns . push ( column )
@@ -279,16 +286,19 @@ export class SQLiteManager {
279286
280287 renameColumn ( oldColumnName : string , newColumnName : string ) : string {
281288 const i = this . findColumn ( oldColumnName )
289+ newColumnName = this . escape ( newColumnName )
290+ let query = ''
282291
283292 if ( typeof i != 'undefined' && this . table . columns ) {
284293 if ( typeof this . findColumn ( newColumnName ) == 'undefined' ) {
285294 this . table . columns [ i ] . name = newColumnName
286295 } else {
287296 throw new Error ( 'Column already exists' )
288297 }
298+ query = this . queryBuilder ( AT . RENAME_COLUMN , { name : oldColumnName } as SQLiteManagerColumn , newColumnName )
289299 }
290300
291- return this . queryBuilder ( AT . RENAME_COLUMN , { name : oldColumnName } as SQLiteManagerColumn , newColumnName )
301+ return query
292302 }
293303
294304 /**
@@ -350,4 +360,17 @@ export class SQLiteManager {
350360 }
351361 return false
352362 }
363+
364+ private escape ( name : string ) : string {
365+ keywords . forEach ( keyword => {
366+ if ( name . toUpperCase ( ) == keyword ) {
367+ throw new Error ( "You can't use a SQLite keyword as a name" )
368+ }
369+ } )
370+
371+ name = name . replace ( / ' / g, "''" )
372+ name = name . replace ( / " / g, '""' )
373+
374+ return name
375+ }
353376}
0 commit comments