@@ -14,7 +14,7 @@ const escapeMap = {
1414 '\\' : '\\\\' ,
1515}
1616
17- const surportedTypes = [ 'select' , 'delete' ]
17+ const surportedTypes = [ 'select' , 'delete' , 'update' , 'insert' ]
1818
1919function escape ( str ) {
2020 const res = [ ]
@@ -141,6 +141,7 @@ function funcToSQL(expr) {
141141 * @return {string }
142142 */
143143function columnsToSQL ( columns , tables ) {
144+ if ( ! columns ) return
144145 const baseTable = Array . isArray ( tables ) && tables [ 0 ]
145146 let isDual = false
146147 if ( baseTable && baseTable . type === 'dual' ) isDual = true
@@ -213,6 +214,40 @@ function withToSql(withExpr) {
213214 return `WITH ${ isRecursive } ${ withExprStr } `
214215}
215216
217+ /**
218+ * @param {Array } sets
219+ * @return {string }
220+ */
221+ function setToSQL ( sets ) {
222+ const baseTable = sets [ 0 ]
223+ const clauses = [ ]
224+ let str = ''
225+ if ( baseTable . column ) str = `\`${ baseTable . column } \``
226+ str = baseTable . value && ( str += ` = ${ exprToSQL ( baseTable . value ) } ` )
227+
228+ clauses . push ( str )
229+
230+ for ( let i = 1 ; i < sets . length ; ++ i ) {
231+ const joinExpr = sets [ i ]
232+
233+ if ( joinExpr . column ) str = `, \`${ joinExpr . column } \``
234+ str = joinExpr . value && ( str += ` = ${ exprToSQL ( joinExpr . value ) } ` )
235+
236+ clauses . push ( str )
237+ }
238+
239+ return clauses . join ( '' )
240+ }
241+
242+ /**
243+ * @param {Array } values
244+ * @return {string }
245+ */
246+ function valuesToSQL ( values ) {
247+ const clauses = values . map ( exprToSQL )
248+ return `(${ clauses . join ( '' ) } )`
249+ }
250+
216251/**
217252 * @param {Object } stmt
218253 * @param {?Array } stmt.with
@@ -256,7 +291,9 @@ function selectToSQL(stmt) {
256291
257292function deleteToSQL ( stmt ) {
258293 const clauses = [ 'DELETE' ]
259- console . warn ( 'delete' , Array . isArray ( stmt . tables ) )
294+ if ( stmt . columns === '*' ) clauses . push ( '*' )
295+ else columnsToSQL ( stmt . columns , stmt . from ) && clauses . push ( columnsToSQL ( stmt . columns , stmt . from ) )
296+
260297 if ( Array . isArray ( stmt . tables ) ) clauses . push ( tablesToSQL ( stmt . tables ) )
261298 if ( Array . isArray ( stmt . from ) ) clauses . push ( 'FROM' , tablesToSQL ( stmt . from ) )
262299
@@ -265,6 +302,28 @@ function deleteToSQL(stmt) {
265302 return clauses . join ( ' ' )
266303}
267304
305+ function updateToSQL ( stmt ) {
306+ const clauses = [ 'UPDATE' ]
307+ if ( has ( stmt , 'table' ) && stmt . table !== null ) clauses . push ( identifierToSql ( stmt . table , false ) )
308+ if ( Array . isArray ( stmt . tables ) ) clauses . push ( tablesToSQL ( stmt . tables ) )
309+ if ( Array . isArray ( stmt . set ) ) clauses . push ( 'SET' , setToSQL ( stmt . set ) )
310+
311+ if ( has ( stmt , 'where' ) && stmt . where !== null ) clauses . push ( `WHERE ${ exprToSQL ( stmt . where ) } ` )
312+
313+ return clauses . join ( ' ' )
314+ }
315+
316+ function insertToSQL ( stmt ) {
317+ const clauses = [ 'INSERT INTO' ]
318+ if ( has ( stmt , 'table' ) && stmt . table !== null ) clauses . push ( identifierToSql ( stmt . table , false ) )
319+ if ( Array . isArray ( stmt . tables ) ) clauses . push ( tablesToSQL ( stmt . tables ) )
320+ if ( Array . isArray ( stmt . columns ) ) clauses . push ( `(${ stmt . columns . map ( identifierToSql ) . join ( ', ' ) } )` )
321+ if ( Array . isArray ( stmt . values ) ) clauses . push ( 'VALUES' , valuesToSQL ( stmt . values ) )
322+ if ( has ( stmt , 'where' ) && stmt . where !== null ) clauses . push ( `WHERE ${ exprToSQL ( stmt . where ) } ` )
323+
324+ return clauses . join ( ' ' )
325+ }
326+
268327function unaryToSQL ( expr ) {
269328 const str = `${ expr . operator } ${ exprToSQL ( expr . expr ) } `
270329 return expr . parentheses ? `(${ str } )` : str
@@ -314,18 +373,20 @@ exprToSQLConvertFn = {
314373typeToSQLFn = {
315374 select : selectToSQL ,
316375 delete : deleteToSQL ,
376+ update : updateToSQL ,
377+ insert : insertToSQL ,
317378}
318379
319- function onlySelectSupported ( expr ) {
380+ function checkSupported ( expr ) {
320381 const ast = expr && expr . ast ? expr . ast : expr
321- if ( ! surportedTypes . includes ( ast . type ) ) throw new Error ( 'Only SELECT statements supported at the moment' )
382+ if ( ! surportedTypes . includes ( ast . type ) ) throw new Error ( ` ${ ast . type } statements not supported at the moment` )
322383}
323384
324385module . exports = function toSQL ( ast ) {
325386 if ( Array . isArray ( ast ) ) {
326- ast . forEach ( onlySelectSupported )
387+ ast . forEach ( checkSupported )
327388 return multipleToSQL ( ast )
328389 }
329- onlySelectSupported ( ast )
390+ checkSupported ( ast )
330391 return unionToSQL ( ast )
331392}
0 commit comments