0
DataSet dataSet = datacontext.query()
    .from(table)
    .select(colNameArr)
    .where(frstCol).eq(dynamicval1).and("").eq("").or("").eq("")....etc
    .execute();

Can this be achieved?, can I dynamically decide the constraint, starting from and(), or() ? if so how can we do it?

Matt
  • 14,906
  • 27
  • 99
  • 149
peaceUser
  • 457
  • 5
  • 19

1 Answers1

1

Example of dynamic SELECT and WHERE clauses:

    // Prepare a data context based on plain old Java objects
    List<TableDataProvider<?>> tableDataProviders = new ArrayList<TableDataProvider<?>>();
    SimpleTableDef tableDef1 = new SimpleTableDef("snippetTableName1", new String[] {"id", "name"});
    tableDataProviders.add(new ArrayTableDataProvider(tableDef1,
            new ArrayList<Object[]>()));
    PojoDataContext dataContext = new PojoDataContext("snippetSchemaName", tableDataProviders);

    // Add some rows
    dataContext.executeUpdate(new UpdateScript() {

        public void run(UpdateCallback callback) {
            callback.insertInto("snippetTableName1").value("id", "id1").value("name", "name1").execute();
            callback.insertInto("snippetTableName1").value("id", "id2").value("name", "name2").execute();
        }
    });

    // Prepare dynamic query
    String[] selectArray = new String[2];
    selectArray[0] = "id";
    selectArray[1] = "name";

    Map<String, Object> whereClauses = new HashMap<String, Object>();
    whereClauses.put("id", "id1");
    whereClauses.put("name", "name1");

    SatisfiedSelectBuilder<?> queryBuilder = dataContext.query().from("snippetTableName1").select(selectArray);
    SatisfiedWhereBuilder<?> whereBuilder = null;

    int i = 0;
    for (Map.Entry<String, Object> whereClause: whereClauses.entrySet()) {
        if (i == 0) {
            whereBuilder = queryBuilder.where(whereClause.getKey()).eq(whereClause.getValue());
        } else {
            whereBuilder = whereBuilder.and(whereClause.getKey()).eq(whereClause.getValue());
        }
        i++;
    }

    DataSet dataSet = whereBuilder.execute();

    while (dataSet.next()) {
        Row row = dataSet.getRow();
        System.out.println("Row: " + row);
    }
TomaszGuzialek
  • 861
  • 1
  • 8
  • 15
  • Hi Tomas, Dynamic what I mean is , no control over "snippetTableName1", new String[] {"id", "name"}, String[] {.....}. In this case!.. – peaceUser Jun 24 '16 at 12:41
  • Sorry, but I can't get what you mean. Maybe show some use case, what do you want to achieve and what is stopping you from achieving that... – TomaszGuzialek Jun 24 '16 at 12:49
  • today I have only id, and name, tmrw, it can change to 100's of columns, how to handle it...! do I have to manually change the codes all the time?..!! – peaceUser Jun 24 '16 at 12:59
  • That is not a MetaModel issue, that is a general programming task. MetaModel takes an array of columns that you want to select and it is up to you to prepare such a list (dynamically! The hardcoded array was just an example). – TomaszGuzialek Jun 24 '16 at 14:12
  • I got you, that it is applicable as column arrays.. but what if the constrains appear.... Metamodel, doesnt have an implemetation class for the WhereBuilder interface... !! I wonder if I can fix it as a programmer..! – peaceUser Jun 26 '16 at 11:04
  • I edited my initial answer and expanded the example with dynamic WHERE clause. I hope that is what you had in mind. – TomaszGuzialek Jun 27 '16 at 17:34
  • Thank you, I will have to try it out in this way, looks promising...! – peaceUser Jun 28 '16 at 12:44