use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class Test_MysqlEnvironmentHandler method createBackupPositive.
@Test
public void createBackupPositive() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
//the columns meta data
DbRecordValuesList column1MetaData = new DbRecordValuesList();
column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
column1MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
column1MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
DbRecordValuesList column2MetaData = new DbRecordValuesList();
column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
column2MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
column2MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
DbRecordValuesList column3MetaData = new DbRecordValuesList();
column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
column3MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "bit"));
column3MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", 1));
DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData, column2MetaData, column3MetaData };
DbRecordValuesList record1Value = new DbRecordValuesList();
record1Value.add(new DbRecordValue("table1", "name1", "value1"));
record1Value.add(new DbRecordValue("table1", "name2", null));
record1Value.add(new DbRecordValue("table1", "name3", "1"));
DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
expect(mockDbProvider.getConnection()).andReturn(mockConnection);
expect(mockConnection.getMetaData()).andReturn(metaData);
expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
expect(metaData.getDriverMajorVersion()).andReturn(5);
expect(metaData.getDriverMinorVersion()).andReturn(1);
expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
//expect the file writer calls
//foreign keys check start
mockFileWriter.write("SET FOREIGN_KEY_CHECKS = 0;" + EOL_MARKER + LINE_SEPARATOR);
//table1
mockFileWriter.write("LOCK TABLES `table1` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.write("DELETE FROM `table1`;" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.write("INSERT INTO `table1` (name1,name2,name3) VALUES('value1',NULL,0x1);" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.flush();
mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.write(LINE_SEPARATOR);
//table2
mockFileWriter.write("LOCK TABLES `table2` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.write("DELETE FROM `table2`;" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.write("INSERT INTO `table2` (name1,name2,name3) VALUES('value1',NULL,0x1);" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.flush();
mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
mockFileWriter.write(LINE_SEPARATOR);
//foreign keys check end
mockFileWriter.write("");
replayAll();
DbTable table1 = new DbTable("table1");
DbTable table2 = new DbTable("table2");
MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
envHandler.addTable(table1);
envHandler.addTable(table2);
envHandler.writeBackupToFile(mockFileWriter);
verifyAll();
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class MysqlEnvironmentHandler method getColumnsToSelect.
@Override
protected List<ColumnDescription> getColumnsToSelect(DbTable table, String userName) throws DbException, ColumnHasNoDefaultValueException {
// TODO Might be replaced with JDBC DatabaseMetaData.getColumns() but should be verified with default values
ArrayList<ColumnDescription> columnsToSelect = new ArrayList<ColumnDescription>();
DbRecordValuesList[] columnsMetaData = null;
try {
columnsMetaData = dbProvider.select("SHOW COLUMNS FROM " + table.getTableName());
} catch (DbException e) {
log.error("Could not get columns for table " + table.getTableName() + ". Check if the table is existing and that the user has permissions. See more details in the trace.");
throw e;
}
for (DbRecordValuesList columnMetaData : columnsMetaData) {
String columnName = (String) columnMetaData.get(MysqlColumnNames.COLUMN_NAME.getName(isJDBC4));
//check if the column should be skipped in the backup
if (!table.getColumnsToExclude().contains(columnName)) {
ColumnDescription colDescription = new MysqlColumnDescription(columnName, (String) columnMetaData.get(MysqlColumnNames.COLUMN_TYPE.getName(isJDBC4)));
columnsToSelect.add(colDescription);
} else {
//if this column has no default value, we cannot skip it in the backup
if (columnMetaData.get(MysqlColumnNames.DEFAULT_COLUMN.getName(isJDBC4)) == null) {
log.error("Cannot skip columns with no default values while creating backup");
throw new ColumnHasNoDefaultValueException(table.getTableName(), columnName);
}
}
}
return columnsToSelect;
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class DatabaseSnapshot method loadTableData.
List<String> loadTableData(String snapshotName, TableDescription table, Map<String, SkipRules> allSkipRules, Map<String, Map<String, String>> allSkipRows, DbProvider dbProvider, Document backupXmlFile) {
if (dbProvider == null) {
// DB provider not specified, use the one from this instance
dbProvider = this.dbProvider;
}
List<String> valuesList = new ArrayList<String>();
if (backupXmlFile == null) {
// load table row data from database
String sqlQuery = construcSelectStatement(table, allSkipRules);
if (sqlQuery != null) {
for (DbRecordValuesList rowValues : dbProvider.select(sqlQuery)) {
// if there are rows for skipping we will find them and remove them from the list
String stringRowValue = rowValues.toString();
// escaping special characters that may
// cause some trouble while saving the snapshot into a file
stringRowValue.replace("&", "&");
stringRowValue.replace("<", "<");
stringRowValue.replace(">", ">");
DatabaseSnapshotBackupUtils dbUtil = new DatabaseSnapshotBackupUtils();
if (allSkipRows != null && !dbUtil.skipRow(allSkipRows.get(table.getName()), stringRowValue)) {
valuesList.add(stringRowValue);
}
}
log.debug("[" + snapshotName + " from database] Loaded " + valuesList.size() + " rows for table " + table.getName());
} else {
log.warn("Skip table " + table.getName() + " because of excluding all columns.");
}
} else {
// load table row data from backup file
List<Element> dbSnapshotNodeList = DatabaseSnapshotUtils.getChildrenByTagName(backupXmlFile, DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
if (dbSnapshotNodeList.size() != 1) {
throw new DatabaseSnapshotException("Bad dabase snapshot backup file. It must have 1 '" + DatabaseSnapshotUtils.NODE_DB_SNAPSHOT + "' node, but it has" + dbSnapshotNodeList.size());
}
for (Element tableNode : DatabaseSnapshotUtils.getChildrenByTagName(dbSnapshotNodeList.get(0), "TABLE")) {
String tableName = tableNode.getAttribute("name");
if (table.getName().equalsIgnoreCase(tableName)) {
List<Element> tableRows = DatabaseSnapshotUtils.getChildrenByTagName(tableNode, "row");
log.debug("[" + snapshotName + " from file] Loaded " + tableRows.size() + " rows for table " + tableName);
for (Element tableRow : DatabaseSnapshotUtils.getChildrenByTagName(tableNode, "row")) {
valuesList.add(tableRow.getTextContent());
}
break;
}
}
}
return valuesList;
}
Aggregations