pers.clare.hisql.service.SQLStoreService Maven / Gradle / Ivy
The newest version!
package pers.clare.hisql.service;
import pers.clare.hisql.exception.HiSqlException;
import pers.clare.hisql.store.SQLCrudStore;
import pers.clare.hisql.store.SQLData;
import pers.clare.hisql.store.SQLStoreFactory;
import pers.clare.hisql.util.SQLQueryUtil;
import pers.clare.hisql.util.SQLStoreUtil;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
public class SQLStoreService extends SQLStorePageService {
public T insert(
SQLCrudStore store
, T entity
) {
if (entity == null) {
return null;
}
try {
Field autoKey = store.getAutoKey();
if (store.isPs()) {
SQLData data = SQLStoreUtil.toInsertSQLData(store, entity);
if (autoKey == null) {
update(data.getSql(), data.getParameters());
} else {
autoKey.set(entity, insert(autoKey.getType(), data.getSql(), data.getParameters()));
}
} else {
String sql = SQLStoreUtil.buildInsertSQL(store, entity);
if (autoKey == null) {
update(sql);
} else {
autoKey.set(entity, insert(autoKey.getType(), sql));
}
}
return entity;
} catch (IllegalAccessException e) {
throw new HiSqlException(e);
}
}
public T[] insertAll(
SQLCrudStore store
, T[] entities
) {
insertAll(store, Arrays.asList(entities));
return entities;
}
public Collection insertAll(
SQLCrudStore store
, Collection entities
) {
if (entities == null || entities.size() == 0) return entities;
for (T entity : entities) {
insert(store, entity);
}
return entities;
}
public int update(
SQLCrudStore store
, T entity
) {
if (entity == null) {
return 0;
}
if (store.isPs()) {
try {
SQLData data = SQLStoreUtil.toUpdateSQLData(store, entity);
return update(data.getSql(), data.getParameters());
} catch (IllegalAccessException e) {
throw new HiSqlException(e);
}
} else {
return update(SQLStoreUtil.buildUpdateSQL(store, entity));
}
}
public final int[] updateAll(
SQLCrudStore sqlStore
, T[] entities
) {
return updateAll(sqlStore, Arrays.asList(entities));
}
public int[] updateAll(
SQLCrudStore store
, Collection entities
) {
if (entities == null || entities.size() == 0) return new int[0];
int[] counts = new int[entities.size()];
int i = 0;
for (T entity : entities) {
counts[i++] = update(store, entity);
}
return counts;
}
public int delete(
SQLCrudStore store
, T entity
) {
if (entity == null) {
return 0;
}
return update(SQLQueryUtil.setValue(store.getDeleteById(), store.getKeyFields(), entity));
}
public int[] deleteAll(
SQLCrudStore sqlStore
, T[] entities
) {
if (entities == null || entities.length == 0) return new int[0];
return deleteAll(sqlStore, Arrays.asList(entities));
}
public int[] deleteAll(
SQLCrudStore store
, Collection entities
) {
if (entities == null || entities.size() == 0) return new int[0];
int[] counts = new int[entities.size()];
int i = 0;
for (T entity : entities) {
counts[i++] = delete(store, entity);
}
return counts;
}
public T findByObject(
T entity
) {
if (entity == null) {
return null;
}
return find(toStore(entity), entity);
}
public T insertByObject(
T entity
) {
if (entity == null) {
return null;
}
return insert(toStore(entity), entity);
}
public int updateByObject(
T entity
) {
if (entity == null) {
return 0;
}
if (entity instanceof String) {
return update((String) entity);
} else {
return update(toStore(entity), entity);
}
}
public int deleteByObject(
T entity
) {
if (entity == null) {
return 0;
}
return delete(toStore(entity), entity);
}
private SQLCrudStore toStore(T entity) {
return SQLStoreFactory.buildCrud(getNaming(), getResultSetConverter(), (Class) entity.getClass());
}
}