killbill-aplcache
Details
pom.xml 1(+0 -1)
diff --git a/pom.xml b/pom.xml
index ddd39cd..c5d52cb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -322,7 +322,6 @@
<groupId>org.antlr</groupId>
<artifactId>stringtemplate</artifactId>
<version>3.2.1</version>
- <scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
util/pom.xml 1(+0 -1)
diff --git a/util/pom.xml b/util/pom.xml
index e3fb81a..a767483 100644
--- a/util/pom.xml
+++ b/util/pom.xml
@@ -85,7 +85,6 @@
<dependency>
<groupId>org.antlr</groupId>
<artifactId>stringtemplate</artifactId>
- <scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
diff --git a/util/src/main/resources/com/ning/billing/util/dao/EntitySqlDao.sql.stg b/util/src/main/resources/com/ning/billing/util/dao/EntitySqlDao.sql.stg
new file mode 100644
index 0000000..da86faf
--- /dev/null
+++ b/util/src/main/resources/com/ning/billing/util/dao/EntitySqlDao.sql.stg
@@ -0,0 +1,173 @@
+group EntitySqlDao;
+
+/** To override in each EntitySqlDao template file **/
+
+tableName() ::= ""
+
+/** Leave out id, account_record_id and tenant_record_id */
+tableFields(prefix) ::= ""
+
+tableValues() ::= ""
+
+historyTableName() ::= ""
+
+historyTableFields(prefix) ::= "<tableFields(prefix)>"
+
+historyTableValues() ::= "<tableValues()>"
+
+/****************************************************/
+
+idField(prefix) ::= <<
+<prefix>id
+>>
+
+recordIdField(prefix) ::= <<
+<prefix>record_id
+>>
+
+historyRecordIdField(prefix) ::= <<
+<prefix>history_record_id
+>>
+
+/** Override this if the Entity isn't tied to an account */
+accountRecordIdField(prefix) ::= <<
+<prefix>account_record_id
+>>
+
+accountRecordIdValue() ::= ":accountRecordId"
+
+tenantRecordIdField(prefix) ::= <<
+<prefix>tenant_record_id
+>>
+
+tenantRecordIdValue() ::= ":tenantRecordId"
+
+allTableFields(prefix) ::= <<
+ <recordIdField(prefix)>
+, <idField(prefix)>
+, <tableFields(prefix)>
+<if(accountRecordIdField(prefix))>, <accountRecordIdField(prefix)><endif>
+, <tenantRecordIdField(prefix)>
+>>
+
+allHistoryTableFields(prefix) ::= <<
+ <recordIdField(prefix)>
+, <idField(prefix)>
+, <historyTableFields(prefix)>
+<if(accountRecordIdField(prefix))>, <accountRecordIdField(prefix)><endif>
+, <tenantRecordIdField(prefix)>
+>>
+
+allHistoryTableValues() ::= <<
+ :recordId
+, :id
+, <historyTableValues()>
+<if(accountRecordIdField(""))>, <accountRecordIdValue()><endif>
+<if(tenantRecordIdField(""))>, <tenantRecordIdValue()><endif>
+>>
+
+auditTableName() ::= "audit_log"
+
+auditTableFields(prefix) ::= <<
+ <prefix>table_name
+, <prefix>record_id
+, <prefix>change_type
+, <prefix>change_date
+, <prefix>changed_by
+, <prefix>reason_code
+, <prefix>comments
+, <prefix>user_token
+, <accountRecordIdField(prefix)>
+, <tenantRecordIdField(prefix)>
+>>
+
+auditTableValues(prefix) ::= <<
+ :table_name
+, :record_id
+, :change_type
+, :change_date
+, :changed_by
+, :reason_code
+, :comments
+, :user_token
+, <accountRecordIdValue()>
+, <tenantRecordIdValue()>
+>>
+
+/** Macros used for multi-tenancy (almost any query should use them!) */
+CHECK_TENANT(prefix) ::= "<prefix>tenant_record_id = :tenantRecordId"
+AND_CHECK_TENANT(prefix) ::= "and <CHECK_TENANT(prefix)>"
+
+getById(id) ::= <<
+select
+<allTableFields("t.")>
+from <tableName()> t
+where <idField("t.")> = :id
+<AND_CHECK_TENANT("t.")>
+;
+>>
+
+getByRecordId(recordId) ::= <<
+select
+<allTableFields("t.")>
+from <tableName()> t
+where <recordIdField("t.")> = :recordId
+<AND_CHECK_TENANT("t.")>
+;
+>>
+
+getRecordId(id) ::= <<
+select
+ <recordIdField("t.")>
+from <tableName()> t
+where <idField("t.")> = :id
+<AND_CHECK_TENANT("t.")>
+;
+>>
+
+getHistoryRecordId(recordId) ::= <<
+select
+ max(<historyRecordIdField("t.")>)
+from <tableName()> t
+where <recordIdField("t.")> = :recordId
+<AND_CHECK_TENANT("t.")>
+;
+>>
+
+get(limit) ::= <<
+select
+<allTableFields("t.")>
+from <tableName()> t
+where <CHECK_TENANT("t.")>
+<if(limit)>limit :limit<endif>
+;
+>>
+
+test() ::= <<
+select
+<allTableFields("t.")>
+from <tableName()> t
+where <CHECK_TENANT("t.")>
+limit 1
+;
+>>
+
+addHistoryFromTransaction() ::= <<
+insert into <historyTableName()> (
+<allHistoryTableFields()>
+)
+values (
+<allHistoryTableValues()>
+)
+;
+>>
+
+insertAuditFromTransaction() ::= <<
+insert into <auditTableName()> (
+<auditTableFields()>
+)
+values (
+<auditTableValues()>
+)
+;
+>>
\ No newline at end of file
diff --git a/util/src/test/java/com/ning/billing/util/dao/TestStringTemplateInheritance.java b/util/src/test/java/com/ning/billing/util/dao/TestStringTemplateInheritance.java
new file mode 100644
index 0000000..0e01612
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/dao/TestStringTemplateInheritance.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.dao;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.antlr.stringtemplate.StringTemplateGroup;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.ning.billing.util.UtilTestSuite;
+
+import com.google.common.collect.ImmutableMap;
+
+public class TestStringTemplateInheritance extends UtilTestSuite {
+
+ InputStream entityStream;
+ InputStream kombuchaStream;
+
+ @BeforeMethod
+ public void setUp() throws Exception {
+ entityStream = this.getClass().getResourceAsStream("/com/ning/billing/util/dao/EntitySqlDao.sql.stg");
+ kombuchaStream = this.getClass().getResourceAsStream("/com/ning/billing/util/dao/Kombucha.sql.stg");
+ }
+
+ @AfterMethod
+ public void tearDown() throws Exception {
+ if (entityStream != null) {
+ entityStream.close();
+ }
+ if (kombuchaStream != null) {
+ kombuchaStream.close();
+ }
+ }
+
+ @Test(groups = "fast")
+ public void testCheckQueries() throws Exception {
+ // From http://www.antlr.org/wiki/display/ST/ST+condensed+--+Templates+and+groups#STcondensed--Templatesandgroups-Withsupergroupfile:
+ // there is no mechanism for automatically loading a mentioned super-group file
+ new StringTemplateGroup(new InputStreamReader(entityStream));
+
+ final StringTemplateGroup kombucha = new StringTemplateGroup(new InputStreamReader(kombuchaStream));
+
+ // Verify non inherited template
+ Assert.assertEquals(kombucha.getInstanceOf("isIsTimeForKombucha").toString(), "select hour(current_timestamp()) = 17 as is_time;");
+
+ // Verify inherited templates
+ Assert.assertEquals(kombucha.getInstanceOf("getById").toString(), "select\n" +
+ " t.record_id\n" +
+ ", t.id\n" +
+ ", t.tea\n" +
+ ", t.mushroom\n" +
+ ", t.sugar\n" +
+ ", t.account_record_id\n" +
+ ", t.tenant_record_id\n" +
+ "from kombucha t\n" +
+ "where t.id = :id\n" +
+ "and t.tenant_record_id = :tenantRecordId\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("getByRecordId").toString(), "select\n" +
+ " t.record_id\n" +
+ ", t.id\n" +
+ ", t.tea\n" +
+ ", t.mushroom\n" +
+ ", t.sugar\n" +
+ ", t.account_record_id\n" +
+ ", t.tenant_record_id\n" +
+ "from kombucha t\n" +
+ "where t.record_id = :recordId\n" +
+ "and t.tenant_record_id = :tenantRecordId\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("getRecordId").toString(), "select\n" +
+ " t.record_id\n" +
+ "from kombucha t\n" +
+ "where t.id = :id\n" +
+ "and t.tenant_record_id = :tenantRecordId\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("getHistoryRecordId").toString(), "select\n" +
+ " max(t.history_record_id)\n" +
+ "from kombucha t\n" +
+ "where t.record_id = :recordId\n" +
+ "and t.tenant_record_id = :tenantRecordId\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("get").toString(), "select\n" +
+ " t.record_id\n" +
+ ", t.id\n" +
+ ", t.tea\n" +
+ ", t.mushroom\n" +
+ ", t.sugar\n" +
+ ", t.account_record_id\n" +
+ ", t.tenant_record_id\n" +
+ "from kombucha t\n" +
+ "where t.tenant_record_id = :tenantRecordId\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("get", ImmutableMap.<String, String>of("limit", "12")).toString(), "select\n" +
+ " t.record_id\n" +
+ ", t.id\n" +
+ ", t.tea\n" +
+ ", t.mushroom\n" +
+ ", t.sugar\n" +
+ ", t.account_record_id\n" +
+ ", t.tenant_record_id\n" +
+ "from kombucha t\n" +
+ "where t.tenant_record_id = :tenantRecordId\n" +
+ "limit :limit\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("test").toString(), "select\n" +
+ " t.record_id\n" +
+ ", t.id\n" +
+ ", t.tea\n" +
+ ", t.mushroom\n" +
+ ", t.sugar\n" +
+ ", t.account_record_id\n" +
+ ", t.tenant_record_id\n" +
+ "from kombucha t\n" +
+ "where t.tenant_record_id = :tenantRecordId\n" +
+ "limit 1\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("addHistoryFromTransaction").toString(), "insert into kombucha_history (\n" +
+ " record_id\n" +
+ ", id\n" +
+ ", tea\n" +
+ ", mushroom\n" +
+ ", sugar\n" +
+ ", account_record_id\n" +
+ ", tenant_record_id\n" +
+ ")\n" +
+ "values (\n" +
+ " :recordId\n" +
+ ", :id\n" +
+ ", :tea\n" +
+ ", :mushroom\n" +
+ ", :sugar\n" +
+ ", :accountRecordId\n" +
+ ", :tenantRecordId\n" +
+ ")\n" +
+ ";");
+ Assert.assertEquals(kombucha.getInstanceOf("insertAuditFromTransaction").toString(), "insert into audit_log (\n" +
+ "table_name\n" +
+ ", record_id\n" +
+ ", change_type\n" +
+ ", change_date\n" +
+ ", changed_by\n" +
+ ", reason_code\n" +
+ ", comments\n" +
+ ", user_token\n" +
+ ", account_record_id\n" +
+ ", tenant_record_id\n" +
+ ")\n" +
+ "values (\n" +
+ " :table_name\n" +
+ ", :record_id\n" +
+ ", :change_type\n" +
+ ", :change_date\n" +
+ ", :changed_by\n" +
+ ", :reason_code\n" +
+ ", :comments\n" +
+ ", :user_token\n" +
+ ", :accountRecordId\n" +
+ ", :tenantRecordId\n" +
+ ")\n" +
+ ";");
+ }
+}
diff --git a/util/src/test/resources/com/ning/billing/util/dao/Kombucha.sql.stg b/util/src/test/resources/com/ning/billing/util/dao/Kombucha.sql.stg
new file mode 100644
index 0000000..4fe6ff0
--- /dev/null
+++ b/util/src/test/resources/com/ning/billing/util/dao/Kombucha.sql.stg
@@ -0,0 +1,21 @@
+group Kombucha: EntitySqlDao;
+
+tableName() ::= "kombucha"
+
+historyTableName() ::= "kombucha_history"
+
+tableFields(prefix) ::= <<
+ <prefix>tea
+, <prefix>mushroom
+, <prefix>sugar
+>>
+
+tableValues() ::= <<
+ :tea
+, :mushroom
+, :sugar
+>>
+
+isIsTimeForKombucha() ::= <<
+select hour(current_timestamp()) = 17 as is_time;
+>>
\ No newline at end of file