Created
February 26, 2014 03:48
-
-
Save coldhawaiian/9223196 to your computer and use it in GitHub Desktop.
FQL Query Builder: a standalone JavaScript class to dynamically generate Facebook Query Language queries.
This file contains hidden or 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
/* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2014 Keoki Zee | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
* | |
* INSTRUCTIONS | |
* ============ | |
* | |
* The `select()` and `where()` methods can either take individual arguments, or | |
* an array of arguments. `WHERE` conditions passed to `where()` are joined | |
* together using the string `" AND "` when `toString()` is called. `where()` | |
* can be called multiple times to add additional `WHERE` conditions. | |
* | |
* EXAMPLES | |
* ======== | |
* | |
* Example 1 | |
* --------- | |
* | |
* // Unix timestamp, expressed in seconds. | |
* var then = (new Date("Wed, 01 Jan 2014 00:00:00 UTC")).getTime() / 1000; | |
* var now = Math.round(Date.now() / 1000); | |
* | |
* var query = (new Query('stream')) | |
* .select('post_id', 'message', 'created_id') | |
* .where('source_id = me()', 'created_time >= ' + then, 'created_time < ' + now) | |
* .orderByDesc('created_time') | |
* .limit(5000) | |
* .toString(); | |
* | |
* Outputs | |
* | |
* SELECT post_id,message,created_id | |
* FROM stream | |
* WHERE (source_id = me()) AND (created_time >= {then}) AND (created_time < {now})` | |
* ORDER BY created_time DESC | |
* LIMIT 5000 | |
* | |
* as one line. | |
* | |
* Example 2 | |
* --------- | |
* | |
* This shows that arrays can be passed to `select()` and `when()`. | |
* | |
* // Unix timestamp, expressed in seconds. | |
* var then = (new Date("Wed, 01 Jan 2014 00:00:00 UTC")).getTime() / 1000; | |
* var now = Math.round(Date.now() / 1000); | |
* | |
* var query = (new Query('stream')) | |
* .select([ | |
* 'post_id', | |
* 'message', | |
* 'created_id' | |
* ]) | |
* .where([ | |
* 'source_id = me()', | |
* 'created_time >= ' + then, | |
* 'created_time < ' + now | |
* ]); | |
* | |
* Example 3 | |
* --------- | |
* | |
* This shows that `when()` can be called multiple times. | |
* | |
* // Unix timestamp, expressed in seconds. | |
* var then = (new Date("Wed, 01 Jan 2014 00:00:00 UTC")).getTime() / 1000; | |
* var now = Math.round(Date.now() / 1000); | |
* | |
* var query = (new Query('stream')) | |
* .select('post_id', 'message', 'created_id') | |
* .where('source_id = me()') | |
* .where('created_time >= ' + then) | |
* .where('created_time < ' + now); | |
*/ | |
var Query = (function () { | |
function Query (table) { | |
this._table = table; | |
this._conditions = []; | |
this._fields = []; | |
this._orderByField = null; | |
this._orderDirection = null; | |
this._max = null; | |
return this; | |
} | |
Query.prototype.select = function () { | |
var fields = arguments[0]; | |
if (!Array.isArray(fields)) { | |
fields = Array.prototype.slice.call(arguments); | |
} | |
this._fields = fields; | |
return this; | |
} | |
Query.prototype.where = function () { | |
var conditions = arguments[0]; | |
if (!Array.isArray(conditions)) { | |
conditions = Array.prototype.slice.call(arguments); | |
} | |
this._conditions = this._conditions.concat(conditions); | |
return this; | |
} | |
Query.prototype.orderByField = function (field) { | |
this._orderByField = field; | |
this._orderDirection = ' ASC'; | |
return this; | |
} | |
Query.prototype.orderByDesc = function (field) { | |
this._orderByField = field; | |
this._orderDirection = ' DESC'; | |
return this; | |
} | |
Query.prototype.limit = function (limit) { | |
this._max = limit; | |
return this; | |
} | |
Query.prototype.toString = function () { | |
var query = [ | |
'SELECT ' + this._fields.join(','), | |
'FROM ' + this._table | |
]; | |
var temp = ''; | |
if (this._conditions.length) { | |
temp = 'WHERE ' + this._conditions | |
.map(function (c) { return '(' + c + ')'; }) | |
.join(' AND '); | |
query.push(temp); | |
} | |
if (this._orderByField) { | |
query.push('ORDER BY ' + this._orderByField + this._orderDirection); | |
} | |
if (this._max) { | |
query.push('LIMIT ' + this._max); | |
} | |
return query.join(' '); | |
}; | |
return Query; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the issues with this class is that it doesn't sanitize input, so that is something that can be improved on.