Files
qbit-data-analysis/src/main/java/com/kingsrook/qbits/dataanalysis/DataAnalysisSchema.java
T
james.maesandClaude Opus 5.5 1076daacde chore: bring the repo to the QRun qbit standard, add producer, tables and home
Repository standard (LICENSE, NOTICE, community pointers, CHANGELOG,
CLAUDE/AGENTS, dependabot, CircleCI with the configure-package-install
pre-step, the release-rc caller workflow, Maven wrapper, checkstyle,
JaCoCo gates, QQQ 4.1.0-RC.5). DataAnalysisQBitProducer and config, the
six data_analysis_ tables with standard permission rules and user record
locks, the analyze permission stand-in, the Analyses app home with real
counts, and the Postgres schema script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 15:32:48 -05:00

93 lines
3.7 KiB
Java

/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2025. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.dataanalysis;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qbits.dataanalysis.model.DataAnalysisTables;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
/*******************************************************************************
** PostgreSQL DDL generated from the same tables and unique keys used by QQQ.
*******************************************************************************/
public final class DataAnalysisSchema
{
/*******************************************************************************
** Utility class.
*******************************************************************************/
private DataAnalysisSchema()
{
}
/*******************************************************************************
** Idempotent table and unique index creation; hosts own migration execution.
*******************************************************************************/
public static String postgresScript(DataAnalysisQBitConfig config)
{
List<String> statements = new ArrayList<>();
for(QTableMetaData table : DataAnalysisTables.tables(config))
{
List<String> columns = new ArrayList<>();
for(QFieldMetaData field : table.getFields().values())
{
columns.add(" " + field.getBackendName() + " " + ("id".equals(field.getName())
? "INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY" : sqlType(field) + (Boolean.TRUE.equals(field.getIsRequired()) ? " NOT NULL" : "")));
}
statements.add("CREATE TABLE IF NOT EXISTS " + table.getName() + " (\n" + String.join(",\n", columns) + "\n)");
if(table.getUniqueKeys() != null)
{
int index = 0;
for(UniqueKey key : table.getUniqueKeys())
{
index++;
statements.add("CREATE UNIQUE INDEX IF NOT EXISTS uq_" + table.getName() + "_" + index + " ON " + table.getName()
+ " (" + String.join(", ", key.getFieldNames().stream().map(name -> table.getField(name).getBackendName()).toList()) + ")");
}
}
}
return (String.join(";\n", statements) + ";\n");
}
/*******************************************************************************
** PostgreSQL types for the table metadata.
*******************************************************************************/
private static String sqlType(QFieldMetaData field)
{
return (switch(field.getType())
{
case INTEGER -> "INTEGER";
case LONG -> "BIGINT";
case BOOLEAN -> "BOOLEAN";
case DATE_TIME -> "TIMESTAMP WITHOUT TIME ZONE";
case TEXT -> "TEXT";
default -> "VARCHAR(" + field.getMaxLength() + ")";
});
}
}