295 lines
5.0 KiB
JavaScript
295 lines
5.0 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 tools from '../unify/tools.js';
|
|
|
|
import Console from './console.js';
|
|
|
|
import database from './database.js';
|
|
|
|
import datatype from '../unify/datatype.js';
|
|
|
|
import collection from '../unify/collection.js';
|
|
|
|
import querySQL from '../unify/querySQL.js';
|
|
|
|
import tableLogger from './tableLogger.js';
|
|
|
|
|
|
export default class databaseManager{
|
|
|
|
tables = new Array();
|
|
|
|
parse( object ) {
|
|
|
|
if( object.type == "table" ) {
|
|
|
|
if( !object.table ) {
|
|
|
|
var objectName = object.getClassName();
|
|
|
|
var tableObject = tools.getTableFromObject( object );
|
|
|
|
|
|
object.table = tableObject;
|
|
|
|
} else {
|
|
|
|
var tableObject = object.table;
|
|
|
|
}
|
|
|
|
this.addTable( tableObject )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addTable( tableObject ) {
|
|
|
|
var className = tools.getClassName( tableObject );
|
|
|
|
if ( !this.getTableByName( className ) ) {
|
|
|
|
this.tables.push( tableObject );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
getTableByName( name ){
|
|
|
|
for( var c = 0; c<this.tables.length; c++ ) {
|
|
|
|
var table = this.tables[c];
|
|
|
|
if( tools.getClassName( table ) == name ) {
|
|
|
|
return table;
|
|
|
|
}
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ensureTableColumsTable() {
|
|
|
|
var tableExists = database.tableExists("tableColumns")
|
|
|
|
|
|
if( !tableExists ) {
|
|
|
|
console.log("New database detected, Creating tableColumns table and columns.");
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
//database
|
|
//setup
|
|
|
|
}
|
|
|
|
|
|
|
|
createTables() {
|
|
|
|
this.ensureTableColumsTable();
|
|
|
|
var tables = this.tables;
|
|
|
|
for( var c = 0; c < tables.length; c++ ) {
|
|
|
|
var table = tables[c];
|
|
|
|
this.createTable( table );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getObjectByclassName( className ) {
|
|
|
|
var objectCollection = new collection();
|
|
|
|
objectCollection.set( this.tables );
|
|
|
|
objectCollection.filter( "class", className );
|
|
|
|
|
|
return objectCollection.getFirstRow();
|
|
|
|
}
|
|
|
|
logTable( tableName, children ) {
|
|
|
|
var tableLog = new tableLogger();
|
|
|
|
tableLog.setTable( tableName, [18, 18] );
|
|
|
|
for ( var c = 0; c < children.length; c++ ) {
|
|
|
|
var child = children[c];
|
|
|
|
var column = this.parseColumn( child, tableName );
|
|
|
|
tableLog.addColumn( column, datatype.TEXT );
|
|
|
|
}
|
|
|
|
tableLog.log();
|
|
|
|
}
|
|
|
|
createTable( object ) {
|
|
|
|
var tableName = tools.getClassName( object );
|
|
|
|
database.createTable( tableName );
|
|
|
|
database.cleanTable( tableName );
|
|
|
|
database.addColumn( "joined", datatype.TEXT, tableName );
|
|
|
|
|
|
var children = object.getChildren();
|
|
|
|
this.logTable( tableName, children );
|
|
|
|
}
|
|
|
|
parseColumnObject( object, tableName, columnName ) {
|
|
|
|
if( tools.objectIsTable( object ) ) {
|
|
|
|
// hasOne
|
|
database.addColumn( columnName + "_id", "INTEGER", tableName );
|
|
|
|
} else {
|
|
|
|
// regular column
|
|
database.addColumn( columnName, object.datatype, tableName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parseColumn( object, tableName ) {
|
|
|
|
var objectName = tools.getClassName( object.parent );
|
|
|
|
var columnName = object.propertyName;
|
|
|
|
if( tools.isUnifyObject( columnName, object ) ) {
|
|
|
|
if( tools.getClassName( object ) == "collection" ) {
|
|
|
|
// collection join
|
|
var column = this.createJoinTable( object, object.datatype );
|
|
|
|
} else {
|
|
|
|
this.parseColumnObject( object, tableName, columnName );
|
|
|
|
var column = columnName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
createJoinTable( object ) {
|
|
|
|
object.update();
|
|
|
|
var parentTableName = object.getLeft();
|
|
|
|
var objectName = object.getRight();
|
|
|
|
var propertyName = object.propertyName;
|
|
|
|
var tableName = object.parentName + "_" + propertyName;
|
|
|
|
|
|
database.createTable( tableName );
|
|
|
|
database.addColumn( "joint", datatype.TEXT, tableName );
|
|
|
|
database.addColumn( parentTableName + "_id", "INTEGER", tableName );
|
|
|
|
database.addColumn( objectName + "_id", "INTEGER", tableName );
|
|
|
|
|
|
var tableLog = new tableLogger();
|
|
|
|
tableLog.setTable( tableName, new Array( 18, 18 ) );
|
|
|
|
tableLog.addColumn( parentTableName + "_id", "INTEGER" );
|
|
|
|
tableLog.addColumn( objectName + "_id", "INTEGER" );
|
|
|
|
tableLog.log();
|
|
|
|
}
|
|
|
|
|
|
createColumn( column_name, type, columns_array ) {
|
|
|
|
var column = new Object();
|
|
|
|
column.name = column_name;
|
|
|
|
column.type = type;
|
|
|
|
if( columns_array ) {
|
|
|
|
columns_array.push( column );
|
|
|
|
}
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
setup() {
|
|
|
|
var tableName = "tableColumns";
|
|
|
|
database.createTable( tableName );
|
|
|
|
|
|
|
|
database.addColumnSimple( "tableName", datatype.TEXT, tableName );
|
|
|
|
database.addColumnSimple( "columnName", datatype.TEXT, tableName );
|
|
|
|
database.addColumnSimple( "datatype", datatype.TEXT, tableName );
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|