@Deprecated public Column[] orderColumns(String[] columnNames, Table table) { Column[] unorderedColumns = table.getColumns(); Column[] orderedColumns = new Column[columnNames.length]; for (int i = 0; i < columnNames.length; i++) { String name = columnNames[i]; for (Column column : unorderedColumns) { if (column.getName().equalsIgnoreCase(name)) { orderedColumns[i] = column; break; } } } return orderedColumns; }
protected List<Column> getBulkLoadableColumns(Table table) { ArrayList<Column> columns = new ArrayList<Column>(Arrays.asList(table.getColumns())); Iterator<Column> iterator = columns.iterator(); // TODO support BLOB and CLOBs in bulk load. For now, remove them while (iterator.hasNext()) { Column column = (Column) iterator.next(); if (column.getMappedTypeCode() == Types.CLOB || column.getMappedTypeCode() == Types.BLOB || column.getMappedTypeCode() == Types.VARBINARY) { iterator.remove(); } } return columns; }
private String createCopyMgrSql() { StringBuilder sql = new StringBuilder("COPY "); String quote = platform.getDatabaseInfo().getDelimiterToken(); sql.append(targetTable.getFullyQualifiedTableName(quote)); sql.append("("); Column[] columns = targetTable.getColumns(); for (Column column : columns) { String columnName = column.getName(); if (StringUtils.isNotBlank(columnName)) { sql.append(quote); sql.append(columnName); sql.append(quote); sql.append(","); } } sql.replace(sql.length() - 1, sql.length(), ")"); sql.append("FROM STDIN with delimiter ',' csv quote ''''"); return sql.toString(); }
protected void determineAutoIncrementColumns(Connection connection, Table table) throws SQLException { Column[] columns = table.getColumns(); for (int idx = 0; idx < columns.length; idx++) { columns[idx].setAutoIncrement(isAutoIncrement(connection, table, columns[idx])); } }
@Override protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException { Table table = super.readTable(connection, metaData, values); if (table != null) { // Sybase does not return the auto-increment status via the database // metadata determineAutoIncrementFromResultSetMetaData(connection, table, table.getColumns()); } return table; }
@Override protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException { Table table = super.readTable(connection, metaData, values); if (table != null) { // Sybase does not return the auto-increment status via the database // metadata determineAutoIncrementFromResultSetMetaData(connection, table, table.getColumns()); } return table; }
/** * When dealing with columns, always use this method to order the columns so * that the primary keys are first. */ public Column[] orderColumnsForTable(Table table) { if (table != null) { Column[] pks = getSyncKeysColumnsForTable(table); Column[] cols = table.getColumns(); List<Column> orderedColumns = new ArrayList<Column>(cols.length); for (int i = 0; i < pks.length; i++) { orderedColumns.add(pks[i]); } for (int i = 0; i < cols.length; i++) { boolean syncKey = false; for (int j = 0; j < pks.length; j++) { if (cols[i].getName().equals(pks[j].getName())) { syncKey = true; break; } } if (!syncKey) { orderedColumns.add(cols[i]); } } Column[] result = orderedColumns.toArray(new Column[orderedColumns.size()]); return filterExcludedColumns(result); } else { return new Column[0]; } }
Column[] columns = table.getColumns(); HashMap<String, Column> names = new HashMap<String, Column>(); String name;
Column[] columns = table.getColumns(); HashMap<String, Column> names = new HashMap<String, Column>(); String name;
public CsvData mapRow(Row row) { return new CsvData(DataEventType.INSERT, toStringData(row, table.getPrimaryKeyColumns()), toStringData(row, table.getColumns())); } });
/** * Select a random row from the table in the connected database. Return null if there are no rows. * * TODO: Cache rows. * * @param sqlTemplate * @param table The table to select a row from. * @return A random row from the table. Null if there are no rows. */ private Row selectRandomRow(Table table) { Row row = null; // Select all rows and return the primary key columns. String sql = platform.createDmlStatement(DmlType.SELECT_ALL, table.getCatalog(), table.getSchema(), table.getName(), table.getPrimaryKeyColumns(), table.getColumns(), null).getSql(); final List<Row> rows = new ArrayList<Row>(); platform.getSqlTemplate().query(sql, RANDOM_SELECT_SIZE, new ISqlRowMapper<Object>() { public Object mapRow(Row row) { rows.add(row); return Boolean.TRUE; } }, null, null); if (rows.size() != 0) { int rowNum = getRand().nextInt(rows.size()); row = rows.get(rowNum); } return row; }
public boolean start(Table table) { if (!batch.isIgnored()) { this.table = table; if (!backwardsCompatible) { String catalogName = table.getCatalog(); println(CsvConstants.CATALOG, StringUtils.isNotBlank(catalogName) ? catalogName : ""); String schemaName = table.getSchema(); println(CsvConstants.SCHEMA, StringUtils.isNotBlank(schemaName) ? schemaName : ""); } String tableKey = table.getTableKey(); String fullyQualifiedTableName = table.getFullyQualifiedTableName(); String previousTableKey = processedTables.get(fullyQualifiedTableName); println(CsvConstants.TABLE, table.getName()); if (!tableKey.equals(previousTableKey)) { println(CsvConstants.KEYS, table.getPrimaryKeyColumns()); println(CsvConstants.COLUMNS, table.getColumns()); this.processedTables.put(fullyQualifiedTableName, tableKey); } return true; } else { return false; } }
public void write(CsvData data) { String sql = null; switch (data.getDataEventType()) { case UPDATE: sql = buildSql(useUpsertStatements ? DmlType.UPSERT : DmlType.UPDATE, data.getParsedData(CsvData.ROW_DATA), currentTable.getColumns()); break; case INSERT: sql = buildSql(useUpsertStatements ? DmlType.UPSERT : DmlType.INSERT, data.getParsedData(CsvData.ROW_DATA), currentTable.getColumns()); break; case DELETE: sql = buildSql(DmlType.DELETE, data.getParsedData(CsvData.PK_DATA), currentTable.getPrimaryKeyColumns()); break; case SQL: sql = data.getParsedData(CsvData.ROW_DATA)[0]; break; case CREATE: IDdlBuilder builder = DdlBuilderFactory.createDdlBuilder(targetDatabaseName); sql = builder.createTable(currentTable); break; default: break; } if (sql != null) { this.payloadMap.get(this.currentBatch).add(sql); } }
protected String getPkDataFor(CsvData data, Column column) { String[] values = data.getParsedData(CsvData.PK_DATA); if (values != null) { Column[] columns = targetTable.getColumns(); int index = -1; for (Column column2 : columns) { if (column2.isPrimaryKey()) { index++; } if (column2.equals(column)) { return values[index]; } } } else { return data.getParsedData(CsvData.ROW_DATA)[targetTable.getColumnIndex(column)]; } return null; }
@Override protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException { String tableName = (String) values.get("TABLE_NAME"); for (int idx = 0; idx < KNOWN_SYSTEM_TABLES.length; idx++) { if (KNOWN_SYSTEM_TABLES[idx].equals(tableName)) { return null; } } Table table = super.readTable(connection, metaData, values); if (table != null) { // Sql Server does not return the auto-increment status via the // database metadata determineAutoIncrementFromResultSetMetaData(connection, table, table.getColumns()); // TODO: Replace this manual filtering using named pks once they are // available // This is then probably of interest to every platform for (int idx = 0; idx < table.getIndexCount();) { IIndex index = table.getIndex(idx); if (index.isUnique() && existsPKWithName(metaData, table, index.getName())) { table.removeIndex(idx); } else { idx++; } } } return table; }
Column[] columns = table.getColumns(); for (Column column : columns) { if (column != null) {
public String createCsvDataSql(Trigger trigger, TriggerHistory triggerHistory, Table originalTable, Channel channel, String whereClause) { Table table = originalTable.copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true); String sql = sqlTemplates.get(INITIAL_LOAD_SQL_TEMPLATE); Column[] columns = table.getColumns(); String columnsText = buildColumnString(symmetricDialect.getInitialLoadTableAlias(), symmetricDialect.getInitialLoadTableAlias(), "", columns, DataEventType.INSERT, false, channel, trigger).columnString; sql = FormatUtils.replace("columns", columnsText, sql); sql = FormatUtils.replace("oracleToClob", trigger.isUseCaptureLobs() ? "to_clob('')||" : "", sql); sql = FormatUtils.replace("tableName", SymmetricUtils.quote(symmetricDialect, table.getName()), sql); sql = FormatUtils.replace("schemaName", triggerHistory == null ? getSourceTablePrefix(trigger) : getSourceTablePrefix(triggerHistory), sql); sql = FormatUtils.replace("whereClause", whereClause, sql); sql = FormatUtils.replace( "primaryKeyWhereString", getPrimaryKeyWhereString(symmetricDialect.getInitialLoadTableAlias(), table.hasPrimaryKey() ? table.getPrimaryKeyColumns() : table.getColumns()), sql); sql = replaceDefaultSchemaAndCatalog(sql); return sql; }
protected Table toAuditTable(Table table) { Table auditTable = table.copy(); String tableName = table.getName(); if (!FormatUtils.isMixedCase(tableName)) { tableName = tableName.toUpperCase(); } auditTable.setName(String.format("%s_AUDIT", tableName)); Column[] columns = auditTable.getColumns(); auditTable.removeAllColumns(); auditTable.addColumn(new Column(COLUMN_AUDIT_ID, true, Types.BIGINT, 0, 0)); auditTable.addColumn(new Column(COLUMN_AUDIT_TIME, false, Types.TIMESTAMP, 0, 0)); auditTable.addColumn(new Column(COLUMN_AUDIT_EVENT, false, Types.CHAR, 1, 0)); for (Column column : columns) { column.setRequired(false); column.setPrimaryKey(false); column.setAutoIncrement(false); auditTable.addColumn(column); } auditTable.removeAllForeignKeys(); auditTable.removeAllIndices(); engine.getDatabasePlatform().alterCaseToMatchDatabaseDefaultCase(auditTable); return auditTable; }
protected void adjustColumns(Table table) { Column[] columns = table.getColumns(); for (int idx = 0; idx < columns.length; idx++) { if (columns[idx].getMappedTypeCode() == Types.FLOAT) { columns[idx].setMappedTypeCode(Types.REAL); } else if ((columns[idx].getMappedTypeCode() == Types.NUMERIC) || (columns[idx].getMappedTypeCode() == Types.DECIMAL)) { if ((columns[idx].getMappedTypeCode() == Types.NUMERIC) && (columns[idx].getSizeAsInt() == 18) && (columns[idx].getScale() == 0)) { columns[idx].setMappedTypeCode(Types.BIGINT); } } else if (TypeMap.isTextType(columns[idx].getMappedTypeCode())) { columns[idx].setDefaultValue(unescape(columns[idx].getDefaultValue(), "'", "''")); } } }
public String createCsvPrimaryKeySql(Trigger trigger, TriggerHistory triggerHistory, Table table, Channel channel, String whereClause) { String sql = sqlTemplates.get(INITIAL_LOAD_SQL_TEMPLATE); Column[] columns = table.getPrimaryKeyColumns(); String columnsText = buildColumnString(symmetricDialect.getInitialLoadTableAlias(), symmetricDialect.getInitialLoadTableAlias(), "", columns, DataEventType.INSERT, false, channel, trigger).toString(); sql = FormatUtils.replace("columns", columnsText, sql); sql = FormatUtils.replace("oracleToClob", trigger.isUseCaptureLobs() ? "to_clob('')||" : "", sql); sql = FormatUtils.replace("tableName", SymmetricUtils.quote(symmetricDialect, table.getName()), sql); sql = FormatUtils.replace("schemaName", triggerHistory == null ? getSourceTablePrefix(trigger) : getSourceTablePrefix(triggerHistory), sql); sql = FormatUtils.replace("whereClause", whereClause, sql); sql = FormatUtils.replace( "primaryKeyWhereString", getPrimaryKeyWhereString(symmetricDialect.getInitialLoadTableAlias(), table.hasPrimaryKey() ? table.getPrimaryKeyColumns() : table.getColumns()), sql); return sql; }