Skip to content

Commit 3427ef7

Browse files
committed
reconstruct codes
1 parent ff451be commit 3427ef7

5 files changed

Lines changed: 38 additions & 61 deletions

File tree

src/column.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ function columnDefinitionToSQL(columnDefinition) {
7575
return column.filter(hasVal).join(' ')
7676
}
7777

78+
function columnToSQL(column, isDual) {
79+
const { expr } = column
80+
if (isDual) expr.isDual = isDual
81+
let str = exprToSQL(expr)
82+
if (column.as !== null) {
83+
str = `${str} AS `
84+
if (column.as.match(/^[a-z_][0-9a-z_]*$/i)) str = `${str}${identifierToSql(column.as)}`
85+
else str = `${str}\`${column.as}\``
86+
}
87+
return str
88+
}
7889
/**
7990
* Stringify column expressions
8091
*
@@ -95,20 +106,7 @@ function columnsToSQL(columns, tables) {
95106
} = columns
96107
result.push(star, toUpper(type))
97108
const exprListArr = exprList || columns
98-
const columnsStr = exprListArr
99-
.map(column => {
100-
const { expr } = column
101-
if (isDual) expr.isDual = isDual
102-
let str = exprToSQL(expr)
103-
if (column.as !== null) {
104-
str = `${str} AS `
105-
if (column.as.match(/^[a-z_][0-9a-z_]*$/i)) str = `${str}${identifierToSql(column.as)}`
106-
107-
else str = `${str}\`${column.as}\``
108-
}
109-
return str
110-
})
111-
.join(', ')
109+
const columnsStr = exprListArr.map(col => columnToSQL(col, isDual)).join(', ')
112110
result.push(`${type && '(' || ''}${columnsStr}${type && ')' || ''}`)
113111
return result.filter(hasVal).join(' ')
114112
}

src/limit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { connector, toUpper } from './util'
2+
import { exprToSQL } from './expr'
3+
4+
function limitToSQL(limit) {
5+
if (!limit) return
6+
const { seperator, value } = limit
7+
return connector('LIMIT', value.map(exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${toUpper(seperator)} `))
8+
}
9+
10+
export {
11+
limitToSQL,
12+
}

src/select.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { exprToSQL, getExprListSQL, orderOrPartitionByToSQL } from './expr'
22
import { columnsToSQL } from './column'
3+
import { limitToSQL } from './limit'
34
import { withToSql } from './with'
45
import { tablesToSQL } from './tables'
56
import { hasVal, commonOptionConnector, connector, topToSQL, toUpper } from './util'
@@ -21,20 +22,7 @@ import { hasVal, commonOptionConnector, connector, topToSQL, toUpper } from './u
2122

2223
function selectToSQL(stmt) {
2324
const {
24-
as_struct_val: asStructVal,
25-
columns,
26-
distinct,
27-
from,
28-
for_sys_time_as_of: forSystem = {},
29-
groupby,
30-
having,
31-
limit,
32-
options,
33-
orderby,
34-
top,
35-
window: windowInfo,
36-
with: withInfo,
37-
where,
25+
as_struct_val: asStructVal, columns, distinct, from, for_sys_time_as_of: forSystem = {}, groupby, having, limit, options, orderby, top, window: windowInfo, with: withInfo, where,
3826
} = stmt
3927
const clauses = [withToSql(withInfo), 'SELECT', toUpper(asStructVal)]
4028
clauses.push(topToSQL(top))
@@ -49,10 +37,7 @@ function selectToSQL(stmt) {
4937
clauses.push(commonOptionConnector('HAVING', exprToSQL, having))
5038
clauses.push(commonOptionConnector('WINDOW', exprToSQL, windowInfo))
5139
clauses.push(orderOrPartitionByToSQL(orderby, 'order by'))
52-
if (limit) {
53-
const { seperator, value } = limit
54-
clauses.push(connector('LIMIT', value.map(exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${seperator.toUpperCase()} `)))
55-
}
40+
clauses.push(limitToSQL(limit))
5641
return clauses.filter(hasVal).join(' ')
5742
}
5843

src/union.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
setVarToSQL,
1414
lockUnlockToSQL,
1515
} from './command'
16-
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
16+
import { orderOrPartitionByToSQL } from './expr'
17+
import { limitToSQL } from './limit'
1718
import { withToSql } from './with'
18-
import { connector } from './util'
1919

2020
const typeToSQLFn = {
2121
alter : alterToSQL,
@@ -50,10 +50,7 @@ function bigQueryToSQL(stmt) {
5050
const result = [withToSql(withExpr), lp, unionToSQL(select), rp]
5151
// process with, orderby and limit
5252
result.push(orderOrPartitionByToSQL(orderby, 'order by'))
53-
if (limit) {
54-
const { seperator, value } = limit
55-
result.push(connector('LIMIT', value.map(exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${seperator.toUpperCase()} `)))
56-
}
53+
result.push(limitToSQL(limit))
5754
return result.filter(val => val).join(' ')
5855
}
5956

src/util.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ function arrayStructTypeToSQL(expr) {
222222
if (isNotArrayOrStruct) return dataTypeUpper
223223
const result = definition && definition.map(field => {
224224
const {
225-
field_name: fieldName,
226-
field_type: fieldType,
225+
field_name: fieldName, field_type: fieldType,
227226
} = field
228227
const fieldResult = [fieldName, arrayStructTypeToSQL(fieldType)]
229228
return fieldResult.filter(hasVal).join(' ')
@@ -273,25 +272,11 @@ function autoIncreatementToSQL(autoIncreatement) {
273272
}
274273

275274
export {
276-
arrayStructTypeToSQL,
277-
autoIncreatementToSQL,
278-
commonKeywordArgsToSQL,
279-
commonOptionConnector,
280-
connector,
281-
commonTypeValue,
282-
columnRefToSQL,
283-
commentToSQL,
284-
createBinaryExpr,
285-
createValueExpr,
286-
DEFAULT_OPT,
287-
escape,
288-
literalToSQL,
289-
identifierToSql,
290-
replaceParams,
291-
returningToSQL,
292-
hasVal,
293-
setParserOpt,
294-
toUpper,
295-
topToSQL,
296-
triggerEventToSQL,
275+
arrayStructTypeToSQL, autoIncreatementToSQL,
276+
commonKeywordArgsToSQL, commonOptionConnector,
277+
connector, commonTypeValue, columnRefToSQL,
278+
commentToSQL, createBinaryExpr, createValueExpr,
279+
DEFAULT_OPT, escape, literalToSQL, identifierToSql,
280+
replaceParams, returningToSQL, hasVal, setParserOpt,
281+
toUpper, topToSQL, triggerEventToSQL,
297282
}

0 commit comments

Comments
 (0)