static String getTypesSQL(@NonNull Container container) { StringBuilder builder = new StringBuilder(); Field[] fields = container.getFields(); FieldContainer primary = container.getPrimaryField(); if (primary == null) primary = getPrimaryFieldContainer(container); Field primaryField = null; if (primary != null) primaryField = primary.getField(); if (fields != null && fields.length > 0) { boolean isFirst = true; for (Field f : fields) { Ignore ignore = f.getAnnotation(Ignore.class); if (ignore != null) { LogUtil.d( CouSyncDb.TAG, CouSyncDb.LOG_HEADER + "ignore field=" + f.getName() + " when " + container.getModelName() + "create type column sql"); continue; } if (primaryField != null && f.equals(primaryField)) { LogUtil.d( CouSyncDb.TAG, CouSyncDb.LOG_HEADER + "jump primary key field=" + f.getName() + " when " + container.getModelName() + "create type column sql"); continue; } String typeSql = getTypeString(f); if (TextUtils.isEmpty(typeSql)) continue; if (isFirst) { isFirst = false; } else builder.append(Statement.COMMA); builder.append(typeSql); } } else { throw new NoFieldException(container.getModelName() + " have no field"); } return builder.toString(); }
private static String getPrimaryString(@NonNull FieldContainer container) { Field field = container.getField(); Class type = field.getType(); String name = field.getName(); String primary; if (type == int.class || type == Integer.class) { primary = name + Statement.INT + Statement.PRIMARY_KEY; if (container.isAuto()) primary += Statement.AUTOINCREMENT; } else if (type == String.class || type == Character.class || type == CharSequence.class) { primary = name + Statement.TEXT + Statement.PRIMARY_KEY; } else throw new NoPrimaryKeyException( "the types of primary key field are int,integer," + "String,Character or CharSequence !"); Unique unique = field.getAnnotation(Unique.class); if (unique != null) primary += Statement.UNIQUE; primary += Statement.COMMA; return primary; }