Files
Unify/framework/server/table.js
2025-12-25 11:16:59 +01:00

320 lines
5.4 KiB
JavaScript

/*
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 clientTable from '../client/table.js';
import tools from '../unify/tools.js';
import document from '../unify/document.js';
import querySQL from '../unify/querySQL.js';
import Console from './console.js';
import collection from '../unify/collection.js';
import permissionManager from '../unify/permissionManager.js';
import userManager from '../server/userManager.js';
export default class table extends clientTable{
__className = "table";
permissionManager = new permissionManager();
type = "table";
constructor() {
super();
Console.setFilename("table.js");
}
setCollectionObject( collectionObject ) {
var newCollection = new collection( collectionObject );
this.setCollection( newCollection );
}
setCollection( collection ) {
if( collection ) {
this.collections = [ collection ];
}
}
parseObject( object, propertyName) {
if( tools.objectIsRenderCollection( object ) )
{
// hasMany collection render join
this.parseRenderCollection( object );
} else if( tools.objectIsTable( object ) ) {
// hasOne
if( propertyName != "table" ) {
this.joinObject( object );
}
}
}
getProperties( parent, child ) {
var entries = Object.entries( parent );
for( var c = 0; c < entries.length; c++ ) {
var entry = entries[ c ];
var object = tools.getObjectByEntry( entry );
var propertyName = tools.getClassNameByEntry( entry );
this.parseObject( object, propertyName);
}
}
joinObject( object ) {
var tableName = tools.getClassName( object );
var parentName = tools.getClassName( tools.getTableFromObject( object.parent ) );
var columnName = object.propertyName;
// ?? doesn't do anything.
//Console.log( "Select * From " + tableName + " where " + parentName + "." + columnName +"_id = " + tableName + ".id" );
}
parseRenderCollection( renderCollection ) {
var collections = renderCollection.collections;
for(var b = 0; b < collections.length;b++) {
var currentCollection = collections[b];
var join = currentCollection.createJoin( renderCollection.propertyName );
}
var renderCollectionObject = renderCollection.getObject();
this.getProperties( renderCollectionObject, renderCollection );
}
getRow() {
this.getProperties( this.constructor );
//console.log("get table from this object", this);
var table = tools.getTableFromObject( this );
var tableName = tools.getClassName( table );
var query = new querySQL();
query.type = "select";
query.table = tableName;
query.find( "id", this.id );
var output = global.database.query( query );
if( output[0] ) {
return output[0];
}
}
// For server side, When being instantiated
create() {
if( typeof document == "undefined" ) {
this.createInstance();
}
}
createInstance( inherit_id = true ) {
this.signed.value = true;
var new_inser_id = global.database.insertRow( this );
if( inherit_id ) {
this.id = new_inser_id;
}
return new_inser_id;
}
updateChildrenPermissions( object, user ) {
if( object.permission ) {
object.permissions = userManager.computePermissions( object, user );
}
if( object.getChildren ) {
var children = object.getChildren();
for (var i = 0; i < children.length; i++) {
var child = children[i]
this.updateChildrenPermissions( child, user );
}
}
}
get( client ) {
var sqlRow = this.getRow();
if( sqlRow ) {
//console.log("get", this.getClassName(), sqlRow);
this.serialize( sqlRow );
this.updated = true;
}
if( client ) {
//this.computePermissions( client );
var user = client.user;
this.updateChildrenPermissions( this, user );
//this.permissions = userManager.computePermissions( this, user );
//this.computePermissions( this, user );
}
}
delete() {
var table = this.table;
var tableName = table.getClassName();
var query = new querySQL();
query.type = "delete";
query.table = tableName;
query.find( "id", this.id );
var output = global.database.query( query );
return output;
}
save() {
if( !this.table ) {
this.getCore().tableManager.parse( this )
}
var table = this.table;
var tableName = table.getClassName();
var query = new querySQL();
query.type = "update";
query.table = tableName;
query.debug = this.debug;
query.addObject( this );
var query = global.database.query( query );
}
find( column, value ) {
var tableName = tools.getTableName( this );
var query = new querySQL();
query.type = "select";
query.table = tableName;
query.find( column, value );
return global.database.query( query );
}
}