use of com.axway.ats.core.dbaccess.DbRecordValuesList 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.DbRecordValuesList in project ats-framework by Axway.
the class AbstractEnvironmentHandler method writeBackupToFile.
/**
* Write the backup to a file
*
* @param fileWriter the file writer
* @throws IOException on io error
* @throws DatabaseEnvironmentCleanupException on error
* @throws DbException on error reading from the database
* @throws ParseException
*/
protected void writeBackupToFile(FileWriter fileWriter) throws IOException, DatabaseEnvironmentCleanupException, DbException, ParseException {
if (disableForeignKeys) {
fileWriter.write(disableForeignKeyChecksStart());
}
for (DbTable dbTable : dbTables) {
if (log.isDebugEnabled()) {
log.debug("Preparing data for backup of table " + (dbTable != null ? dbTable.getTableName() : null));
}
List<ColumnDescription> columnsToSelect = null;
columnsToSelect = getColumnsToSelect(dbTable, dbConnection.getUser());
if (columnsToSelect == null || columnsToSelect.size() == 0) {
// it contains some meaningful data and so it has columns
throw new DatabaseEnvironmentCleanupException("No columns to backup for table " + (dbTable != null ? dbTable.getTableName() : ""));
}
StringBuilder selectQuery = new StringBuilder();
selectQuery.append("SELECT ");
selectQuery.append(getColumnsString(columnsToSelect));
selectQuery.append(" FROM ");
selectQuery.append(dbTable.getTableName());
DbQuery query = new DbQuery(selectQuery.toString());
DbRecordValuesList[] records = dbProvider.select(query, DbReturnModes.ESCAPED_STRING);
writeTableToFile(columnsToSelect, dbTable, records, fileWriter);
}
if (disableForeignKeys) {
fileWriter.write(disableForeignKeyChecksEnd());
}
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList 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);
}
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class DatabaseOperations method getValue.
/**
* Gets a single value from a database. </br>
* It returns the first value of the first returned row.
*
* <p><strong>Note</strong>: If the selected DB object is array - BLOB, LONG BLOB ... we return it this way:
* <code>[ 10, 24, -35]</code>
* </p>
*
* @param sqlQuery the SQL query to run
* @return
*/
@PublicAtsApi
public String getValue(String sqlQuery) {
try {
log.debug("Executing query: " + sqlQuery);
DbRecordValuesList[] rsList = dbProvider.select(sqlQuery);
if (rsList != null && rsList.length > 0) {
if (rsList.length > 1) {
log.warn("SQL query '" + sqlQuery + "' returned " + rsList.length + " rows of results");
}
if (rsList[0].size() > 1) {
log.warn("SQL query '" + sqlQuery + "' returned " + rsList[0].size() + " values per row");
}
return rsList[0].get(0).getValueAsString();
}
} catch (DbException e) {
throw new DatabaseOperationsException("Error getting value from DB with query '" + sqlQuery + "'", e);
}
return null;
}
use of com.axway.ats.core.dbaccess.DbRecordValuesList in project ats-framework by Axway.
the class CassandraDbProvider method rowCount.
@Override
public int rowCount(String tableName, String whereCondition) throws DbException, NumberValidationException {
int returnCount;
String sql = "SELECT " + " COUNT(*)" + " FROM " + tableName;
if (whereCondition != null && whereCondition.length() > 0) {
sql += " WHERE " + whereCondition;
}
DbRecordValuesList[] records = select(new DbQuery(sql, new ArrayList<Object>()), DbReturnModes.STRING);
try {
returnCount = records == null ? 0 : ((Long) records[0].get("count")).intValue();
} catch (NumberFormatException nfe) {
throw new NumberValidationException("The row count could not be converted to integer", nfe);
}
return returnCount;
}
Aggregations