]> source.dussan.org Git - jackcess.git/commitdiff
check for too many pk indexes on a table; add convenience method to TableBuilder...
authorJames Ahlborn <jtahlborn@yahoo.com>
Sun, 15 May 2011 21:47:53 +0000 (21:47 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Sun, 15 May 2011 21:47:53 +0000 (21:47 +0000)
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
src/java/com/healthmarketscience/jackcess/TableBuilder.java

index 3029881ce1a17309ce4f17a0fa09acbd2bd226ea..96d7aaad3b16b3966097d5e2c751e967c3f07763 100644 (file)
@@ -1145,12 +1145,20 @@ public class Database
     if(!indexes.isEmpty()) {
       // now, validate the indexes
       Set<String> idxNames = new HashSet<String>();
+      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;
+        }
       }
     }
     
index 740a68464109dd3ada6e20a8fe4661b26f7717c8..51e8697c29896c896bb90f12adcda5a4857e46e0 100644 (file)
@@ -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}.
    */