Skip to content

Commit

Permalink
MONDRIAN: Fix loader for BOOLEAN columns in Oracle.
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//open/mondrian/": change = 3425]
  • Loading branch information
aspen committed Apr 4, 2005
1 parent d97ec3c commit f2d5643
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions testsrc/main/mondrian/test/loader/MondrianFoodMartLoader.java
Expand Up @@ -90,6 +90,8 @@ public class MondrianFoodMartLoader {
private FileWriter fileOutput = null;

private SqlQuery sqlQuery;
private String booleanColumnType;
private String bigIntColumnType;
private final HashMap mapTableNameToColumns = new HashMap();

public MondrianFoodMartLoader(String[] args) {
Expand Down Expand Up @@ -209,6 +211,18 @@ private void load() throws Exception {
final DatabaseMetaData metaData = connection.getMetaData();
sqlQuery = new SqlQuery(metaData);

booleanColumnType = "SMALLINT";
if (sqlQuery.isPostgres()) {
booleanColumnType = "BOOLEAN";
} else if (sqlQuery.isMySQL()) {
booleanColumnType = "BIT";
}

bigIntColumnType = "BIGINT";
if (sqlQuery.isOracle()) {
bigIntColumnType = "DECIMAL(15,0)";
}

try {
createTables(); // This also initializes mapTableNameToColumns
if (data) {
Expand Down Expand Up @@ -294,7 +308,6 @@ private void loadDataFromFile() throws Exception {
String tableName = matcher.group(1); // e.g. "foo"
String columnNames = matcher.group(2);
String values = matcher.group(3);
// Util.discard(values); // Not needed now

// If table just changed, flush the previous batch.
if (!tableName.equals(prevTable)) {
Expand All @@ -307,8 +320,10 @@ private void loadDataFromFile() throws Exception {
batchSize = 0;
prevTable = tableName;
quotedTableName = quoteId(tableName);
quotedColumnNames = columnNames.replaceAll("`", sqlQuery.getQuoteIdentifierString());
String[] splitColumnNames = columnNames.replaceAll("`", "").replaceAll(" ", "").split(",");
quotedColumnNames = columnNames
.replaceAll("`", sqlQuery.getQuoteIdentifierString());
String[] splitColumnNames = columnNames.replaceAll("`", "")
.replaceAll(" ", "").split(",");
Column[] columns = (Column[]) mapTableNameToColumns.get(tableName);

orderedColumns = new Column[columns.length];
Expand Down Expand Up @@ -414,7 +429,11 @@ private String getMassagedValues(Column[] columns, String values) throws Excepti
if (i > 0) {
sb.append(",");
}
sb.append(columnValue(individualValues[i], columns[i]));
String value = individualValues[i];
if (value != null && value.trim().equals("NULL")) {
value = null;
}
sb.append(columnValue(value, columns[i]));
}
return sb.toString();

Expand Down Expand Up @@ -682,6 +701,8 @@ private void createIndexes() throws Exception {
createIndex(false, "reserve_employee", "i_reserve_employee_dept_id", new String[] {"department_id"});
createIndex(false, "reserve_employee", "i_reserve_employee_store_id", new String[] {"store_id"});
createIndex(false, "reserve_employee", "i_reserve_employee_super_id", new String[] {"supervisor_id"});
createIndex(false, "salary", "i_salary_pay_date", new String[] {"pay_date"});
createIndex(false, "salary", "i_salary_employee", new String[] {"employee_id"});
createIndex(false, "sales_fact_1997", "i_sales_1997_customer_id", new String[] {"customer_id"});
createIndex(false, "sales_fact_1997", "i_sales_1997_product_id", new String[] {"product_id"});
createIndex(false, "sales_fact_1997", "i_sales_1997_promotion_id", new String[] {"promotion_id"});
Expand All @@ -707,11 +728,6 @@ private void createIndexes() throws Exception {
createIndex(false, "time_by_day", "i_time_quarter", new String[] {"quarter"});
createIndex(false, "time_by_day", "i_time_month", new String[] {"month_of_year"});

createIndex(false, "salary", "i_salary_pay_date", new String[] {"pay_date"});
createIndex(false, "salary", "i_salary_employee", new String[] {"employee_id"});
createIndex(true, "employee", "i_employee_id", new String[] {"employee_id"});
createIndex(false, "employee", "i_employee_store", new String[] {"store_id"});
createIndex(false, "employee", "i_employee_supvsr", new String[] {"supervisor_id"});
if (outputDirectory != null) {
fileOutput.close();
}
Expand Down Expand Up @@ -739,18 +755,18 @@ private void createIndex(
try {
StringBuffer buf = new StringBuffer();
if (jdbcOutput) {
try {
buf.append("DROP INDEX ")
.append(quoteId(indexName));
if (sqlQuery.isMySQL()) {
buf.append(" ON ")
.append(quoteId(tableName));
}
final String deleteDDL = buf.toString();
executeDDL(deleteDDL);
} catch (Exception e1) {
System.out.println("Drop failed: but continue");
}
try {
buf.append("DROP INDEX ")
.append(quoteId(indexName));
if (sqlQuery.isMySQL()) {
buf.append(" ON ")
.append(quoteId(tableName));
}
final String deleteDDL = buf.toString();
executeDDL(deleteDDL);
} catch (Exception e1) {
System.out.println("Drop failed: but continue");
}
}

buf = new StringBuffer();
Expand Down Expand Up @@ -785,18 +801,6 @@ private void createTables() throws Exception {
fileOutput = new FileWriter(new File(outputDirectory, "createTables.sql"));
}

String booleanColumnType = "SMALLINT";
if (sqlQuery.isPostgres()) {
booleanColumnType = "BOOLEAN";
} else if (sqlQuery.isMySQL()) {
booleanColumnType = "BIT";
}

String bigIntColumnType = "BIGINT";
if (sqlQuery.isOracle()) {
bigIntColumnType = "DECIMAL(15,0)";
}

createTable("sales_fact_1997", new Column[] {
new Column("product_id", "INTEGER", "NOT NULL"),
new Column("time_id", "INTEGER", "NOT NULL"),
Expand Down Expand Up @@ -852,11 +856,11 @@ private void createTables() throws Exception {
new Column("store_invoice", "DECIMAL(10,4)", ""),
});
createTable("currency", new Column[] {
new Column("currency_id", "INTEGER", "NOT NULL"),
new Column("date", "DATE", "NOT NULL"),
new Column("currency", "VARCHAR(30)", "NOT NULL"),
new Column("conversion_ratio", "DECIMAL(10,4)", "NOT NULL"),
});
new Column("currency_id", "INTEGER", "NOT NULL"),
new Column("date", "DATE", "NOT NULL"),
new Column("currency", "VARCHAR(30)", "NOT NULL"),
new Column("conversion_ratio", "DECIMAL(10,4)", "NOT NULL"),
});
createTable("account", new Column[] {
new Column("account_id", "INTEGER", "NOT NULL"),
new Column("account_parent", "INTEGER", ""),
Expand Down Expand Up @@ -1136,7 +1140,7 @@ private void createTable(String name, Column[] columns) {
// We're going to load the data without [re]creating
// the table, so let's remove the data.
try {
executeDDL("DELETE FROM " + quoteId(name));
executeDDL("DELETE FROM " + quoteId(name));
} catch (SQLException e) {
throw MondrianResource.instance().newCreateTableFailed(name, e);
}
Expand All @@ -1160,7 +1164,7 @@ private void createTable(String name, Column[] columns) {
buf.append(")");
final String ddl = buf.toString();
executeDDL("DROP TABLE " + quoteId(name));
executeDDL(ddl);
executeDDL(ddl);
} catch (Exception e) {
throw MondrianResource.instance().newCreateTableFailed(name, e);
}
Expand Down Expand Up @@ -1379,18 +1383,24 @@ private String columnValue(String columnValue, Column column) throws Exception {

/*
* Output for a BOOLEAN (Postgres) or BIT (other DBMSs)
*
* FIXME This code assumes that only a boolean column would
* map onto booleanColumnType. It would be better if we had a
* logical and physical type for each column.
*/
} else if (columnType.startsWith("BOOLEAN") || columnType.startsWith("BIT")) {
if (!sqlQuery.isMySQL()) {
if (columnValue.trim().equals("1")) {
} else if (columnType.equals(booleanColumnType)) {
String trimmedValue = columnValue.trim();
if (!sqlQuery.isMySQL() &&
!sqlQuery.isOracle()) {
if (trimmedValue.equals("1")) {
return "true";
} else if (columnValue.trim().equals("0")) {
} else if (trimmedValue.equals("0")) {
return "false";
}
} else {
if (columnValue.trim().equals("true")) {
if (trimmedValue.equals("true")) {
return "1";
} else if (columnValue.trim().equals("false")) {
} else if (trimmedValue.equals("false")) {
return "0";
}
}
Expand Down Expand Up @@ -1478,3 +1488,6 @@ public Column(String name, String type, String constraint) {
}
}
}


// End MondrianFoodMartLoader.java

0 comments on commit f2d5643

Please sign in to comment.