-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewModel.js
160 lines (147 loc) · 5.8 KB
/
ViewModel.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var JQB;
(function (JQB) {
var ViewModel = (function () {
function ViewModel(options) {
var _this = this;
this.schema = ko.observable(null);
this.queryTables = ko.observableArray([]);
this.schema(new JQB.Schema.Database(options.schemaCsv));
this.sql = ko.computed(function () {
return _this.computeSql();
});
}
ViewModel.prototype.queryTable = function (name) {
for (var i = 0, a = this.queryTables(); i < a.length; i++) {
if (a[i].name() === name)
return a[i];
}
return null;
};
ViewModel.prototype.addTableToQuery = function (table) {
var qt = new QueryTable(this, table);
this.queryTables.push(qt);
if (this.queryTables().length > 1) {
qt.tryToAutoJoin();
}
};
ViewModel.prototype.table = function (name) {
for (var i = 0, a = this.queryTables(); i < a.length; i++) {
if (a[i].name() === name) {
return a[i];
}
}
return null;
};
ViewModel.prototype.computeSql = function () {
if (this.queryTables().length === 0) {
return '(nothing selected)';
}
var columnLists = [];
var tableSql = [];
for (var i = 0, a = this.queryTables(); i < a.length; i++) {
var table = a[i];
columnLists = columnLists.concat(table.getColumnSql());
tableSql.push(table.getSourceSql());
}
return 'select ' + columnLists.join(', ') + "\n" + tableSql.join("\n");
};
return ViewModel;
})();
JQB.ViewModel = ViewModel;
var QueryTable = (function () {
function QueryTable(root, table) {
this.root = root;
this.table = table;
this.name = ko.observable('');
this.name(table.name);
var cols = [];
for (var i = 0; i < table.columns.length; i++) {
cols.push(new QueryColumn(table.columns[i]));
}
this.columns = ko.observableArray(cols);
}
QueryTable.prototype.column = function (name) {
for (var i = 0, a = this.columns(); i < a.length; i++) {
if (a[i].name() === name)
return a[i];
}
return null;
};
QueryTable.prototype.tryToAutoJoin = function () {
var otherTables = [];
for (var i = 0, a = this.root.queryTables(); i < a.length; i++) {
if (this === a[i])
break;
otherTables.push(a[i].table);
}
var fks = this.table.guessForeignKeys(otherTables);
if (fks.length === 0) {
return;
}
for (var i = 0; i < fks.length; i++) {
var fk = fks[i];
var primary = this.root.table(fk.primary.name);
var fkTable = this.root.table(fk.fkOwner.name);
for (var j = 0; j < fk.primaryColumns.length; j++) {
var primaryColumn = primary.column(fk.primaryColumns[j].name);
var fkColumn = fkTable.column(fk.fkColumns[j].name);
fkColumn.joins.push(new JoinCondition(primary, primaryColumn));
}
}
};
QueryTable.prototype.getColumnSql = function () {
var cols = [];
for (var i = 0, a = this.columns(); i < a.length; i++) {
var col = a[i];
if (!col.view())
continue;
cols.push(this.alias() + '.`' + col.name() + '`');
}
return cols;
};
QueryTable.prototype.getSourceSql = function () {
if (this.isRoot()) {
return "from `" + this.name() + "` " + this.alias();
}
var sql = "join `" + this.name() + "` " + this.alias() + " on ";
var joinClauses = [];
for (var i = 0, cols = this.columns(); i < cols.length; i++) {
for (var j = 0, joins = cols[i].joins(); j < joins.length; j++) {
var join = joins[j];
var joinClause = this.alias() + '.`' + cols[i].name() + '` = ' + join.primary.alias() + '.`' + join.primaryColumn.name() + '`';
joinClauses.push(joinClause);
}
}
if (joinClauses.length === 0) {
joinClauses.push('(NEED JOIN DEFINITION)');
}
return sql + joinClauses.join(' and ');
};
QueryTable.prototype.isRoot = function () {
return this.root.queryTables.indexOf(this) === 0;
};
QueryTable.prototype.alias = function () {
return 't' + this.root.queryTables.indexOf(this);
};
return QueryTable;
})();
JQB.QueryTable = QueryTable;
var QueryColumn = (function () {
function QueryColumn(column) {
this.column = column;
this.joins = ko.observableArray([]);
this.name = ko.observable(column.name);
this.view = ko.observable(!column.isPkOrFk());
}
return QueryColumn;
})();
JQB.QueryColumn = QueryColumn;
var JoinCondition = (function () {
function JoinCondition(primary, primaryColumn) {
this.primary = primary;
this.primaryColumn = primaryColumn;
}
return JoinCondition;
})();
JQB.JoinCondition = JoinCondition;
})(JQB || (JQB = {}));