Search in sources :

Example 1 with SqlDialect

use of org.umlg.sqlg.sql.dialect.SqlDialect in project sqlg by pietermartin.

the class VertexLabel method delete.

/**
 * delete the table
 */
void delete() {
    String schema = getSchema().getName();
    String tableName = VERTEX_PREFIX + getLabel();
    SqlDialect sqlDialect = this.sqlgGraph.getSqlDialect();
    sqlDialect.assertTableName(tableName);
    StringBuilder sql = new StringBuilder("DROP TABLE IF EXISTS ");
    sql.append(sqlDialect.maybeWrapInQoutes(schema));
    sql.append(".");
    sql.append(sqlDialect.maybeWrapInQoutes(tableName));
    if (sqlDialect.supportsCascade()) {
        sql.append(" CASCADE");
    }
    if (logger.isDebugEnabled()) {
        logger.debug(sql.toString());
    }
    if (sqlDialect.needsSemicolon()) {
        sql.append(";");
    }
    Connection conn = sqlgGraph.tx().getConnection();
    try (Statement stmt = conn.createStatement()) {
        stmt.execute(sql.toString());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SqlDialect(org.umlg.sqlg.sql.dialect.SqlDialect)

Example 2 with SqlDialect

use of org.umlg.sqlg.sql.dialect.SqlDialect in project sqlg by pietermartin.

the class SqlgUtil method dropDb.

public static void dropDb(SqlgGraph sqlgGraph) {
    SqlDialect sqlDialect = sqlgGraph.getSqlDialect();
    Connection conn = sqlgGraph.tx().getConnection();
    dropDb(sqlDialect, conn);
}
Also used : SqlDialect(org.umlg.sqlg.sql.dialect.SqlDialect)

Example 3 with SqlDialect

use of org.umlg.sqlg.sql.dialect.SqlDialect in project sqlg by pietermartin.

the class EdgeLabel method createEdgeTable.

private void createEdgeTable(VertexLabel outVertexLabel, VertexLabel inVertexLabel, Map<String, PropertyType> columns, Properties additional) {
    String schema = outVertexLabel.getSchema().getName();
    String tableName = EDGE_PREFIX + getLabel();
    SqlDialect sqlDialect = this.sqlgGraph.getSqlDialect();
    sqlDialect.assertTableName(tableName);
    StringBuilder sql = new StringBuilder(sqlDialect.createTableStatement());
    sql.append(sqlDialect.maybeWrapInQoutes(schema));
    sql.append(".");
    sql.append(sqlDialect.maybeWrapInQoutes(tableName));
    sql.append("(");
    sql.append(sqlDialect.maybeWrapInQoutes("ID"));
    sql.append(" ");
    sql.append(sqlDialect.getAutoIncrementPrimaryKeyConstruct());
    if (columns.size() > 0) {
        sql.append(", ");
    }
    buildColumns(this.sqlgGraph, columns, sql, additional);
    sql.append(", ");
    sql.append(sqlDialect.maybeWrapInQoutes(inVertexLabel.getFullName() + Topology.IN_VERTEX_COLUMN_END));
    sql.append(" ");
    sql.append(sqlDialect.getForeignKeyTypeDefinition());
    sql.append(", ");
    sql.append(sqlDialect.maybeWrapInQoutes(outVertexLabel.getFullName() + Topology.OUT_VERTEX_COLUMN_END));
    sql.append(" ");
    sql.append(sqlDialect.getForeignKeyTypeDefinition());
    // foreign key definition start
    if (this.sqlgGraph.getTopology().isImplementingForeignKeys()) {
        sql.append(", ");
        sql.append("FOREIGN KEY (");
        sql.append(sqlDialect.maybeWrapInQoutes(inVertexLabel.getSchema().getName() + "." + inVertexLabel.getLabel() + Topology.IN_VERTEX_COLUMN_END));
        sql.append(") REFERENCES ");
        sql.append(sqlDialect.maybeWrapInQoutes(inVertexLabel.getSchema().getName()));
        sql.append(".");
        sql.append(sqlDialect.maybeWrapInQoutes(VERTEX_PREFIX + inVertexLabel.getLabel()));
        sql.append(" (");
        sql.append(sqlDialect.maybeWrapInQoutes("ID"));
        sql.append(") ");
        if (sqlDialect.supportsDeferrableForeignKey()) {
            sql.append("DEFERRABLE");
        }
        sql.append(", FOREIGN KEY (");
        sql.append(sqlDialect.maybeWrapInQoutes(outVertexLabel.getSchema().getName() + "." + outVertexLabel.getLabel() + Topology.OUT_VERTEX_COLUMN_END));
        sql.append(") REFERENCES ");
        sql.append(sqlDialect.maybeWrapInQoutes(outVertexLabel.getSchema().getName()));
        sql.append(".");
        sql.append(sqlDialect.maybeWrapInQoutes(VERTEX_PREFIX + outVertexLabel.getLabel()));
        sql.append(" (");
        sql.append(sqlDialect.maybeWrapInQoutes("ID"));
        sql.append(") ");
        if (sqlDialect.supportsDeferrableForeignKey()) {
            sql.append("DEFERRABLE");
        }
        if (sqlDialect.needForeignKeyIndex() && sqlDialect.isIndexPartOfCreateTable()) {
            // This is true for Cockroachdb
            sql.append(", INDEX (");
            sql.append(sqlDialect.maybeWrapInQoutes(inVertexLabel.getSchema().getName() + "." + inVertexLabel.getLabel() + Topology.IN_VERTEX_COLUMN_END));
            sql.append("), INDEX (");
            sql.append(sqlDialect.maybeWrapInQoutes(outVertexLabel.getSchema().getName() + "." + outVertexLabel.getLabel() + Topology.OUT_VERTEX_COLUMN_END));
            sql.append(")");
        }
    }
    // foreign key definition end
    sql.append(")");
    if (sqlDialect.needsSemicolon()) {
        sql.append(";");
    }
    if (sqlDialect.needForeignKeyIndex() && !sqlDialect.isIndexPartOfCreateTable()) {
        sql.append("\nCREATE INDEX");
        if (sqlDialect.requiresIndexName()) {
            sql.append(" ");
            sql.append(sqlDialect.maybeWrapInQoutes(sqlDialect.indexName(SchemaTable.of(schema, tableName).withOutPrefix(), EDGE_PREFIX, "_idx", Collections.singletonList(inVertexLabel.getSchema().getName() + "_" + inVertexLabel.getLabel() + Topology.IN_VERTEX_COLUMN_END))));
        }
        sql.append(" ON ");
        sql.append(sqlDialect.maybeWrapInQoutes(schema));
        sql.append(".");
        sql.append(sqlDialect.maybeWrapInQoutes(tableName));
        sql.append(" (");
        sql.append(sqlDialect.maybeWrapInQoutes(inVertexLabel.getSchema().getName() + "." + inVertexLabel.getLabel() + Topology.IN_VERTEX_COLUMN_END));
        sql.append(");");
        sql.append("\nCREATE INDEX");
        if (sqlDialect.requiresIndexName()) {
            sql.append(" ");
            sql.append(sqlDialect.maybeWrapInQoutes(sqlDialect.indexName(SchemaTable.of(schema, tableName).withOutPrefix(), EDGE_PREFIX, "_idx", Collections.singletonList(outVertexLabel.getSchema().getName() + "_" + outVertexLabel.getLabel() + Topology.OUT_VERTEX_COLUMN_END))));
        }
        sql.append(" ON ");
        sql.append(sqlDialect.maybeWrapInQoutes(schema));
        sql.append(".");
        sql.append(sqlDialect.maybeWrapInQoutes(tableName));
        sql.append(" (");
        sql.append(sqlDialect.maybeWrapInQoutes(outVertexLabel.getSchema().getName() + "." + outVertexLabel.getLabel() + Topology.OUT_VERTEX_COLUMN_END));
        sql.append(");");
    }
    if (logger.isDebugEnabled()) {
        logger.debug(sql.toString());
    }
    Connection conn = this.sqlgGraph.tx().getConnection();
    try (Statement stmt = conn.createStatement()) {
        stmt.execute(sql.toString());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SqlDialect(org.umlg.sqlg.sql.dialect.SqlDialect)

Example 4 with SqlDialect

use of org.umlg.sqlg.sql.dialect.SqlDialect in project sqlg by pietermartin.

the class Index method addIndex.

private void addIndex(SqlgGraph sqlgGraph, SchemaTable schemaTable) {
    String prefix = this.abstractLabel instanceof VertexLabel ? VERTEX_PREFIX : EDGE_PREFIX;
    StringBuilder sql = new StringBuilder("CREATE ");
    if (IndexType.UNIQUE.equals(getIndexType())) {
        sql.append("UNIQUE ");
    }
    sql.append("INDEX ");
    SqlDialect sqlDialect = sqlgGraph.getSqlDialect();
    sql.append(sqlDialect.maybeWrapInQoutes(getName()));
    sql.append(" ON ");
    sql.append(sqlDialect.maybeWrapInQoutes(schemaTable.getSchema()));
    sql.append(".");
    sql.append(sqlDialect.maybeWrapInQoutes(prefix + schemaTable.getTable()));
    if (this.indexType.isGIN()) {
        sql.append(" USING GIN");
    }
    sql.append(" (");
    List<PropertyColumn> props = getProperties();
    if (IndexType.GIN_FULLTEXT.equals(getIndexType().getName())) {
        sql.append("to_tsvector(");
        String conf = indexType.getProperties().get(IndexType.GIN_CONFIGURATION);
        if (conf != null) {
            // need single quotes, no double
            sql.append("'" + conf + "'");
            sql.append(",");
        }
        int count = 1;
        for (PropertyColumn property : props) {
            sql.append(sqlDialect.maybeWrapInQoutes(property.getName()));
            if (count++ < props.size()) {
                sql.append(" || ' ' || ");
            }
        }
        sql.append(")");
    } else {
        int count = 1;
        for (PropertyColumn property : props) {
            sql.append(sqlDialect.maybeWrapInQoutes(property.getName()));
            // This is for mariadb. It needs to know how many characters to index.
            if (property.getPropertyType().isString() && sqlgGraph.getSqlDialect().requiresIndexLengthLimit()) {
                // This number is for MariaDb TEXT data type.
                // 192 crashes with "Caused by: java.sql.SQLException: Specified key was too long; max key length is 767 bytes"
                // Some or other Innodb byte count magic I can't be bothered to understand.
                sql.append("(191)");
            }
            if (count++ < props.size()) {
                sql.append(", ");
            }
        }
    }
    sql.append(")");
    if (sqlDialect.needsSemicolon()) {
        sql.append(";");
    }
    if (logger.isDebugEnabled()) {
        logger.debug(sql.toString());
    }
    Connection conn = sqlgGraph.tx().getConnection();
    try (Statement stmt = conn.createStatement()) {
        stmt.execute(sql.toString());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SqlDialect(org.umlg.sqlg.sql.dialect.SqlDialect)

Example 5 with SqlDialect

use of org.umlg.sqlg.sql.dialect.SqlDialect in project sqlg by pietermartin.

the class Index method delete.

/**
 * delete the index from the database
 *
 * @param sqlgGraph
 */
void delete(SqlgGraph sqlgGraph) {
    StringBuilder sql = new StringBuilder("DROP INDEX IF EXISTS ");
    SqlDialect sqlDialect = sqlgGraph.getSqlDialect();
    sql.append(sqlDialect.maybeWrapInQoutes(getParentLabel().getSchema().getName()));
    sql.append(".");
    sql.append(sqlDialect.maybeWrapInQoutes(getName()));
    if (sqlDialect.needsSemicolon()) {
        sql.append(";");
    }
    if (logger.isDebugEnabled()) {
        logger.debug(sql.toString());
    }
    Connection conn = sqlgGraph.tx().getConnection();
    try (Statement stmt = conn.createStatement()) {
        stmt.execute(sql.toString());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SqlDialect(org.umlg.sqlg.sql.dialect.SqlDialect)

Aggregations

SqlDialect (org.umlg.sqlg.sql.dialect.SqlDialect)7 PropertyVetoException (java.beans.PropertyVetoException)1 Connection (java.sql.Connection)1 SqlgPlugin (org.umlg.sqlg.SqlgPlugin)1 SqlgDataSource (org.umlg.sqlg.structure.SqlgDataSourceFactory.SqlgDataSource)1