Files

613 lines
9.7 KiB
JavaScript
Raw Permalink Normal View History

2025-12-25 11:16:59 +01:00
/*
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 );
}
}
}