123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- "use strict";
- var equalObjects = require('./equalObjects');
- var Id = require('./Id');
- var Parse = require('parse/node');
- function flattenOrQueries(where) {
- if (!Object.prototype.hasOwnProperty.call(where, '$or')) {
- return where;
- }
- var accum = [];
- for (var i = 0; i < where.$or.length; i++) {
- accum = accum.concat(where.$or[i]);
- }
- return accum;
- }
- function stringify(object) {
- if (typeof object !== 'object' || object === null) {
- if (typeof object === 'string') {
- return '"' + object.replace(/\|/g, '%|') + '"';
- }
- return object + '';
- }
- if (Array.isArray(object)) {
- var copy = object.map(stringify);
- copy.sort();
- return '[' + copy.join(',') + ']';
- }
- var sections = [];
- var keys = Object.keys(object);
- keys.sort();
- for (var k = 0; k < keys.length; k++) {
- sections.push(stringify(keys[k]) + ':' + stringify(object[keys[k]]));
- }
- return '{' + sections.join(',') + '}';
- }
- function queryHash(query) {
- if (query instanceof Parse.Query) {
- query = {
- className: query.className,
- where: query._where
- };
- }
- var where = flattenOrQueries(query.where || {});
- var columns = [];
- var values = [];
- var i;
- if (Array.isArray(where)) {
- var uniqueColumns = {};
- for (i = 0; i < where.length; i++) {
- var subValues = {};
- var keys = Object.keys(where[i]);
- keys.sort();
- for (var j = 0; j < keys.length; j++) {
- subValues[keys[j]] = where[i][keys[j]];
- uniqueColumns[keys[j]] = true;
- }
- values.push(subValues);
- }
- columns = Object.keys(uniqueColumns);
- columns.sort();
- } else {
- columns = Object.keys(where);
- columns.sort();
- for (i = 0; i < columns.length; i++) {
- values.push(where[columns[i]]);
- }
- }
- var sections = [columns.join(','), stringify(values)];
- return query.className + ':' + sections.join('|');
- }
- function contains(haystack, needle) {
- if (needle && needle.__type && needle.__type === 'Pointer') {
- for (const i in haystack) {
- const ptr = haystack[i];
- if (typeof ptr === 'string' && ptr === needle.objectId) {
- return true;
- }
- if (ptr.className === needle.className && ptr.objectId === needle.objectId) {
- return true;
- }
- }
- return false;
- }
- if (Array.isArray(needle)) {
- for (const need of needle) {
- if (contains(haystack, need)) {
- return true;
- }
- }
- }
- return haystack.indexOf(needle) > -1;
- }
- function matchesQuery(object, query) {
- if (query instanceof Parse.Query) {
- var className = object.id instanceof Id ? object.id.className : object.className;
- if (className !== query.className) {
- return false;
- }
- return matchesQuery(object, query._where);
- }
- for (var field in query) {
- if (!matchesKeyConstraints(object, field, query[field])) {
- return false;
- }
- }
- return true;
- }
- function equalObjectsGeneric(obj, compareTo, eqlFn) {
- if (Array.isArray(obj)) {
- for (var i = 0; i < obj.length; i++) {
- if (eqlFn(obj[i], compareTo)) {
- return true;
- }
- }
- return false;
- }
- return eqlFn(obj, compareTo);
- }
- function matchesKeyConstraints(object, key, constraints) {
- if (constraints === null) {
- return false;
- }
- if (key.indexOf('.') >= 0) {
-
- var keyComponents = key.split('.');
- var subObjectKey = keyComponents[0];
- var keyRemainder = keyComponents.slice(1).join('.');
- return matchesKeyConstraints(object[subObjectKey] || {}, keyRemainder, constraints);
- }
- var i;
- if (key === '$or') {
- for (i = 0; i < constraints.length; i++) {
- if (matchesQuery(object, constraints[i])) {
- return true;
- }
- }
- return false;
- }
- if (key === '$and') {
- for (i = 0; i < constraints.length; i++) {
- if (!matchesQuery(object, constraints[i])) {
- return false;
- }
- }
- return true;
- }
- if (key === '$nor') {
- for (i = 0; i < constraints.length; i++) {
- if (matchesQuery(object, constraints[i])) {
- return false;
- }
- }
- return true;
- }
- if (key === '$relatedTo') {
-
- return false;
- }
-
- if (object[key] && object[key].__type == 'Date') {
- object[key] = new Date(object[key].iso);
- }
-
- if (typeof constraints !== 'object') {
- if (Array.isArray(object[key])) {
- return object[key].indexOf(constraints) > -1;
- }
- return object[key] === constraints;
- }
- var compareTo;
- if (constraints.__type) {
- if (constraints.__type === 'Pointer') {
- return equalObjectsGeneric(object[key], constraints, function (obj, ptr) {
- return typeof obj !== 'undefined' && ptr.className === obj.className && ptr.objectId === obj.objectId;
- });
- }
- return equalObjectsGeneric(object[key], Parse._decode(key, constraints), equalObjects);
- }
-
- for (var condition in constraints) {
- var _compareTo;
- compareTo = constraints[condition];
- if ((_compareTo = compareTo) !== null && _compareTo !== void 0 && _compareTo.__type) {
- compareTo = Parse._decode(key, compareTo);
- }
- switch (condition) {
- case '$lt':
- if (object[key] >= compareTo) {
- return false;
- }
- break;
- case '$lte':
- if (object[key] > compareTo) {
- return false;
- }
- break;
- case '$gt':
- if (object[key] <= compareTo) {
- return false;
- }
- break;
- case '$gte':
- if (object[key] < compareTo) {
- return false;
- }
- break;
- case '$eq':
- if (!equalObjects(object[key], compareTo)) {
- return false;
- }
- break;
- case '$ne':
- if (equalObjects(object[key], compareTo)) {
- return false;
- }
- break;
- case '$in':
- if (!contains(compareTo, object[key])) {
- return false;
- }
- break;
- case '$nin':
- if (contains(compareTo, object[key])) {
- return false;
- }
- break;
- case '$all':
- if (!object[key]) {
- return false;
- }
- for (i = 0; i < compareTo.length; i++) {
- if (object[key].indexOf(compareTo[i]) < 0) {
- return false;
- }
- }
- break;
- case '$exists':
- {
- const propertyExists = typeof object[key] !== 'undefined';
- const existenceIsRequired = constraints['$exists'];
- if (typeof constraints['$exists'] !== 'boolean') {
-
-
- break;
- }
- if (!propertyExists && existenceIsRequired || propertyExists && !existenceIsRequired) {
- return false;
- }
- break;
- }
- case '$regex':
- if (typeof compareTo === 'object') {
- return compareTo.test(object[key]);
- }
-
- var expString = '';
- var escapeEnd = -2;
- var escapeStart = compareTo.indexOf('\\Q');
- while (escapeStart > -1) {
-
- expString += compareTo.substring(escapeEnd + 2, escapeStart);
- escapeEnd = compareTo.indexOf('\\E', escapeStart);
- if (escapeEnd > -1) {
- expString += compareTo.substring(escapeStart + 2, escapeEnd).replace(/\\\\\\\\E/g, '\\E').replace(/\W/g, '\\$&');
- }
- escapeStart = compareTo.indexOf('\\Q', escapeEnd);
- }
- expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
- var exp = new RegExp(expString, constraints.$options || '');
- if (!exp.test(object[key])) {
- return false;
- }
- break;
- case '$nearSphere':
- if (!compareTo || !object[key]) {
- return false;
- }
- var distance = compareTo.radiansTo(object[key]);
- var max = constraints.$maxDistance || Infinity;
- return distance <= max;
- case '$within':
- if (!compareTo || !object[key]) {
- return false;
- }
- var southWest = compareTo.$box[0];
- var northEast = compareTo.$box[1];
- if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
-
- return false;
- }
- return object[key].latitude > southWest.latitude && object[key].latitude < northEast.latitude && object[key].longitude > southWest.longitude && object[key].longitude < northEast.longitude;
- case '$containedBy':
- {
- for (const value of object[key]) {
- if (!contains(compareTo, value)) {
- return false;
- }
- }
- return true;
- }
- case '$geoWithin':
- {
- if (compareTo.$polygon) {
- const points = compareTo.$polygon.map(geoPoint => [geoPoint.latitude, geoPoint.longitude]);
- const polygon = new Parse.Polygon(points);
- return polygon.containsPoint(object[key]);
- }
- if (compareTo.$centerSphere) {
- const [WGS84Point, maxDistance] = compareTo.$centerSphere;
- const centerPoint = new Parse.GeoPoint({
- latitude: WGS84Point[1],
- longitude: WGS84Point[0]
- });
- const point = new Parse.GeoPoint(object[key]);
- const distance = point.radiansTo(centerPoint);
- return distance <= maxDistance;
- }
- break;
- }
- case '$geoIntersects':
- {
- const polygon = new Parse.Polygon(object[key].coordinates);
- const point = new Parse.GeoPoint(compareTo.$point);
- return polygon.containsPoint(point);
- }
- case '$options':
-
-
- break;
- case '$maxDistance':
-
-
- break;
- case '$select':
- return false;
- case '$dontSelect':
- return false;
- default:
- return false;
- }
- }
- return true;
- }
- var QueryTools = {
- queryHash: queryHash,
- matchesQuery: matchesQuery
- };
- module.exports = QueryTools;
|