Define getRelationID on Access

This helps callers that want to know what the identity is for a
given relation when its wrapped up in a protobuf.x

Change-Id: I5c0730a832c44be59a55f01cf93c1ff5ee9f0832
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/main/java/com/google/gwtorm/client/Access.java b/src/main/java/com/google/gwtorm/client/Access.java
index c989135..27a26f7 100644
--- a/src/main/java/com/google/gwtorm/client/Access.java
+++ b/src/main/java/com/google/gwtorm/client/Access.java
@@ -50,6 +50,9 @@
   /** @return the name of this relation. */
   String getRelationName();
 
+  /** @return the id of this relation (if defined), otherwise 0. */
+  int getRelationID();
+
   /**
    * Iterate through all members of the relation.
    *
diff --git a/src/main/java/com/google/gwtorm/jdbc/AccessGen.java b/src/main/java/com/google/gwtorm/jdbc/AccessGen.java
index 573e8d2..4be2247 100644
--- a/src/main/java/com/google/gwtorm/jdbc/AccessGen.java
+++ b/src/main/java/com/google/gwtorm/jdbc/AccessGen.java
@@ -83,6 +83,7 @@
     implementConstructor();
     implementGetString("getRelationName", model.getRelationName());
     implementGetString("getInsertOneSql", model.getInsertOneSql(dialect));
+    implementGetRelationID();
 
     if (model.getPrimaryKey() != null) {
       if (model.getDependentColumns().isEmpty()) {
@@ -173,6 +174,17 @@
     mv.visitEnd();
   }
 
+  private void implementGetRelationID() {
+    final MethodVisitor mv =
+        cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getRelationID", Type
+            .getMethodDescriptor(Type.INT_TYPE, new Type[] {}), null, null);
+    mv.visitCode();
+    new CodeGenSupport(mv).push(model.getRelationID());
+    mv.visitInsn(IRETURN);
+    mv.visitMaxs(-1, -1);
+    mv.visitEnd();
+  }
+
   private void implementMissingGetString(final String methodName,
       final String why) {
     final MethodVisitor mv =
diff --git a/src/main/java/com/google/gwtorm/nosql/AccessGen.java b/src/main/java/com/google/gwtorm/nosql/AccessGen.java
index 71828c4..d2fffe7 100644
--- a/src/main/java/com/google/gwtorm/nosql/AccessGen.java
+++ b/src/main/java/com/google/gwtorm/nosql/AccessGen.java
@@ -97,6 +97,7 @@
     implementStaticFields();
     implementConstructor();
     implementGetString("getRelationName", model.getRelationName());
+    implementGetRelationID();
     implementGetObjectCodec();
     implementGetIndexes();
 
@@ -238,6 +239,17 @@
     mv.visitEnd();
   }
 
+  private void implementGetRelationID() {
+    final MethodVisitor mv =
+        cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getRelationID", Type
+            .getMethodDescriptor(Type.INT_TYPE, new Type[] {}), null, null);
+    mv.visitCode();
+    new CodeGenSupport(mv).push(model.getRelationID());
+    mv.visitInsn(IRETURN);
+    mv.visitMaxs(-1, -1);
+    mv.visitEnd();
+  }
+
   private void implementGetObjectCodec() {
     final MethodVisitor mv =
         cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getObjectCodec", Type
diff --git a/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java b/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java
index 7c221b5..55fd759 100644
--- a/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java
+++ b/src/test/java/com/google/gwtorm/server/PhoneBookDbTestCase.java
@@ -102,12 +102,14 @@
     final PhoneBookDb schema = open();
     assertNotNull(schema.people());
     assertEquals("people", schema.people().getRelationName());
+    assertEquals(1, schema.people().getRelationID());
   }
 
   public void testGetAddressAccess() throws Exception {
     final PhoneBookDb schema = open();
     assertNotNull(schema.addresses());
     assertEquals("addresses", schema.addresses().getRelationName());
+    assertEquals(2, schema.addresses().getRelationID());
   }
 
   public void testGetAllRelations() throws Exception {