public void testAllFields() { Query query = Query.select().from(TestViewModel.VIEW) .leftJoin(Thing.TABLE, TestViewModel.TEST_MODEL_ID.eq(Thing.ID)); List<Field<?>> fields = query.getFields(); for (Property<?> p : TestViewModel.PROPERTIES) { assertTrue(fields.contains(p)); } for (Property<?> p : Thing.PROPERTIES) { assertTrue(fields.contains(p)); } assertEquals(TestViewModel.PROPERTIES.length + Thing.PROPERTIES.length, fields.size()); }
/** * Construct a LongProperty from a literal value and with the given alias, e.g. "1000000000000 AS trillion" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static LongProperty literal(long literal, String selectAs) { return new LongProperty(null, String.valueOf(literal), selectAs, null); }
public void testDefaultProjection() { final String BIRTHDAY = "birthday"; final String IS_HAPPY = "isHappy"; final List<Field<?>> columns = new ArrayList<>(); columns.add(TestModel.FIRST_NAME); columns.add(TestModel.LAST_NAME); columns.add(TestModel.BIRTHDAY.as(BIRTHDAY)); columns.add(Field.field(IS_HAPPY)); final String[] columnNames = {TestModel.FIRST_NAME.getName(), TestModel.LAST_NAME.getName(), BIRTHDAY, IS_HAPPY}; ProjectionMap map = new ProjectionMap(); map.put(TestModel.FIRST_NAME); map.put(TestModel.LAST_NAME); map.put(BIRTHDAY, TestModel.BIRTHDAY); map.put(IS_HAPPY); // test names the same String[] names = map.getDefaultProjectionNames(); assertTrue(Arrays.deepEquals(columnNames, names)); // test fields the same List<Field<?>> fields = map.getDefaultProjection(); assertTrue(fields.equals(columns)); }
/** * Same as {@link #updateRow(TableModel)} with the ability to specify a ConflictAlgorithm for handling constraint * violations * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if success, false otherwise */ protected final boolean updateRow(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isModified()) { // nothing changed return true; } if (!item.isSaved()) { return false; } Class<? extends TableModel> modelClass = item.getClass(); Table table = getTable(modelClass); Update update = Update.table(table).fromTemplate(item).where(table.getRowIdProperty().eq(item.getRowId())); if (conflictAlgorithm != null) { update.onConflict(conflictAlgorithm); } boolean result = updateInternal(update) > 0; if (result) { notifyForTable(DataChangedNotifier.DBOperation.UPDATE, item, table, item.getRowId()); item.markSaved(); } return result; }
/** * Utility method to get the rowid of the model, if it exists. * * @return {@value #NO_ID} if this model was not added to the database */ public long getRowId() { Long id = null; String idPropertyName = getRowIdProperty().getName(); if (setValues != null && setValues.containsKey(idPropertyName)) { id = (Long) setValues.get(idPropertyName); } else if (values != null && values.containsKey(idPropertyName)) { id = (Long) values.get(idPropertyName); } if (id != null) { return id; } return NO_ID; }
/** * Delete the row with the given row ID * * @param modelClass the model class corresponding to the table to delete from * @param id the row ID of the record * @return true if delete was successful */ public boolean delete(Class<? extends TableModel> modelClass, long id) { Table table = getTable(modelClass); int rowsUpdated = deleteInternal(Delete.from(table).where(table.getRowIdProperty().eq(id))); if (rowsUpdated > 0) { notifyForTable(DataChangedNotifier.DBOperation.DELETE, null, table, id); } return rowsUpdated > 0; }
/** * @param rowid the new rowid for this model * @return this model instance, to allow chaining calls */ public TableModel setRowId(long rowid) { if (rowid == NO_ID) { clearValue(getRowIdProperty()); } else { if (setValues == null) { setValues = newValuesStorage(); } setValues.put(getRowIdProperty().getName(), rowid); } return this; }
protected <TYPE extends TableModel> SquidCursor<TYPE> fetchItemById(Class<TYPE> modelClass, long id, Property<?>... properties) { Table table = getTable(modelClass); return fetchFirstItem(modelClass, table.getRowIdProperty().eq(id), properties); }
/** * Construct a LongProperty from a literal value and with the given alias, e.g. "1000000000000 AS trillion" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static LongProperty literal(long literal, String selectAs) { return new LongProperty(null, String.valueOf(literal), selectAs, null); }
/** * Construct a LongProperty from a {@link Function} and with the given alias, e.g. "SUM(column) AS sum" * * @param function the function * @param selectAs the alias to use. May be null. */ public static LongProperty fromFunction(Function<Long> function, String selectAs) { return new LongProperty(function, selectAs); }
.from(ThingJoin.SUBQUERY).orderBy(ThingJoin.THING_1_ID.asc())); try { assertEquals(3, cursor.getCount());
.end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test).from(TestModel.TABLE).orderBy(TestModel.ID.asc()), test, "negative", "zero", "positive"); .end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test).from(TestModel.TABLE).orderBy(TestModel.ID.asc()), test, "other", "zero", "default"); assertExpectedValues(Query.select(hasLongName).from(TestModel.TABLE).orderBy(TestModel.ID.asc()), hasLongName, 0, 0, 1); assertExpectedValues(Query.select(hasLongName2).from(TestModel.TABLE).orderBy(TestModel.ID.asc()), hasLongName2, FAIL, FAIL, PASS);
.set(TriggerTester.STR_1, Trigger.newValueOf(TestViewModel.TEST_NAME)) .set(TriggerTester.STR_2, Trigger.newValueOf(TestViewModel.EMPLOYEE_NAME)) .where(TriggerTester.ID.eq(Trigger.oldValueOf(TestViewModel.TEST_MODEL_ID))); Trigger updateInsteadOf = Trigger.insteadOf("view_update") .updateOn(TestViewModel.VIEW, TestViewModel.TEST_NAME, TestViewModel.EMPLOYEE_NAME) .where(TriggerTester.ID.eq(Trigger.oldValueOf(TestViewModel.TEST_MODEL_ID))); Trigger deleteInsteadOf = Trigger.insteadOf("view_delete").deleteOn(TestViewModel.VIEW) .perform(delete);
values.put(TestModel.FIRST_NAME.getName(), "A"); values.put(TestModel.LAST_NAME.getName(), "B"); values.put(TestModel.BIRTHDAY.getName(), 1); // Putting an int where long expected values.put(TestModel.IS_HAPPY.getName(), 1); // Putting an int where boolean expected values.put(TestModel.SOME_DOUBLE.getName(), 1); // Putting an int where double expected assertTrue(checkTypesOn.get(TestModel.FIRST_NAME.getName()) instanceof String); assertTrue(checkTypesOn.get(TestModel.LAST_NAME.getName()) instanceof String); assertTrue(checkTypesOn.get(TestModel.BIRTHDAY.getName()) instanceof Long); assertTrue(checkTypesOn.get(TestModel.IS_HAPPY.getName()) instanceof Boolean); assertTrue(checkTypesOn.get(TestModel.SOME_DOUBLE.getName()) instanceof Double);
public void testJoinOnSameTableUsingAlias() { int rowsWithManager = database.count(Employee.class, Employee.MANAGER_ID.gt(0)); assertEquals(5, rowsWithManager); StringProperty managerName = Employee.NAME.as(managerTable, "managerName"); LongProperty managerId = managerTable.qualifyField(Employee.ID); Join join = Join.inner(managerTable, Employee.MANAGER_ID.eq(managerId)); Query query = Query.select(employeeName, managerName).from(Employee.TABLE).join(join).orderBy(managerId.asc());
.updateOn(TriggerTester.TABLE, TriggerTester.VALUE_1) .perform(Update.table(TriggerTester.TABLE).set(TriggerTester.VALUE_2, TriggerTester.VALUE_1) .where(TriggerTester.ID.eq(Trigger.newValueOf(TriggerTester.ID)))); CompiledStatement compiledTrigger = trigger.compile(database.getCompileContext()); .where(TriggerTester.ID.eq(idTest2)); CompiledStatement compiledUpdate = update.compile(database.getCompileContext()); database.tryExecSql(compiledUpdate.sql, compiledUpdate.sqlArgs);
public void testPropertyAliasing() { LongProperty p = TestModel.ID; assertEquals(p.getQualifiedExpression(), "testModels._id"); assertEquals(p.getExpression(), "_id"); assertEquals(p.getName(), "_id"); LongProperty basicAlias = p.as("newAlias"); assertEquals(p.tableModelName, basicAlias.tableModelName); assertEquals(p.getExpression(), basicAlias.getExpression()); assertEquals("newAlias", basicAlias.getName()); assertEquals("SELECT testModels._id AS newAlias", Query.select(basicAlias).toString()); LongProperty aliasWithTable = p.as("newTable", "newAlias"); assertEquals(TestModel.class, aliasWithTable.tableModelName.modelClass); assertEquals("newTable", aliasWithTable.tableModelName.tableName); assertEquals(p.getExpression(), aliasWithTable.getExpression()); assertEquals("newAlias", aliasWithTable.getName()); assertEquals("SELECT newTable._id AS newAlias", Query.select(aliasWithTable).toString()); assertEquals(TestViewModel.class, asSelectionFromTableNoAlias.tableModelName.modelClass); assertEquals(TestViewModel.VIEW.getName(), asSelectionFromTableNoAlias.tableModelName.tableName); assertEquals(basicAlias.getName(), asSelectionFromTableNoAlias.getExpression()); assertEquals(basicAlias.getName(), asSelectionFromTableNoAlias.getName()); assertFalse(asSelectionFromTableNoAlias.hasAlias()); assertEquals("SELECT testView.newAlias AS newAlias", Query.select(asSelectionFromTableNoAlias).toString()); LongProperty asSelectionFromTableWithAlias = basicAlias.asSelectionFromTable(TestViewModel.VIEW, "superAlias"); assertEquals(TestViewModel.class, asSelectionFromTableWithAlias.tableModelName.modelClass); assertEquals(TestViewModel.VIEW.getName(), asSelectionFromTableWithAlias.tableModelName.tableName); assertEquals(basicAlias.getName(), asSelectionFromTableWithAlias.getExpression());
.updateOn(TriggerTester.TABLE, TriggerTester.VALUE_1) .perform(Update.table(TriggerTester.TABLE).set(TriggerTester.VALUE_2, TriggerTester.VALUE_1) .where(TriggerTester.ID.eq(Trigger.newValueOf(TriggerTester.ID)))); CompiledStatement compiledTrigger = trigger.compile(database.getCompileContext()); .where(TriggerTester.ID.eq(idTest2)); CompiledStatement compiledUpdate = update.compile(database.getCompileContext()); database.tryExecSql(compiledUpdate.sql, compiledUpdate.sqlArgs);