use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class AbstractJdbcExpert method createIndexSql.
public Sql createIndexSql(Entity<?> en, EntityIndex index) {
StringBuilder sb = new StringBuilder();
if (index.isUnique())
sb.append("Create UNIQUE Index ");
else
sb.append("Create Index ");
if (index.getName().contains("$"))
sb.append(TableName.render(new CharSegment(index.getName())));
else
sb.append(index.getName());
sb.append(" ON ").append(en.getTableName()).append("(");
for (EntityField field : index.getFields()) {
if (field instanceof MappingField) {
MappingField mf = (MappingField) field;
sb.append(mf.getColumnNameInSql()).append(',');
} else {
throw Lang.makeThrow(DaoException.class, "%s %s is NOT a mapping field, can't use as index field!!", en.getClass(), field.getName());
}
}
sb.setCharAt(sb.length() - 1, ')');
return Sqls.create(sb.toString());
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class AbstractJdbcExpert method setupEntityField.
// ====================================================================
// 下面为子类默认实现几个接口函数
public void setupEntityField(Connection conn, Entity<?> en) {
List<MappingField> mfs = new ArrayList<MappingField>();
for (MappingField mf : en.getMappingFields()) {
if (mf.getTypeMirror().isEnum()) {
mfs.add(mf);
}
}
if (mfs.isEmpty())
return;
Statement stat = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
try {
// 获取数据库元信息
stat = conn.createStatement();
rs = stat.executeQuery(createResultSetMetaSql(en));
rsmd = rs.getMetaData();
// 循环字段检查
List<String> columnNames = new ArrayList<String>();
List<String> columnLabels = new ArrayList<String>();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnNames.add(rsmd.getColumnName(i));
columnLabels.add(rsmd.getColumnLabel(i));
}
for (MappingField mf : mfs) {
try {
int ci = columnNames.indexOf(mf.getColumnName()) + 1;
if (ci == 0) {
log.debugf("Can not find @Column(%s) in table/view (%s), skip checking", mf.getColumnName(), rsmd.getTableName(1));
continue;
}
// 枚举类型在数据库中的值
if (Daos.isIntLikeColumn(rsmd, ci)) {
mf.setColumnType(ColType.INT);
} else {
mf.setColumnType(ColType.VARCHAR);
}
} catch (Exception e) {
}
}
} catch (Exception e) {
if (log.isDebugEnabled())
log.debugf("Table '%s' doesn't exist!", en.getViewName());
} finally // Close ResultSet and Statement
{
Daos.safeClose(stat, rs);
}
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class NutDao method insert.
public <T> T insert(final T t, boolean ignoreNull, boolean ignoreZero, boolean ignoreBlankStr) {
Object obj = Lang.first(t);
Entity<?> en = getEntity(obj.getClass());
List<String> names = new ArrayList<String>();
for (MappingField mf : en.getMappingFields()) {
Object tmp = mf.getValue(obj);
if (ignoreNull && tmp == null) {
continue;
}
if (ignoreZero && (tmp == null || (tmp instanceof Number && ((Number) tmp).intValue() == 0))) {
continue;
}
if (ignoreBlankStr && (tmp instanceof CharSequence && Strings.isBlank((CharSequence) tmp)))
continue;
names.add(mf.getName());
}
FieldFilter ff = FieldFilter.create(obj.getClass(), "^(" + Strings.join("|", names.toArray()) + ")$");
Molecule<T> m = new Molecule<T>() {
public void run() {
insert(t);
setObj(t);
}
};
return ff.run(m);
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class EntityObjectContext method keys.
public Set<String> keys() {
Set<String> names = new HashSet<String>(en.getMappingFields().size());
names.add(ME);
for (MappingField mf : en.getMappingFields()) names.add(mf.getName());
names.addAll(ext.keySet());
return names;
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class ExtDaoInvocationHandler method dataDict.
/** 根据Pojo生成数据字典,zdoc格式 */
public static StringBuilder dataDict(Dao dao, String... packages) {
StringBuilder sb = new StringBuilder();
List<Class<?>> ks = new ArrayList<Class<?>>();
for (String packageName : packages) {
ks.addAll(Scans.me().scanPackage(packageName));
}
Iterator<Class<?>> it = ks.iterator();
while (it.hasNext()) {
Class<?> klass = it.next();
if (klass.getAnnotation(Table.class) == null)
it.remove();
}
// log.infof("Found %d table class", ks.size());
JdbcExpert exp = dao.getJdbcExpert();
Entity<?> entity = null;
String line = "-------------------------------------------------------------------\n";
sb.append("#title:数据字典\n");
sb.append("#author:wendal\n");
sb.append("#index:0,1\n").append(line);
for (Class<?> klass : ks) {
sb.append(line);
entity = dao.getEntity(klass);
sb.append("表名 ").append(entity.getTableName()).append("\n\n");
if (!Strings.isBlank(entity.getTableComment()))
sb.append("表注释: ").append(entity.getTableComment());
sb.append("\t").append("Java类名 ").append(klass.getName()).append("\n\n");
sb.append("\t||序号||列名||数据类型||主键||非空||默认值||java属性名||java类型||注释||\n");
int index = 1;
for (MappingField field : entity.getMappingFields()) {
String dataType = exp.evalFieldType(field);
sb.append("\t||").append(index++).append("||").append(field.getColumnName()).append("||").append(dataType).append("||").append(field.isPk()).append("||").append(field.isNotNull()).append("||").append(field.getDefaultValue(null) == null ? " " : field.getDefaultValue(null)).append("||").append(field.getName()).append("||").append(field.getTypeClass().getName()).append("||").append(field.getColumnComment() == null ? " " : field.getColumnComment()).append("||\n");
}
}
return sb;
}
Aggregations