org.jaxdb.ddlx.MySQLCompiler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ddlx Show documentation
Show all versions of ddlx Show documentation
DDLx is a XML definition of the DDL specification. It is a vendor-agnostic, SQL compliant XSD
used to create SQL Schemas in XML. The DDLx Schema utilizes the power of XML Schema Validation
to provides a cohesive structured model for the creation of SQL Schemas.
/* Copyright (c) 2015 JAX-DB
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* You should have received a copy of The MIT License (MIT) along with this
* program. If not, see .
*/
package org.jaxdb.ddlx;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import org.jaxdb.vendor.DBVendor;
import org.jaxdb.www.ddlx_0_4.xLygluGCXAA.$Column;
import org.jaxdb.www.ddlx_0_4.xLygluGCXAA.$Index;
import org.jaxdb.www.ddlx_0_4.xLygluGCXAA.$Integer;
import org.jaxdb.www.ddlx_0_4.xLygluGCXAA.$Named;
import org.jaxdb.www.ddlx_0_4.xLygluGCXAA.$Table;
class MySQLCompiler extends Compiler {
@Override
public DBVendor getVendor() {
return DBVendor.MY_SQL;
}
@Override
void init(final Connection connection) {
}
@Override
List triggers(final $Table table) {
if (table.getTriggers() == null)
return super.triggers(table);
final String tableName = table.getName$().text();
final List<$Table.Triggers.Trigger> triggers = table.getTriggers().getTrigger();
final List statements = new ArrayList<>();
final StringBuilder builder = new StringBuilder();
for (final $Table.Triggers.Trigger trigger : triggers) {
for (final String action : trigger.getActions$().text()) {
builder.append("DELIMITER |\n");
builder.append("CREATE TRIGGER ").append(q(SQLDataTypes.getTriggerName(tableName, trigger, action))).append(" ").append(trigger.getTime$().text()).append(" ").append(action).append(" ON ").append(q(tableName)).append('\n');
builder.append(" FOR EACH ROW\n");
builder.append(" BEGIN\n");
final String text = trigger.text().toString();
// FIXME: This does not work because the whitespace is trimmed before we can check it
int j = -1;
for (int i = 0; i < text.length();) {
final char ch = text.charAt(i++);
if (ch == '\n' || ch == '\r')
continue;
++j;
if (ch != ' ' && ch != '\t')
break;
}
builder.append(" ").append(text.trim().replace("\n" + text.substring(0, j), "\n ")).append('\n');
builder.append(" END;\n");
builder.append("|\n");
builder.append("DELIMITER");
}
statements.add(new CreateStatement(builder.toString()));
builder.setLength(0);
}
statements.addAll(super.triggers(table));
return statements;
}
@Override
String $null(final $Table table, final $Column column) {
return column.getNull$() != null ? !column.getNull$().text() ? "NOT NULL" : "NULL" : "";
}
@Override
String $autoIncrement(final $Table table, final $Integer column) {
return isAutoIncrement(column) ? $Integer.GenerateOnInsert$.AUTO_5FINCREMENT.text() : "";
}
@Override
String dropIndexOnClause(final $Table table) {
return " ON " + q(table.getName$().text());
}
@Override
CreateStatement createIndex(final boolean unique, final String indexName, final $Index.Type$ type, final String tableName, final $Named ... columns) {
return new CreateStatement("CREATE " + (unique ? "UNIQUE " : "") + "INDEX " + q(indexName) + " USING " + type.text() + " ON " + q(tableName) + " (" + SQLDataTypes.csvNames(getVendor().getDialect(), columns) + ")");
}
}