Back to project page TodoList.
The source code is released under:
Apache License
If you think the Android project TodoList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package lyc.simplesqlite.util; /* ww w . j av a2s . com*/ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import lyc.simplesqlite.annotation.Column; import lyc.simplesqlite.annotation.Table; import lyc.simplesqlite.exception.AnnotationNotFoundException; import static lyc.simplesqlite.util.StringUtils.Callback; /** * Created by ivan on 14-9-25. */ public class MetaData { private MetaData() { } public static String tableName(Class<?> clz) { Table table = clz.getAnnotation(Table.class); if (table != null) { return table.value(); } throw new AnnotationNotFoundException(Table.class); } public static String schema(Class<?> clz) { StringBuilder sb = new StringBuilder(); Table table = clz.getAnnotation(Table.class); if (table != null) { Field[] fields = clz.getDeclaredFields(); final List<String> primaryKeyColumns = new ArrayList<String>(); String columns = StringUtils.join(fields, new Callback<Field>() { public String process(Field field) { Column column = field.getAnnotation(Column.class); if (column.isPrimaryKey()) { primaryKeyColumns.add(column.name()); } return String .format("%s %s %s", column.name(), column.type(), column.notNull() ? "NOT NULL" : ""); } }); if (columns.length() > 0) { sb.append(String.format("CREATE TABLE %s (", table.value())); sb.append(columns); String primaryKeys = StringUtils.join(primaryKeyColumns.toArray(), ", "); if (primaryKeys.length() > 0) { sb.append(", "); sb.append(String.format("PRIMARY KEY (%s)", primaryKeys)); } sb.append(");"); return sb.toString(); } else { throw new AnnotationNotFoundException(Column.class); } } throw new AnnotationNotFoundException(Table.class); } public static String list() { return null; } }