Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,26 @@
this._addListArgs(arguments, '_onConflict');
this.where = this.and = function() {
return this._addExpression(arguments, '_onConflictWhere');
}
};
this.onConstraint = function(name) {
this._onConstraint = name;
return this;
}
};
return this;
}
};

Insert.prototype.doNothing = function () {
this._doNothing = true;
return this;
}
};

Insert.prototype.doUpdate = function (cols) {
this._doUpdate = cols || true;
this.where = this.and = function () {
return this._addExpression(arguments, '_doUpdateWhere');
}
};
return this;
}
};

// Add "template tag"
sql.templ.helpers.ifNotEmpty = function(val, opts, contents, ctx) {
Expand Down Expand Up @@ -176,7 +176,7 @@
}
: function (row, opts) {
return sql._handleValues(_.values(row), opts);
}
};

var values = this._values.map(function (row) {
return '(' + handleRow(row, opts).join(', ') + ')';
Expand All @@ -189,7 +189,7 @@
Values.prototype.types = function (types) {
this._types = types || true;
return this;
}
};

function typeCoerce(val) {
if (typeof val === 'number') {
Expand All @@ -210,7 +210,7 @@
Values.prototype.columns = function () {
this._columns = true;
return this;
}
};
Values.prototype.as = Select.prototype.as;
Values.prototype._toNestedString = Select.prototype._toNestedString;
Values.prototype._aliasToString = function (opts) {
Expand All @@ -221,7 +221,7 @@
alias += ' (' + sql._handleColumns(_.keys(this._values[0])) + ')';
}
return alias;
}
};

// Convert objects to JSON
// HACK: we are pollluting sql namepsace here, but currently there is no way around,
Expand All @@ -236,14 +236,18 @@
return _convert(JSON.stringify(val));

return _convert(val);
}
};

// Use SQL-99 syntax for arrays since it's easier to implement
sql.conversions.Array = function(arr) {
return 'ARRAY[' + arr.map(sql.convert).join(', ') + ']';
var pgTypes = {
number: 'bigint[]',
string: 'text[]'
};
return 'ARRAY[' + arr.map(sql.convert).join() + ']::' + pgTypes[(typeof arr[0]).toLowerCase()];
};

if (typeof exports != 'undefined')
if (typeof exports !== 'undefined')
module.exports = pgsql;
else
window.PostgresBricks = pgsql;
Expand Down