Deveel.Data.Sql.Sequences.SequenceManager.GetSequence C# (CSharp) Method

GetSequence() public method

public GetSequence ( ObjectName sequenceName ) : ISequence
sequenceName ObjectName
return ISequence
        public ISequence GetSequence(ObjectName sequenceName)
        {
            // Is the generator already in the cache?
            ISequence sequence;

            if (!seqCache.TryGet(sequenceName, out sequence)) {
                // This sequence generator is not in the cache so we need to query the
                // sequence table for this.
                var seqi = Transaction.GetTable(SequenceInfoTableName);

                var schemaVal = Field.VarChar(sequenceName.Parent.FullName);
                var nameVal = Field.VarChar(sequenceName.Name);
                var list = seqi.SelectRowsEqual(2, nameVal, 1, schemaVal).ToList();

                if (list.Count == 0) {
                    throw new ArgumentException(String.Format("Sequence '{0}' not found.", sequenceName));
                } else if (list.Count() > 1) {
                    throw new Exception("Assert failed: multiple sequence keys with same name.");
                }

                int rowIndex = list.First();
                var sid = seqi.GetValue(rowIndex, 0);
                var sschema = seqi.GetValue(rowIndex, 1);
                var sname = seqi.GetValue(rowIndex, 2);
                var stype = seqi.GetValue(rowIndex, 3);

                // Is this a custom sequence generator?
                // (stype == 1) == true
                if (stype.IsEqualTo(OneValue)) {
                    // Native generator.
                    sequence = new Sequence(this, (SqlNumber) sid.Value, SequenceInfo.Native(sequenceName));
                } else {
                    // Query the sequence table.
                    var seq = Transaction.GetTable(SequenceTableName);

                    list = seq.SelectRowsEqual(0, sid).ToList();

                    if (!list.Any())
                        throw new Exception("Sequence table does not contain sequence information.");
                    if (list.Count() > 1)
                        throw new Exception("Sequence table contains multiple generators for id.");

                    rowIndex = list.First();
                    var lastValue = (SqlNumber) seq.GetValue(rowIndex, 1).Value;
                    var increment = (SqlNumber) seq.GetValue(rowIndex, 2).Value;
                    var minvalue = (SqlNumber) seq.GetValue(rowIndex, 3).Value;
                    var maxvalue = (SqlNumber) seq.GetValue(rowIndex, 4).Value;
                    var start = (SqlNumber) seq.GetValue(rowIndex, 5).Value;
                    var cache = (long) seq.GetValue(rowIndex, 6).AsBigInt();
                    bool cycle = seq.GetValue(rowIndex, 7).AsBoolean();

                    var info = new SequenceInfo(sequenceName, start, increment, minvalue, maxvalue, cache, cycle);
                    sequence = new Sequence(this, (SqlNumber) sid.Value, lastValue, info);

                    // Put the generator in the cache
                    seqCache.Set(sequenceName, sequence);
                }

            }

            // Return the generator
            return sequence;
        }

Usage Example

Example #1
0
            public ITable GetTable(int offset)
            {
                var table    = transaction.GetTable(SystemSchema.SequenceInfoTableName);
                int p        = 0;
                int rowIndex = -1;

                foreach (var row in table)
                {
                    var seqType = row.GetValue(3);
                    if (seqType.IsEqualTo(OneValue))
                    {
                        if (p == offset)
                        {
                            rowIndex = row.RowId.RowNumber;
                            break;
                        }

                        p++;
                    }
                }

                if (rowIndex == -1)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }

                var seqId  = table.GetValue(rowIndex, 0);
                var schema = ObjectName.Parse(table.GetValue(rowIndex, 1).Value.ToString());
                var name   = table.GetValue(rowIndex, 2).Value.ToString();

                var tableName = new ObjectName(schema, name);

                // Find this id in the 'sequence' table
                var seqTable = transaction.GetTable(SystemSchema.SequenceTableName);

                var index = seqTable.GetIndex(0);
                var list  = index.SelectEqual(seqId);

                if (!list.Any())
                {
                    throw new Exception("No SEQUENCE table entry for sequence.");
                }

                int seqRowI = list.First();

                // Generate the DataTableInfo
                var tableInfo = CreateTableInfo(schema, name);

                // Last value for this sequence generated by the transaction
                DataObject lastValue;

                try {
                    var sequence = manager.GetSequence(tableName);
                    if (sequence == null)
                    {
                        throw new ObjectNotFoundException(tableName);
                    }

                    lastValue = DataObject.Number(sequence.GetCurrentValue());
                } catch (Exception) {
                    lastValue = DataObject.BigInt(-1);
                }

                // The current value of the sequence generator
                var currentValue = DataObject.Number(manager.GetCurrentValue(tableName));

                // Read the rest of the values from the SEQUENCE table.
                var topValue    = seqTable.GetValue(seqRowI, 1);
                var incrementBy = seqTable.GetValue(seqRowI, 2);
                var minValue    = seqTable.GetValue(seqRowI, 3);
                var maxValue    = seqTable.GetValue(seqRowI, 4);
                var start       = seqTable.GetValue(seqRowI, 5);
                var cache       = seqTable.GetValue(seqRowI, 6);
                var cycle       = seqTable.GetValue(seqRowI, 7);


                return(new SequenceTable(transaction.Database.Context, tableInfo)
                {
                    TopValue = topValue,
                    LastValue = lastValue,
                    CurrentValue = currentValue,
                    Increment = incrementBy,
                    MinValue = minValue,
                    MaxValue = maxValue,
                    Start = start,
                    Cache = cache,
                    Cycle = cycle
                });
            }