Example #1
0
        _setter:function (vals, model) {
            if (!comb.isUndefined(vals)) {
                if (model.isNew) {
                    if (!comb.isNull(vals)) {
                        this.addAssociations(vals, model);
                        //this.__setValue(model, vals);
                    } else {
                        this.__setValue(model, []);
                    }
                } else {
                    model.__isChanged = true;
                    model[this.removeAssociationFlagName] = true;
                    this._cacheOldVals(model);
                    if (!comb.isNull(vals)) {
                        //ensure its an array!
                        vals = (comb.isArray(vals) ? vals : [vals]).map(function (m) {
                            return this._toModel(m);
                        }, this);
                    } else {
                        vals = [];
                    }
                    this.__setValue(model, vals);
                }
            }

        },
Example #2
0
 __loadModel:function(parent, model, setValue){
     var ret = new Promise();
     setValue = comb.isBoolean(setValue) ? setValue : true;
     if (comb.isArray(model)) {
         var pl = new PromiseList(model.map(hitch(this, function(m){
             //dont set the value because we have an array
             //and it will be set in the callback
             return this.__loadModel(parent, m, false);
         })), true);
         if (setValue) {
             pl.then(hitch(this, function(models){
                 //now we can set the value!!!
                 this.__setValue(parent, models);
                 ret.callback(models);
             }), hitch(ret, "errback"));
         } else {
             ret = pl;
         }
     } else {
         model = this._toModel(model, true);
         var recip = this.model._findAssociation(this);
         if (recip) {
             recip = recip[1];
             recip.__setValue(model, parent);
         }
         setValue && this.__setValue(parent, model);
         //call hook to finish other model associations
         model._hook("post", "load").then(hitch(this, function(){
             ret.callback(model);
         }), hitch(ret, "errback"));
     }
     return ret;
 },
Example #3
0
 _getAssociationKey:function(){
     var options = this.__opts, key, ret = [], lk, rk;
     if (!comb.isUndefinedOrNull((key = options.key))) {
         if (this.supportsStringKey && comb.isString(key)) {
             //normalize the key first!
             ret = [
                 [this.isOwner ? this.defaultLeftKey : key],
                 [this.isOwner ? key : this.defaultRightKey]
             ];
         } else if (this.supportsHashKey && comb.isHash(key)) {
             var leftKey = Object.keys(key)[0];
             var rightKey = key[leftKey];
             ret = [
                 [leftKey],
                 [rightKey]
             ];
         } else if (this.supportsCompositeKey && comb.isArray(key)) {
             ret = [
                 [key],
                 null
             ];
         }
     } else if (this.supportsLeftAndRightKey && (!comb.isUndefinedOrNull((lk = options.leftKey)) && !comb.isUndefinedOrNull((rk = options.rightKey)))) {
         ret = [
             array.toArray(lk), array.toArray(rk)
         ];
     } else {
         //todo handle composite primary keys
         ret = [
             [this.defaultLeftKey],
             [this.defaultRightKey]
         ];
     }
     return ret;
 },
 enroll:function (clas) {
     if (comb.isArray(clas)) {
         return this.addClasses(clas);
     } else {
         return this.addClass(clas);
     }
 }
Example #5
0
File: helper.js Project: C2FO/hive
exports.execute = function(command, hive, socket) {
    var commandArr = parseCommand(command), ret, cmd;
    console.log(commandArr);
    if (comb.isArray(commandArr) && commandArr.length) {
        //then we know its an array that can be used to
        //call the hive server
        cmd = commandArr.shift();
        socket.write(hive[cmd].apply(hive, commandArr) + "\n");
    } else if (commandArr != "error") {
        cmd = commandArr;
        switch (cmd) {
            case "flushAll" :
                socket.write(hive.flushAll() + "\n");
                break;
            case "stats" :
                socket.write(stats() + "\n");
                break;
            case "quit":
                socket.end();
                break;
        }
    } else {
        socket.write("Invalid command!" + "\n");
    }
};
Example #6
0
 findById:function (id) {
     var pk = this.primaryKey;
     pk = pk.length == 1 ? pk[0] : pk;
     var q = {};
     if (comb.isArray(id) && comb.isArray(pk)) {
         if (id.length === pk.length) {
             pk.forEach(function (k, i) {
                 q[k] = id[i]
             });
         } else {
             throw new ModelError("findById : ids length does not equal the primaryKeys length.");
         }
     } else {
         q[pk] = id;
     }
     return this.filter(q).one();
 },
Example #7
0
 _insertValuesSql:function (sql) {
     var values = this.__opts.values;
     if (comb.isArray(values) && values.length == 0) {
         return " VALUES ()";
     } else {
         return this._super(arguments);
     }
 },
Example #8
0
 this._filter(parent)[this._fetchMethod]().then(hitch(this, function(result){
     var p;
     if ((comb.isArray(result) && result.length) || comb.isHash(result)) {
         p = this.__loadModel(parent, result);
     } else {
         this.__setValue(parent, result);
         p = new Promise().callback(result);
     }
     p.then(hitch(ret, "callback"), hitch(ret, "errback"));
 }), hitch(ret, "errback"));
Example #9
0
exports.parse = function(fileNames, browser) {
	if (comb.isArray(fileNames)) {
		var string = comb.string;
		logger.info(string.style(string.format("Running %d tests", fileNames.length)));
		for (var i in fileNames) {
			runTest(fileNames[i], browser);
		}
	} else {
		runTest(fileNames, browser);
	}

}
Example #10
0
 __transactionError:function (err, promise) {
     if (comb.isArray(err)) {
         for (var i in err) {
             var e = err[i];
             if (comb.isArray(e) && e.length == 2) {
                 var realE = e[1];
                 if (realE != "ROLLBACK") {
                     promise.errback(realE);
                     break;
                 } else {
                     promise.callback();
                     break;
                 }
             } else {
                 promise.errback(e);
                 break;
             }
         }
     } else {
         if (e != "ROLLBACK") {
             throw e;
         }
     }
 },
Example #11
0
 __graphAliasColumns:function(graphAliases) {
   var ret = [];
   if (comb.isArray(graphAliases)) {
     var newGraphAliases = {};
     for (var i in graphAliases) {
       newGraphAliases[graphAliases[i][0]] = graphAliases[i][1];
     }
     graphAliases = newGraphAliases;
   }
   for (var colAlias in graphAliases) {
     var tc = graphAliases[colAlias];
     var identifier = tc[2] || new sql.QualifiedIdentifier(tc[0], tc[1]);
     if (tc[2] || tc[1] != colAlias) {
       identifier = new sql.AliasedExpression(identifier, colAlias);
     }
     ret.push(identifier);
   }
   return ret;
 },
Example #12
0
 _postSave:function (next, model) {
     var loaded = this.associationLoaded(model), vals;
     if (loaded && (vals = this.getAssociation(model))) {
         if (comb.isArray(vals) && vals.length) {
             this._clearAssociations(model);
             var pl = this.addAssociations(vals, model);
             if (this.isEager()) {
                 pl = pl.chain(hitch(this, "fetch", model), next)
             }
             pl.both(next);
         } else {
             next();
         }
     } else if (this.isEager() && !loaded) {
         this.fetch(model).both(next);
     } else {
         next();
     }
 },
Example #13
0
        dropView : function(names) {
            var ret = new Promise(), l = names.length;
            if (comb.isArray(names)) {
                var drop = hitch(this, function(i) {
                    if (i < l) {
                        var name = names[i++];
                        this.executeDdl(format("DROP VIEW %s", this.__quoteSchemaTable(name))).then(hitch(this, function() {
                            this.removeCachedSchema(name);
                            drop(i);
                        }), hitch(ret, "errback"));
                    } else {
                        ret.callback();
                    }
                });
                drop(0);
                return ret;
            } else {
                return this.dropView(argsToArray(arguments));

            }
        },
Example #14
0
 __constraintDefinitionSql : function(constraint) {
     var sql = constraint.name ? format("CONSTRAINT %s ", this.__quoteIdentifier(constraint.name)) : "";
     switch (constraint.type) {
         case "check":
             var check = constraint.check;
             sql += format("CHECK %s", this.__filterExpr(comb.isArray(check) && check.length == 1 ? check[0] : check));
             break;
         case "primaryKey":
             sql += format("PRIMARY KEY %s", this.literal(constraint.columns));
             break;
         case "foreignKey":
             sql += this.__columnReferencesTableConstraintSql(constraint);
             break;
         case "unique":
             sql += format("UNIQUE %s", this.literal(constraint.columns));
             break;
         default:
             throw new DatabaseError(format("Invalid constriant type %s, should be 'check', 'primaryKey', foreignKey', or 'unique'", contraint.type));
     }
     return sql;
 },
Example #15
0
				loadSchemas : function(tableNames, database) {
					var ret = new Promise(), pl, ps;
					if (comb.isArray(tableNames)) {
						if (tableNames.length) {
							ps = tableNames.map(function(name) {
								return this.loadSchema(name, database);
							}, this);
							pl = new PromiseList(ps);
							pl.addCallback(function(r) {
								// loop through and load the results
								ret.callback.apply(ret, r.map(function(o) {
									return o[1];
								}));
							});
							pl.addErrback(hitch(ret, "errback"));
						} else {
							ret.callback(null);
						}
					} else if (comb.isObject((tableNames))) {
						ps = [];
						for (var i in tableNames) {
							//load the schemas
							ps.push(this.loadSchemas(tableNames[i], i));
						}
						pl = new PromiseList(ps).then(function(r) {
							var tables = [];
							r.forEach(function(ts) {
								//remove the first item
								ts.shift();
								tables.push.apply(tables, ts);
							});
							//apply it so they are called back as arguments;
							ret.callback.apply(ret, tables);
						}, comb.hitch(ret, "errback"));

					} else {
						throw new Error("tables names must be an array");
					}
					return ret;
				},
Example #16
0
 addAssociations:function (items, model) {
     var ret = new comb.Promise(), pl;
     if (model.isNew) {
         (comb.isArray(items) ? items : [items]).map(function (item) {
             return this.addAssociation(item, model, false);
         }, this);
         ret.callback(model);
     } else {
         pl = model._checkTransaction(comb.hitch(this, function () {
             return new PromiseList((comb.isArray(items) ? items : [items]).map(function (item) {
                 return this.addAssociation(item, model, false);
             }, this));
         }));
         pl.then(hitch(this, function () {
             if (!model.isNew && this.associationLoaded(model)) {
                 this.parent._reloadAssociationsForType(this.type, this.model, model).then(hitch(ret, "callback", model), hitch(ret, "errback"));
             } else {
                 ret.callback(model);
             }
         }), hitch(ret, "errback"));
     }
     return ret;
 },
Example #17
0
        forceDropTable : function(names) {
            var ret = new Promise(), l = names.length;
            if (comb.isArray(names)) {
                var drop = hitch(this, function(i) {
                    if (i < l) {
                        var name = names[i++];
                        this.executeDdl(this.__dropTableSql(name)).both(hitch(this, function() {
                            this.removeCachedSchema(name);
                            drop(i);
                        }));
                    } else {
                        ret.callback();
                    }
                });
                drop(0);
                return ret;
            } else {
                return this.dropTable(argsToArray(arguments).filter(function(i) {
                    return comb.isString(i)
                }));

            }
        },
Example #18
0
 return this._checkTransaction(options, hitch(this, function () {
     var ps;
     if (comb.isArray(items)) {
         ps = items.map(function (o) {
             if (!comb.isInstanceOf(o, this)) {
                 o = new this(o);
             }
             return o.save(null, options);
         }, this);
         var ret = new comb.Promise();
         return new PromiseList(ps, true).then(hitch(ret, "callback"), hitch(ret, "errback"));
     } else {
         var ret = new comb.Promise();
         try {
             if (!comb.isInstanceOf(items, this)) {
                 items = new this(items);
             }
             ret = items.save(null, options);
         } catch (e) {
             ret.errback(e);
         }
         return ret;
     }
 }));
Example #19
0
 removeItems:function (items, model, remove) {
     //todo make this more efficient!!!!
     var ret = new comb.Promise();
     if (model.isNew) {
         (comb.isArray(items) ? items : [items]).map(function (item) {
             return this.removeItem(item, model, remove, false);
         }, this);
         ret.callback(model);
     } else {
         var pl = model._checkTransaction(comb.hitch(this, function () {
             return new PromiseList((comb.isArray(items) ? items : [items]).map(function (item) {
                 return this.removeItem(item, model, remove, false);
             }, this));
         }));
         pl.then(hitch(this, function () {
             if (this.associationLoaded(model)) {
                 this.parent._reloadAssociationsForType(this.type, this.model, model).then(hitch(ret, "callback", model), hitch(ret, "errback"));
             } else {
                 ret.callback(model);
             }
         }), hitch(ret, "errback"));
     }
     return ret;
 },
Example #20
0
 __setValue:function(parent, model){
     parent.__associations[this.name] = this._fetchMethod == "all" ? !comb.isArray(model) ? [model] : model : comb.isArray(model) ? model[0] : model;
     return parent.__associations[this.name];
 },
Example #21
0
 PG_DB.__defineGetter__("sqls", function () {
     return (comb.isArray(this.__sqls) ? this.__sqls : (this.__sqls = []));
 });
Example #22
0
 parent.prototype[removeNames] = function (item, remove) {
     return comb.isArray(item) ? self.removeItems(item, this, remove) : self.removeItem(item, this, remove);
 };
Example #23
0
 parent.prototype[addNames] = function (items) {
     return comb.isArray(items) ? self.addAssociations(items, this) : self.addAssociation(items, this);
 };
Example #24
0
 parent.prototype[addName] = function (item) {
     return comb.isArray(item) ? self.addAssociations(item, this) : self.addAssociation(item, this, true);
 };
Example #25
0
 var pl = model._checkTransaction(comb.hitch(this, function () {
     return new PromiseList((comb.isArray(items) ? items : [items]).map(function (item) {
         return this.removeItem(item, model, remove, false);
     }, this));
 }));