_filter:function (parent) { var keys = this._getAssociationKey(parent); var options = this.__opts || {}; var ds; if (!comb.isUndefined((ds = options.dataset)) && comb.isFunction(ds)) { ds = ds.apply(parent, [parent]); } if (!ds) { ds = this.model.dataset.naked().innerJoin(this.joinTableName, comb.array.zip(keys[1], this.modelPrimaryKey.map(function (k) { return sql.stringToIdentifier(k); })).concat(comb.array.zip(keys[0], this.parentPrimaryKey.map(function (k) { return parent[k] })))); var recip = this.model._findAssociation(this); recip && (recip = recip[1]); ds.rowCb = comb.hitch(this, function (item) { var ret = new comb.Promise(); var model = this._toModel(item, true); recip && recip.__setValue(model, parent); //call hook to finish other model associations model._hook("post", "load").then(hitch(this, function () { ret.callback(model); }), hitch(ret, "errback")); return ret; }); } return this._setDatasetOptions(ds); },
_filter:function(parent){ var keys = this._getAssociationKey(parent); var options = this.__opts || {}; var ds; if (!comb.isUndefined((ds = options.dataset)) && comb.isFunction(ds)) { ds = ds.apply(parent, [parent]); } if (!ds) { ds = this.model.dataset.naked().innerJoin(this.joinTableName, comb.array.zip(keys[1], this.modelPrimaryKey.map(function(k){ return sql.stringToIdentifier(k); })).concat(comb.array.zip(keys[0], this.parentPrimaryKey.map(function(k){ return parent[k] })))) } return this._setDatasetOptions(ds); },
graphEach:function(cb) { // Reject tables with nil datasets, as they are excluded from // the result set var datasets = comb.array.toArray(this.__opts.graph.tableAliases).filter(function(e) { return !comb.isUndefinedOrNull(e[1]) }); // Get just the list of table aliases into a local variable, for speed var tableAliases = datasets.map(function(e) { return e[0] }); datasets = datasets.map(function(e) { return [e[0], e[1], e[1].rowCb] }); // Use the manually set graph aliases, if any, otherwise // use the ones automatically created by .graph var columnAliases = this.__opts.graphAliases || this.__opts.graph.columnAliases; var ret = new comb.Promise(); return this.fetchRows(this.selectSql, function(r) { var graph = {}; // Create the sub hashes, one per table tableAliases.forEach(function(ta) { graph[ta] = {}; }); // Split the result set based on the column aliases // If there are columns in the result set that are // not in column_aliases, they are ignored for (var colAlias in columnAliases) { var tc = columnAliases[colAlias]; var ta = tc[0], column = tc[1]; !graph[ta] && (graph[ta] = {}); graph[ta][column] = r[colAlias]; } datasets.forEach(function(d) { var ta = d[0], ds = d[1], dsCb = d[2]; var g = graph[ta]; if (!comb.isEmpty(g) && Object.keys(g).some(function(x) { return !comb.isUndefinedOrNull(g[x]); })) { graph[ta] = dsCb ? dsCb(g) : g; } else { graph[ta] = null; } }); cb(graph); }); }
exports.create = function (name, supers, modelOptions) { !patio && (patio = require("./index")); var db, ds, tableName, ret = new comb.Promise() if (comb.isString(name)) { tableName = name; db = patio.defaultDatabase; ds = db.from(tableName); } else if (comb.isInstanceOf(name, patio.Dataset)) { ds = name; tableName = ds.firstSourceAlias; db = ds.db; } checkAndAddDBToTable(db, MODELS); checkAndAddDBToTable(db, DEFERRED_MODELS); var hasSuper = false, deffer = false; if (comb.isHash(supers) || comb.isUndefinedOrNull(supers)) { modelOptions = supers; supers = [Model]; } else if (comb.isString(supers)) { //if this happens then assume its the name of a table that is deferred. //check if it is loaded if ((model = MODELS.get(db)[checkAndTransformName(supers)]) != null) { supers = [model] } else { //its not loaded check if it is deferred if ((model = DEFERRED_MODELS.get(db)[checkAndTransformName(supers)]) != null) { DEFERRED_MODELS.get(db)[tableName] = ret; model.then(function (SuperModel) { DEFERRED_MODELS.get(db)[tableName] = null; exports.create(name, SuperModel, modelOptions).then(hitch(ret, "callback"), hitch(ret, "errback")); }, hitch(ret, "errback")); } else { //couldnt find it thrown an error! return ret.errback(new ModelError("Invalid super class " + supers)); } return ret; } } else { hasSuper = true; } supers = comb.array.toArray(supers); var model; if ((model = MODELS.get(db)[checkAndTransformName(tableName.toString())]) != null) { ret.callback(model); } else if ((model = DEFERRED_MODELS.get(db)[checkAndTransformName(tableName.toString())]) != null) { ret = model; } else { DEFERRED_MODELS.get(db)[tableName] = ret; var DEFAULT_PROTO = {instance:{}, static:{}}; modelOptions = merge(DEFAULT_PROTO, modelOptions || {}); modelOptions.instance._hooks = ["save", "update", "remove", "load"]; modelOptions.instance.__hooks = {pre:{}, post:{}}; //Mixin the column setter/getters db.schema(tableName).then(function (schema) { modelOptions.static.__schema = schema; modelOptions.static.__tableName = tableName; modelOptions.static.__db = db; model = comb.define(supers.concat(modelOptions.plugins || []), modelOptions); if (hasSuper) { supers.forEach(hitch(model, "inherits")); } ["pre", "post"].forEach(function (op) { var optionsOp = modelOptions[op]; if (optionsOp) { for (var i in optionsOp) { model[op](i, optionsOp[i]); } } }); MODELS.get(db)[tableName] = model; //clear it out DEFERRED_MODELS.get(db)[tableName] = null; ret.callback(model); }, comb.hitch(ret, "errback")); } return ret; };
function (r) { return this.literal(comb.array.toArray(r)); }, this).join(this._static.COMMA_SEPARATOR)))];
fullTextSql:function (cols, term, opts) { opts = opts || {}; return format("MATCH %s AGAINST (%s%s)", this.literal(comb.array.toArray(cols)), this.literal(comb.array.toArray(term).join(" ")), opts.boolean ? " IN BOOLEAN MODE" : ""); },
fullTextSearch:function (cols, terms, opts) { opts = opts || {}; cols = comb.array.toArray(cols).map(this.stringToIdentifier, this); return this.filter(sql.literal(this.fullTextSql(cols, terms, opts))); },