use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class CassandraDbProvider method select.
@Override
public DbRecordValuesList[] select(DbQuery dbQuery, DbReturnModes dbReturnMode) throws DbException {
connect();
ArrayList<DbRecordValuesList> dbRecords = new ArrayList<DbRecordValuesList>();
String sqlQuery = dbQuery.getQuery();
if (allowFiltering) {
sqlQuery += " ALLOW FILTERING";
}
if (log.isDebugEnabled()) {
log.debug(sqlQuery);
}
ResultSet results = session.execute(sqlQuery);
int currentRow = 0;
Iterator<Row> it = results.iterator();
while (it.hasNext()) {
Row row = it.next();
currentRow++;
if (log.isDebugEnabled()) {
log.debug("Result row number: " + currentRow);
}
DbRecordValuesList recordList = new DbRecordValuesList();
for (Definition columnDefinition : row.getColumnDefinitions()) {
DbColumn dbColumn = new DbColumn(columnDefinition.getTable(), columnDefinition.getName());
dbColumn.setColumnType(columnDefinition.getType().getName().toString());
Object value = extractObjectFromResultSet(row, columnDefinition);
DbRecordValue recordValue = new DbRecordValue(dbColumn, value);
recordList.add(recordValue);
}
dbRecords.add(recordList);
}
return dbRecords.toArray(new DbRecordValuesList[] {});
}
use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class MysqlDbProvider method extractTableIndexes.
@Override
protected Map<String, String> extractTableIndexes(String tableName, DatabaseMetaData databaseMetaData, String catalog) throws DbException {
String sql = "SELECT TABLE_NAME,INDEX_NAME,NON_UNIQUE,SEQ_IN_INDEX,COLUMN_NAME,COLLATION,SUB_PART,PACKED,NULLABLE,INDEX_TYPE " + "FROM INFORMATION_SCHEMA.STATISTICS " + "WHERE TABLE_NAME='" + tableName + "'";
String indexName = null;
Map<String, String> indexes = new HashMap<>();
for (DbRecordValuesList valueList : select(sql)) {
StringBuilder info = new StringBuilder();
for (DbRecordValue dbValue : valueList) {
String value = dbValue.getValueAsString();
String name = dbValue.getDbColumn().getColumnName();
if ("INDEX_NAME".equalsIgnoreCase(name)) {
indexName = value;
} else {
info.append(", " + name + "=" + value);
}
}
if (indexName == null) {
indexName = "NULL_NAME_FOUND_FOR_INDEX_OF_TABLE_" + tableName;
log.warn("IndexName column not found in query polling for index properties:\nQuery: " + sql + "\nQuery result: " + valueList.toString() + "\nWe will use the following as an index name: " + indexName);
}
indexes.put(indexName, info.toString());
}
return indexes;
}
use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class MssqlDbProvider 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();
MssqlColumnDescription columnDescription = new MssqlColumnDescription(name, type);
// otherwise we get the object address
if (columnDescription.isTypeBinary() || "text".equals(type) || "ntext".equals(type)) {
/*
* http://jtds.sourceforge.net/faq.html
* By default useLOBs is true. In such cases calling getObject on the result set returns
* a LOB object, not a java String. In order to get java String it is needed to use the getString method.
* If useLOBs is false - getObject returns java String
*
* "useLOBs=false;" can be added in the connection URL, or we can simply call the getString method here
*/
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 MssqlEnvironmentHandler method writeTableToFile.
@Override
protected void writeTableToFile(List<ColumnDescription> columns, DbTable table, DbRecordValuesList[] records, FileWriter fileWriter) throws IOException, ParseException {
if (this.includeDeleteStatements) {
fileWriter.write("DELETE FROM " + table.getTableName() + ";" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
if (table.getAutoIncrementResetValue() != null) {
fileWriter.write("DBCC CHECKIDENT ('" + table.getTableName() + "', RESEED, " + table.getAutoIncrementResetValue() + ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
if (records.length > 0) {
StringBuilder insertStatement = new StringBuilder();
String insertBegin = "INSERT INTO " + table.getTableName() + "(" + getColumnsString(columns) + ") VALUES (";
String insertEnd = null;
if (table.isIdentityColumnPresent()) {
insertBegin = "SET IDENTITY_INSERT " + table.getTableName() + " ON; " + insertBegin;
insertEnd = "); SET IDENTITY_INSERT " + table.getTableName() + " OFF;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
} else {
insertEnd = ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
}
for (DbRecordValuesList record : records) {
insertStatement.append(insertBegin);
for (int i = 0; i < record.size(); i++) {
DbRecordValue recordValue = record.get(i);
String fieldValue = (String) recordValue.getValue();
// extract specific values depending on their type
insertStatement.append(extractValue(columns.get(i), fieldValue));
insertStatement.append(",");
}
//remove the last comma
insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
insertStatement.append(insertEnd);
}
fileWriter.write(insertStatement.toString());
}
}
use of com.axway.ats.core.dbaccess.DbRecordValue in project ats-framework by Axway.
the class MysqlEnvironmentHandler method writeTableToFile.
@Override
protected void writeTableToFile(List<ColumnDescription> columns, DbTable table, DbRecordValuesList[] records, FileWriter fileWriter) throws IOException {
if (this.addLocks && table.isLockTable()) {
fileWriter.write("LOCK TABLES `" + table.getTableName() + "` WRITE;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
if (this.includeDeleteStatements) {
fileWriter.write("DELETE FROM `" + table.getTableName() + "`;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
if (table.getAutoIncrementResetValue() != null) {
fileWriter.write("SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
fileWriter.write("ALTER TABLE `" + table.getTableName() + "` AUTO_INCREMENT = " + table.getAutoIncrementResetValue() + ";" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
// To handle this, lock the table again
if (this.addLocks && table.isLockTable()) {
fileWriter.write("LOCK TABLES `" + table.getTableName() + "` WRITE;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
}
if (records.length > 0) {
StringBuilder insertStatement = new StringBuilder();
String insertBegin = "INSERT INTO `" + table.getTableName() + "` (" + getColumnsString(columns) + ") VALUES(";
String insertEnd = ");" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR;
for (DbRecordValuesList record : records) {
// clear the StringBuilder current data
// it is a little better (almost the same) than insertStatement.setLength( 0 ); as performance
insertStatement.delete(0, insertStatement.length());
insertStatement.append(insertBegin);
for (int i = 0; i < record.size(); i++) {
DbRecordValue recordValue = record.get(i);
String fieldValue = (String) recordValue.getValue();
if (fieldValue == null) {
fieldValue = "NULL";
}
// extract specific values depending on their type
insertStatement.append(extractValue(columns.get(i), fieldValue));
insertStatement.append(",");
}
//remove the last comma
insertStatement.delete(insertStatement.length() - 1, insertStatement.length());
insertStatement.append(insertEnd);
fileWriter.write(insertStatement.toString());
fileWriter.flush();
}
}
if (this.addLocks && table.isLockTable()) {
fileWriter.write("UNLOCK TABLES;" + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
fileWriter.write(AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
Aggregations