Esempio n. 1
0
        public void CreateNormalSequence()
        {
            var sequenceManager = new SequenceManager(transaction);

            var sequenceName = ObjectName.Parse("APP.test_sequence");
            var seqInfo = new SequenceInfo(sequenceName, new SqlNumber(0), new SqlNumber(1), new SqlNumber(0), new SqlNumber(Int64.MaxValue), 126);

            ISequence sequence =null;
            Assert.DoesNotThrow(() => sequence = sequenceManager.CreateSequence(seqInfo));
            Assert.IsNotNull(sequence);
        }
Esempio n. 2
0
        public void TestSetup()
        {
            testSequenceName = ObjectName.Parse("APP.test_sequence");

            var dbConfig = Configuration.Configuration.Empty;
            dbConfig.SetValue(DatabaseConfigKeys.DatabaseName, "testdb");

            var systemContext = new SystemContext(Configuration.Configuration.SystemDefault);
            var dbContext = new DatabaseContext(systemContext, dbConfig);
            var database = new Database(dbContext);
            database.Create("SA", "12345");
            database.Open();

            transaction = database.CreateTransaction(TransactionIsolation.Serializable);

            if (TestContext.CurrentContext.Test.Name != "CreateNormalSequence") {
                var seqInfo = new SequenceInfo(testSequenceName, new SqlNumber(0), new SqlNumber(1), new SqlNumber(0), new SqlNumber(Int64.MaxValue), 126);
                transaction.CreateSequence(seqInfo);
            }
        }
Esempio n. 3
0
 public static void CreateSequence(this ITransaction transaction, SequenceInfo sequenceInfo)
 {
     transaction.CreateObject(sequenceInfo);
 }
Esempio n. 4
0
 public Sequence(SequenceManager manager, SqlNumber id, SqlNumber lastValue, SequenceInfo sequenceInfo)
 {
     this.manager = manager;
     Id = id;
     FullName = sequenceInfo.SequenceName;
     SequenceInfo = sequenceInfo;
     LastValue = lastValue;
     CurrentValue = lastValue;
 }
Esempio n. 5
0
 public Sequence(SequenceManager manager, SqlNumber id, SequenceInfo sequenceInfo)
     : this(manager, id, SqlNumber.Null, sequenceInfo)
 {
 }
Esempio n. 6
0
        private Sequence CreateCustomSequence(ObjectName sequenceName, SequenceInfo sequenceInfo)
        {
            // The SEQUENCE and SEQUENCE_INFO table
            var seq = Transaction.GetMutableTable(SystemSchema.SequenceTableName);
            var seqi = Transaction.GetMutableTable(SystemSchema.SequenceInfoTableName);

            var list = seqi.SelectRowsEqual(2, DataObject.VarChar(sequenceName.Name), 1, DataObject.VarChar(sequenceName.Parent.FullName));
            if (list.Any())
                throw new Exception(String.Format("Sequence generator with name '{0}' already exists.", sequenceName));

            // Generate a unique id for the sequence info table
            var uniqueId = Transaction.NextTableId(SystemSchema.SequenceInfoTableName);

            // Insert the new row
            var dataRow = seqi.NewRow();
            dataRow.SetValue(0, DataObject.Number(uniqueId));
            dataRow.SetValue(1, DataObject.VarChar(sequenceName.Parent.FullName));
            dataRow.SetValue(2, DataObject.VarChar(sequenceName.Name));
            dataRow.SetValue(3, DataObject.BigInt(2));
            seqi.AddRow(dataRow);

            // Insert into the SEQUENCE table.
            dataRow = seq.NewRow();
            dataRow.SetValue(0, DataObject.Number(uniqueId));
            dataRow.SetValue(1, DataObject.Number(sequenceInfo.StartValue));
            dataRow.SetValue(2, DataObject.Number(sequenceInfo.Increment));
            dataRow.SetValue(3, DataObject.Number(sequenceInfo.MinValue));
            dataRow.SetValue(4, DataObject.Number(sequenceInfo.MaxValue));
            dataRow.SetValue(5, DataObject.Number(sequenceInfo.StartValue));
            dataRow.SetValue(6, DataObject.BigInt(sequenceInfo.Cache));
            dataRow.SetValue(7, DataObject.Boolean(sequenceInfo.Cycle));
            seq.AddRow(dataRow);

            return new Sequence(this, uniqueId, sequenceInfo);
        }
Esempio n. 7
0
        public ISequence GetSequence(ObjectName sequenceName)
        {
            // Is the generator already in the cache?
            Sequence sequence;

            if (!sequenceKeyMap.TryGetValue(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(SystemSchema.SequenceInfoTableName);

                var schemaVal = DataObject.VarChar(sequenceName.Parent.FullName);
                var nameVal = DataObject.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(SystemSchema.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
                    sequenceKeyMap[sequenceName] = sequence;
                }

            }

            // Return the generator
            return sequence;
        }
Esempio n. 8
0
        public ISequence CreateSequence(SequenceInfo sequenceInfo)
        {
            if (sequenceInfo == null)
                throw new ArgumentNullException("sequenceInfo");

            var sequenceName = sequenceInfo.SequenceName;

            // If the Sequence or SequenceInfo tables don't exist then
            // We can't add or remove native tables
            if (sequenceName.Equals(SystemSchema.SequenceTableName) ||
                sequenceName.Equals(SystemSchema.SequenceInfoTableName) ||
                !Transaction.TableExists(SystemSchema.SequenceTableName) ||
                !Transaction.TableExists(SystemSchema.SequenceInfoTableName)) {
                return null;
            }

            if (sequenceInfo.Type == SequenceType.Native)
                return CreateNativeTableSequence(sequenceName);

            return CreateCustomSequence(sequenceName, sequenceInfo);
        }