Permalink
Browse files

manual merge of pull request

  • Loading branch information...
2 parents 2ccf88a + 8784622 commit 2c1bc9162eb7cd7ae24d637cbcbea6ac4892dba7 @JerrySievert committed May 14, 2013
Showing with 67 additions and 32 deletions.
  1. +1 −1 create_collection.sql
  2. +41 −4 find.sql
  3. +25 −27 save.sql
@@ -11,7 +11,7 @@ boolean AS $$
var plan1 = plv8.prepare('INSERT INTO collection (name) VALUES ($1)', [ 'varchar' ]);
var plan2 = plv8.prepare('CREATE TABLE col_' + collection +
- ' (col_' + collection + '_id INT NOT NULL PRIMARY KEY, data JSON)');
+ ' (col_' + collection + '_id CHARACTER VARYING NOT NULL PRIMARY KEY, data JSON)');
var plan3 = plv8.prepare('CREATE SEQUENCE seq_col_' + collection);
var ret;
View
@@ -37,7 +37,7 @@ BOOLEAN AS $$
return (obj === undefined ? 'f' : 't');
$$ LANGUAGE plv8 IMMUTABLE STRICT;
-CREATE OR REPLACE FUNCTION find (collection varchar, terms json) RETURNS
+CREATE OR REPLACE FUNCTION find (collection varchar, terms json, lim int, skip int) RETURNS
SETOF json AS $$
var table = 'col_' + collection;
var sql = "SELECT data FROM " + table;
@@ -47,15 +47,52 @@ SETOF json AS $$
where = JSON.parse(where);
sql += " " + where.sql;
- var plan = plv8.prepare(sql, where.types);
- var rows = plan.execute(where.binds);
+ if (lim > -1 )
+ {
+ sql += "limit " + lim;
+ }
+ if (skip > 0)
+ {
+ sql += "offset " + skip;
+ }
+
+ try {
+ plv8.subtransaction(function(){
+ var plan = plv8.prepare(sql, where.types);
+ rows = plan.execute(where.binds);
+ plan.free();
+ });
+ }
+ catch(err) {
+ if (err=='Error: relation "' + table + '" does not exist')
+ {
+ rows = []
+ }
+ }
var ret = [ ];
for (var i = 0; i < rows.length; i++) {
ret.push(JSON.stringify(rows[i].data));
}
- plan.free();
return ret;
$$ LANGUAGE plv8 STRICT;
+
+CREATE OR REPLACE FUNCTION find (collection varchar, terms json) RETURNS
+SETOF json AS $$
+ var full_find = plv8.find_function("find(varchar,json,int,int)");
+ var results = full_find(collection,terms,-1,0);
+ return results;
+$$ LANGUAGE plv8 STRICT;
+
+CREATE OR REPLACE FUNCTION find (collection varchar, terms json, lim int) RETURNS
+SETOF json AS $$
+ var full_find = plv8.find_function("find(varchar,json,int,int)");
+ var results = full_find(collection,terms,lim,0);
+ return results;
+$$ LANGUAGE plv8 STRICT;
+
+
+
+
View
@@ -3,43 +3,41 @@ BOOLEAN AS $$
var obj = JSON.parse(data);
var id = obj._id;
- // if there is no id, naively assume an insert
- if (id === undefined) {
+ // First, lets try an update, see if that works. If so, then the data must exist ;)
+ // We will do it in an tranaction, so we can create the collection if it doesnt exist
-
-
- var seq;
- try
- {
- plv8.subtransaction(function(){
- seq = plv8.prepare("SELECT nextval('seq_col_" + collection + "') AS id");
- });
- }
- catch(err)
- {
- if (err == 'Error: relation "seq_col_' + collection + '" does not exist')
- {
+ try {
+ plv8.subtransaction(function() {
+ var update = plv8.prepare("UPDATE col_" + collection + " SET data = $1 WHERE col_" + collection + "_id = $2", [ 'json', 'character varying' ]);
+ res = update.execute([ data, id ]);
+ });
+ } catch(err) {
+ if (err == 'Error: relation "seq_col_' + collection + '" does not exist') {
var create_collection = plv8.find_function("create_collection");
res = create_collection(collection);
- seq = plv8.prepare("SELECT nextval('seq_col_" + collection + "') AS id");
- }
+ var update = plv8.prepare("UPDATE col_" + collection + " SET data = $1 WHERE col_" + collection + "_id = $2", [ 'json', 'character varying' ]);
+ res = update.execute([ data, id ]);
+ }
}
+ }
- var rows = seq.execute([ ]);
-
- id = rows[0].id;
- obj._id = id;
+ if (res == 0) { // If it didnt affect anything, it must be a new row. Insert.
+ var seq;
+ seq = plv8.prepare("SELECT nextval('seq_col_" + collection + "') AS id");
+
+
+ if (obj._id === undefined) {
+ var rows = seq.execute([ ]);
+ id = rows[0].id;
+ obj._id = id;
+ seq.free();
+ }
- seq.free();
var insert = plv8.prepare("INSERT INTO col_" + collection +
- " (col_" + collection + "_id, data) VALUES ($1, $2)", [ 'int', 'json']);
+ " (col_" + collection + "_id, data) VALUES ($1, $2)", [ 'character varying', 'json']);
insert.execute([ id, JSON.stringify(obj) ]);
insert.free();
- } else {
- var update = plv8.prepare("UPDATE col_" + collection +
- " SET data = $1 WHERE col_" + collection + "_id = $2", [ 'json', 'int' ]);
- update.execute([ data, id ]);
}
return true;

0 comments on commit 2c1bc91

Please sign in to comment.