Search in sources :

Example 26 with Database

use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.

the class AbstractSymmetricDialect method dropTablesAndDatabaseObjects.

public void dropTablesAndDatabaseObjects() {
    Database modelFromDatabase = readSymmetricSchemaFromDatabase();
    platform.dropDatabase(modelFromDatabase, true);
    dropRequiredDatabaseObjects();
}
Also used : Database(org.jumpmind.db.model.Database)

Example 27 with Database

use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.

the class DatabaseXmlUtil method read.

/*
     * Reads the database model given by the reader.
     * 
     * @param reader The reader that returns the model XML
     * 
     * @return The database model
     */
public static Database read(Reader reader, boolean validate) {
    try {
        boolean done = false;
        Database database = null;
        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        parser.setInput(reader);
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT && !done) {
            switch(eventType) {
                case XmlPullParser.START_DOCUMENT:
                    database = new Database();
                    break;
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    if (name.equalsIgnoreCase("database")) {
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                database.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("catalog")) {
                                database.setCatalog(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("schema")) {
                                database.setSchema(attributeValue);
                            }
                        }
                    } else if (name.equalsIgnoreCase("table")) {
                        Table table = nextTable(parser, database.getCatalog(), database.getSchema());
                        if (table != null) {
                            database.addTable(table);
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("database")) {
                        done = true;
                    }
                    break;
            }
            eventType = parser.next();
        }
        if (validate) {
            database.initialize();
        }
        return database;
    } catch (XmlPullParserException e) {
        throw new IoException(e);
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) XmlPullParser(org.xmlpull.v1.XmlPullParser) IoException(org.jumpmind.exception.IoException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 28 with Database

use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.

the class AbstractDatabasePlatform method dropTables.

public void dropTables(boolean continueOnError, Table... tables) {
    Database db = new Database();
    for (Table table : tables) {
        db.addTable(table);
    }
    dropDatabase(db, continueOnError);
}
Also used : Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database)

Example 29 with Database

use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.

the class AbstractDatabasePlatform method checkSymTablePermissions.

public List<PermissionResult> checkSymTablePermissions(PermissionType... permissionTypes) {
    List<PermissionResult> results = new ArrayList<PermissionResult>();
    Database database = new Database();
    PermissionResult createResult = getCreateSymTablePermission(database);
    PermissionResult createTriggerResult = getCreateSymTriggerPermission();
    PermissionResult dropTriggerResult = getDropSymTriggerPermission();
    boolean drop = false;
    for (PermissionType permissionType : permissionTypes) {
        switch(permissionType) {
            case CREATE_TABLE:
                results.add(createResult);
                break;
            case ALTER_TABLE:
                PermissionResult alterResult = getAlterSymTablePermission(database);
                results.add(alterResult);
                break;
            case CREATE_TRIGGER:
                results.add(createTriggerResult);
                break;
            case DROP_TRIGGER:
                results.add(dropTriggerResult);
                break;
            case EXECUTE:
                PermissionResult executeResult = getExecuteSymPermission();
                results.add(executeResult);
                break;
            case DROP_TABLE:
                drop = true;
                break;
            case CREATE_FUNCTION:
                PermissionResult createFunctionResult = getCreateSymFunctionPermission();
                results.add(createFunctionResult);
                break;
            case CREATE_ROUTINE:
                PermissionResult createRoutineResult = getCreateSymRoutinePermission();
                results.add(createRoutineResult);
                break;
        }
    }
    PermissionResult dropPermission = getDropSymTablePermission();
    if (drop) {
        results.add(dropPermission);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) Database(org.jumpmind.db.model.Database)

Example 30 with Database

use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.

the class AbstractDatabasePlatform method alterTables.

public void alterTables(boolean continueOnError, Table... desiredTables) {
    Database currentDatabase = new Database();
    Database desiredDatabase = new Database();
    StringBuilder tablesProcessed = new StringBuilder();
    for (Table table : desiredTables) {
        tablesProcessed.append(table.getFullyQualifiedTableName());
        tablesProcessed.append(", ");
        desiredDatabase.addTable(table);
        Table currentTable = ddlReader.readTable(table.getCatalog(), table.getSchema(), table.getName());
        if (currentTable != null) {
            currentDatabase.addTable(currentTable);
        }
    }
    if (tablesProcessed.length() > 1) {
        tablesProcessed.replace(tablesProcessed.length() - 2, tablesProcessed.length(), "");
    }
    String alterSql = ddlBuilder.alterDatabase(currentDatabase, desiredDatabase);
    if (StringUtils.isNotBlank(alterSql.trim())) {
        log.info("Running alter sql:\n{}", alterSql);
        String delimiter = getDdlBuilder().getDatabaseInfo().getSqlCommandDelimiter();
        new SqlScript(alterSql, getSqlTemplate(), !continueOnError, false, false, delimiter, null).execute(getDatabaseInfo().isRequiresAutoCommitForDdl());
    } else {
        log.info("Tables up to date.  No alters found for {}", tablesProcessed);
    }
}
Also used : Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) SqlScript(org.jumpmind.db.sql.SqlScript)

Aggregations

Database (org.jumpmind.db.model.Database)37 Table (org.jumpmind.db.model.Table)21 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)9 Test (org.junit.Test)7 IOException (java.io.IOException)6 IoException (org.jumpmind.exception.IoException)6 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)5 IDdlBuilder (org.jumpmind.db.platform.IDdlBuilder)4 SqlException (org.jumpmind.db.sql.SqlException)4 SqlScript (org.jumpmind.db.sql.SqlScript)4 DbExport (org.jumpmind.symmetric.io.data.DbExport)4 DbImport (org.jumpmind.symmetric.io.data.DbImport)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 InputStreamReader (java.io.InputStreamReader)2 LinkedHashMap (java.util.LinkedHashMap)2 AddTableChange (org.jumpmind.db.alter.AddTableChange)2 RemoveTableChange (org.jumpmind.db.alter.RemoveTableChange)2 TableChange (org.jumpmind.db.alter.TableChange)2 Column (org.jumpmind.db.model.Column)2