示例#1
0
        public void TestSetup()
        {
            var tableInfo = new TableInfo(ObjectName.Parse("APP.people"));

            tableInfo.AddColumn("id", PrimitiveTypes.Bit());
            tableInfo.AddColumn("first_name", PrimitiveTypes.String(), true);
            tableInfo.AddColumn("last_name", PrimitiveTypes.String());
            tableInfo.AddColumn("age", PrimitiveTypes.TinyInt());
            table = new TemporaryTable(tableInfo);

            var tempTable = (TemporaryTable)table;

            tempTable.NewRow(new[] {
                DataObject.BigInt(1),
                DataObject.String("Antonello"),
                DataObject.String("Provenzano"),
                DataObject.Null()
            });
            tempTable.NewRow(new[] {
                DataObject.BigInt(2),
                DataObject.String("Moritz"),
                DataObject.String("Krull"),
                DataObject.TinyInt(31)
            });

            tempTable.BuildIndexes();
        }
示例#2
0
        public void SelectRowsWhereStaticId(int id, int expectedRow)
        {
            var result = table.SelectRows(0, SqlExpressionType.Equal, DataObject.BigInt(id));
            var list   = result.ToList();

            Assert.IsNotEmpty(list);
            Assert.AreEqual(1, list.Count);
            Assert.AreEqual(expectedRow, list[0]);
        }
示例#3
0
        private void AddRow(TemporaryTable tmpTable, long id, string name, DateTimeOffset date)
        {
            var row = new DataObject[3];

            row[0] = DataObject.BigInt(id);
            row[1] = DataObject.String(name);
            row[2] = DataObject.Date(date);
            tmpTable.NewRow(row);
        }
示例#4
0
        private static Privileges QueryPrivileges(IQuery queryContext, ITable grantTable, string grantee,
                                                  DbObjectType objectType, ObjectName objectName, bool withOption, bool withPublic)
        {
            var objectCol      = grantTable.GetResolvedColumnName(1);
            var paramCol       = grantTable.GetResolvedColumnName(2);
            var granteeCol     = grantTable.GetResolvedColumnName(3);
            var grantOptionCol = grantTable.GetResolvedColumnName(4);
            var granterCol     = grantTable.GetResolvedColumnName(5);

            ITable t1 = grantTable;

            // All that match the given object parameter
            // It's most likely this will reduce the search by the most so we do
            // it first.
            t1 = t1.SimpleSelect(queryContext, paramCol, SqlExpressionType.Equal, SqlExpression.Constant(DataObject.String(objectName.FullName)));

            // The next is a single exhaustive select through the remaining records.
            // It finds all grants that match either public or the grantee is the
            // username, and that match the object type.

            // Expression: ("grantee_col" = username OR "grantee_col" = 'public')
            var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol), SqlExpression.Constant(DataObject.String(grantee)));

            if (withPublic)
            {
                userCheck = SqlExpression.Or(userCheck, SqlExpression.Equal(SqlExpression.Reference(granteeCol),
                                                                            SqlExpression.Constant(DataObject.String(User.PublicName))));
            }

            // Expression: ("object_col" = object AND
            //              ("grantee_col" = username OR "grantee_col" = 'public'))
            // All that match the given username or public and given object
            var expr = SqlExpression.And(SqlExpression.Equal(SqlExpression.Reference(objectCol),
                                                             SqlExpression.Constant(DataObject.BigInt((int)objectType))), userCheck);

            // Are we only searching for grant options?
            if (withOption)
            {
                var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
                                                           SqlExpression.Constant(DataObject.BooleanTrue));
                expr = SqlExpression.And(expr, grantOptionCheck);
            }

            t1 = t1.ExhaustiveSelect(queryContext, expr);

            // For each grant, merge with the resultant priv object
            Privileges privs = Privileges.None;

            foreach (var row in t1)
            {
                var priv = (int)row.GetValue(0).AsBigInt();
                privs |= (Privileges)priv;
            }

            return(privs);
        }
示例#5
0
        public void SelectGreater()
        {
            var id = DataObject.BigInt(1);

            var result = table.SelectRowsGreater(0, id);

            Assert.IsNotNull(result);
            Assert.IsNotEmpty(result);
            Assert.AreEqual(2, result.Count());

            Assert.AreEqual(1, result.First());
        }
示例#6
0
        private void RevokeAllGrantsFromUser(DbObjectType objectType, ObjectName objectName, string revoker, string user, bool withOption = false)
        {
            var grantTable = QueryContext.GetMutableTable(SystemSchema.UserGrantsTableName);

            var objectCol      = grantTable.GetResolvedColumnName(1);
            var paramCol       = grantTable.GetResolvedColumnName(2);
            var granteeCol     = grantTable.GetResolvedColumnName(3);
            var grantOptionCol = grantTable.GetResolvedColumnName(4);
            var granterCol     = grantTable.GetResolvedColumnName(5);

            ITable t1 = grantTable;

            // All that match the given object parameter
            // It's most likely this will reduce the search by the most so we do
            // it first.
            t1 = t1.SimpleSelect(QueryContext, paramCol, SqlExpressionType.Equal,
                                 SqlExpression.Constant(DataObject.String(objectName.FullName)));

            // The next is a single exhaustive select through the remaining records.
            // It finds all grants that match either public or the grantee is the
            // username, and that match the object type.

            // Expression: ("grantee_col" = username)
            var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol),
                                                SqlExpression.Constant(DataObject.String(user)));

            // Expression: ("object_col" = object AND
            //              "grantee_col" = username)
            // All that match the given username or public and given object
            var expr =
                SqlExpression.And(
                    SqlExpression.Equal(SqlExpression.Reference(objectCol),
                                        SqlExpression.Constant(DataObject.BigInt((int)objectType))), userCheck);

            // Are we only searching for grant options?
            var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
                                                       SqlExpression.Constant(DataObject.Boolean(withOption)));

            expr = SqlExpression.And(expr, grantOptionCheck);

            // Make sure the granter matches up also
            var granterCheck = SqlExpression.Equal(SqlExpression.Reference(granterCol),
                                                   SqlExpression.Constant(DataObject.String(revoker)));

            expr = SqlExpression.And(expr, granterCheck);

            t1 = t1.ExhaustiveSelect(QueryContext, expr);

            // Remove these rows from the table
            grantTable.Delete(t1);
        }
示例#7
0
        public void SelectEqualTwoColumns()
        {
            var name = DataObject.String("test1");
            var id   = DataObject.BigInt(1);

            var result = table.SelectRowsEqual(1, name, 0, id);

            Assert.IsNotNull(result);
            Assert.IsNotEmpty(result);

            var index = result.First();

            Assert.AreEqual(0, index);
        }
示例#8
0
        private ISequence CreateNativeTableSequence(ObjectName tableName)
        {
            var table    = Transaction.GetMutableTable(SystemSchema.SequenceInfoTableName);
            var uniqueId = Transaction.NextTableId(SystemSchema.SequenceInfoTableName);

            var dataRow = table.NewRow();

            dataRow.SetValue(0, DataObject.Number(uniqueId));
            dataRow.SetValue(1, DataObject.VarChar(tableName.Parent.FullName));
            dataRow.SetValue(2, DataObject.VarChar(tableName.Name));
            dataRow.SetValue(3, DataObject.BigInt(1));
            table.AddRow(dataRow);

            return(new Sequence(this, uniqueId, SequenceInfo.Native(tableName)));
        }
示例#9
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));
        }
示例#10
0
        /// <summary>
        /// Updates the state of the sequence key in the sequence tables in the
        /// database.
        /// </summary>
        /// <param name="sequence"></param>
        /// <remarks>
        /// The update occurs on an independent transaction.
        /// </remarks>
        private void UpdateSequenceState(Sequence sequence)
        {
            // We need to update the sequence key state.

            // The sequence table
            var seq = Transaction.GetMutableTable(SystemSchema.SequenceTableName);

            // Find the row with the id for this sequence.
            var list = seq.SelectRowsEqual(0, DataObject.Number(sequence.Id)).ToList();

            // Checks
            var count = list.Count();

            if (count == 0)
            {
                throw new ObjectNotFoundException(sequence.FullName);
            }

            if (count > 1)
            {
                throw new Exception("Assert failed: multiple id for sequence.");
            }

            // Create the DataRow
            var dataRow = seq.GetRow(list.First());

            // Set the content of the row data
            dataRow.SetValue(0, DataObject.Number(sequence.Id));
            dataRow.SetValue(1, DataObject.Number(sequence.LastValue));
            dataRow.SetValue(2, DataObject.Number(sequence.SequenceInfo.Increment));
            dataRow.SetValue(3, DataObject.Number(sequence.SequenceInfo.MinValue));
            dataRow.SetValue(4, DataObject.Number(sequence.SequenceInfo.MaxValue));
            dataRow.SetValue(5, DataObject.Number(sequence.SequenceInfo.StartValue));
            dataRow.SetValue(6, DataObject.BigInt(sequence.SequenceInfo.Cache));
            dataRow.SetValue(7, DataObject.Boolean(sequence.SequenceInfo.Cycle));

            // Update the row
            seq.UpdateRow(dataRow);
        }
示例#11
0
        public void ScalarWithTwoArgument()
        {
            Factory2 factory2 = null;

            Assert.DoesNotThrow(() => factory2 = new Factory2());

            IFunction function = null;
            var       args     = new DataObject[] { DataObject.BigInt(2), DataObject.Number(new SqlNumber(54)) };

            Assert.DoesNotThrow(() => function = factory2.ResolveFunction("add2", args));
            Assert.IsNotNull(function);

            InvokeResult result = null;

            Assert.DoesNotThrow(() => result = function.Execute(args));
            Assert.IsNotNull(result);

            Assert.IsInstanceOf <SqlNumber>(result.ReturnValue.Value);

            var value = ((SqlNumber)result.ReturnValue.Value).ToInt64();

            Assert.AreEqual(56, value);
        }
示例#12
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
                });
            }
示例#13
0
文件: Row.cs 项目: meikeric/deveeldb
 public void SetValue(int columnIndex, long value)
 {
     SetValue(columnIndex, DataObject.BigInt(value));
 }