-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (83 loc) · 2.64 KB
/
index.js
File metadata and controls
97 lines (83 loc) · 2.64 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
90
91
92
93
94
95
96
97
'use strict';
var assert = require('assert');
var getDialect = require('../dialect');
var util = require('util');
var Node = function(type) {
/* jshint unused: false */
this.nodes = [];
};
Node.prototype.toNode = function() {
return this;
};
Node.prototype.add = function(node) {
assert(node, 'Error while trying to add a non-existant node to a query');
this.nodes.push(typeof node === 'string' ? new TextNode(node) : node.toNode());
return this;
};
Node.prototype.unshift = function(node) {
assert(node, 'Error while trying to add a non-existant node to a query');
this.nodes.unshift(typeof node === 'string' ? new TextNode(node) : node.toNode());
return this;
};
// Before the change that introduced parallel dialects, every node could be turned
// into a query. The parallel dialects change made it impossible to change some nodes
// into a query because not all nodes are constructed with the sql instance.
var determineDialect = function(query, dialect) {
var sql = query.sql || (query.table && query.table.sql);
var Dialect;
if (dialect) {
// dialect is specified
Dialect = getDialect(dialect);
} else if (sql && sql.dialect) {
// dialect is not specified, use the dialect from the sql instance
Dialect = sql.dialect;
} else {
// dialect is not specified, use the default dialect
Dialect = require('../').dialect;
}
return Dialect;
};
var initializeDialect = function(Dialect, query) {
var config = query.sql ? query.sql.config : {};
return new Dialect(config);
};
Node.prototype.toQuery = function(dialect) {
var Dialect = determineDialect(this, dialect);
return initializeDialect(Dialect, this).getQuery(this);
};
Node.prototype.toNamedQuery = function(name, dialect) {
if (!name || typeof name !== 'string' || name === '') {
throw new Error('A query name has to be a non-empty String.');
}
var query = this.toQuery(dialect);
query.name = name;
return query;
};
Node.prototype.toString = function(dialect) {
var Dialect = determineDialect(this, dialect);
return initializeDialect(Dialect, this).getString(this);
};
Node.prototype.addAll = function(nodes) {
for(var i = 0, len = nodes.length; i < len; i++) {
this.add(nodes[i]);
}
return this;
};
Node.define = function(def) {
var c = function() {
Node.call(this);
};
// allow custom sub-class constructor
if(def.constructor && def.constructor !== {}.constructor) {
c = def.constructor;
}
util.inherits(c, Node);
for (var key in def) {
if (def.hasOwnProperty(key)) {
c.prototype[key] = def[key];
}
}
return c;
};
module.exports = Node;
var TextNode = require(__dirname + '/text');