Exemplo n.º 1
0
 public static String getCreateTableSQL(Class<? extends Model> modelClass) {
   Model m = null;
   try {
     m = modelClass.newInstance();
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   }
   StringBuilder sb = new StringBuilder(100);
   sb.append("CREATE TABLE IF NOT EXISTS ");
   sb.append(m.tableName())
       .append("(")
       .append(m.idColumnName())
       .append(" INTEGER PRIMARY KEY AUTOINCREMENT");
   for (Field f : m.getClass().getDeclaredFields()) {
     if (f.isAnnotationPresent(Column.class)) {
       Column c = f.getAnnotation(Column.class);
       if (!c.pk()) {
         sb.append(",").append(c.name()).append(" ").append(c.type());
       }
     }
   }
   sb.append(");");
   return sb.toString();
 }
Exemplo n.º 2
0
  public static String getDeleteSQL(Model entity) {

    return "DELETE FROM "
        + entity.tableName()
        + " WHERE "
        + entity.idColumnName()
        + " = "
        + entity.getId();
  }