Skip to content

Commit

Permalink
把TableNameManager类合并到ModelManager类中
Browse files Browse the repository at this point in the history
  • Loading branch information
ffpy committed Nov 26, 2017
1 parent a07f210 commit c00e24c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 95 deletions.
11 changes: 5 additions & 6 deletions sqlpal/src/org/sqlpal/SqlPal.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ public static void init(String configFilename, ConnectionFactory connectionFacto
ConfigManager.init(configFilename);
ConnectionManager.init(connectionFactory);
ClassManager.init();
TableNameManager.init();
ModelManager.init();

ClassManager.destroy();
ModelManager.init();
}

/**
* 销毁
*/
public static void destroy() {
try {
ConfigManager.destroy();
ConnectionManager.destroy();
TableNameManager.destroy();
ModelManager.destroy();
ModelManager.destroy();
ClassManager.destroy();
ConnectionManager.destroy();
ConfigManager.destroy();
} catch (SQLException e) {
e.printStackTrace();
}
Expand Down
4 changes: 2 additions & 2 deletions sqlpal/src/org/sqlpal/crud/DataSupport.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.sqlpal.crud;

import org.sqlpal.manager.TableNameManager;
import com.sun.istack.internal.NotNull;
import org.sqlpal.manager.ModelManager;

import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -17,7 +17,7 @@ public abstract class DataSupport {
* 获取表名
*/
public String getTableName() {
return TableNameManager.getTableName(getClass());
return ModelManager.getTableName(getClass());
}

/**
Expand Down
8 changes: 3 additions & 5 deletions sqlpal/src/org/sqlpal/crud/DeleteHandler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.sqlpal.crud;

import org.sqlpal.common.ModelField;
import org.sqlpal.manager.TableNameManager;
import org.sqlpal.util.EmptyUtils;
import org.sqlpal.manager.ModelManager;
import org.sqlpal.util.SqlUtils;
import org.sqlpal.util.StatementUtils;
import com.sun.istack.internal.NotNull;

Expand Down Expand Up @@ -51,8 +49,8 @@ int deleteAll(@NotNull final Class<? extends DataSupport> modelClass, @NotNull f
Integer row = new DataHandler().execute(new DefaultExecuteCallback<Integer>() {
@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.delete(
TableNameManager.getTableName(modelClass), conditions.length > 0 ? conditions[0] : null));
return connection.prepareStatement(SqlFactory.delete(
ModelManager.getTableName(modelClass), conditions.length > 0 ? conditions[0] : null));
}

@Override
Expand All @@ -73,7 +71,7 @@ public Integer onExecute(PreparedStatement statement) throws SQLException {

@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.delete(model.getTableName(), primaryKeyFields));
return connection.prepareStatement(SqlFactory.delete(model.getTableName(), primaryKeyFields));
}

@Override
Expand Down
10 changes: 4 additions & 6 deletions sqlpal/src/org/sqlpal/crud/QueryHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.sqlpal.crud;

import org.sqlpal.manager.ModelManager;
import org.sqlpal.manager.TableNameManager;
import org.sqlpal.util.EmptyUtils;
import org.sqlpal.util.SqlUtils;
import org.sqlpal.util.StatementUtils;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
Expand Down Expand Up @@ -74,8 +72,8 @@ <T extends DataSupport> List<T> find(@NotNull final Class<? extends DataSupport>
List<T> list = new DataHandler().execute(new DefaultExecuteCallback<List<T>>() {
@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.find(
TableNameManager.getTableName(modelClass), columns, conditions, orderBy, limit, offset));
return connection.prepareStatement(SqlFactory.find(
ModelManager.getTableName(modelClass), columns, conditions, orderBy, limit, offset));
}

@Override
Expand Down Expand Up @@ -115,8 +113,8 @@ <T extends Number> T aggregate(@NotNull final Class<? extends DataSupport> model
return new DataHandler().execute(new DefaultExecuteCallback<T>() {
@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.find(
TableNameManager.getTableName(modelClass), columns, conditions, null, 0, 0));
return connection.prepareStatement(SqlFactory.find(
ModelManager.getTableName(modelClass), columns, conditions, null, 0, 0));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions sqlpal/src/org/sqlpal/crud/SaveHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.sqlpal.common.ModelField;
import org.sqlpal.manager.ModelManager;
import org.sqlpal.util.EmptyUtils;
import org.sqlpal.util.SqlUtils;
import org.sqlpal.util.StatementUtils;
import com.sun.istack.internal.NotNull;

Expand Down Expand Up @@ -50,7 +49,7 @@ void saveAll(@NotNull List<? extends DataSupport> models) throws SQLException {

@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.insert(model.getTableName(), allFields), Statement.RETURN_GENERATED_KEYS);
return connection.prepareStatement(SqlFactory.insert(model.getTableName(), allFields), Statement.RETURN_GENERATED_KEYS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package org.sqlpal.util;
package org.sqlpal.crud;

import org.sqlpal.common.ModelField;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import org.sqlpal.util.EmptyUtils;

import java.util.List;

/**
* SQL语句工具类
* SQL语句工厂类
*/
public class SqlUtils {
public class SqlFactory {

/**
* 创建插入语句
* @param tableName 表名
* @param fields 要插入的字段列表
* @return 返回插入语句
*/
public static String insert(@NotNull String tableName, @NotNull List<ModelField> fields) {
static String insert(@NotNull String tableName, @NotNull List<ModelField> fields) {
if (EmptyUtils.isEmpty(tableName) || fields.isEmpty()) return "";

StringBuilder sql = new StringBuilder();
Expand All @@ -44,7 +45,7 @@ public static String insert(@NotNull String tableName, @NotNull List<ModelField>
* @param fields 要查找的字段列表,用and连接
* @return 返回删除语句
*/
public static String delete(@NotNull String tableName, @NotNull List<ModelField> fields) {
static String delete(@NotNull String tableName, @NotNull List<ModelField> fields) {
if (EmptyUtils.isEmpty(tableName) || fields.isEmpty()) return "";
StringBuilder where = new StringBuilder();
for (ModelField bean : fields) {
Expand All @@ -61,7 +62,7 @@ public static String delete(@NotNull String tableName, @NotNull List<ModelField>
* @param where where条件
* @return 返回删除语句
*/
public static String delete(@NotNull String tableName, @Nullable String where) {
static String delete(@NotNull String tableName, @Nullable String where) {
if (EmptyUtils.isEmpty(tableName)) return "";
StringBuilder sql = new StringBuilder();
sql.append("DELETE FROM ").append(tableName);
Expand All @@ -78,7 +79,7 @@ public static String delete(@NotNull String tableName, @Nullable String where) {
* @param updatedFields 要更新的字段
* @return 返回更新语句
*/
public static String update(@NotNull String tableName, @NotNull List<ModelField> primaryKeyFields, @NotNull List<ModelField> updatedFields) {
static String update(@NotNull String tableName, @NotNull List<ModelField> primaryKeyFields, @NotNull List<ModelField> updatedFields) {
if (EmptyUtils.isEmpty(primaryKeyFields)) return "";

StringBuilder where = new StringBuilder();
Expand All @@ -97,7 +98,7 @@ public static String update(@NotNull String tableName, @NotNull List<ModelField>
* @param updatedFields 要更新的字段
* @return 返回更新语句
*/
public static String update(@NotNull String tableName, @NotNull String where, @NotNull List<ModelField> updatedFields) {
static String update(@NotNull String tableName, @NotNull String where, @NotNull List<ModelField> updatedFields) {
if (EmptyUtils.isEmpty(tableName) || EmptyUtils.isEmpty(where) || EmptyUtils.isEmpty(updatedFields)) return "";

StringBuilder sql = new StringBuilder();
Expand All @@ -123,8 +124,8 @@ public static String update(@NotNull String tableName, @NotNull String where, @N
* @param offset 偏移
* @return 返回查询语句
*/
public static String find(@NotNull String tableName, @Nullable String[] columns, @Nullable String[] conditions,
@Nullable String[] orderBy, int limit, int offset) {
static String find(@NotNull String tableName, @Nullable String[] columns, @Nullable String[] conditions,
@Nullable String[] orderBy, int limit, int offset) {
if (EmptyUtils.isEmpty(tableName)) return "";
StringBuilder sql = new StringBuilder();

Expand Down
5 changes: 2 additions & 3 deletions sqlpal/src/org/sqlpal/crud/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.sqlpal.common.ModelField;
import org.sqlpal.manager.ModelManager;
import org.sqlpal.util.EmptyUtils;
import org.sqlpal.util.SqlUtils;
import org.sqlpal.util.StatementUtils;
import com.sun.istack.internal.NotNull;

Expand Down Expand Up @@ -56,7 +55,7 @@ int updateAll(@NotNull DataSupport model, @NotNull final String... conditions) t

@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.update(model.getTableName(), conditions[0], allFields));
return connection.prepareStatement(SqlFactory.update(model.getTableName(), conditions[0], allFields));
}

@Override
Expand All @@ -78,7 +77,7 @@ public Integer onExecute(PreparedStatement statement) throws SQLException {

@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
return connection.prepareStatement(SqlUtils.update(model.getTableName(), primaryKeyFields, updatedFields));
return connection.prepareStatement(SqlFactory.update(model.getTableName(), primaryKeyFields, updatedFields));
}

@Override
Expand Down
37 changes: 33 additions & 4 deletions sqlpal/src/org/sqlpal/manager/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.sqlpal.annotation.AutoIncrement;
import org.sqlpal.annotation.PrimaryKey;
import org.sqlpal.annotation.Table;
import org.sqlpal.config.Config;
import org.sqlpal.common.ModelField;
import org.sqlpal.exception.ConfigurationException;
Expand All @@ -20,22 +21,33 @@
* 模型管理器
*/
public class ModelManager {
private static ConcurrentHashMap<String, ArrayList<String>> primaryKeyNamesMap; // 模型类对应的主键
private static ConcurrentHashMap<String, ArrayList<String>> primaryKeyNamesMap; // 表名对应的主键
private static ConcurrentHashMap<String, String> autoIncrementMap; // 模型类对应的自增字段
private static ConcurrentHashMap<String, String> tableNameMap; // 模型类对应的表名

/**
* 初始化字段信息
*/
public static void init() {
primaryKeyNamesMap = new ConcurrentHashMap<>();
autoIncrementMap = new ConcurrentHashMap<>();
tableNameMap = new ConcurrentHashMap<>();

Config config = ConfigManager.getConfig();
for (String className : config.getMapping()) {
Class<?> cls = ClassManager.getClass(className);

// 获取Model类的表名
Table annotation = cls.getAnnotation(Table.class);
if (annotation == null) {
throw new ConfigurationException("请为" + className + "添加TableName注解以指定表名");
}
String tableName = annotation.name();
tableNameMap.put(className, tableName);

// 获取Model类的主键字段
ArrayList<String> primaryKeyNames = new ArrayList<>();
Field[] fields = cls.getDeclaredFields();

for (Field field : fields) {
field.setAccessible(true);
if (!isSupportedField(field)) continue;
Expand All @@ -48,11 +60,9 @@ public static void init() {
autoIncrementMap.put(className, field.getName());
}
}

if (primaryKeyNames.isEmpty()) {
throw new ConfigurationException("找不到主键,请为" + className + "添加PrimaryKey注解以指定主键");
}

primaryKeyNamesMap.put(className, primaryKeyNames);
}
}
Expand All @@ -69,6 +79,10 @@ public static void destroy() {
autoIncrementMap.clear();
autoIncrementMap = null;
}
if (tableNameMap != null) {
tableNameMap.clear();
tableNameMap = null;
}
}

/**
Expand Down Expand Up @@ -225,6 +239,21 @@ public static String getAutoIncrement(@NotNull Class<? extends DataSupport> mode
return autoIncrementMap.get(modelClass.getName());
}

/**
* 获取Model类所映射的表名
* @param modelClass Model类的Class
* @return 返回表名
* @throws ConfigurationException 初始化错误
*/
public static String getTableName(Class<? extends DataSupport> modelClass) throws ConfigurationException {
String tableName = tableNameMap.get(modelClass.getName());
if (tableName == null) {
throw new ConfigurationException("找不到表名,请把" + modelClass.getName() + "添加到" +
ConfigManager.getConfigFilename() + "的mapping中");
}
return tableName;
}

/**
* 字段遍历接口
*/
Expand Down
57 changes: 0 additions & 57 deletions sqlpal/src/org/sqlpal/manager/TableNameManager.java

This file was deleted.

0 comments on commit c00e24c

Please sign in to comment.