Пример #1
0
 /**
  * Store a list of records. A record that already exists will be updated. A transaction will be
  * used. If there is a problem with storing one of the records the whole operation will be rolled
  * back.
  *
  * @param records - the records to store or update
  * @throws DBException in case of a database problem
  * @throws IllegalArgumentException when the given record cannot be stored
  * @throws IllegalStateException when the columns that are part of the primary key have not all
  *     been assigned a value
  */
 public void store(List<Record> records)
     throws DBException, IllegalArgumentException, IllegalStateException {
   Boolean[] insert = new Boolean[records.size()];
   startTransaction();
   int r = 0;
   try {
     for (Record record : records)
       if (isStorable(record)) insert[r++] = doStore(record);
       else
         throw new IllegalArgumentException(
             String.format("Record (%s) cannot be stored!", record.toString(false)));
   } catch (Exception e) {
     rollbackTransactions();
     throw new DBException(e);
   }
   commitTransaction();
   // Inform client:
   r = 0;
   for (Record record : records) {
     Boolean inserted = insert[r++];
     if (inserted == null) continue; // record was unchanged
     else if (inserted) client.storageEvent(RecordOperation.Inserted, record.getReference(), this);
     else client.storageEvent(RecordOperation.Updated, record.getReference(), this);
   }
 }
Пример #2
0
 /**
  * Stores a single record, if it already exists it is updated. Note that this method does not
  * start a new transaction. If this is a desired the client code should take care of that by first
  * calling {@link #startTransaction()}. However, if an error occurs any open transaction will be
  * rolled back!
  *
  * @param record - the record to store or update; records of internal schemata will be rejected
  * @throws DBConstraintException when a table/index constraint is violated
  * @throws DBException in case of a database problem
  * @throws IllegalArgumentException when the given record cannot be stored
  * @throws IllegalStateException when the columns that are part of the primary key have not all
  *     been assigned a value
  */
 public void store(Record record)
     throws DBException, IllegalArgumentException, IllegalStateException {
   if (!isStorable(record))
     throw new IllegalArgumentException(
         String.format("Record (%s) cannot be stored!", record.toString(false)));
   Boolean insert = null;
   try {
     insert = doStore(record);
   } catch (DBException e) {
     rollbackTransactions(); // !!!
     throw e;
   }
   // Inform client:
   if (insert == null) return; // record was unchanged
   else if (insert) client.storageEvent(RecordOperation.Inserted, record.getReference(), this);
   else client.storageEvent(RecordOperation.Updated, record.getReference(), this);
 }
Пример #3
0
 /**
  * Deletes a single record. Note that this method does not start a new transaction. If this is a
  * desired the client code should take care of that by first calling {@link #startTransaction()}.
  * However, if an error occurs any open transaction will be rolled back!
  *
  * @param record - the record to delete
  * @throws DBException
  */
 public void delete(Record record) throws DBException {
   if (!isStorable(record)) return;
   try {
     doDelete(record);
   } catch (DBException e) {
     rollbackTransactions(); // !!!
     throw e;
   }
   // Inform client:
   client.storageEvent(RecordOperation.Deleted, record.getReference(), this);
 }
Пример #4
0
 /**
  * Insert a single record, if it already exists a DuplicateException will be thrown. Note that
  * this method does not start a new transaction. If this is a desired the client code should take
  * care of that by first calling {@link #startTransaction()}. However, if an error occurs any open
  * transaction will be rolled back!
  *
  * @param record
  * @throws DBPrimaryKeyException when the record already exists
  * @throws DBConstraintException when a table/index constraint is violated
  * @throws DBException in case of another database problem
  * @throws IllegalArgumentException when the given record cannot be stored
  * @throws IllegalStateException when the columns that are part of the primary key have not all
  *     been assigned a value
  */
 public void insert(Record record)
     throws DBPrimaryKeyException, DBConstraintException, DBException, IllegalArgumentException,
         IllegalStateException {
   if (!isStorable(record))
     throw new IllegalArgumentException(
         String.format("Record (%s) cannot be inserted!", record.toString(false)));
   boolean inserted = false;
   try {
     inserted = doInsert(record);
   } catch (DBException e) {
     rollbackTransactions(); // !!!
     throw e;
   }
   // Inform client if a real insert happened:
   if (inserted) client.storageEvent(RecordOperation.Inserted, record.getReference(), this);
 }
Пример #5
0
 /**
  * Deletes a series of records. A transaction will be used. Upon an error the whole operation will
  * be rolled back.
  *
  * @param records - the records to delete
  * @throws DBException
  */
 public void delete(Collection<Record> records) throws DBException {
   startTransaction();
   List<Record> deleted = new ArrayList<Record>(records.size());
   try {
     for (Record record : records)
       if (isStorable(record)) {
         if (doDelete(record)) deleted.add(record);
       }
   } catch (DBException e) {
     rollbackTransactions();
     throw e;
   }
   commitTransaction();
   // Inform client:
   for (Record record : deleted)
     client.storageEvent(RecordOperation.Deleted, record.getReference(), this);
 }