aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2010-07-16 04:19:59 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2010-07-16 04:19:59 +0000
commit4a0bbd462cb4a1bed43934cefdf1087ed360b128 (patch)
tree56fc297b90a8bc57362bfccc4bca93a87e6cdabc
parent33165ebc3a26a85e7b2474a249a1a4caecee4471 (diff)
downloadjackcess-4a0bbd462cb4a1bed43934cefdf1087ed360b128.tar.gz
jackcess-4a0bbd462cb4a1bed43934cefdf1087ed360b128.zip
Make Database.escapeIdentifier public; add methods to TableBuilder and ColumnBuilder to optionally escape names (#3029500)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@474 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r--src/changes/changes.xml4
-rw-r--r--src/java/com/healthmarketscience/jackcess/ColumnBuilder.java9
-rw-r--r--src/java/com/healthmarketscience/jackcess/Database.java2
-rw-r--r--src/java/com/healthmarketscience/jackcess/ImportUtil.java10
-rw-r--r--src/java/com/healthmarketscience/jackcess/TableBuilder.java41
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/CursorTest.java4
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java68
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java4
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/ExportTest.java12
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java16
10 files changed, 109 insertions, 61 deletions
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1772db5..9a89d0a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -13,6 +13,10 @@
Fix some places where table/columns names were not being handled
case-insensitively.
</action>
+ <action dev="jahlborn" type="fix" issue="3029500">
+ Make Database.escapeIdentifier public; add methods to TableBuilder and
+ ColumnBuilder to optionally escape names.
+ </action>
</release>
<release version="1.2.0" date="2010-04-18">
<action dev="bhamail" type="update" issue="1451628">
diff --git a/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java b/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java
index 97fca0a..06e7356 100644
--- a/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java
+++ b/src/java/com/healthmarketscience/jackcess/ColumnBuilder.java
@@ -156,6 +156,15 @@ public class ColumnBuilder {
}
/**
+ * Escapes the new column's name using {@link Database#escapeIdentifier}.
+ */
+ public ColumnBuilder escapeName()
+ {
+ _name = Database.escapeIdentifier(_name);
+ return this;
+ }
+
+ /**
* Creates a new Column with the currently configured attributes.
*/
public Column toColumn() {
diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java
index d6786ba..b445141 100644
--- a/src/java/com/healthmarketscience/jackcess/Database.java
+++ b/src/java/com/healthmarketscience/jackcess/Database.java
@@ -1178,7 +1178,7 @@ public class Database
/**
* @return A table or column name escaped for Access
*/
- static String escape(String s) {
+ public static String escapeIdentifier(String s) {
if (isReservedWord(s)) {
return ESCAPE_PREFIX + s;
}
diff --git a/src/java/com/healthmarketscience/jackcess/ImportUtil.java b/src/java/com/healthmarketscience/jackcess/ImportUtil.java
index 8aa7ed6..d13a7af 100644
--- a/src/java/com/healthmarketscience/jackcess/ImportUtil.java
+++ b/src/java/com/healthmarketscience/jackcess/ImportUtil.java
@@ -90,7 +90,7 @@ public class ImportUtil
{
ResultSetMetaData md = source.getMetaData();
- name = Database.escape(name);
+ name = Database.escapeIdentifier(name);
Table table = null;
if(!useExistingTable || ((table = db.getTable(name)) == null)) {
@@ -98,7 +98,7 @@ public class ImportUtil
List<Column> columns = new LinkedList<Column>();
for (int i = 1; i <= md.getColumnCount(); i++) {
Column column = new Column();
- column.setName(Database.escape(md.getColumnName(i)));
+ column.setName(Database.escapeIdentifier(md.getColumnName(i)));
int lengthInUnits = md.getColumnDisplaySize(i);
column.setSQLType(md.getColumnType(i), lengthInUnits);
DataType type = column.getType();
@@ -269,7 +269,7 @@ public class ImportUtil
}
try {
- name = Database.escape(name);
+ name = Database.escapeIdentifier(name);
Table table = null;
if(!useExistingTable || ((table = db.getTable(name)) == null)) {
@@ -277,8 +277,8 @@ public class ImportUtil
String[] columnNames = line.split(delim);
for (int i = 0; i < columnNames.length; i++) {
- columns.add(new ColumnBuilder(Database.escape(columnNames[i]),
- DataType.TEXT)
+ columns.add(new ColumnBuilder(columnNames[i], DataType.TEXT)
+ .escapeName()
.setLength((short)DataType.TEXT.getMaxSize())
.toColumn());
}
diff --git a/src/java/com/healthmarketscience/jackcess/TableBuilder.java b/src/java/com/healthmarketscience/jackcess/TableBuilder.java
index d7225d8..13ff1df 100644
--- a/src/java/com/healthmarketscience/jackcess/TableBuilder.java
+++ b/src/java/com/healthmarketscience/jackcess/TableBuilder.java
@@ -42,20 +42,59 @@ public class TableBuilder {
private String _name;
/** columns for the new table */
private List<Column> _columns = new ArrayList<Column>();
-
+ /** whether or not table/columns names are automatically escaped */
+ private boolean _escapeIdentifiers;
+
public TableBuilder(String name) {
+ this(name, false);
+ }
+
+ public TableBuilder(String name, boolean escapeIdentifiers) {
_name = name;
+ _escapeIdentifiers = escapeIdentifiers;
+ if(_escapeIdentifiers) {
+ _name = Database.escapeIdentifier(_name);
+ }
}
+
/**
* Adds a Column to the new table.
*/
public TableBuilder addColumn(Column column) {
+ if(_escapeIdentifiers) {
+ column.setName(Database.escapeIdentifier(column.getName()));
+ }
_columns.add(column);
return this;
}
/**
+ * Adds a Column to the new table.
+ */
+ public TableBuilder addColumn(ColumnBuilder columnBuilder) {
+ return addColumn(columnBuilder.toColumn());
+ }
+
+ /**
+ * Sets whether or not subsequently added columns will have their names
+ * automatically escaped
+ */
+ public TableBuilder setEscapeIdentifiers(boolean escapeIdentifiers) {
+ _escapeIdentifiers = escapeIdentifiers;
+ return this;
+ }
+
+ /**
+ * Escapes the new table's name using {@link Database#escapeIdentifier}.
+ */
+ public TableBuilder escapeName()
+ {
+ _name = Database.escapeIdentifier(_name);
+ return this;
+ }
+
+ /**
* Creates a new Table in the given Database with the currently configured
* attributes.
*/
diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
index 12b4d44..4fda31a 100644
--- a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java
@@ -75,8 +75,8 @@ public class CursorTest extends TestCase {
Database db = create(fileFormat);
Table table = new TableBuilder("test")
- .addColumn(new ColumnBuilder("id", DataType.LONG).toColumn())
- .addColumn(new ColumnBuilder("value", DataType.TEXT).toColumn())
+ .addColumn(new ColumnBuilder("id", DataType.LONG))
+ .addColumn(new ColumnBuilder("value", DataType.TEXT))
.toTable(db);
for(Map<String,Object> row : createTestTableData()) {
diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
index 7052e41..4601cfa 100644
--- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
@@ -132,8 +132,8 @@ public class DatabaseTest extends TestCase {
try {
new TableBuilder("test")
- .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("a", DataType.MEMO).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.TEXT))
+ .addColumn(new ColumnBuilder("a", DataType.MEMO))
.toTable(db);
fail("created table with duplicate column names?");
} catch(IllegalArgumentException e) {
@@ -143,7 +143,7 @@ public class DatabaseTest extends TestCase {
try {
new TableBuilder("test")
.addColumn(new ColumnBuilder("A", DataType.TEXT)
- .setLengthInUnits(352).toColumn())
+ .setLengthInUnits(352))
.toTable(db);
fail("created table with invalid column length?");
} catch(IllegalArgumentException e) {
@@ -152,8 +152,7 @@ public class DatabaseTest extends TestCase {
try {
new TableBuilder("test")
- .addColumn(new ColumnBuilder("A_" + createString(70), DataType.TEXT)
- .toColumn())
+ .addColumn(new ColumnBuilder("A_" + createString(70), DataType.TEXT))
.toTable(db);
fail("created table with too long column name?");
} catch(IllegalArgumentException e) {
@@ -161,13 +160,13 @@ public class DatabaseTest extends TestCase {
}
new TableBuilder("test")
- .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.TEXT))
.toTable(db);
try {
new TableBuilder("Test")
- .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.TEXT))
.toTable(db);
fail("create duplicate tables?");
} catch(IllegalArgumentException e) {
@@ -419,9 +418,9 @@ public class DatabaseTest extends TestCase {
Table table =
new TableBuilder("test")
- .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("B", DataType.MEMO).toColumn())
- .addColumn(new ColumnBuilder("C", DataType.OLE).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.TEXT))
+ .addColumn(new ColumnBuilder("B", DataType.MEMO))
+ .addColumn(new ColumnBuilder("C", DataType.OLE))
.toTable(db);
String testStr = "This is a test";
@@ -456,9 +455,7 @@ public class DatabaseTest extends TestCase {
for (int i = 0; i < numColumns; i++)
{
- Column column = new ColumnBuilder("column_" + i, DataType.MEMO)
- .toColumn();
- bigTableBuilder.addColumn(column);
+ bigTableBuilder.addColumn(new ColumnBuilder("column_" + i, DataType.MEMO));
}
Table bigTable = bigTableBuilder.toTable(db);
@@ -566,7 +563,7 @@ public class DatabaseTest extends TestCase {
Database db = create(fileFormat);
Table table = new TableBuilder("test")
- .addColumn(new ColumnBuilder("A", DataType.MONEY).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.MONEY))
.toTable(db);
table.addRow(new BigDecimal("-2341234.03450"));
@@ -602,7 +599,7 @@ public class DatabaseTest extends TestCase {
Database db = create(fileFormat);
Table table = new TableBuilder("test")
- .addColumn(new ColumnBuilder("A", DataType.GUID).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.GUID))
.toTable(db);
table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}");
@@ -648,7 +645,7 @@ public class DatabaseTest extends TestCase {
Table table = new TableBuilder("test")
.addColumn(col)
.addColumn(new ColumnBuilder("B", DataType.NUMERIC)
- .setScale(8).setPrecision(28).toColumn())
+ .setScale(8).setPrecision(28))
.toTable(db);
table.addRow(new BigDecimal("-1234.03450"),
@@ -776,9 +773,9 @@ public class DatabaseTest extends TestCase {
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
Database db = create(fileFormat);
Table newTable = new TableBuilder("NewTable")
- .addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER).toColumn())
- .addColumn(new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR).toColumn())
- .addColumn(new ColumnBuilder("c").setSQLType(Types.VARCHAR).toColumn())
+ .addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER))
+ .addColumn(new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR))
+ .addColumn(new ColumnBuilder("c").setSQLType(Types.VARCHAR))
.toTable(db);
String lval = createString(2000); // "--2000 chars long text--";
@@ -868,8 +865,8 @@ public class DatabaseTest extends TestCase {
Table table = new TableBuilder("test")
.addColumn(new ColumnBuilder("a", DataType.LONG)
- .setAutoNumber(true).toColumn())
- .addColumn(new ColumnBuilder("b", DataType.TEXT).toColumn())
+ .setAutoNumber(true))
+ .addColumn(new ColumnBuilder("b", DataType.TEXT))
.toTable(db);
doTestAutoNumber(table);
@@ -929,9 +926,8 @@ public class DatabaseTest extends TestCase {
Database db = create(fileFormat);
Table table = new TableBuilder("test")
- .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME)
- .toColumn())
+ .addColumn(new ColumnBuilder("name", DataType.TEXT))
+ .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME))
.toTable(db);
// since jackcess does not really store millis, shave them off before
@@ -1025,11 +1021,11 @@ public class DatabaseTest extends TestCase {
Database db = create(fileFormat);
Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn())
+ .addColumn(new ColumnBuilder("name", DataType.TEXT))
.addColumn(new ColumnBuilder("id", DataType.LONG)
- .setAutoNumber(true).toColumn())
+ .setAutoNumber(true))
.addColumn(new ColumnBuilder("data", DataType.TEXT)
- .setLength(JetFormat.TEXT_FIELD_MAX_LENGTH).toColumn())
+ .setLength(JetFormat.TEXT_FIELD_MAX_LENGTH))
.toTable(db);
for(int i = 0; i < 10; ++i) {
@@ -1145,15 +1141,15 @@ public class DatabaseTest extends TestCase {
static void createTestTable(Database db) throws Exception {
new TableBuilder("test")
- .addColumn(new ColumnBuilder("A", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("B", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("C", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("D", DataType.LONG).toColumn())
- .addColumn(new ColumnBuilder("E", DataType.BYTE).toColumn())
- .addColumn(new ColumnBuilder("F", DataType.DOUBLE).toColumn())
- .addColumn(new ColumnBuilder("G", DataType.FLOAT).toColumn())
- .addColumn(new ColumnBuilder("H", DataType.INT).toColumn())
- .addColumn(new ColumnBuilder("I", DataType.SHORT_DATE_TIME).toColumn())
+ .addColumn(new ColumnBuilder("A", DataType.TEXT))
+ .addColumn(new ColumnBuilder("B", DataType.TEXT))
+ .addColumn(new ColumnBuilder("C", DataType.TEXT))
+ .addColumn(new ColumnBuilder("D", DataType.LONG))
+ .addColumn(new ColumnBuilder("E", DataType.BYTE))
+ .addColumn(new ColumnBuilder("F", DataType.DOUBLE))
+ .addColumn(new ColumnBuilder("G", DataType.FLOAT))
+ .addColumn(new ColumnBuilder("H", DataType.INT))
+ .addColumn(new ColumnBuilder("I", DataType.SHORT_DATE_TIME))
.toTable(db);
}
diff --git a/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java b/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java
index 488f6bd..afffdd5 100644
--- a/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java
@@ -55,8 +55,8 @@ public class ErrorHandlerTest extends TestCase
Table table =
new TableBuilder("test")
- .addColumn(new ColumnBuilder("col", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("val", DataType.LONG).toColumn())
+ .addColumn(new ColumnBuilder("col", DataType.TEXT))
+ .addColumn(new ColumnBuilder("val", DataType.LONG))
.toTable(db);
table.addRow("row1", 1);
diff --git a/test/src/java/com/healthmarketscience/jackcess/ExportTest.java b/test/src/java/com/healthmarketscience/jackcess/ExportTest.java
index a0528f4..062cc8a 100644
--- a/test/src/java/com/healthmarketscience/jackcess/ExportTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/ExportTest.java
@@ -59,12 +59,12 @@ public class ExportTest extends TestCase
Database db = create(fileFormat);
Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("col1", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("col2", DataType.LONG).toColumn())
- .addColumn(new ColumnBuilder("col3", DataType.DOUBLE).toColumn())
- .addColumn(new ColumnBuilder("col4", DataType.OLE).toColumn())
- .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN).toColumn())
- .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME).toColumn())
+ .addColumn(new ColumnBuilder("col1", DataType.TEXT))
+ .addColumn(new ColumnBuilder("col2", DataType.LONG))
+ .addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
+ .addColumn(new ColumnBuilder("col4", DataType.OLE))
+ .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
+ .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME))
.toTable(db);
t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(), true, df.parse("19801231 00:00:00"));
diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java
index 7e0d722..53370a4 100644
--- a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java
@@ -146,8 +146,8 @@ public class IndexCodesTest extends TestCase {
Database db = create(Database.FileFormat.V2000, true);
Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("row", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn())
+ .addColumn(new ColumnBuilder("row", DataType.TEXT))
+ .addColumn(new ColumnBuilder("data", DataType.TEXT))
.toTable(db);
for(int i = 0; i < 256; ++i) {
@@ -179,8 +179,8 @@ public class IndexCodesTest extends TestCase {
Database db = create(Database.FileFormat.V2000, true);
// Table t = new TableBuilder("Table1")
-// .addColumn(new ColumnBuilder("key", DataType.TEXT).toColumn())
-// .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn())
+// .addColumn(new ColumnBuilder("key", DataType.TEXT))
+// .addColumn(new ColumnBuilder("data", DataType.TEXT))
// .toTable(db);
// for(int i = 0; i <= 0xFFFF; ++i) {
@@ -195,8 +195,8 @@ public class IndexCodesTest extends TestCase {
// }
Table t = new TableBuilder("Table5")
- .addColumn(new ColumnBuilder("name", DataType.TEXT).toColumn())
- .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn())
+ .addColumn(new ColumnBuilder("name", DataType.TEXT))
+ .addColumn(new ColumnBuilder("data", DataType.TEXT))
.toTable(db);
char c = (char)0x3041; // crazy 7F 02 ... A0
@@ -218,13 +218,13 @@ public class IndexCodesTest extends TestCase {
addCombos(t, 0, "", cs, 5);
// t = new TableBuilder("Table2")
-// .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn())
+// .addColumn(new ColumnBuilder("data", DataType.TEXT))
// .toTable(db);
// writeChars(0x0000, t);
// t = new TableBuilder("Table3")
-// .addColumn(new ColumnBuilder("data", DataType.TEXT).toColumn())
+// .addColumn(new ColumnBuilder("data", DataType.TEXT))
// .toTable(db);
// writeChars(0x0400, t);