sequence method

Future<int> sequence(
  1. SqfEntitySequenceBase seq,
  2. bool increase, {
  3. bool reset = false,
})

Implementation

Future<int> sequence(SqfEntitySequenceBase seq, bool increase,
    {bool reset = false}) async {
  final Database db = (await this.db)!;
  if (reset) {
    await db.execute(
        'UPDATE sqfentitysequences SET value=${seq.startWith} where id=?',
        [seq.sequenceName]);
    return seq.startWith;
  }
  final resCurrent = await db.rawQuery(
      'SELECT value from sqfentitysequences where id=?', [seq.sequenceName]);
  final currentVal = resCurrent.first.values.first as int;
  if (!increase) {
    return currentVal;
  } else {
    final nextVal = currentVal + seq.incrementBy;
    if (nextVal >= seq.minValue && nextVal <= seq.maxValue) {
      await db.execute(
          'UPDATE sqfentitysequences SET value=$nextVal where id=?',
          [seq.sequenceName]);
      return nextVal;
    } else if (seq.cycle!) {
      return sequence(seq, true, reset: true);
    } else {
      throw Exception(
          'SQFENTITIY: sequence (${seq.sequenceName}) exceeds its minValue or maxValue ');
    }
  }
}