use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class CassandraEnvironmentHandler method writeTableToFile.
@Override
protected void writeTableToFile(List<ColumnDescription> columns, DbTable table, DbRecordValuesList[] records, FileWriter fileWriter) throws IOException, ParseException {
if (this.includeDeleteStatements) {
fileWriter.write("TRUNCATE " + table.getTableName() + ";" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
if (records.length > 0) {
String counterColumnName = getCounterColumn(records);
if (counterColumnName == null) {
final String insertBegin = "INSERT INTO " + table.getTableName() + "(" + getColumnsString(columns) + ") VALUES (";
final String insertEnd = ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
StringBuilder insertStatement = new StringBuilder();
for (DbRecordValuesList dbRow : records) {
insertStatement.setLength(0);
insertStatement.append(insertBegin);
for (int i = 0; i < dbRow.size(); i++) {
// extract specific values depending on their type
insertStatement.append(extractValue(columns.get(i), dbRow.get(i).getValue()));
insertStatement.append(",");
}
//remove the last comma
insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
insertStatement.append(insertEnd);
fileWriter.write(insertStatement.toString());
}
} else {
// This is the way to insert rows for tables having counter data type. Only one column could have such type.
// others are UUIDs. SET counterColumnName=+3/-2 just increases/decreases such counter. If not already existing
// then 0 is assumed as initial value.
String insertBegin = "UPDATE " + table.getTableName() + " SET " + counterColumnName + " = " + counterColumnName + " + <the counter value> WHERE ";
final String insertEnd = EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
StringBuilder insertStatement = new StringBuilder();
for (DbRecordValuesList dbRow : records) {
insertStatement.setLength(0);
insertStatement.append(insertBegin);
String counterValue = "";
for (int i = 0; i < dbRow.size(); i++) {
DbRecordValue dbValue = dbRow.get(i);
String dbColumnName = dbValue.getDbColumn().getColumnName();
if (dbColumnName.equals(counterColumnName)) {
// this is a counter, we will apply it later
counterValue = dbValue.getValue().toString();
} else {
// extract specific values depending on their type
insertStatement.append(dbColumnName + " = " + extractValue(columns.get(i), dbValue.getValue()));
insertStatement.append(" AND ");
}
}
//remove the last 'AND'
insertStatement.delete(insertStatement.length() - 5, insertStatement.length());
insertStatement.append(insertEnd);
fileWriter.write(insertStatement.toString().replace("<the counter value>", counterValue));
}
}
}
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class MockDbProvider method select.
public DbRecordValuesList[] select(DbQuery query) {
final int resultCount = 2;
DbRecordValuesList[] resultSet = new DbRecordValuesList[resultCount];
for (int i = 0; i < resultCount; i++) {
DbRecordValuesList result = new DbRecordValuesList();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Timestamp stamp = new Timestamp(calendar.getTime().getTime());
result.add(new DbRecordValue("asd", "key0", "value" + i + seed));
result.add(new DbRecordValue("", "key1", "value" + i + seed));
result.add(new DbRecordValue("", "key2", "value" + i + seed));
result.add(new DbRecordValue("numeric", "key", i));
result.add(new DbRecordValue("binary", "key", new byte[] { 1, 2, (byte) i }));
result.add(new DbRecordValue("boolean", "keyString", "0"));
result.add(new DbRecordValue("boolean", "keyNumber", 1L));
result.add(new DbRecordValue("Date", "today", stamp));
if (!useChangedValues) {
result.add(new DbRecordValue("", "test_key", "test_value"));
} else {
result.add(new DbRecordValue("", "test_key", "test_value_changed"));
}
resultSet[i] = result;
}
if (useChangedValues) {
useChangedValues = false;
}
return resultSet;
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class DbFolder method refresh.
private void refresh() throws RbvException {
newMetaDataMap = new HashMap<String, MetaData>();
//store the current meta data map and clear the map holding all meta data
//this way we will be able to detect any changes including added and removed
//meta data
HashMap<String, MetaData> oldMetaDataMap = allMetaDataMap;
allMetaDataMap = new HashMap<String, MetaData>();
log.debug("Run DB query '" + this.searchQuery.getQuery() + "'");
DbRecordValuesList[] queryResults;
try {
queryResults = dbProvider.select(this.searchQuery);
} catch (DbException dbe) {
throw new RbvException(dbe);
}
if (queryResults != null) {
for (DbRecordValuesList queryResult : queryResults) {
DbMetaData currentData = new DbMetaData();
StringBuffer metaDataHash = new StringBuffer();
for (DbRecordValue recordValue : queryResult) {
DbMetaDataKey key = new DbMetaDataKey(recordValue.getDbColumn());
Object value = recordValue.getValue();
currentData.putProperty(key.toString(), value);
//calculate the hash
metaDataHash.append(key.toString());
metaDataHash.append(recordValue.getValueAsString());
}
try {
//compute MD5 so we don't keep the whole StringBuffer in memory
MessageDigest metaDataHashDigest = MessageDigest.getInstance("MD5");
String metaDataSum = new String(metaDataHashDigest.digest(metaDataHash.toString().getBytes()));
if (!oldMetaDataMap.containsKey(metaDataSum)) {
newMetaDataMap.put(metaDataSum, currentData);
}
//always put the record in the map holding all meta data
allMetaDataMap.put(metaDataSum, currentData);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
didPollingOccured = true;
}
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class MockDbProvider method select.
@Override
public DbRecordValuesList[] select(DbQuery dbQuery) throws DbException {
DbRecordValuesList[] resultSet = null;
DbRecordValuesList result = new DbRecordValuesList();
result.add(new DbRecordValue("asd", "key0", "value00"));
//testing db exception
if (dbQuery.getQuery().contains("wrongTableName")) {
throw new DbException("wrong table name");
//test more than one result values
} else if (dbQuery.getQuery().contains("moreValues")) {
resultSet = new DbRecordValuesList[2];
resultSet[0] = result;
DbRecordValuesList sresult = new DbRecordValuesList();
sresult.add(new DbRecordValue("dfg", "key0", "value11"));
resultSet[1] = result;
//testing with DatabaseRow and DatabaseCell classes
} else if (dbQuery.getQuery().contains("tableWithManyRows")) {
if (dbQuery.getQuery().contains("*")) {
resultSet = new DbRecordValuesList[2];
DbRecordValuesList firstDbRow = new DbRecordValuesList();
firstDbRow.add(new DbRecordValue("tableWithManyRows", "firstColumnName", "value01"));
firstDbRow.add(new DbRecordValue("tableWithManyRows", "secondColumnName", "value02"));
resultSet[0] = firstDbRow;
DbRecordValuesList secondDbRow = new DbRecordValuesList();
secondDbRow.add(new DbRecordValue("tableWithManyRows", "firstColumnName", "value11"));
secondDbRow.add(new DbRecordValue("tableWithManyRows", "secondColumnName", "value12"));
resultSet[1] = secondDbRow;
} else {
resultSet = new DbRecordValuesList[2];
DbRecordValuesList firstDbRow = new DbRecordValuesList();
firstDbRow.add(new DbRecordValue("tableWithManyRows", "selectColumnName", "value1"));
resultSet[0] = firstDbRow;
DbRecordValuesList secondDbRow = new DbRecordValuesList();
secondDbRow.add(new DbRecordValue("tableWithManyRows", "selectColumnName", "value2"));
resultSet[1] = secondDbRow;
}
//testing no value result
} else if (!dbQuery.getQuery().contains("nullValue")) {
resultSet = new DbRecordValuesList[1];
resultSet[0] = result;
}
return resultSet;
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class Test_OracleEnvironmentHandler method createBackupNoForeignKeysNoLockNoDelete.
@Test
public void createBackupNoForeignKeysNoLockNoDelete() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
DbTable table1 = new DbTable("table1");
DbTable table2 = new DbTable("table2");
//the columns meta data
DbRecordValuesList column1MetaData = new DbRecordValuesList();
column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
column1MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
column1MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData };
DbRecordValuesList record1Value = new DbRecordValuesList();
record1Value.add(new DbRecordValue("table1", "name1", "value1"));
DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
envHandler.addTable(table1);
envHandler.addTable(table2);
expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
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
//table1
mockFileWriter.write("INSERT INTO table1(name1) VALUES ('value1');" + EOL_MARKER + LINE_SEPARATOR);
//table2
mockFileWriter.write("INSERT INTO table2(name1) VALUES ('value1');" + EOL_MARKER + LINE_SEPARATOR);
replay(mockDbConnection);
replay(mockDbProvider);
replay(mockFileWriter);
envHandler.setForeignKeyCheck(false);
envHandler.setIncludeDeleteStatements(false);
envHandler.setLockTables(false);
envHandler.writeBackupToFile(mockFileWriter);
verify(mockDbConnection);
verify(mockDbProvider);
verify(mockFileWriter);
}
Aggregations