Skip to content

Commit

Permalink
update orm generatedKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreampie committed Dec 29, 2015
1 parent 57d718e commit 8908a0c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
57 changes: 23 additions & 34 deletions resty-orm/src/main/java/cn/dreampie/orm/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ private PreparedStatement getPreparedStatement(boolean showSql, Connection conn,
PreparedStatement pst;
//如果没有自动生成的主键 则不获取
String generatedKey = tableMeta.getGeneratedKey();
boolean generated = tableMeta.getGenerator() == null;
if (!generatedKey.isEmpty() && generated) {
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
pst = conn.prepareStatement(sql, new String[]{generatedKey});
} else {
pst = conn.prepareStatement(sql);
Expand Down Expand Up @@ -318,8 +318,8 @@ private PreparedStatement getPreparedStatement(boolean showSql, Connection conn,
PreparedStatement pst = null;
//如果没有自动生成的主键 则不获取
String generatedKey = tableMeta.getGeneratedKey();
boolean generated = tableMeta.getGenerator() == null;
if (!generatedKey.isEmpty() && generated) {
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
String[] returnKeys = new String[params.length];
for (int i = 0; i < params.length; i++) {
returnKeys[i] = generatedKey;
Expand Down Expand Up @@ -450,7 +450,7 @@ private Object[] getPrimaryValues(TableMeta tableMeta) {
protected void setGeneratedKey(PreparedStatement pst, TableMeta tableMeta) throws SQLException {
String generatedKey = tableMeta.getGeneratedKey();
if (!generatedKey.isEmpty()) {
boolean generated = tableMeta.getGenerator() == null;
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
if (get(generatedKey) == null) {
ResultSet rs = pst.getGeneratedKeys();
Expand All @@ -469,7 +469,7 @@ protected void setGeneratedKey(PreparedStatement pst, TableMeta tableMeta) throw
protected void setGeneratedKey(PreparedStatement pst, TableMeta tableMeta, List<? extends Entity> models) throws SQLException {
String generatedKey = tableMeta.getGeneratedKey();
if (!generatedKey.isEmpty()) {
boolean generated = tableMeta.getGenerator() == null;
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
ResultSet rs = pst.getGeneratedKeys();
for (Entity<?> model : models) {
Expand Down Expand Up @@ -686,11 +686,9 @@ public boolean save() {
}
String generatedKey = tableMeta.getGeneratedKey();

boolean generated = tableMeta.getGenerator() == null;
if (!generatedKey.isEmpty() && get(generatedKey) == null) {
if (!generated) {
set(generatedKey, tableMeta.getGenerator().generateKey());
}
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (!generated && get(generatedKey) == null) {
set(generatedKey, tableMeta.getGenerator().generateKey());
}

DataSourceMeta dsm = getDataSourceMeta();
Expand Down Expand Up @@ -764,12 +762,10 @@ public boolean save(List<M> models) {
String generatedKey = tableMeta.getGeneratedKey();

//是否需要主键生成器生成值
boolean generated = tableMeta.getGenerator() == null;
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
Generator generator = tableMeta.getGenerator();
if (!generatedKey.isEmpty() && get(generatedKey) == null) {
if (!generated) {
firstModel.set(generatedKey, generator.generateKey());
}
if (!generated && get(generatedKey) == null) {
firstModel.set(generatedKey, generator.generateKey());
}

DataSourceMeta dsm = firstModel.getDataSourceMeta();
Expand All @@ -795,7 +791,7 @@ public boolean save(List<M> models) {
for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
//如果是自动生成主键 使用生成器生成
if (!generatedKey.isEmpty() && !generated && columns[j].equals(generatedKey) && models.get(i).get(generatedKey) == null) {
if (!generated && columns[j].equals(generatedKey) && models.get(i).get(generatedKey) == null) {
models.get(i).set(columns[j], generator.generateKey());
}
params[i][j] = models.get(i).get(columns[j]);
Expand Down Expand Up @@ -882,14 +878,16 @@ public boolean update() {

String generatedKey = tableMeta.getGeneratedKey();

if (getModifyAttrNames(generatedKey).length <= 0) {
String[] columns = getModifyAttrNames(generatedKey);
if (columns.length <= 0) {
logger.warn("Could not found any modified attributes.");
return false;
}

boolean hasGeneratedKey = !generatedKey.isEmpty();
//是否使用数据库自增
Object id = null;
if (!generatedKey.isEmpty()) {
if (hasGeneratedKey) {
id = get(generatedKey);
checkNotNull(id, "You can't update model without Generated Key " + generatedKey + ".");
}
Expand All @@ -903,7 +901,7 @@ public boolean update() {
String[] pkeys = tableMeta.getPrimaryKey();
int i = 0;
int j = 0;
if (!generatedKey.isEmpty()) {
if (hasGeneratedKey) {
ids = new Object[pkeys.length + 1];
keys = new String[pkeys.length + 1];
keys[j++] = generatedKey;
Expand All @@ -925,21 +923,12 @@ public boolean update() {
params = values;
}

String[] columns = getModifyAttrNames(generatedKey);

//判断是否有更新
if (columns.length <= 0) {
logger.warn("Could not found any data to update.");
return false;
} else {
String sql = dialect.update(tableMeta.getTableName(), getAlias(), where, columns);
if (update(sql, params)) {
clearModifyAttrs();
return true;
}
return false;
String sql = dialect.update(tableMeta.getTableName(), getAlias(), where, columns);
if (update(sql, params)) {
clearModifyAttrs();
return true;
}

return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public @interface Table {
String name();//表名

String generatedKey() default Base.DEFAULT_GENERATED_KEY;//自动生成的主键
String generatedKey() default Base.DEFAULT_GENERATED_KEY;//自动生成的主键 如果没有自动生成的主键设置为空字符串

String sequence() default "";//序列值

Expand Down

0 comments on commit 8908a0c

Please sign in to comment.