From fbb9cdefe1e66c596757c6f5923a264033076d77 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Wed, 7 Sep 2016 16:19:14 +0800 Subject: [PATCH] --- src/META-INF/persistence-template.xml | 47 +++++++++++++++++ src/org/redkale/source/DataDefaultSource.java | 50 ++++++++++++++----- src/org/redkale/source/DistributeTable.java | 24 +++++++++ .../source/DistributeTableStrategy.java | 29 +++++++++++ src/org/redkale/source/EntityInfo.java | 43 +++++++++++++--- 5 files changed, 174 insertions(+), 19 deletions(-) create mode 100644 src/META-INF/persistence-template.xml create mode 100644 src/org/redkale/source/DistributeTable.java create mode 100644 src/org/redkale/source/DistributeTableStrategy.java diff --git a/src/META-INF/persistence-template.xml b/src/META-INF/persistence-template.xml new file mode 100644 index 000000000..c674c631c --- /dev/null +++ b/src/META-INF/persistence-template.xml @@ -0,0 +1,47 @@ + + + + + + + NONE + + + + + + + + + + + + + + + + + + + + + + + NONE + + + + + + + + + \ No newline at end of file diff --git a/src/org/redkale/source/DataDefaultSource.java b/src/org/redkale/source/DataDefaultSource.java index b18b4fb8c..70d53391f 100644 --- a/src/org/redkale/source/DataDefaultSource.java +++ b/src/org/redkale/source/DataDefaultSource.java @@ -36,6 +36,10 @@ public final class DataDefaultSource implements DataSource, Function info.primaryValue.get()) info.primaryValue.set(v); - } else { - long v = rs.getLong(1) / info.allocationSize; - if (v > info.primaryValue.get()) info.primaryValue.set(v); + try { + Statement stmt = conn.createStatement(); + + ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable(values[0])); + if (rs.next()) { + if (primaryType == int.class) { + int v = rs.getInt(1) / info.allocationSize; + if (v > info.primaryValue.get()) info.primaryValue.set(v); + } else { + long v = rs.getLong(1) / info.allocationSize; + if (v > info.primaryValue.get()) info.primaryValue.set(v); + } } + rs.close(); + stmt.close(); + } catch (SQLException se) { + if (info.tableStrategy == null) throw se; } - rs.close(); - stmt.close(); info.initedPrimaryValue = true; } } @@ -393,7 +402,22 @@ public final class DataDefaultSource implements DataSource, Function + * 详情见: http://redkale.org + * + * @author zhangjx + */ +@Target({TYPE}) +@Retention(RUNTIME) +public @interface DistributeTable { + + Class strategy(); +} diff --git a/src/org/redkale/source/DistributeTableStrategy.java b/src/org/redkale/source/DistributeTableStrategy.java new file mode 100644 index 000000000..e06d8e45d --- /dev/null +++ b/src/org/redkale/source/DistributeTableStrategy.java @@ -0,0 +1,29 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.source; + +import java.io.Serializable; + +/** + * + *

+ * 详情见: http://redkale.org + * + * @author zhangjx + * @param + */ +public interface DistributeTableStrategy { + + default String getTable(String table, Serializable primary) { + return null; + } + + default String getTable(String table, FilterNode node) { + return null; + } + + public String getTable(String table, T bean); +} diff --git a/src/org/redkale/source/EntityInfo.java b/src/org/redkale/source/EntityInfo.java index a0c40074d..4026dd89a 100644 --- a/src/org/redkale/source/EntityInfo.java +++ b/src/org/redkale/source/EntityInfo.java @@ -37,7 +37,7 @@ public final class EntityInfo { private final Class type; //类对应的数据表名, 如果是VirtualEntity 类, 则该字段为null - private final String table; + final String table; private final Creator creator; @@ -62,11 +62,19 @@ public final class EntityInfo { final String notcontainSQL; //用于反向LIKE使用 + final String tablenotexistSqlstate; //用于判断表不存在的使用 + + final String tablecopySQL; //用于复制表结构使用 + + final Set tables = new HashSet<>(); //用于存在table_20160202类似这种分布式表 + + final DistributeTableStrategy tableStrategy; + final String querySQL; private final Attribute[] queryAttributes; //数据库中所有字段 - final String insertSQL; + private String insertSQL; final Attribute[] insertAttributes; //数据库中所有可新增字段 @@ -145,6 +153,15 @@ public final class EntityInfo { this.fullloader = fullloader; this.table = (t == null) ? type.getSimpleName().toLowerCase() : (t.catalog().isEmpty()) ? t.name() : (t.catalog() + '.' + t.name()); } + DistributeTable dt = type.getAnnotation(DistributeTable.class); + DistributeTableStrategy dts = null; + try { + dts = (dt == null) ? null : dt.strategy().newInstance(); + } catch (Exception e) { + logger.severe(type + " init DistributeTableStrategy error", e); + } + this.tableStrategy = dts; + this.creator = Creator.create(type); Attribute idAttr0 = null; Map aliasmap0 = null; @@ -231,7 +248,7 @@ public final class EntityInfo { if (insertsb2.length() > 0) insertsb2.append(','); insertsb2.append('?'); } - this.insertSQL = "INSERT INTO " + table + "(" + insertsb + ") VALUES(" + insertsb2 + ")"; + this.insertSQL = "INSERT INTO " + (this.tableStrategy == null ? table : "${newtable}") + "(" + insertsb + ") VALUES(" + insertsb2 + ")"; StringBuilder updatesb = new StringBuilder(); for (String col : updatecols) { if (updatesb.length() > 0) updatesb.append(", "); @@ -259,6 +276,9 @@ public final class EntityInfo { if (conf == null) conf = new Properties(); this.containSQL = conf.getProperty(JDBC_CONTAIN_SQLTEMPLATE, "LOCATE(${keystr}, ${column}) > 0"); this.notcontainSQL = conf.getProperty(JDBC_NOTCONTAIN_SQLTEMPLATE, "LOCATE(${keystr}, ${column}) = 0"); + + this.tablenotexistSqlstate = conf.getProperty(JDBC_TABLENOTEXIST_SQLSTATE, "42S02"); + this.tablecopySQL = conf.getProperty(JDBC_TABLECOPY_SQLTEMPLATE, "CREATE TABLE ${newtable} LIKE ${oldtable}"); } public void createPrimaryValue(T src) { @@ -295,16 +315,27 @@ public final class EntityInfo { return table == null; } + public String getInsertSQL(T bean) { + if (this.tableStrategy == null) return insertSQL; + return insertSQL.replace("${newtable}", getTable(bean)); + } + public String getTable(Serializable primary) { - return table; + if (tableStrategy == null) return table; + String t = tableStrategy.getTable(table, primary); + return t == null || t.isEmpty() ? table : t; } public String getTable(FilterNode node) { - return table; + if (tableStrategy == null) return table; + String t = tableStrategy.getTable(table, node); + return t == null || t.isEmpty() ? table : t; } public String getTable(T bean) { - return table; + if (tableStrategy == null) return table; + String t = tableStrategy.getTable(table, bean); + return t == null || t.isEmpty() ? table : t; } public Attribute getPrimary() {