Search in sources :

Example 1 with CelestaException

use of ru.curs.celesta.CelestaException in project celesta by CourseOrchestra.

the class DdlAdaptor method createFk.

/**
 * Creates foreign key in the DB.
 *
 * @param conn DB connection
 * @param fk   foreign key from score
 */
public void createFk(Connection conn, ForeignKey fk) {
    try {
        List<String> sqlList = ddlGenerator.createFk(conn, fk);
        processSql(conn, sqlList);
    } catch (CelestaException e) {
        throw new CelestaException("Cannot create foreign key '%s': %s", fk.getConstraintName(), e.getMessage());
    }
}
Also used : CelestaException(ru.curs.celesta.CelestaException)

Example 2 with CelestaException

use of ru.curs.celesta.CelestaException in project celesta by CourseOrchestra.

the class DdlAdaptor method createTable.

/**
 * Creates a table "from scratch" in the database.
 *
 * @param conn Connection
 * @param te   Table for creation (accepts also table in case if such table exists)
 */
public void createTable(Connection conn, TableElement te) {
    String sql = ddlGenerator.createTable(te);
    try {
        processSql(conn, sql);
        conn.commit();
        List<String> sqlList;
        sqlList = ddlGenerator.updateVersioningTrigger(conn, te);
        processSql(conn, sqlList);
        sqlList = ddlGenerator.afterCreateTable(conn, te);
        processSql(conn, sqlList);
    } catch (SQLException | CelestaException e) {
        throw new CelestaException("Error on creating %s: %s", te.getName(), e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) CelestaException(ru.curs.celesta.CelestaException)

Example 3 with CelestaException

use of ru.curs.celesta.CelestaException in project celesta by CourseOrchestra.

the class DdlAdaptor method createIndex.

/**
 * Creates a table index in the grain.
 *
 * @param conn  DB connection
 * @param index index description
 */
public void createIndex(Connection conn, Index index) {
    List<String> sqlList = ddlGenerator.createIndex(index);
    try {
        processSql(conn, sqlList);
        conn.commit();
    } catch (Exception e) {
        throw new CelestaException(String.format("Cannot create index '%s': %s", index.getName(), e.getMessage()), e);
    }
}
Also used : CelestaException(ru.curs.celesta.CelestaException) SQLException(java.sql.SQLException) CelestaException(ru.curs.celesta.CelestaException)

Example 4 with CelestaException

use of ru.curs.celesta.CelestaException in project celesta by CourseOrchestra.

the class DdlGenerator method createView.

final String createView(View v) {
    try {
        SQLGenerator gen = getViewSQLGenerator();
        StringWriter sw = new StringWriter();
        PrintWriter bw = new PrintWriter(sw);
        v.createViewScript(bw, gen);
        bw.flush();
        String sql = sw.toString();
        return sql;
    } catch (IOException e) {
        throw new CelestaException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) SQLGenerator(ru.curs.celesta.score.SQLGenerator) IOException(java.io.IOException) CelestaException(ru.curs.celesta.CelestaException) PrintWriter(java.io.PrintWriter)

Example 5 with CelestaException

use of ru.curs.celesta.CelestaException in project celesta by CourseOrchestra.

the class FirebirdDdlGenerator method updateVersioningTrigger.

@Override
List<String> updateVersioningTrigger(Connection conn, TableElement t) {
    List<String> result = new ArrayList<>();
    String triggerName = getVersionCheckTriggerName(t);
    // First of all, we are about to check if trigger exists
    try {
        TriggerQuery query = new TriggerQuery().withSchema(t.getGrain().getName()).withName(triggerName).withTableName(t.getName());
        boolean triggerExists = this.triggerExists(conn, query);
        if (t instanceof VersionedElement) {
            VersionedElement ve = (VersionedElement) t;
            String sql;
            if (ve.isVersioned()) {
                if (!triggerExists) {
                    // CREATE TRIGGER
                    sql = "CREATE TRIGGER \"" + triggerName + "\" " + "for " + tableString(t.getGrain().getName(), t.getName()) + " BEFORE UPDATE \n" + " AS \n" + " BEGIN \n" + "   IF (OLD.\"recversion\" = NEW.\"recversion\")\n" + "     THEN NEW.\"recversion\" = NEW.\"recversion\" + 1;" + "   ELSE " + "     EXCEPTION VERSION_CHECK_ERROR;" + " END";
                    result.add(sql);
                    this.rememberTrigger(query);
                }
            } else {
                if (triggerExists) {
                    // DROP TRIGGER
                    result.add(dropTrigger(query));
                }
            }
        }
    } catch (CelestaException e) {
        throw new CelestaException("Could not update version check trigger on %s.%s: %s", t.getGrain().getName(), t.getName(), e.getMessage());
    }
    return result;
}
Also used : VersionedElement(ru.curs.celesta.score.VersionedElement) ArrayList(java.util.ArrayList) TriggerQuery(ru.curs.celesta.event.TriggerQuery) CelestaException(ru.curs.celesta.CelestaException)

Aggregations

CelestaException (ru.curs.celesta.CelestaException)114 SQLException (java.sql.SQLException)74 ResultSet (java.sql.ResultSet)61 PreparedStatement (java.sql.PreparedStatement)46 Statement (java.sql.Statement)33 ArrayList (java.util.ArrayList)21 BasicTable (ru.curs.celesta.score.BasicTable)20 Connection (java.sql.Connection)19 DbColumnInfo (ru.curs.celesta.dbutils.meta.DbColumnInfo)18 LinkedList (java.util.LinkedList)17 TriggerQuery (ru.curs.celesta.event.TriggerQuery)17 Grain (ru.curs.celesta.score.Grain)17 Matcher (java.util.regex.Matcher)16 Column (ru.curs.celesta.score.Column)16 IntegerColumn (ru.curs.celesta.score.IntegerColumn)16 DbIndexInfo (ru.curs.celesta.dbutils.meta.DbIndexInfo)15 DateTimeColumn (ru.curs.celesta.score.DateTimeColumn)15 StringColumn (ru.curs.celesta.score.StringColumn)15 IOException (java.io.IOException)14 DbFkInfo (ru.curs.celesta.dbutils.meta.DbFkInfo)14