use of com.codename1.db.Database in project CodenameOne by codenameone.
the class SQLMap method selectImpl.
/**
* Fetches the components from the database matching the given cmp description, the fields that aren't
* null within the cmp will match the where clause
* @param where the where statement
* @param propertyClass the class of the property business object
* @param params the parameters to use in the where statement
* @param orderBy the column to order by, can be null to ignore order
* @param ascending true to indicate ascending order
* @param maxElements the maximum number of elements returned can be 0 or lower to ignore
* @param page the page within the query to match the max elements value
* @return the result of the query
*/
private java.util.List<PropertyBusinessObject> selectImpl(PropertyBusinessObject cmp, String where, Class propertyClass, Object[] params, Property orderBy, boolean ascending, int maxElements, int page) throws IOException, InstantiationException {
String tableName = getTableName(cmp);
StringBuilder createStatement = new StringBuilder("SELECT * FROM ");
createStatement.append(tableName);
if (where != null && where.length() > 0) {
if (!where.toUpperCase().startsWith(" WHERE ")) {
createStatement.append(" WHERE ");
}
createStatement.append(where);
}
if (orderBy != null) {
createStatement.append(" ORDER BY ");
createStatement.append(getColumnName(orderBy));
if (!ascending) {
createStatement.append(" DESC");
}
}
if (maxElements > 0) {
createStatement.append(" LIMIT ");
createStatement.append(maxElements);
if (page > 0) {
createStatement.append(" OFFSET ");
createStatement.append(page * maxElements);
}
}
Cursor c = null;
try {
ArrayList<PropertyBusinessObject> response = new ArrayList<PropertyBusinessObject>();
c = executeQuery(createStatement.toString(), params);
while (c.next()) {
PropertyBusinessObject pb = (PropertyBusinessObject) propertyClass.newInstance();
for (PropertyBase p : pb.getPropertyIndex()) {
Row currentRow = c.getRow();
SqlType t = getSqlType(p);
if (t == SqlType.SQL_EXCLUDE) {
continue;
}
Object value = t.getValue(currentRow, c.getColumnIndex(getColumnName(p)), p);
if (p instanceof Property) {
((Property) p).set(value);
}
}
response.add(pb);
}
c.close();
return response;
} catch (Throwable t) {
Log.e(t);
if (c != null) {
c.close();
}
if (t instanceof IOException) {
throw ((IOException) t);
} else {
throw new IOException(t.toString());
}
}
}
use of com.codename1.db.Database in project CodenameOne by codenameone.
the class DatabaseTests method testOpenOrCreate.
private void testOpenOrCreate() throws Exception {
String dbName = "testdb";
// Start out fresh
Database.delete(dbName);
Database db = Database.openOrCreate(dbName);
this.assertNotNull(db, "Failed to open database " + dbName);
this.assertTrue(Database.exists(dbName), "Database.exists() returns false after openOrCreate");
String path = Database.getDatabasePath(dbName);
this.assertTrue(FileSystemStorage.getInstance().exists(path), "Database doesn't exist after creation. Expected to be at " + path);
db.close();
Database.delete(dbName);
this.assertTrue(!FileSystemStorage.getInstance().exists(path), "Failed to delete database. Still exists at " + path);
}
use of com.codename1.db.Database in project CodenameOne by codenameone.
the class DatabaseTests method testSimpleQueries.
private void testSimpleQueries() throws Exception {
String dbName = "testdb";
Database.delete(dbName);
Database db = Database.openOrCreate(dbName);
db.execute("create table tests (name text)");
db.execute("insert into tests values ('Steve'), ('Mike'), ('Ryan')");
Cursor c = db.executeQuery("select count(*) from tests");
c.next();
this.assertEqual(3, c.getRow().getInteger(0), "Expected result of 3 for count(*) after inserting 3 rows");
}
Aggregations