Skip to content

Commit

Permalink
Merge pull request #123 from mekjaer/develop
Browse files Browse the repository at this point in the history
added possibility to specify name of table in typed interface.
  • Loading branch information
mekjaer committed Sep 20, 2013
2 parents ca3e844 + 178d33a commit 488e1dd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/changes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
19-September-2013:
==================
+ Multiple tables of same type in typed interface can be added to same group, by specifying a custom name in the constructor.

10-September-2013:
==================
- Deprecated ReadTransaction.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public abstract class AbstractTable<Cursor, View, Query> extends AbstractTableOr
public AbstractTable(EntityTypes<?, View, Cursor, Query> types) {
this(types, new Table());
}

/**
* Can be used to specify a different name than the class. Allows for more of same type in typed interface
* @param types
* @param group
* @param tableName
*/
public AbstractTable(EntityTypes<?, View, Cursor, Query> types, Group group, String tableName) {
this(types, group.getTable(tableName));
}

public AbstractTable(EntityTypes<?, View, Cursor, Query> types, Group group) {
this(types, group.getTable(types.getTableClass().getSimpleName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* This class is automatically generated from the .ftl templates */
public class Templates {
public static final String TABLE = "${java_header}\r\n<#if packageName?has_content>\r\npackage ${packageName};\r\n</#if>\r\n\r\nimport com.tightdb.*;\r\nimport com.tightdb.typed.*;\r\n\r\n/**\r\n * This class represents a TightDB table and was automatically generated.\r\n */\r\n<#if isNested>public class ${tableName} extends AbstractSubtable<${cursorName}, ${viewName}, ${queryName}> {\r\n<#else>public class ${tableName} extends AbstractTable<${cursorName}, ${viewName}, ${queryName}> {\r\n</#if>\r\n\tpublic static final EntityTypes<${tableName}, ${viewName}, ${cursorName}, ${queryName}> TYPES = new EntityTypes<${tableName}, ${viewName}, ${cursorName}, ${queryName}>(${tableName}.class, ${viewName}.class, ${cursorName}.class, ${queryName}.class);\r\n\r\n<#foreach f in columns><#if f.isSubtable>\tpublic final ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}, ${f.subTableName}> ${f.name} = new ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}, ${f.subTableName}>(TYPES, table, ${f.index}, \"${f.name}\", ${f.subTableName}.class);\r\n<#else>\tpublic final ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}> ${f.name} = new ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}>(TYPES, table, ${f.index}, \"${f.name}\");\r\n</#if></#foreach>\r\n<#if isNested>\tpublic ${tableName}(Table subtable) {\r\n\t\tsuper(TYPES, subtable);\r\n\t}\r\n<#else>\tpublic ${tableName}() {\r\n\t\tsuper(TYPES);\r\n\t}\r\n\r\n\tpublic ${tableName}(Group group) {\r\n\t\tsuper(TYPES, group);\r\n\t}\r\n</#if>\r\n\tpublic static void specifyStructure(TableSpec spec) {\r\n<#foreach f in columns><#if f.isSubtable> add${f.type}Column(spec, \"${f.name}\", new ${f.subTableName}(null));\r\n<#else> add${f.type}Column(spec, \"${f.name}\");\r\n</#if></#foreach> }\r\n\r\n${add}\r\n\r\n${insert}\r\n\r\n}\r\n";
public static final String TABLE = "${java_header}\r\n<#if packageName?has_content>\r\npackage ${packageName};\r\n</#if>\r\n\r\nimport com.tightdb.*;\r\nimport com.tightdb.typed.*;\r\n\r\n/**\r\n * This class represents a TightDB table and was automatically generated.\r\n */\r\n<#if isNested>public class ${tableName} extends AbstractSubtable<${cursorName}, ${viewName}, ${queryName}> {\r\n<#else>public class ${tableName} extends AbstractTable<${cursorName}, ${viewName}, ${queryName}> {\r\n</#if>\r\n\tpublic static final EntityTypes<${tableName}, ${viewName}, ${cursorName}, ${queryName}> TYPES = new EntityTypes<${tableName}, ${viewName}, ${cursorName}, ${queryName}>(${tableName}.class, ${viewName}.class, ${cursorName}.class, ${queryName}.class);\r\n\r\n<#foreach f in columns><#if f.isSubtable>\tpublic final ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}, ${f.subTableName}> ${f.name} = new ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}, ${f.subTableName}>(TYPES, table, ${f.index}, \"${f.name}\", ${f.subTableName}.class);\r\n<#else>\tpublic final ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}> ${f.name} = new ${f.type}TableColumn<${cursorName}, ${viewName}, ${queryName}>(TYPES, table, ${f.index}, \"${f.name}\");\r\n</#if></#foreach>\r\n<#if isNested>\tpublic ${tableName}(Table subtable) {\r\n\t\tsuper(TYPES, subtable);\r\n\t}\r\n<#else>\tpublic ${tableName}() {\r\n\t\tsuper(TYPES);\r\n\t}\r\n\r\n\tpublic ${tableName}(Group group) {\r\n\t\tsuper(TYPES, group);\r\n\t}\r\n\t\r\n\tpublic ${tableName}(Group group, String tableName) {\r\n\t\tsuper(TYPES, group, tableName);\r\n\t}\r\n</#if>\r\n\tpublic static void specifyStructure(TableSpec spec) {\r\n<#foreach f in columns><#if f.isSubtable> add${f.type}Column(spec, \"${f.name}\", new ${f.subTableName}(null));\r\n<#else> add${f.type}Column(spec, \"${f.name}\");\r\n</#if></#foreach> }\r\n\r\n${add}\r\n\r\n${insert}\r\n\r\n}\r\n";
public static final String TABLE_ADD = " public ${cursorName} add(<#foreach f in columns><#if (f_index > 0)>, </#if>${f.paramType} ${f.name}</#foreach>) {\r\n return insert(size()<#foreach f in columns>, ${f.name}</#foreach>);\r\n }";
public static final String TABLE_INSERT = " public ${cursorName} insert(long position<#foreach f in columns>, ${f.paramType} ${f.name}</#foreach>) {\r\n try {\r\n <#foreach f in columns><#if f.isSubtable> insert${f.type}(${f.index}, position, ${f.name});\r\n <#else>\tinsert${f.type}(${f.index}, position, ${f.name});\r\n </#if></#foreach> insertDone();\r\n\r\n return cursor(position);\r\n } catch (Exception e) {\r\n throw new RuntimeException(\"Error occured while adding/inserting a new row!\", e);\r\n }\r\n }";
public static final String CURSOR = "${java_header}\r\n<#if packageName?has_content>\r\npackage ${packageName};\r\n</#if>\r\n\r\nimport com.tightdb.*;\r\nimport com.tightdb.typed.*;\r\n\r\n/**\r\n * This class represents a TightDB cursor and was automatically generated.\r\n */\r\npublic class ${name} extends AbstractCursor<${name}> {\r\n\r\n<#foreach f in columns><#if f.isSubtable> public final ${f.type}CursorColumn<${name}, ${viewName}, ${queryName}, ${f.subCursorName}, ${f.subTableName}> ${f.name};\r\n<#else> public final ${f.type}CursorColumn<${cursorName}, ${viewName}, ${queryName}> ${f.name};\r\n</#if></#foreach>\r\n\tpublic ${cursorName}(TableOrView tableOrView, long position) {\r\n\t\tsuper(${tableName}.TYPES, tableOrView, position);\r\n\r\n<#foreach f in columns><#if f.isSubtable> ${f.name} = new ${f.type}CursorColumn<${cursorName}, ${viewName}, ${queryName}, ${f.subCursorName}, ${f.subTableName}>(${tableName}.TYPES, this, ${f.index}, \"${f.name}\", ${f.subTableName}.class);\r\n<#else> ${f.name} = new ${f.type}CursorColumn<${cursorName}, ${viewName}, ${queryName}>(${tableName}.TYPES, this, ${f.index}, \"${f.name}\");\r\n</#if></#foreach>\t}\r\n\r\n<#foreach f in columns><#if f.isSubtable>\tpublic ${f.subTableName} get${f.name?cap_first}() {\r\n<#else>\tpublic ${f.fieldType} get${f.name?cap_first}() {\r\n</#if>\t\treturn this.${f.name}.get();\r\n\t}\r\n\r\n<#if f.isSubtable>\tpublic void set${f.name?cap_first}(${f.subTableName} ${f.name}) {\r\n<#else>\tpublic void set${f.name?cap_first}(${f.fieldType} ${f.name}) {\r\n</#if>\t\tthis.${f.name}.set(${f.name});\r\n\t}\r\n\r\n</#foreach>\t@Override\r\n\tpublic AbstractColumn<?, ?, ?, ?>[] columns() {\r\n\t\treturn getColumnsArray(<#foreach f in columns>${f.name}<#if f_has_next>, </#if></#foreach>);\r\n\t}\r\n\r\n public void set(<#foreach f in columns><#if (f_index > 0)>, </#if><#if f.isSubtable>${f.subTableName}<#else>${f.fieldType}</#if> ${f.name}</#foreach>) {\r\n<#foreach f in columns>\r\n this.${f.name}.set(${f.name});\r\n</#foreach>\r\n }\r\n \r\n}\r\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import com.tightdb.typed.*;
public ${tableName}(Group group) {
super(TYPES, group);
}

public ${tableName}(Group group, String tableName) {
super(TYPES, group, tableName);
}
</#if>
public static void specifyStructure(TableSpec spec) {
<#foreach f in columns><#if f.isSubtable> add${f.type}Column(spec, "${f.name}", new ${f.subTableName}(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void shouldCreateTablesInGroup() {
group3.close();

}


@Test
public void testHasTable() {
Group group = new Group();
assertEquals(group.hasTable(null), false);
Expand Down
28 changes: 28 additions & 0 deletions tightdb-java-test/src/test/java/com/tightdb/typed/TableTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.tightdb.typed;

import static org.testng.AssertJUnit.assertEquals;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.nio.ByteBuffer;
import java.util.Date;

import com.tightdb.Group;
import com.tightdb.Mixed;
import com.tightdb.test.TestEmployeeTable;

Expand Down Expand Up @@ -39,6 +41,32 @@ public void clear() {
employees.clear();
assertEquals(true, employees.isEmpty());
}


@Test
public void multipleTablesOfSameTypeInGroup() {
Group group = new Group();

TestEmployeeTable t0 = new TestEmployeeTable(group);

assertEquals(1, group.size());
assertEquals("TestEmployeeTable", group.getTableName(0));


TestEmployeeTable t1 = new TestEmployeeTable(group, "t1");
TestEmployeeTable t2 = new TestEmployeeTable(group, "t2");

t2.add("NoName", "Test Mixed Binary", 1, true, new byte[] { 1, 2, 3 }, new Date(), new byte[] { 3, 2, 1 },null);

assertEquals(3, group.size());

assertEquals("t1", group.getTableName(1));
assertEquals("t2", group.getTableName(2));


TestEmployeeTable t2Out = new TestEmployeeTable(group, "t2");
assertEquals("NoName", t2Out.get(0).getFirstName());
}

@Test
public void shouldRetrieveRowsByIndex() {
Expand Down

0 comments on commit 488e1dd

Please sign in to comment.