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;
}