diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2010-07-16 04:19:59 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2010-07-16 04:19:59 +0000 |
commit | 4a0bbd462cb4a1bed43934cefdf1087ed360b128 (patch) | |
tree | 56fc297b90a8bc57362bfccc4bca93a87e6cdabc /src/java | |
parent | 33165ebc3a26a85e7b2474a249a1a4caecee4471 (diff) | |
download | jackcess-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
Diffstat (limited to 'src/java')
4 files changed, 55 insertions, 7 deletions
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. */ |