/** * @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; }
ISQLitePreparedStatement preparedStatement = insertCache.getPreparedInsert(this, table, conflictAlgorithm); item.bindValuesForInsert(table, preparedStatement); newRow = preparedStatement.executeInsert(); } finally { if (result) { notifyForTable(DataChangedNotifier.DBOperation.INSERT, item, table, newRow); item.setRowId(newRow); item.markSaved();
@Override public Void visitDouble(Property<Double> property, ISQLitePreparedStatement preparedStatement, ModelAndIndex data) { Double val = data.model.get(property, false); if (val == null) { preparedStatement.bindNull(data.index); } else { preparedStatement.bindDouble(data.index, val); } return null; }
/** * Save a model to the database. Creates a new row if the model does not have an ID, otherwise updates the row with * the corresponding row ID. If a new row is inserted, the model will have its ID set to the corresponding row ID. * Any constraint violations will be resolved using the specified * {@link com.yahoo.squidb.sql.TableStatement.ConflictAlgorithm ConflictAlgorithm}. * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if current the model data is stored in the database * @see #persist(TableModel) */ public boolean persistWithOnConflict(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isSaved()) { return insertRow(item, conflictAlgorithm); } if (!item.isModified()) { return true; } return updateRow(item, conflictAlgorithm); }
/** * 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; }
void bindValuesForInsert(Table table, ISQLitePreparedStatement preparedInsert) { LongProperty rowidProperty = getRowIdProperty(); Property<?>[] allProperties = table.getProperties(); ModelAndIndex modelAndIndex = new ModelAndIndex(this); for (Property<?> property : allProperties) { if (property == rowidProperty) { long rowid = getRowId(); if (rowid == TableModel.NO_ID) { preparedInsert.bindNull(modelAndIndex.index); } else { preparedInsert.bindLong(modelAndIndex.index, rowid); } } else { property.accept(valueBindingVisitor, preparedInsert, modelAndIndex); } modelAndIndex.index++; } }
/** * Deprecated alias for {@link #getRowIdProperty()} */ @Deprecated public LongProperty getIdProperty() { return getRowIdProperty(); }
/** * @return true if this model has been persisted to the database */ public boolean isSaved() { return getRowId() != NO_ID; }
private long insertRowLegacy(TableModel item, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { ValuesStorage mergedValues = item.getMergedValues(); if (mergedValues.size() == 0) { return -1; } Insert insert = Insert.into(table).fromValues(mergedValues); if (conflictAlgorithm != null) { insert.onConflict(conflictAlgorithm); } return insertInternal(insert); }
/** * 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; }
void bindValuesForInsert(Table table, ISQLitePreparedStatement preparedInsert) { LongProperty rowidProperty = getRowIdProperty(); Property<?>[] allProperties = table.getProperties(); ModelAndIndex modelAndIndex = new ModelAndIndex(this); for (Property<?> property : allProperties) { if (property == rowidProperty) { long rowid = getRowId(); if (rowid == TableModel.NO_ID) { preparedInsert.bindNull(modelAndIndex.index); } else { preparedInsert.bindLong(modelAndIndex.index, rowid); } } else { property.accept(valueBindingVisitor, preparedInsert, modelAndIndex); } modelAndIndex.index++; } }
/** * 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; }
/** * Save a model to the database. Creates a new row if the model does not have an ID, otherwise updates the row with * the corresponding row ID. If a new row is inserted, the model will have its ID set to the corresponding row ID. * Any constraint violations will be resolved using the specified * {@link com.yahoo.squidb.sql.TableStatement.ConflictAlgorithm ConflictAlgorithm}. * * @param item the model to save * @param conflictAlgorithm the conflict algorithm to use * @return true if current the model data is stored in the database * @see #persist(TableModel) */ public boolean persistWithOnConflict(TableModel item, TableStatement.ConflictAlgorithm conflictAlgorithm) { if (!item.isSaved()) { return insertRow(item, conflictAlgorithm); } if (!item.isModified()) { return true; } return updateRow(item, conflictAlgorithm); }
/** * Deprecated alias for {@link #getRowId()} */ @Deprecated public long getId() { return getRowId(); }
private long insertRowLegacy(TableModel item, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { ValuesStorage mergedValues = item.getMergedValues(); if (mergedValues.size() == 0) { return -1; } Insert insert = Insert.into(table).fromValues(mergedValues); if (conflictAlgorithm != null) { insert.onConflict(conflictAlgorithm); } return insertInternal(insert); }
/** * @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; }
ISQLitePreparedStatement preparedStatement = insertCache.getPreparedInsert(this, table, conflictAlgorithm); item.bindValuesForInsert(table, preparedStatement); newRow = preparedStatement.executeInsert(); } finally { if (result) { notifyForTable(DataChangedNotifier.DBOperation.INSERT, item, table, newRow); item.setRowId(newRow); item.markSaved();
@Override public Void visitBoolean(Property<Boolean> property, ISQLitePreparedStatement preparedStatement, ModelAndIndex data) { Boolean val = data.model.get(property, false); if (val == null) { preparedStatement.bindNull(data.index); } else { preparedStatement.bindLong(data.index, val ? 1 : 0); } return null; }
/** * Construct a SquidCursorAdapter that will use the model class's default id property to implement * {@link #getItemId(int)}. * * @param model an instance of the model type to use for this cursor. See note at the top of this file. * @see #SquidCursorAdapter(AbstractModel, Property) */ public SquidCursorAdapter(T model) { this(model, model instanceof TableModel ? ((TableModel) model).getRowIdProperty() : null); }
/** * @return true if this model has been persisted to the database */ public boolean isSaved() { return getRowId() != NO_ID; }