Skip to content

Instantly share code, notes, and snippets.

@thejh
Created February 12, 2012 20:57
Show Gist options
  • Save thejh/1810814 to your computer and use it in GitHub Desktop.
Save thejh/1810814 to your computer and use it in GitHub Desktop.
/**
* @private
* Query a Schema for a row or rows by example
*
* @param {object} name name of Schema OR a Schema object
* @param {object} example example to query
* @param {boolean} single true to return a single row, otherwise all matching rows
* @returns {object} A single row matching example, or all matching rows
*/
function find(name, example, single, _) {
var schema = getSchema(name);
name = schema.name;
example = example || {};
var where = Schema.where(name, example);
var query = [
'SELECT',
' *',
'FROM',
' ' + name
];
if (where.length) {
query.push('WHERE');
query.push(where.join(' AND '));
}
if (single) {
var ret = SQL.getDataRow(query, _);
return empty(ret) ? false : Schema.onLoad(schema, ret);
}
else {
return Schema.onLoad(schema, SQL.getDataRows(query, _));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment