1 |
|
2 | var actions, criterion, rawPrototype,
|
3 | __slice = [].slice;
|
4 |
|
5 | criterion = require('criterion');
|
6 |
|
7 | actions = require('./actions');
|
8 |
|
9 | rawPrototype = {
|
10 | sql: function() {
|
11 | return this._sql;
|
12 | },
|
13 | params: function() {
|
14 | return this._params;
|
15 | }
|
16 | };
|
17 |
|
18 | module.exports = {
|
19 | raw: function() {
|
20 | var object, params, sql;
|
21 | sql = arguments[0], params = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
|
22 | object = Object.create(rawPrototype);
|
23 | object._sql = sql;
|
24 | object._params = params;
|
25 | return object;
|
26 | },
|
27 | fluent: function(key, value) {
|
28 | var object;
|
29 | object = Object.create(this);
|
30 | object[key] = value;
|
31 | return object;
|
32 | },
|
33 | _escape: function(string) {
|
34 | return string;
|
35 | },
|
36 | _action: actions.select('*'),
|
37 | _joins: [],
|
38 | insert: function(data) {
|
39 | if ('object' !== typeof data) {
|
40 | throw new Error('data argument must be an object');
|
41 | }
|
42 | return this.fluent('_action', actions.insert(data));
|
43 | },
|
44 | insertMany: function(array) {
|
45 | var keysOfFirstRecord, msg;
|
46 | if (!Array.isArray(array)) {
|
47 | throw new Error('array argument must be an array');
|
48 | }
|
49 | if (array.length === 0) {
|
50 | throw new Error('array argument is empty - no records to insert');
|
51 | }
|
52 | msg = 'all records in the argument array must have the same keys.';
|
53 | keysOfFirstRecord = Object.keys(array[0]);
|
54 | array.forEach(function(data) {
|
55 | var keys;
|
56 | keys = Object.keys(data);
|
57 | if (keys.length !== keysOfFirstRecord.length) {
|
58 | throw new Error(msg);
|
59 | }
|
60 | return keysOfFirstRecord.forEach(function(key) {
|
61 | if (data[key] == null) {
|
62 | throw new Error(msg);
|
63 | }
|
64 | });
|
65 | });
|
66 | return this.fluent('_action', actions.insertMany(array));
|
67 | },
|
68 | escape: function(arg) {
|
69 | return this.fluent('_escape', arg);
|
70 | },
|
71 | select: function() {
|
72 | return this.fluent('_action', actions.select.apply(null, arguments));
|
73 | },
|
74 | "delete": function() {
|
75 | return this.fluent('_action', actions["delete"]());
|
76 | },
|
77 | update: function(updates) {
|
78 | return this.fluent('_action', actions.update(updates));
|
79 | },
|
80 | join: function() {
|
81 | var criterionArgs, join, object, sql;
|
82 | sql = arguments[0], criterionArgs = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
|
83 | join = {
|
84 | sql: sql
|
85 | };
|
86 | if (criterionArgs.length !== 0) {
|
87 | join.criterion = criterion.apply(null, criterionArgs);
|
88 | }
|
89 | object = Object.create(this);
|
90 | object._joins = this._joins.slice();
|
91 | object._joins.push(join);
|
92 | return object;
|
93 | },
|
94 | "with": function(arg) {
|
95 | if (!(('object' === typeof arg) && Object.keys(arg).length !== 0)) {
|
96 | throw new Error('with must be called with an object that has at least one property');
|
97 | }
|
98 | return this.fluent('_with', arg);
|
99 | },
|
100 | group: function(arg) {
|
101 | return this.fluent('_group', arg);
|
102 | },
|
103 | order: function(arg) {
|
104 | return this.fluent('_order', arg);
|
105 | },
|
106 | limit: function(arg) {
|
107 | return this.fluent('_limit', parseInt(arg, 10));
|
108 | },
|
109 | offset: function(arg) {
|
110 | return this.fluent('_offset', parseInt(arg, 10));
|
111 | },
|
112 | table: function(table) {
|
113 | return this.fluent('_table', table);
|
114 | },
|
115 | where: function() {
|
116 | var args, where;
|
117 | args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
118 | where = criterion.apply(null, args);
|
119 | return this.fluent('_where', this._where != null ? this._where.and(where) : where);
|
120 | },
|
121 | having: function() {
|
122 | var args, having;
|
123 | args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
124 | having = criterion.apply(null, args);
|
125 | return this.fluent('_having', this._having != null ? this._having.and(having) : having);
|
126 | },
|
127 | sql: function() {
|
128 | return this._action.sql(this);
|
129 | },
|
130 | params: function() {
|
131 | return this._action.params(this);
|
132 | }
|
133 | };
|