Exemple #1
0
  public void testInsertWithDefaultValues() {
    // insert into things default values;
    Insert insert = Insert.into(Thing.TABLE).defaultValues();
    CompiledStatement compiled = insert.compile();

    verifyCompiledSqlArgs(compiled, 0);

    int rowsBeforeInsert = dao.count(Thing.class, Criterion.all);
    assertEquals(3, dao.insert(insert));
    int rowsAfterInsert = dao.count(Thing.class, Criterion.all);

    assertEquals(rowsBeforeInsert + 1, rowsAfterInsert);

    // get the newest
    Thing newThing = null;
    SquidCursor<Thing> cursor = null;
    try {
      cursor =
          dao.query(
              Thing.class, Query.select(Thing.PROPERTIES).orderBy(Order.desc(Thing.ID)).limit(1));
      if (cursor.moveToFirst()) {
        newThing = new Thing(cursor);
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }

    assertNotNull(newThing);
    assertEquals(Thing.DEFAULT_FOO, newThing.getFoo());
    assertEquals(Thing.DEFAULT_BAR, newThing.getBar().intValue());
    assertEquals(Thing.DEFAULT_IS_ALIVE, newThing.isAlive().booleanValue());
  }
 public static void customLoad(ArrayList list, String db, String sqry) throws Exception {
   BigliettoTreno model;
   for (CachedRowSet crs = Query.select(db, sqry); crs.next(); list.add(model)) {
     model = new BigliettoTreno();
     populate(model, crs);
   }
 }
  public static void load(BigliettoTreno model, String db, String id) throws Exception {
    ArrayList params = new ArrayList();
    params.add(id);

    CachedRowSet crs = Query.select(LOAD_BIGLIETTO_TRENO, params, db);
    if (crs.next()) {
      populate(model, crs);
    }
  }
Exemple #4
0
  public void testInsertWithQuery() {
    double pi = Math.PI;

    Criterion criterion = Thing.QUX.gt(pi);
    int numThingsMatching = dao.count(Thing.class, criterion);

    // insert into testModels select foo, bar, isAlive from things where qux > 3.1415...;
    Query query =
        Query.select(Thing.FOO, Thing.BAR, Thing.IS_ALIVE).from(Thing.TABLE).where(criterion);
    Insert insert =
        Insert.into(TestModel.TABLE)
            .columns(TestModel.LAST_NAME, TestModel.LUCKY_NUMBER, TestModel.IS_HAPPY)
            .select(query);
    CompiledStatement compiled = insert.compile();

    verifyCompiledSqlArgs(compiled, 1, pi);

    int testModelsBeforeInsert = dao.count(TestModel.class, Criterion.all);
    assertEquals(3, dao.insert(insert));
    int testModelsAfterInsert = dao.count(TestModel.class, Criterion.all);
    assertEquals(testModelsBeforeInsert + numThingsMatching, testModelsAfterInsert);
  }
 /**
  * Counts all entities of the specified type matching the given criteria and parameters. This is a
  * convenience method for: <code>count(type, Query.select().where(criteria, parameters))</code>
  *
  * @param type The type of the entities which should be counted.
  * @param criteria A parameterized WHERE statement used to determine the result set which will be
  *     counted.
  * @param parameters A varargs array of parameters to be passed to the executed prepared
  *     statement. The length of this array <i>must</i> match the number of parameters (denoted by
  *     the '?' char) in the <code>criteria</code>.
  * @return The number of entities of the given type which match the specified criteria.
  */
 public <K> int count(Class<? extends RawEntity<K>> type, String criteria, Object... parameters)
     throws SQLException {
   return count(type, Query.select().where(criteria, parameters));
 }
 /**
  * Counts all entities of the specified type. This method is actually a delegate for: <code>
  * count(Class&lt;? extends Entity&gt;, Query)</code>
  *
  * @param type The type of the entities which should be counted.
  * @return The number of entities of the specified type.
  */
 public <K> int count(Class<? extends RawEntity<K>> type) throws SQLException {
   return count(type, Query.select());
 }
 /**
  * Convenience method to select all entities of the given type with the specified, parameterized
  * criteria. The <code>criteria</code> String specified is appended to the SQL prepared statement
  * immediately following the <code>WHERE</code>.
  *
  * <p>Example:
  *
  * <pre>manager.find(Person.class, "name LIKE ? OR age &gt; ?", "Joe", 9);</pre>
  *
  * <p>This actually delegates the call to the {@link #find(Class, Query)} method, properly
  * parameterizing the {@link Query} object.
  *
  * @param type The type of the entities to retrieve.
  * @param criteria A parameterized WHERE statement used to determine the results.
  * @param parameters A varargs array of parameters to be passed to the executed prepared
  *     statement. The length of this array <i>must</i> match the number of parameters (denoted by
  *     the '?' char) in the <code>criteria</code>.
  * @return An array of entities of the given type which match the specified criteria.
  */
 public <T extends RawEntity<K>, K> T[] find(Class<T> type, String criteria, Object... parameters)
     throws SQLException {
   return find(type, Query.select().where(criteria, parameters));
 }
 /**
  * Returns all entities of the given type. This actually peers the call to the {@link #find(Class,
  * Query)} method.
  *
  * @param type The type of entity to retrieve.
  * @return An array of all entities which correspond to the given type.
  */
 public <T extends RawEntity<K>, K> T[] find(Class<T> type) throws SQLException {
   return find(type, Query.select());
 }
 public IterableIterator<T> select(Class cls, String predicate) {
   Query<T> query = new QueryImpl<T>(getStorage());
   return query.select(cls, iterator(), predicate);
 }
 public static void customLoad(BigliettoTreno model, String db, String sqry) throws Exception {
   CachedRowSet crs = Query.select(db, sqry);
   if (crs.next()) {
     populate(model, crs);
   }
 }