Search in sources :

Example 6 with Column

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

the class DbFill method selectRandomRecord.

private void selectRandomRecord(ISqlTransaction tran, Table table) {
    Row row = null;
    if (hasDependentColumns(table)) {
        Map<Column, Object> dependent = getDependentColumnValues(table);
        if (dependent.size() > 0) {
            row = selectSpecificRow(tran, table, dependent.keySet().toArray(new Column[dependent.size()]), dependent.values().toArray(new Object[dependent.size()]));
            saveDependentColumnValues(table, row);
        } else {
            row = selectRandomRow(tran, table);
            saveDependentColumnValues(table, row);
        }
    } else {
        row = selectRandomRow(tran, table);
    }
    currentRowValues.put(table.getName(), row);
}
Also used : Column(org.jumpmind.db.model.Column) Row(org.jumpmind.db.sql.Row)

Example 7 with Column

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

the class OracleBulkDatabaseWriter method convertObjectValues.

/**
     * @param rowData
     * @param columns
     * @return
     */
protected Object[] convertObjectValues(Object[] values, Column[] columns) {
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            Object value = values[i];
            Column column = columns.length > i ? columns[i] : null;
            if (column != null) {
                values[i] = convertObjectValue(value, column);
            }
        }
    }
    return values;
}
Also used : Column(org.jumpmind.db.model.Column)

Example 8 with Column

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

the class MySqlBulkDatabaseWriter method getCommaDeliminatedColumns.

protected String getCommaDeliminatedColumns(Column[] cols) {
    DatabaseInfo dbInfo = platform.getDatabaseInfo();
    String quote = dbInfo.getDelimiterToken();
    StringBuilder columns = new StringBuilder();
    columns.append("(");
    // The target syntax is (where BLOB is involved):
    // ('column1', 'column2', @hexColumn3) SET 'column3'=UNHEX(@hexColumn3);
    Map<String, String> blobColumns = new LinkedHashMap<String, String>();
    if (cols != null && cols.length > 0) {
        for (Column column : cols) {
            boolean blob = column.isOfBinaryType();
            if (blob) {
                String hexVariable = String.format("@%s_hex", column.getName());
                blobColumns.put(column.getName(), hexVariable);
                columns.append(hexVariable);
            } else {
                columns.append(quote);
                columns.append(column.getName());
                columns.append(quote);
            }
            columns.append(",");
        }
        columns.replace(columns.length() - 1, columns.length(), "");
        columns.append(")");
        // Build this (optional) clause:
        // SET 'column3'=UNHEX(@hexColumn3);
        StringBuilder setClause = new StringBuilder();
        for (String columnName : blobColumns.keySet()) {
            if (setClause.length() == 0) {
                setClause.append(" SET ");
            }
            setClause.append(quote);
            setClause.append(columnName);
            setClause.append(quote);
            setClause.append("=UNHEX(");
            setClause.append(blobColumns.get(columnName));
            setClause.append("),");
        }
        if (setClause.length() > 0) {
            setClause.setLength(setClause.length() - 1);
            columns.append(setClause);
        }
        return columns.toString();
    } else {
        return " ";
    }
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) Column(org.jumpmind.db.model.Column) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with Column

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

the class SqlPersistenceManager method find.

public <T> List<T> find(Class<T> clazz, Map<String, Object> conditions, String catalogName, String schemaName, String tableName) {
    if (conditions == null || conditions.size() == 0) {
        return find(clazz, catalogName, schemaName, tableName);
    } else {
        try {
            Table table = findTable(catalogName, schemaName, tableName);
            T object = clazz.newInstance();
            LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
            LinkedHashMap<String, Object> objectValuesByColumnName = new LinkedHashMap<String, Object>();
            Column[] keys = new Column[conditions.size()];
            Set<String> keyPropertyNames = conditions.keySet();
            boolean[] nullKeyValues = new boolean[conditions.size()];
            int index = 0;
            for (String propertyName : keyPropertyNames) {
                Column column = objectToTableMapping.get(propertyName);
                if (column != null) {
                    keys[index] = column;
                    nullKeyValues[index] = conditions.get(propertyName) == null;
                    objectValuesByColumnName.put(column.getName(), conditions.get(propertyName));
                    index++;
                } else {
                    throw new IllegalStateException("Could not find a database column that maps to the " + propertyName + " property on " + clazz.getName() + ".  Make sure the property is defined on the class and " + "the matching column is defined in the database table");
                }
            }
            Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
            DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), keys, columns, nullKeyValues, null);
            String sql = statement.getSql();
            Object[] values = statement.getValueArray(objectValuesByColumnName);
            int[] types = statement.getTypes();
            List<Row> rows = databasePlatform.getSqlTemplate().query(sql, values, types);
            List<T> objects = new ArrayList<T>();
            for (Row row : rows) {
                object = clazz.newInstance();
                Set<String> propertyNames = objectToTableMapping.keySet();
                for (String propertyName : propertyNames) {
                    Object value = row.get(objectToTableMapping.get(propertyName).getName());
                    BeanUtils.copyProperty(object, propertyName, value);
                }
                objects.add(object);
            }
            return objects;
        } catch (Exception e) {
            throw toRuntimeException(e);
        }
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) Column(org.jumpmind.db.model.Column)

Example 10 with Column

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

the class SqlPersistenceManager method count.

@Override
public <T> int count(Class<T> clazz, String catalogName, String schemaName, String tableName, Map<String, Object> conditions) {
    if (conditions == null || conditions.size() == 0) {
        return count(catalogName, schemaName, tableName);
    } else {
        try {
            Table table = findTable(catalogName, schemaName, tableName);
            T object = clazz.newInstance();
            LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
            LinkedHashMap<String, Object> objectValuesByColumnName = new LinkedHashMap<String, Object>();
            Column[] keys = new Column[conditions.size()];
            Set<String> keyPropertyNames = conditions.keySet();
            boolean[] nullKeyValues = new boolean[conditions.size()];
            int index = 0;
            for (String propertyName : keyPropertyNames) {
                Column column = objectToTableMapping.get(propertyName);
                if (column != null) {
                    keys[index] = column;
                    nullKeyValues[index] = conditions.get(propertyName) == null;
                    objectValuesByColumnName.put(column.getName(), conditions.get(propertyName));
                    index++;
                } else {
                    throw new IllegalStateException("Could not find a database column that maps to the " + propertyName + " property on " + clazz.getName() + ".  Make sure the property is defined on the class and " + "the matching column is defined in the database table");
                }
            }
            DmlStatement statement = databasePlatform.createDmlStatement(DmlType.COUNT, table.getCatalog(), table.getSchema(), table.getName(), keys, keys, nullKeyValues, null);
            String sql = statement.getSql();
            Object[] values = statement.getValueArray(objectValuesByColumnName);
            return databasePlatform.getSqlTemplate().queryForInt(sql, values);
        } catch (Exception e) {
            throw toRuntimeException(e);
        }
    }
}
Also used : Table(org.jumpmind.db.model.Table) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) Column(org.jumpmind.db.model.Column)

Aggregations

Column (org.jumpmind.db.model.Column)179 Table (org.jumpmind.db.model.Table)78 ArrayList (java.util.ArrayList)34 IndexColumn (org.jumpmind.db.model.IndexColumn)23 PlatformColumn (org.jumpmind.db.model.PlatformColumn)21 Test (org.junit.Test)16 Row (org.jumpmind.db.sql.Row)15 LinkedHashMap (java.util.LinkedHashMap)12 ResultSet (java.sql.ResultSet)11 DmlStatement (org.jumpmind.db.sql.DmlStatement)10 SqlException (org.jumpmind.db.sql.SqlException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 IIndex (org.jumpmind.db.model.IIndex)9 HashMap (java.util.HashMap)8 ForeignKey (org.jumpmind.db.model.ForeignKey)8 CsvData (org.jumpmind.symmetric.io.data.CsvData)8 PreparedStatement (java.sql.PreparedStatement)7 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 Reference (org.jumpmind.db.model.Reference)6