From 5f93a558aefae1d95fef260240c64fe1011e1e56 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Sun, 15 May 2011 21:47:53 +0000 Subject: check for too many pk indexes on a table; add convenience method to TableBuilder for adding pk index git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@562 f203690c-595d-4dc9-a70b-905162fa7fd2 --- src/java/com/healthmarketscience/jackcess/Database.java | 8 ++++++++ src/java/com/healthmarketscience/jackcess/TableBuilder.java | 10 ++++++++++ 2 files changed, 18 insertions(+) (limited to 'src/java/com') diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index 3029881..96d7aaa 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -1145,12 +1145,20 @@ public class Database if(!indexes.isEmpty()) { // now, validate the indexes Set idxNames = new HashSet(); + boolean foundPk = false; for(IndexBuilder index : indexes) { index.validate(colNames); if(!idxNames.add(index.getName().toUpperCase())) { throw new IllegalArgumentException("duplicate index name: " + index.getName()); } + if(index.isPrimaryKey()) { + if(foundPk) { + throw new IllegalArgumentException( + "found second primary key index: " + index.getName()); + } + foundPk = true; + } } } diff --git a/src/java/com/healthmarketscience/jackcess/TableBuilder.java b/src/java/com/healthmarketscience/jackcess/TableBuilder.java index 740a684..51e8697 100644 --- a/src/java/com/healthmarketscience/jackcess/TableBuilder.java +++ b/src/java/com/healthmarketscience/jackcess/TableBuilder.java @@ -102,6 +102,16 @@ public class TableBuilder { return this; } + /** + * Sets the names of the primary key columns for this table. Convenience + * method for creating a primary key index on a table. + */ + public TableBuilder setPrimaryKey(String... colNames) { + addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME) + .addColumns(colNames).setPrimaryKey()); + return this; + } + /** * Escapes the new table's name using {@link Database#escapeIdentifier}. */ -- cgit v1.2.3