コード例 #1
0
ファイル: ModelUtils.java プロジェクト: wdxtub/18641-Spotgram
  public static String getSelectSQL(
      Class<? extends Model> cls,
      String fieldName1,
      Object value,
      String fieldName2,
      Object fValue,
      Object tValue) {
    // TODO Auto-generated method stub
    Model entity;
    String arg1 = value.toString();
    String fArg2 = fValue.toString();
    String tArg2 = tValue.toString();
    try {
      entity = cls.newInstance();
      String columnName1 = entity.columnName(fieldName1);
      String columnName2 = entity.columnName(fieldName2);
      DataType type = cls.getDeclaredField(fieldName1).getAnnotation(Column.class).type();
      if (type == DataType.TEXT) {
        arg1 = "'" + value + "'";
      }
      type = cls.getDeclaredField(fieldName2).getAnnotation(Column.class).type();
      if (type == DataType.TEXT || type == DataType.DATE || type == DataType.DATETIME) {
        fArg2 = "'" + fValue + "'";
        tArg2 = "'" + tValue + "'";
      }

      return "SELECT * FROM "
          + entity.tableName()
          + " WHERE "
          + columnName1
          + " = "
          + arg1
          + " AND "
          + columnName2
          + " >= "
          + fArg2
          + " AND "
          + columnName2
          + " <= "
          + tArg2;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
コード例 #2
0
ファイル: ModelUtils.java プロジェクト: wdxtub/18641-Spotgram
 public static String getSelectSQL(Class<? extends Model> cls, String fieldName, Object value) {
   Model entity;
   String args = value.toString();
   String selectSQLString = "";
   try {
     entity = cls.newInstance();
     String where = entity.columnName(fieldName) + " = ";
     if (!fieldName.equals("id")) {
       DataType type = cls.getDeclaredField(fieldName).getAnnotation(Column.class).type();
       if (type == DataType.TEXT) {
         args = "'" + value + "'";
       }
     }
     selectSQLString = "SELECT * FROM " + entity.tableName() + " WHERE " + where + args;
     if (entity instanceof Note) {
       selectSQLString += " order by date desc";
     }
     System.out.println(selectSQLString);
     return selectSQLString;
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   }
 }