First commit
This commit is contained in:
612
framework/server/collection/collection.js
Normal file
612
framework/server/collection/collection.js
Normal file
@@ -0,0 +1,612 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||||
as published by the ESA.
|
||||
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import querySQL from '../../unify/querySQL.js';
|
||||
|
||||
import joinSQL from '../../unify/joinSQL.js';
|
||||
|
||||
import tools from '../../unify/tools.js';
|
||||
|
||||
import unify from '../../unify/unify.js';
|
||||
|
||||
|
||||
import queryChildren from "./collection.queryChildren.js";
|
||||
|
||||
import queryRoot from "./collection.queryRoot.js";
|
||||
|
||||
import queryCount from "./collection.queryCount.js";
|
||||
|
||||
|
||||
|
||||
import searchable from '../../unify/sql/searchable.js';
|
||||
|
||||
import placeholder from '../../unify/placeholder.js';
|
||||
|
||||
|
||||
|
||||
export default class collection {
|
||||
|
||||
__className = "collection";
|
||||
|
||||
|
||||
queryChildren = new queryChildren();
|
||||
|
||||
queryRoot = new queryRoot();
|
||||
|
||||
queryCount = new queryCount();
|
||||
|
||||
|
||||
query = new querySQL();
|
||||
|
||||
filterObject;
|
||||
|
||||
|
||||
|
||||
find( ...values ) {
|
||||
|
||||
this.query.find( ...values );
|
||||
|
||||
}
|
||||
|
||||
createFilterObject() {
|
||||
|
||||
if( !this.filterObject ) {
|
||||
|
||||
this.filterObject = new placeholder();
|
||||
}
|
||||
|
||||
|
||||
this.filterObject.__className = "placeholder";
|
||||
|
||||
unify.extend( this.filterObject );
|
||||
|
||||
}
|
||||
|
||||
getCollectionObject() {
|
||||
|
||||
//var collection = this.getCollection();
|
||||
|
||||
var realObject = new this.object();
|
||||
|
||||
unify.extend( realObject );
|
||||
|
||||
return realObject;
|
||||
|
||||
}
|
||||
|
||||
createCollectionSearchable( collection, child ) {
|
||||
|
||||
var tableName = collection.createInstance().getClassName();
|
||||
//tableName
|
||||
var search = new searchable( "./" + collection.propertyName + "/" + child.propertyName );
|
||||
|
||||
this.filterObject[ collection.propertyName ][ child.propertyName ] = search;
|
||||
|
||||
}
|
||||
|
||||
handleFilterCollectionChildren( collectionObject, collection ) {
|
||||
|
||||
var collectionChildren = collectionObject.getChildren();
|
||||
|
||||
for( var b = 0; b < collectionChildren.length; b++ ) {
|
||||
|
||||
var child = collectionChildren[b];
|
||||
|
||||
this.createCollectionSearchable( collection, child );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
handleCollectionSearchables( child ) {
|
||||
|
||||
if( child.type == "collection") {
|
||||
|
||||
var collectionObject = child.createInstance();
|
||||
|
||||
unify.extend( collectionObject );
|
||||
|
||||
//console.log("get selectedUser from this", child);
|
||||
|
||||
this.filterObject[ child.propertyName ].tableName = collectionObject.getClassName()
|
||||
|
||||
this.filterObject[ child.propertyName ].type = "collection";
|
||||
|
||||
this.handleFilterCollectionChildren( collectionObject, child )
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createSearchable( child, parent ) {
|
||||
|
||||
//console.log("get news from this", parent);
|
||||
|
||||
var search = new searchable( parent.getClassName() + "/" + child.propertyName );
|
||||
|
||||
this.filterObject[ child.propertyName ] = search;
|
||||
|
||||
}
|
||||
|
||||
createSearchables( object ) {
|
||||
|
||||
var children = object.getChildren();
|
||||
|
||||
for( var c = 0; c < children.length; c++ ) {
|
||||
|
||||
var child = children[c];
|
||||
|
||||
this.createSearchable( child, object );
|
||||
|
||||
this.handleCollectionSearchables( child );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createSearchableID() {
|
||||
|
||||
var search = new searchable( "./id" );
|
||||
|
||||
this.filterObject[ "id" ] = search;
|
||||
|
||||
}
|
||||
|
||||
getFilter() {
|
||||
|
||||
this.createFilterObject();
|
||||
|
||||
this.createSearchableID();
|
||||
|
||||
var object = this.getCollectionObject();
|
||||
|
||||
this.createSearchables( object );
|
||||
|
||||
//console.log("this.filterObject", this.filterObject);
|
||||
|
||||
return this.filterObject;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
sync() {
|
||||
|
||||
|
||||
/*
|
||||
var object = this.createInstance();
|
||||
|
||||
var tableName = tools.getTableName( object );
|
||||
|
||||
this.query.type = "select";
|
||||
this.query.table = tableName;
|
||||
|
||||
var rows = global.database.query( this.query );
|
||||
*/
|
||||
|
||||
|
||||
var rows = this.querySelect( false, this.filterObject, false );
|
||||
|
||||
|
||||
//console.log("rows", rows);
|
||||
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
|
||||
var row = rows[i];
|
||||
|
||||
var object = this.createInstance();
|
||||
|
||||
object.serialize( row );
|
||||
|
||||
this.rows[i] = object;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//console.log(this.query);
|
||||
|
||||
//console.log(rows);
|
||||
|
||||
return this.rows;
|
||||
|
||||
}
|
||||
|
||||
length() {
|
||||
|
||||
return this.rows.length;
|
||||
|
||||
}
|
||||
|
||||
get( index ) {
|
||||
|
||||
if( index ) {
|
||||
|
||||
return this.rows[index];
|
||||
|
||||
} else {
|
||||
|
||||
return this.rows[0];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pop() {
|
||||
|
||||
return this.rows.pop();
|
||||
|
||||
}
|
||||
|
||||
shift() {
|
||||
|
||||
return this.rows.shift();
|
||||
|
||||
}
|
||||
|
||||
getRows() {
|
||||
|
||||
return this.rows;
|
||||
|
||||
}
|
||||
|
||||
|
||||
constructor() {
|
||||
|
||||
this.queryChildren.collection = this;
|
||||
|
||||
this.queryRoot.collection = this;
|
||||
|
||||
this.queryCount.collection = this;
|
||||
|
||||
|
||||
}
|
||||
|
||||
querySelect( userQuery, filterObject, client ) {
|
||||
|
||||
if( !userQuery ) {
|
||||
|
||||
userQuery = new querySQL();
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( this.parent ) {
|
||||
|
||||
this.queryChildren.queryType = "select"
|
||||
|
||||
return this.queryChildren.query( userQuery, filterObject );
|
||||
|
||||
} else {
|
||||
|
||||
this.queryRoot.queryType = "select"
|
||||
|
||||
return this.queryRoot.query( userQuery, filterObject, client );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
countRows() {
|
||||
|
||||
return this.queryCount.query();
|
||||
|
||||
}
|
||||
|
||||
filterQuery( query, filterObject ) {
|
||||
|
||||
this.prepareOrder( query, filterObject );
|
||||
|
||||
//console.log("filterQuery", filterObject);
|
||||
|
||||
if( filterObject.direction ) {
|
||||
|
||||
if( filterObject.direction.toLowerCase() == "asc" || filterObject.direction.toLowerCase() == "desc" ) {
|
||||
|
||||
query.direction = tools.htmlEntities( filterObject.direction );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if( filterObject.limit && typeof filterObject.limit == "number" ) {
|
||||
|
||||
query.limit = parseInt( filterObject.limit );
|
||||
|
||||
}
|
||||
|
||||
if( filterObject.from && typeof filterObject.from == "number" ) {
|
||||
|
||||
query.offset = parseInt( filterObject.from );
|
||||
|
||||
}
|
||||
|
||||
if( filterObject.offset && typeof filterObject.offset == "number" ) {
|
||||
|
||||
query.offset = parseInt( filterObject.offset );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
prepareOrder( unionQuery, filterObject ) {
|
||||
|
||||
if( filterObject.order ) {
|
||||
|
||||
var searchable = filterObject.order;
|
||||
|
||||
//console.log("search order . ", filterObject.order, searchable.getColumnName());
|
||||
|
||||
if( searchable ) {
|
||||
|
||||
var columnName = searchable.path.replace("/", ".");
|
||||
|
||||
unionQuery.orderBy( columnName );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
searchQuery( query, filterObject ) {
|
||||
|
||||
if( filterObject ) {
|
||||
|
||||
|
||||
if( filterObject.search) {
|
||||
|
||||
filterObject.search.path = "./"
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
query.filter = filterObject;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
if( filterObject.search ) {
|
||||
|
||||
var searchables = filterObject.search.findPath( "." );
|
||||
|
||||
for ( var i = 0; i < searchables.length; i++ ) {
|
||||
|
||||
var currentSearchable = searchables[i];
|
||||
|
||||
var columnName = currentSearchable.getColumnName();
|
||||
|
||||
var searchValue = currentSearchable.getValue();
|
||||
|
||||
var operator = currentSearchable.getOperator();
|
||||
|
||||
if( !searchValue ) {
|
||||
|
||||
var searchValue = "";
|
||||
|
||||
}
|
||||
|
||||
query.find( columnName, searchValue, operator, true, true );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
getID( object ) {
|
||||
|
||||
var id = object.id;
|
||||
|
||||
if( !id ) {
|
||||
|
||||
id = object.parent.id;
|
||||
|
||||
}
|
||||
|
||||
if( !id ) {
|
||||
|
||||
id = this.id;
|
||||
|
||||
}
|
||||
|
||||
return id;
|
||||
|
||||
}
|
||||
|
||||
rowExists( id ) {
|
||||
|
||||
var rows = this.rows;
|
||||
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
|
||||
var currentChild = rows[i];
|
||||
|
||||
if( id == currentChild.id ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
getRowByID( id ) {
|
||||
|
||||
var rows = this.rows;
|
||||
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
|
||||
var currentChild = rows[i];
|
||||
|
||||
if( id == currentChild.id ) {
|
||||
|
||||
return currentChild;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
findID( rows, id ) {
|
||||
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
|
||||
var currentChild = rows[i];
|
||||
|
||||
console.log("find id", id, currentChild.id);
|
||||
|
||||
if( id == currentChild.id ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
delete( object ) {
|
||||
|
||||
this.remove( object );
|
||||
|
||||
}
|
||||
|
||||
remove( object ) {
|
||||
|
||||
var row_id = object.id;
|
||||
|
||||
//var joinTable = this.tableName + "_" + this.propertyName;
|
||||
var joinTable = this.parentName + "_" + this.propertyName;
|
||||
|
||||
var query = new querySQL();
|
||||
|
||||
query.type = "delete";
|
||||
|
||||
query.table = joinTable;
|
||||
|
||||
query.find( this.getLeft() + "_id", this.parent.id );
|
||||
|
||||
query.find( this.getRight() + "_id", row_id ); //row.tableName
|
||||
|
||||
|
||||
global.database.query( query );
|
||||
|
||||
}
|
||||
|
||||
add( object ) {
|
||||
|
||||
var row_id = object.id;
|
||||
|
||||
var query = new querySQL();
|
||||
|
||||
var parentName = this.getParentName();
|
||||
|
||||
var joinTable = parentName + "_" + this.propertyName;
|
||||
|
||||
|
||||
query.type = "insert";
|
||||
|
||||
query.table = joinTable;
|
||||
|
||||
query.setColumn( this.getLeft() + "_id", this.parent.id );
|
||||
|
||||
query.setColumn( this.getRight() + "_id", row_id ); //row.tableName
|
||||
|
||||
|
||||
global.database.query( query );
|
||||
|
||||
}
|
||||
|
||||
getObject() {
|
||||
|
||||
if( this.propertyName == "storageCollection" ) {
|
||||
|
||||
return this.parent;
|
||||
|
||||
} else {
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
queryInsert( parent_id, last_insert_id ) {
|
||||
|
||||
var joinTable = this.parentName + "_" + this.propertyName;
|
||||
|
||||
var left = this.getLeft();
|
||||
|
||||
var right = this.getRight();
|
||||
|
||||
|
||||
var query = new querySQL( );
|
||||
|
||||
query.type = "insert";
|
||||
|
||||
query.table = joinTable;
|
||||
|
||||
query.setValue( right + "_id", last_insert_id );
|
||||
|
||||
query.setValue( left + "_id", parent_id );
|
||||
|
||||
console.log( query );
|
||||
|
||||
|
||||
return global.database.query( query );
|
||||
|
||||
}
|
||||
|
||||
|
||||
count() {
|
||||
|
||||
var userQuery = new querySQL();
|
||||
|
||||
var filterObject = new Object();
|
||||
|
||||
var client = false;
|
||||
|
||||
unify.extend( filterObject );
|
||||
|
||||
if( this.parent ) {
|
||||
|
||||
this.queryChildren.queryType = "count"
|
||||
|
||||
return this.queryChildren.query( userQuery, filterObject );
|
||||
|
||||
} else {
|
||||
|
||||
this.queryRoot.queryType = "count"
|
||||
|
||||
return this.queryRoot.query( userQuery, filterObject, client );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
123
framework/server/collection/collection.queryChildren.js
Normal file
123
framework/server/collection/collection.queryChildren.js
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||||
as published by the ESA.
|
||||
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import querySQL from '../../unify/querySQL.js';
|
||||
|
||||
import joinSQL from '../../unify/joinSQL.js';
|
||||
|
||||
import tools from '../../unify/tools.js';
|
||||
|
||||
|
||||
export default class queryChildren{
|
||||
|
||||
queryType = "select";
|
||||
|
||||
type = "collectionQuery"
|
||||
|
||||
__className = "queryChildren"
|
||||
|
||||
createQuery( joinTableName, joinQuery, id ) {
|
||||
|
||||
var query = new querySQL( );
|
||||
|
||||
query.type = this.queryType;
|
||||
|
||||
query.table = joinTableName;
|
||||
|
||||
query.debug = joinQuery.debug;
|
||||
|
||||
query.find( this.collection.getLeft() + "_id", id );
|
||||
|
||||
|
||||
return query;
|
||||
|
||||
}
|
||||
|
||||
createJoin( joinQuery ) {
|
||||
|
||||
var join = new joinSQL( joinQuery );
|
||||
|
||||
join.table = this.collection.tableName;
|
||||
|
||||
join.name = join.table;
|
||||
|
||||
join.left.table = join.name;
|
||||
|
||||
join.right.column = this.collection.getRight() + "_id";
|
||||
|
||||
join.find( "signed", true );
|
||||
|
||||
return join;
|
||||
|
||||
}
|
||||
|
||||
query( joinQuery, filterObject ) {
|
||||
|
||||
|
||||
var object = this.collection.getObject();
|
||||
|
||||
|
||||
var leftTable = this.collection.getParentName();
|
||||
|
||||
var rightTable = object.propertyName;
|
||||
|
||||
|
||||
var id = this.collection.getID( object );
|
||||
|
||||
if( !id ) {
|
||||
|
||||
if( this.collection.parent ) {
|
||||
|
||||
var id = this.collection.parent.id;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var joinTableName = leftTable + "_" + rightTable;
|
||||
|
||||
var query = this.createQuery( joinTableName, joinQuery, id );
|
||||
|
||||
var join = this.createJoin( joinQuery, join );
|
||||
|
||||
|
||||
if( filterObject ) {
|
||||
|
||||
this.collection.filterQuery( join, filterObject )
|
||||
|
||||
|
||||
|
||||
if( filterObject.search ) {
|
||||
|
||||
//console.log("searchQuery", join, filterObject);
|
||||
|
||||
this.collection.searchQuery( join, filterObject );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
join.setFilterAddress("./");
|
||||
|
||||
query.addJoin( join );
|
||||
|
||||
var result = global.database.query( query );
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
82
framework/server/collection/collection.queryCount.js
Normal file
82
framework/server/collection/collection.queryCount.js
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||||
as published by the ESA.
|
||||
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import querySQL from '../../unify/querySQL.js';
|
||||
|
||||
import joinSQL from '../../unify/joinSQL.js';
|
||||
|
||||
import tools from '../../unify/tools.js';
|
||||
|
||||
|
||||
export default class queryCount{
|
||||
|
||||
createJoin() {
|
||||
|
||||
var join = new joinSQL();
|
||||
|
||||
join.table = this.tableName;
|
||||
|
||||
join.name = "children";
|
||||
|
||||
join.left.table = join.name;
|
||||
|
||||
join.right.column = this.getRight() + "_id";
|
||||
|
||||
join.find( "signed", "true" );
|
||||
//join.find( this.tableName + ".joined", "true" ); // please fix
|
||||
|
||||
return join;
|
||||
|
||||
}
|
||||
|
||||
createQuery() {
|
||||
|
||||
var object = this.getObject();
|
||||
|
||||
|
||||
var leftTable = this.parentName;
|
||||
|
||||
var rightTable = object.propertyName;
|
||||
|
||||
var id = this.collection.getID();
|
||||
|
||||
var joinTableName = leftTable + "_" + rightTable;
|
||||
|
||||
|
||||
var query = new querySQL();
|
||||
|
||||
query.table = joinTableName;
|
||||
|
||||
query.find( this.getLeft() + "_id", id );
|
||||
|
||||
|
||||
return query;
|
||||
|
||||
}
|
||||
|
||||
query() {
|
||||
|
||||
var query = this.createQuery();
|
||||
|
||||
var join = this.createJoin();
|
||||
|
||||
query.addJoin( join );
|
||||
|
||||
// bug : todo : count not length
|
||||
return global.database.query( query ).length;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
321
framework/server/collection/collection.queryRoot.js
Normal file
321
framework/server/collection/collection.queryRoot.js
Normal file
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||||
as published by the ESA.
|
||||
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import querySQL from '../../unify/querySQL.js';
|
||||
|
||||
import joinSQL from '../../unify/joinSQL.js';
|
||||
|
||||
import tools from '../../unify/tools.js';
|
||||
|
||||
|
||||
export default class queryRoot{
|
||||
|
||||
queryType = "select";
|
||||
|
||||
type = "collectionQuery"
|
||||
|
||||
__className = "queryRoot"
|
||||
|
||||
prepareSelectQuery( query, object, userQuery ) {
|
||||
|
||||
query.table = this.collection.getTableName();
|
||||
|
||||
query.type = this.queryType;
|
||||
|
||||
query.setFilterAddress( query.table + "/" );
|
||||
|
||||
query.extractColumns( object, query.table );
|
||||
|
||||
query.find( "signed", true );
|
||||
|
||||
query.find( "joined", false );
|
||||
|
||||
query.debug = userQuery.debug;
|
||||
|
||||
|
||||
return query;
|
||||
|
||||
}
|
||||
|
||||
createChildrenJoin( joinTableName, collectionObject, object ) {
|
||||
|
||||
var childObject = new collectionObject.object()
|
||||
|
||||
var childObjectName = tools.getClassName( childObject );
|
||||
|
||||
|
||||
var childrenJoin = new joinSQL( );
|
||||
|
||||
childrenJoin.table = childObjectName;
|
||||
|
||||
childrenJoin.setFilterAddress( "./" + collectionObject.propertyName + "/" )
|
||||
|
||||
childrenJoin.joinType = "LEFT";
|
||||
|
||||
childrenJoin.name = collectionObject.propertyName;
|
||||
|
||||
childrenJoin.left.table = joinTableName;
|
||||
|
||||
childrenJoin.left.column = collectionObject.getRight() + "_id";
|
||||
|
||||
|
||||
childrenJoin.right.table = childrenJoin.name;
|
||||
|
||||
childrenJoin.right.column = "id";
|
||||
|
||||
|
||||
//childrenJoin.find( "signed", true );
|
||||
|
||||
//childrenJoin.find( "joined", true );
|
||||
|
||||
|
||||
return childrenJoin;
|
||||
|
||||
}
|
||||
|
||||
createParentJoin( joinTableName, collectionObject ) {
|
||||
|
||||
var parentJoin = new joinSQL( );
|
||||
|
||||
parentJoin.table = this.collection.getTableName();
|
||||
|
||||
parentJoin.joinType = "INNER";
|
||||
|
||||
parentJoin.name = "parentJoin";
|
||||
|
||||
|
||||
|
||||
parentJoin.left.table = joinTableName;
|
||||
|
||||
parentJoin.left.column = collectionObject.getLeft() + "_id";
|
||||
|
||||
parentJoin.right.table = parentJoin.name;
|
||||
|
||||
parentJoin.right.column = "id";
|
||||
|
||||
|
||||
|
||||
|
||||
return parentJoin;
|
||||
|
||||
}
|
||||
|
||||
createJoinQuery( joinTableName, object, collectionObject ) {
|
||||
|
||||
var query = new joinSQL( );
|
||||
|
||||
query.table = joinTableName;
|
||||
|
||||
query.joinType = "LEFT";
|
||||
|
||||
|
||||
query.name = joinTableName;
|
||||
|
||||
|
||||
|
||||
query.left.table = joinTableName;
|
||||
|
||||
query.left.column = collectionObject.getLeft() + "_id";
|
||||
|
||||
query.right.table = this.collection.getTableName();
|
||||
|
||||
query.right.column = "id";
|
||||
|
||||
query.extractColumns( object, "parentJoin" );
|
||||
|
||||
|
||||
return query;
|
||||
|
||||
}
|
||||
|
||||
searchChildrenJoin( child, childrenJoin, filterObject ) {
|
||||
|
||||
if( filterObject.search ) {
|
||||
|
||||
var searchables = filterObject.search.findPath( "./" + child.propertyName );
|
||||
|
||||
|
||||
for ( var i = 0; i < searchables.length; i++ ) {
|
||||
|
||||
var currentSearchable = searchables[i];
|
||||
|
||||
var columnName = currentSearchable.getColumnName();
|
||||
|
||||
var searchValue = currentSearchable.getValue();
|
||||
|
||||
var operator = currentSearchable.getOperator();
|
||||
|
||||
|
||||
if( !searchValue ) {
|
||||
|
||||
var searchValue = "";
|
||||
|
||||
}
|
||||
|
||||
childrenJoin.find( columnName, searchValue, operator, true, true );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
composeJoinQuery( query, collectionObject, childrenJoin, parentJoin ) {
|
||||
|
||||
query.addGroup( collectionObject.getLeft() + "_id" );
|
||||
|
||||
//childrenJoin.addJoin( );
|
||||
|
||||
query.addJoin( childrenJoin );
|
||||
|
||||
//query.addJoin( parentJoin );
|
||||
|
||||
}
|
||||
|
||||
createJoinTableName( collectionObject ) {
|
||||
|
||||
var leftTable = this.collection.getTableName();
|
||||
|
||||
var rightTable = collectionObject.propertyName;
|
||||
|
||||
var joinTableName = leftTable + "_" + rightTable;
|
||||
|
||||
|
||||
return joinTableName;
|
||||
|
||||
}
|
||||
|
||||
queryFilterChild( query, object, child, filterObject ) {
|
||||
|
||||
var collectionObject = object[ child.propertyName ];
|
||||
|
||||
var tableName = collectionObject.tableName;
|
||||
|
||||
|
||||
var joinTableName = this.createJoinTableName( collectionObject );
|
||||
|
||||
var joinQuery = this.createJoinQuery( joinTableName, object, collectionObject );
|
||||
|
||||
var childrenJoin = this.createChildrenJoin( joinTableName, collectionObject, object );
|
||||
|
||||
//var parentJoin = this.createParentJoin( joinTableName, collectionObject );
|
||||
|
||||
|
||||
|
||||
|
||||
this.composeJoinQuery( joinQuery, collectionObject, childrenJoin );
|
||||
|
||||
//this.searchChildrenJoin( child, childrenJoin, filterObject );
|
||||
|
||||
|
||||
query.addJoin( joinQuery );
|
||||
|
||||
//unionQuery.addUnion( joinQuery );
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
queryChildren( filterObject, query, client ) {
|
||||
|
||||
var children = filterObject.getChildren();
|
||||
|
||||
var joined = false;
|
||||
|
||||
|
||||
|
||||
|
||||
for( var c = 0; c < children.length; c++ ) {
|
||||
|
||||
var child = children[c];
|
||||
|
||||
if( child.type == "collection" ) {
|
||||
|
||||
var object = new this.collection.object();
|
||||
|
||||
core.parse( object, client );
|
||||
|
||||
//console.log("child.propertyName", child);
|
||||
|
||||
//console.log("filterObject.search", filterObject.search);
|
||||
|
||||
var tableName = filterObject.getClassName();
|
||||
|
||||
//console.log("filterObject.search.findPath", "./" + child.tableName );
|
||||
|
||||
//if( filterObject.search && filterObject.search.findPath( "./" + child.tableName ) ) {
|
||||
|
||||
if( filterObject.search && filterObject.search.findPath( "./" + child.propertyName ) ) {
|
||||
|
||||
this.queryFilterChild( query, object, child, filterObject );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
searchQuery( query, filterObject ) {
|
||||
|
||||
if( filterObject ) {
|
||||
|
||||
|
||||
if( filterObject.search) {
|
||||
|
||||
filterObject.search.path = query.tableName + "/"
|
||||
|
||||
}
|
||||
|
||||
query.filter = filterObject;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
query( userQuery, filterObject, client ) {
|
||||
|
||||
var core = this.collection.getCore();
|
||||
|
||||
var object = new this.collection.object();
|
||||
|
||||
var query = new querySQL();
|
||||
|
||||
|
||||
//console.log("the good ")
|
||||
|
||||
this.prepareSelectQuery( query, object, userQuery );
|
||||
|
||||
this.searchQuery( query, filterObject );
|
||||
|
||||
this.collection.filterQuery( query, filterObject );
|
||||
|
||||
this.queryChildren( filterObject, query, client );
|
||||
|
||||
|
||||
query.debug = userQuery.debug;
|
||||
|
||||
|
||||
|
||||
var parentResult = global.database.query( query );
|
||||
|
||||
|
||||
return parentResult;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user