Skip to content

Commit c56ab0c

Browse files
committed
format code
1 parent 0ecf296 commit c56ab0c

9 files changed

Lines changed: 114 additions & 117 deletions

File tree

src/column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function columnDefinitionToSQL(columnDefinition) {
7777
*/
7878
function columnsToSQL(columns, tables) {
7979
if (!columns) return
80+
if (columns === '*') return columns
8081
const baseTable = Array.isArray(tables) && tables[0]
8182
let isDual = false
8283
if (baseTable && baseTable.type === 'dual') isDual = true

src/command.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import has from 'has'
2-
import { tablesToSQL } from './tables'
2+
import { tablesToSQL, tableToSQL } from './tables'
33
import { identifierToSql } from './util'
44
import { exprToSQL } from './expr'
55

@@ -23,21 +23,16 @@ function truncateToSQL(stmt) {
2323
}
2424

2525
function renameToSQL(stmt) {
26-
const type = `${stmt.type && stmt.type.toUpperCase()} TABLE `
26+
const { type, table } = stmt
2727
const clauses = []
28-
if (has(stmt, 'table') && stmt.table !== null) {
29-
for (const tables of stmt.table) {
30-
const renameInfo = []
31-
for (const tableInfo of tables) {
32-
let str = ''
33-
if (has(tableInfo, 'db') && tableInfo.db !== null) str += `${identifierToSql(tableInfo.db)}.`
34-
if (has(tableInfo, 'table') && tableInfo.table !== null) str += identifierToSql(tableInfo.table, false)
35-
renameInfo.push(str)
36-
}
28+
const prefix = `${type && type.toUpperCase()} TABLE`
29+
if (table) {
30+
for (const tables of table) {
31+
const renameInfo = tables.map(tableToSQL)
3732
clauses.push(renameInfo.join(' TO '))
3833
}
3934
}
40-
return type + clauses.join(', ')
35+
return `${prefix} ${clauses.join(', ')}`
4136
}
4237

4338
function useToSQL(stmt) {

src/create.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { columnDefinitionToSQL } from './column'
44
import { constraintDefinitionToSQL } from './constrain'
55
import { tablesToSQL, tableOptionToSQL } from './tables'
66
import { unionToSQL } from './union'
7+
import { toUpper, hasVal } from './util'
78

89
function createDefinitionToSQL(definition) {
10+
if (!definition) return []
911
const { resource } = definition
1012
switch (resource) {
1113
case 'column':
@@ -21,31 +23,23 @@ function createDefinitionToSQL(definition) {
2123

2224
function createToSQL(stmt) {
2325
const {
24-
type,
25-
keyword,
26+
type, keyword, table, like, as,
2627
temporary,
2728
if_not_exists: ifNotExists,
28-
table,
29-
like,
3029
create_definitions: createDefinition,
3130
table_options: tableOptions,
3231
ignore_replace: ignoreReplace,
33-
as,
3432
query_expr: queryExpr,
3533
} = stmt
36-
const action = type.toUpperCase()
37-
const sql = [action]
38-
if (temporary) sql.push(temporary.toUpperCase())
39-
sql.push(keyword.toUpperCase())
40-
if (ifNotExists) sql.push(ifNotExists.toUpperCase())
34+
const sql = [toUpper(type), toUpper(temporary), toUpper(keyword), toUpper(ifNotExists)]
4135
const tableName = tablesToSQL(table)
4236
sql.push(tableName)
4337
if (like) {
4438
const { type: likeType, table: likeTable } = like
45-
sql.push(likeType.toUpperCase())
39+
sql.push(toUpper(likeType))
4640
const likeTableName = tablesToSQL(likeTable)
4741
sql.push(likeTableName)
48-
return sql.join(' ')
42+
return sql.filter(hasVal).join(' ')
4943
}
5044
if (createDefinition) {
5145
const createDefinitionList = createDefinition.map(createDefinitionToSQL)
@@ -55,10 +49,10 @@ function createToSQL(stmt) {
5549
const tableOptionList = tableOptions.map(tableOptionToSQL)
5650
sql.push(tableOptionList.join(' '))
5751
}
58-
if (ignoreReplace) sql.push(ignoreReplace.toUpperCase())
59-
if (as) sql.push(as.toUpperCase())
52+
sql.push(toUpper(ignoreReplace))
53+
sql.push(toUpper(as))
6054
if (queryExpr) sql.push(unionToSQL(queryExpr))
61-
return sql.join(' ')
55+
return sql.filter(hasVal).join(' ')
6256
}
6357

6458
export {

src/delete.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import has from 'has'
21
import { columnsToSQL } from './column'
32
import { tablesToSQL } from './tables'
43
import { exprToSQL } from './expr'
4+
import { commonOptionConnector, hasVal } from './util'
55

66
function deleteToSQL(stmt) {
77
const clauses = ['DELETE']
88
const { columns, from, table, where } = stmt
99
const columnInfo = columnsToSQL(columns, from)
10-
// if (columns === '*') clauses.push('*')
11-
if (columnInfo) clauses.push(columnInfo)
12-
10+
clauses.push(columnInfo)
1311
if (Array.isArray(table)) {
1412
if (!(table.length === 1 && table[0].addition === true)) clauses.push(tablesToSQL(table))
1513
}
16-
if (Array.isArray(from)) clauses.push('FROM', tablesToSQL(from))
17-
if (has(stmt, 'where') && where !== null) clauses.push(`WHERE ${exprToSQL(where)}`)
18-
19-
return clauses.join(' ')
14+
clauses.push(commonOptionConnector('FROM', tablesToSQL, from))
15+
clauses.push(commonOptionConnector('WHERE', exprToSQL, where))
16+
return clauses.filter(hasVal).join(' ')
2017
}
2118

2219
export {

src/select.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import has from 'has'
21
import { exprToSQL, getExprListSQL } from './expr'
32
import { columnsToSQL } from './column'
43
import { withToSql } from './with'
54
import { tablesToSQL } from './tables'
5+
import { hasVal, commonOptionConnector, connector } from './util'
66

77
/**
88
* @param {Object} stmt
@@ -18,31 +18,23 @@ import { tablesToSQL } from './tables'
1818
* @param {?Array} stmt.limit
1919
* @return {string}
2020
*/
21-
function selectToSQL(stmt) {
22-
const clauses = ['SELECT']
23-
24-
if (has(stmt, 'with') && Array.isArray(stmt.with)) clauses.unshift(withToSql(stmt.with))
25-
if (has(stmt, 'options') && Array.isArray(stmt.options)) clauses.push(stmt.options.join(' '))
26-
if (has(stmt, 'distinct') && stmt.distinct !== null) clauses.push(stmt.distinct)
27-
28-
if (stmt.columns === '*') clauses.push('*')
29-
else clauses.push(columnsToSQL(stmt.columns, stmt.from))
3021

22+
function selectToSQL(stmt) {
23+
const { with: withInfo, options, distinct, columns, from, where, groupby, having, orderby, limit } = stmt
24+
const clauses = [withToSql(withInfo), 'SELECT']
25+
if (Array.isArray(options)) clauses.push(options.join(' '))
26+
clauses.push(distinct, columnsToSQL(columns, from))
3127
// FROM + joins
32-
if (Array.isArray(stmt.from)) clauses.push('FROM', tablesToSQL(stmt.from))
33-
34-
if (has(stmt, 'where') && stmt.where !== null) clauses.push(`WHERE ${exprToSQL(stmt.where)}`)
35-
if (Array.isArray(stmt.groupby) && stmt.groupby.length > 0) clauses.push('GROUP BY', getExprListSQL(stmt.groupby).join(', '))
36-
if (has(stmt, 'having') && stmt.having !== null) clauses.push(`HAVING ${exprToSQL(stmt.having)}`)
37-
38-
if (Array.isArray(stmt.orderby) && stmt.orderby.length > 0) {
39-
const orderExpressions = stmt.orderby.map(expr => `${exprToSQL(expr.expr)} ${expr.type}`)
40-
clauses.push('ORDER BY', orderExpressions.join(', '))
28+
clauses.push(commonOptionConnector('FROM', tablesToSQL, from))
29+
clauses.push(commonOptionConnector('WHERE', exprToSQL, where))
30+
clauses.push(connector('GROUP BY', getExprListSQL(groupby).join(', ')))
31+
clauses.push(commonOptionConnector('HAVING', exprToSQL, having))
32+
if (Array.isArray(orderby)) {
33+
const orderExpressions = orderby.map(expr => `${exprToSQL(expr.expr)} ${expr.type}`)
34+
clauses.push(connector('ORDER BY', orderExpressions.join(', ')))
4135
}
42-
43-
if (Array.isArray(stmt.limit)) clauses.push('LIMIT', stmt.limit.map(exprToSQL))
44-
45-
return clauses.join(' ')
36+
if (Array.isArray(limit)) clauses.push(connector('LIMIT', limit.map(exprToSQL)))
37+
return clauses.filter(hasVal).join(' ')
4638
}
4739

4840
export {

src/tables.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import has from 'has'
2-
import { identifierToSql } from './util'
1+
import { identifierToSql, hasVal } from './util'
32
import { exprToSQL } from './expr'
43

4+
function tableToSQL(tableInfo) {
5+
const { table, db, as, expr } = tableInfo
6+
const database = identifierToSql(db)
7+
const tableName = table ? identifierToSql(table) : exprToSQL(expr)
8+
const str = database ? `${database}.${tableName}` : tableName
9+
if (as) return `${str} AS ${identifierToSql(as)}`
10+
return str
11+
}
12+
513
/**
614
* @param {Array} tables
715
* @return {string}
@@ -10,33 +18,18 @@ function tablesToSQL(tables) {
1018
const baseTable = tables[0]
1119
const clauses = []
1220
if (baseTable.type === 'dual') return 'DUAL'
13-
let str = baseTable.table ? identifierToSql(baseTable.table) : exprToSQL(baseTable.expr)
14-
15-
if (baseTable.db && baseTable.db !== null) str = `${identifierToSql(baseTable.db)}.${str}`
16-
if (baseTable.as !== null) str = `${str} AS ${identifierToSql(baseTable.as)}`
17-
18-
clauses.push(str)
19-
21+
clauses.push(tableToSQL(baseTable))
2022
for (let i = 1; i < tables.length; ++i) {
2123
const joinExpr = tables[i]
22-
23-
str = (joinExpr.join && joinExpr.join !== null) ? ` ${joinExpr.join} ` : str = ', '
24-
25-
if (joinExpr.table) {
26-
if (joinExpr.db !== null) str = `${str}${identifierToSql(joinExpr.db)}.`
27-
str = `${str}${identifierToSql(joinExpr.table)}`
28-
} else {
29-
str = `${str}${exprToSQL(joinExpr.expr)}`
30-
}
31-
32-
if (joinExpr.as !== null) str = `${str} AS ${identifierToSql(joinExpr.as)}`
33-
if (has(joinExpr, 'on') && joinExpr.on !== null) str = `${str} ON ${exprToSQL(joinExpr.on)}`
34-
if (has(joinExpr, 'using')) str = `${str} USING (${joinExpr.using.map(identifierToSql).join(', ')})`
35-
36-
clauses.push(str)
24+
const { on, using, join } = joinExpr
25+
const str = []
26+
str.push(join ? ` ${join}` : ',')
27+
str.push(tableToSQL(joinExpr))
28+
if (on) str.push(`ON ${exprToSQL(on)}`)
29+
if (using) str.push(`USING (${using.map(identifierToSql).join(', ')})`)
30+
clauses.push(str.join(' '))
3731
}
38-
39-
return clauses.join('')
32+
return clauses.filter(hasVal).join('')
4033
}
4134

4235
function tableOptionToSQL(tableOption) {
@@ -50,4 +43,5 @@ function tableOptionToSQL(tableOption) {
5043
export {
5144
tablesToSQL,
5245
tableOptionToSQL,
46+
tableToSQL,
5347
}

src/update.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ function setToSQL(sets) {
2222

2323
function updateToSQL(stmt) {
2424
const clauses = ['UPDATE']
25-
2625
// cross-table update
2726
if (has(stmt, 'table') && stmt.table !== null) clauses.push(tablesToSQL(stmt.table))
2827
if (Array.isArray(stmt.set)) clauses.push('SET', setToSQL(stmt.set))
29-
3028
if (has(stmt, 'where') && stmt.where !== null) clauses.push(`WHERE ${exprToSQL(stmt.where)}`)
31-
3229
return clauses.join(' ')
3330
}
3431

src/util.js

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,34 @@ const escapeMap = {
1212
'\\' : '\\\\',
1313
}
1414

15+
function commonOptionConnector(keyword, action, opt) {
16+
if (!opt) return
17+
return `${keyword.toUpperCase()} ${action(opt)}`
18+
}
19+
20+
function connector(keyword, str) {
21+
if (!str) return
22+
return `${keyword.toUpperCase()} ${str}`
23+
}
24+
1525
/**
1626
* @param {(Array|boolean|string|number|null)} value
1727
* @return {Object}
1828
*/
1929
function createValueExpr(value) {
20-
let expr = {}
2130
const type = typeof value
22-
23-
if (Array.isArray(value)) expr = { type: 'expr_list', value: value.map(createValueExpr) }
24-
else if (type === 'boolean') expr = { type: 'bool', value }
25-
else if (type === 'string') expr = { type: 'string', value }
26-
else if (type === 'number') expr = { type: 'number', value }
27-
else if (value === null) expr = { type: 'null', value: null }
28-
else throw new Error(`Cannot convert value "${value}" to SQL`)
29-
30-
return expr
31+
if (Array.isArray(value)) return { type: 'expr_list', value: value.map(createValueExpr) }
32+
if (value === null) return { type: 'null', value: null }
33+
switch (type) {
34+
case 'boolean':
35+
return { type: 'bool', value }
36+
case 'string':
37+
return { type: 'string', value }
38+
case 'number':
39+
return { type: 'number', value }
40+
default:
41+
throw new Error(`Cannot convert value "${value}" to SQL`)
42+
}
3143
}
3244

3345
/**
@@ -38,18 +50,15 @@ function createValueExpr(value) {
3850
*/
3951
function createBinaryExpr(operator, left, right) {
4052
const expr = { operator, type: 'binary_expr' }
41-
42-
expr.left = left && left.type ? expr.left = left : createValueExpr(left)
43-
53+
expr.left = has(left, 'type') ? left : createValueExpr(left)
4454
if (operator === 'BETWEEN' || operator === 'NOT BETWEEN') {
4555
expr.right = {
4656
type : 'expr_list',
4757
value : [createValueExpr(right[0]), createValueExpr(right[1])],
4858
}
49-
} else {
50-
expr.right = right && right.type ? expr.right = right : expr.right = createValueExpr(right)
59+
return expr
5160
}
52-
61+
expr.right = has(right, 'type') ? right : createValueExpr(right)
5362
return expr
5463
}
5564

@@ -94,23 +103,39 @@ function escape(str) {
94103

95104
function identifierToSql(ident, isDual) {
96105
if (isDual === true) return `'${ident}'`
106+
if (!ident) return
97107
return `\`${ident}\``
98108
}
99109

100110
function literalToSQL(literal) {
101-
const { type } = literal
102-
let { value } = literal
103-
104-
if (type === 'number') {
105-
/* nothing */
106-
} else if (type === 'string') value = `'${escape(value)}'`
107-
else if (type === 'bool') value = value ? 'TRUE' : 'FALSE'
108-
else if (type === 'null') value = 'NULL'
109-
else if (type === 'star') value = '*'
110-
else if (['time', 'date', 'timestamp'].includes(type)) value = `${type.toUpperCase()} '${value}'`
111-
else if (type === 'param') value = `:${value}`
112-
113-
return literal.parentheses ? `(${value})` : value
111+
const { type, parentheses, value } = literal
112+
let str = value
113+
switch (type) {
114+
case 'string':
115+
str = `'${escape(value)}'`
116+
break
117+
case 'boolean':
118+
case 'bool':
119+
str = value ? 'TRUE' : 'FALSE'
120+
break
121+
case 'null':
122+
str = 'NULL'
123+
break
124+
case 'star':
125+
str = '*'
126+
break
127+
case 'param':
128+
str = `:${value}`
129+
break
130+
case 'time':
131+
case 'date':
132+
case 'timestamp':
133+
str = `${type.toUpperCase()} '${value}'`
134+
break
135+
default:
136+
break
137+
}
138+
return parentheses ? `(${str})` : str
114139
}
115140

116141
function replaceParams(ast, params) {
@@ -152,6 +177,8 @@ function commentToSQL(comment) {
152177
}
153178

154179
export {
180+
commonOptionConnector,
181+
connector,
155182
commonTypeValue,
156183
columnRefToSQL,
157184
commentToSQL,

0 commit comments

Comments
 (0)