-
Notifications
You must be signed in to change notification settings - Fork 223
/
alias-namespace.js
58 lines (49 loc) · 1.99 KB
/
alias-namespace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import G from 'generatorics'
// this class is responsible for generating the aliases that appear in each SQL query
// this has different rules depending on whether we are aliasing a column or table and on whether we are minifying
export default class AliasNamespace {
constructor(minify, aliasPrefix) {
this.minify = !!minify
this.aliasPrefix = aliasPrefix ?? ''
// a generator for infinite alias names, starting with the shortest possible
// this is helpful for generating the names when minifying
this.mininym = G.baseNAll(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#$'
)
// keep track of all the table names we've used since these have to be unique in each query
this.usedTableAliases = new Set()
// we can re-use aliases for columns since columns names dont need to be globally unique because they will be prefixed
// this object will remember alias assignments for each column name so we can reuse them
this.columnAssignments = {}
}
generate(type, name) {
// if minifiying, make everything ugly and unique.
if (this.minify) {
// tables definitely all need unique names
if (type === 'table') {
return `${this.aliasPrefix}${this.mininym.next().value.join('')}`
}
// but if its a column, we dont need to worry about the uniqueness from other columns
// because the columns will get prefixed with the parent(s)
if (!this.columnAssignments[name]) {
this.columnAssignments[name] = this.mininym.next().value.join('')
}
return this.columnAssignments[name]
}
// otherwise, lets make it readable
if (type === 'column') {
return name
}
name = this.aliasPrefix + name
.replace(/\s+/g, '')
.replace(/[^a-zA-Z0-9]/g, '_')
.slice(0, 10)
// the table aliases must be unique
// just append a "$" until its a unique name
while (this.usedTableAliases.has(name)) {
name += '$'
}
this.usedTableAliases.add(name)
return name
}
}