This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release: v0.1.0 with range coercion support
- Loading branch information
Showing
14 changed files
with
720 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"unused": "vars", | ||
|
||
// Relaxing options | ||
"expr": true, | ||
"laxcomma": true, | ||
|
||
// Environments | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FILES = $(shell find . -name '*.js' -not -path '*node_modules*') | ||
|
||
check: | ||
jshint $(FILES) | ||
|
||
test: | ||
mocha | ||
|
||
test-watch: | ||
mocha --reporter min --watch --growl | ||
|
||
.PHONY: jshint test test-watch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,7 @@ | ||
module.exports = function (pg) { | ||
var RANGE_MATCHER = /(\[|\()("((?:\\"|[^"])*)"|[^"]*),("((?:\\"|[^"])*)"|[^"]*)(\]|\))/; | ||
var Range = require("./lib/range"); | ||
var parser = require("./lib/parser"); | ||
|
||
var types = { | ||
DATE: 1082, | ||
TIMESTAMP: 1114, | ||
TIMESTAMPTZ: 1184, | ||
DATERANGE: 3912, | ||
TSRANGE: 3908, | ||
TSTZRANGE: 3910 | ||
}; | ||
|
||
function parseRangeSegment(whole, quoted) { | ||
if (quoted) { | ||
return quoted.replace(/\\(.)/g, '$1'); | ||
} | ||
return whole; | ||
} | ||
|
||
function makeRangeParser(dateType) { | ||
var parseDate = pg.types.getTypeParser(dateType, "text"); | ||
|
||
return function parseRange(val) { | ||
var matches = val.match(RANGE_MATCHER); | ||
|
||
if (!matches) { | ||
// empty | ||
return { | ||
"lower": null, | ||
"upper": null, | ||
"bounds": null | ||
}; | ||
} | ||
|
||
var bounds = matches[1] + matches[6]; | ||
var lower = parseRangeSegment(matches[2], matches[3]); | ||
var upper = parseRangeSegment(matches[4], matches[5]); | ||
|
||
return { | ||
"lower": parseDate(lower), | ||
"upper": parseDate(upper), | ||
"bounds": bounds | ||
}; | ||
}; | ||
} | ||
|
||
pg.types.setTypeParser(types.DATERANGE, makeRangeParser(types.DATE)); | ||
pg.types.setTypeParser(types.TSRANGE, makeRangeParser(types.TIMESTAMP)); | ||
pg.types.setTypeParser(types.TSTZRANGE, makeRangeParser(types.TIMESTAMPTZ)); | ||
module.exports = { | ||
Range: Range, | ||
install: parser.install | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var _ = require("lodash") | ||
, Range = require("./range"); | ||
|
||
var RANGE_MATCHER = /(\[|\()("((?:\\"|[^"])*)"|[^"]*),("((?:\\"|[^"])*)"|[^"]*)(\]|\))/; | ||
|
||
var oids = { | ||
INTEGER: 23, | ||
BIGINT: 20, | ||
NUMERIC: 1700, | ||
TIMESTAMP: 1114, | ||
TIMESTAMPTZ: 1184, | ||
DATE: 1082, | ||
|
||
INT4RANGE: 3904, | ||
INT8RANGE: 3926, | ||
NUMRANGE: 3906, | ||
TSRANGE: 3908, | ||
TSTZRANGE: 3910, | ||
DATERANGE: 3912, | ||
}; | ||
|
||
function parseRangeSegment(whole, quoted) { | ||
if (quoted) { | ||
return quoted.replace(/\\(.)/g, "$1"); | ||
} | ||
if (whole === "") { | ||
return null; | ||
} | ||
return whole; | ||
} | ||
|
||
function parseRange(parseBound, rangeLiteral) { | ||
var matches = rangeLiteral.match(RANGE_MATCHER); | ||
|
||
if (!matches) { | ||
// empty | ||
return Range(); | ||
} | ||
|
||
var bounds = matches[1] + matches[6]; | ||
var lower = parseRangeSegment(matches[2], matches[3]); | ||
var upper = parseRangeSegment(matches[4], matches[5]); | ||
|
||
return Range( | ||
lower ? parseBound(lower) : null, | ||
upper ? parseBound(upper) : null, | ||
bounds); | ||
} | ||
|
||
function install(pg, rangeOid, subtypeOid) { | ||
var subtypeParser; | ||
|
||
if (!rangeOid && !subtypeOid) { | ||
install(pg, oids.INT4RANGE, oids.INTEGER); | ||
install(pg, oids.INT8RANGE, oids.BIGINT); | ||
install(pg, oids.NUMRANGE, oids.NUMERIC); | ||
install(pg, oids.TSRANGE, oids.TIMESTAMP); | ||
install(pg, oids.TSTZRANGE, oids.TIMESTAMPTZ); | ||
install(pg, oids.DATERANGE, oids.DATE); | ||
} | ||
|
||
subtypeParser = pg.types.getTypeParser(subtypeOid, "text"); | ||
pg.types.setTypeParser(rangeOid, _.partial(parseRange, subtypeParser)); | ||
} | ||
|
||
module.exports = { | ||
install: install, | ||
parseRange: parseRange | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
var util = require("util"); | ||
|
||
var VALID_BOUNDS = ["[]", "[)", "(]", "()"]; | ||
|
||
function formatBound(value, prepare) { | ||
if (value === null) { | ||
return ""; | ||
} | ||
|
||
value = prepare(value); | ||
if (/[()[\],"\\]/.test(value)) { | ||
// quote bound only if necessary | ||
value = "\"" + value.replace(/(\\|")/, "\\$1") + "\""; | ||
} | ||
return value; | ||
} | ||
|
||
function Range(lower, upper, bounds) { | ||
if (!(this instanceof Range)) { | ||
return new Range(lower, upper, bounds); | ||
} | ||
|
||
if (!lower && !upper && !bounds) { | ||
this.empty = true; | ||
return; | ||
} | ||
|
||
if (lower && upper && lower > upper) { | ||
throw new Error("invalid range: lower bound greater than upper bound"); | ||
} | ||
|
||
bounds = bounds || "[)"; | ||
if (VALID_BOUNDS.indexOf(bounds) === -1) { | ||
throw new Error(util.format("invalid bounds: %s", bounds)); | ||
} | ||
|
||
this.lower = lower; | ||
this.upper = upper; | ||
this.bounds = bounds; | ||
} | ||
|
||
Range.prototype.toPostgres = function (prepare) { | ||
if (this.empty) { | ||
return "empty"; | ||
} | ||
|
||
return util.format("%s%s,%s%s", | ||
this.bounds[0], | ||
formatBound(this.lower, prepare), | ||
formatBound(this.upper, prepare), | ||
this.bounds[1]); | ||
}; | ||
|
||
Range.prototype.toJSON = function () { | ||
if (this.empty) { | ||
return { lower: null, upper: null, bounds: null }; | ||
} | ||
return { | ||
lower: this.lower, | ||
upper: this.upper, | ||
bounds: this.bounds | ||
}; | ||
}; | ||
|
||
module.exports = Range; |
Oops, something went wrong.