use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class OracleDbProvider method parseDbRecordAsObject.
@Override
protected DbRecordValue parseDbRecordAsObject(DbColumn dbColumn, ResultSet res, int columnIndex) throws IOException, SQLException {
DbRecordValue recordValue = null;
String type = dbColumn.getColumnType().toLowerCase();
String name = dbColumn.getColumnName().toLowerCase();
OracleColumnDescription columnDescription = new OracleColumnDescription(name, type);
if (columnDescription.isTypeBinary()) {
recordValue = new DbRecordValue(dbColumn, res.getString(columnIndex));
} else {
recordValue = new DbRecordValue(dbColumn, res.getObject(columnIndex));
}
return recordValue;
}
use of com.axway.ats.core.dbaccess.DbRecordValue 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.DbRecordValue 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.DbRecordValue 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.DbRecordValue in project ats-framework by Axway.
the class DatabaseOperations method getDatabaseDataAsStrings.
/**
* <p>
* Gets data from a database table. All values as returned as String representations as needed.<br>
* For example:
* <ul>
* <li>TIMESTAMPs are retuned as java.sql.Timestamp.toString() format
* (yyyy-MM-dd HH:mm:ss.SSS and millis instead of nanos as documented in JavaSE Doc)</li>
* <li>BLOBs bytes are represented in hex format. Beware not to select too big data cells.</li>
* </ul>
* </p>
* @param sqlQuery the SQL SELECT query to run. Client is resposible for any
* possible escaping needed so thread this as not security-safe method
* @return the found database data
*/
@PublicAtsApi
public DatabaseRow[] getDatabaseDataAsStrings(String sqlQuery) {
List<DatabaseRow> dbRows = new LinkedList<DatabaseRow>();
try {
log.debug("Executing query: " + sqlQuery);
DbRecordValuesList[] rsList = dbProvider.select(new DbQuery(sqlQuery), DbReturnModes.STRING);
if (rsList != null) {
for (DbRecordValuesList rs : rsList) {
Iterator<DbRecordValue> it = rs.iterator();
if (it.hasNext()) {
DatabaseRow dbRow = new DatabaseRow();
while (it.hasNext()) {
DbRecordValue dbRecordValue = it.next();
dbRow.addCell(new DatabaseCell(dbRecordValue.getDbColumn().getColumnName(), dbRecordValue.getValueAsString()));
}
dbRows.add(dbRow);
}
}
}
return dbRows.toArray(new DatabaseRow[dbRows.size()]);
} catch (DbException e) {
throw new DatabaseOperationsException("Error getting data from DB with query '" + sqlQuery + "'", e);
}
}
Aggregations