forked from paytm/node-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateIndex.js
More file actions
89 lines (75 loc) · 2.06 KB
/
Copy pathcreateIndex.js
File metadata and controls
89 lines (75 loc) · 2.06 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
var _ = require('lodash');
var Node = require('./');
var sliced = require('sliced');
var CreateIndexNode = module.exports = Node.define({
type: 'CREATE INDEX',
constructor: function(table, indexName) {
Node.call(this);
if (table.type === 'CREATE INDEX') {
// implement copy constructor with duck typing
var other = table;
this.table = other.table;
this.options = {
algorithm : other.options.algorithm,
// not deep cloning, because column nodes are immutable
columns : _.clone(other.options.columns),
indexName : other.options.indexName,
parser : other.options.parser,
type : other.options.type
};
} else {
this.table = table;
this.options = {
algorithm : undefined,
columns : [],
indexName : indexName,
parser : undefined,
type : undefined
};
}
},
unique: function() {
var node = new CreateIndexNode(this);
node.options.type = 'unique';
return node;
},
spatial: function() {
var node = new CreateIndexNode(this);
node.options.type = 'spatial';
return node;
},
fulltext: function() {
var node = new CreateIndexNode(this);
node.options.type = 'fulltext';
return node;
},
using: function(algorithm) {
var node = new CreateIndexNode(this);
node.options.algorithm = algorithm;
return node;
},
on: function() {
var args = sliced(arguments);
var node = new CreateIndexNode(this);
node.options.columns = node.options.columns.concat(args);
return node;
},
withParser: function(parser) {
var node = new CreateIndexNode(this);
node.options.parser = parser;
return node;
},
indexName: function() {
var result = this.options.indexName;
if (!result) {
var columns = this.options.columns.map(function(col) {
return col.name;
}).sort();
result = [this.table._name];
result = result.concat(columns);
result = result.join('_');
}
return result;
}
});