Ejemplo n.º 1
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            if (ob1.IsNull)
            {
                return(ob1);
            }
            if (ob2.IsNull)
            {
                return(ob2);
            }

            char cEscape = '\\';

            if (Escape is ConstantExpression)
            {
                // TODO: some more checks...
                var escapeValue = ((ConstantExpression)Escape).Value;
                cEscape = escapeValue.ToString()[0];
            }

            string val     = ob1.CastTo(PrimitiveTypes.String()).ToStringValue();
            string pattern = ob2.CastTo(PrimitiveTypes.String()).ToStringValue();

            return(DataObject.Boolean(PatternSearch.FullPatternMatch(pattern, val, cEscape)));
        }
Ejemplo n.º 2
0
        private void AddTestData(IQuery context)
        {
            var table = context.GetMutableTable(ObjectName.Parse("APP.people"));
            var row   = table.NewRow();

            // row.SetValue("id", DataObject.Integer(0));
            row.SetDefault(0, context);
            row.SetValue("first_name", DataObject.String("John"));
            row.SetValue("last_name", DataObject.String("Doe"));
            row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1977, 01, 01)));
            row.SetValue("active", DataObject.Boolean(false));
            table.AddRow(row);

            row = table.NewRow();

            // row.SetValue("id", DataObject.Integer(1));
            row.SetDefault(0, context);
            row.SetValue("first_name", DataObject.String("Jane"));
            row.SetValue("last_name", DataObject.String("Doe"));
            row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1978, 11, 01)));
            row.SetValue("active", DataObject.Boolean(true));
            table.AddRow(row);

            row = table.NewRow();

            // row.SetValue("id", DataObject.Integer(2));
            row.SetDefault(0, context);
            row.SetValue("first_name", DataObject.String("Roger"));
            row.SetValue("last_name", DataObject.String("Rabbit"));
            row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1985, 05, 05)));
            row.SetValue("active", DataObject.Boolean(true));
            table.AddRow(row);

            context.Commit();
        }
Ejemplo n.º 3
0
        private static SqlExpression VisitConstantExpression(SqlConstantExpressionNode expressionNode)
        {
            var        sqlValue = expressionNode.Value;
            DataObject obj;

            if (sqlValue is SqlString)
            {
                obj = DataObject.VarChar((SqlString)sqlValue);
            }
            else if (sqlValue is SqlBoolean)
            {
                obj = DataObject.Boolean((SqlBoolean)sqlValue);
            }
            else if (sqlValue is SqlNumber)
            {
                obj = DataObject.Number((SqlNumber)sqlValue);
            }
            else if (sqlValue is SqlNull)
            {
                obj = DataObject.Null();
            }
            else
            {
                throw new NotSupportedException("Constant value not supported.");
            }

            return(SqlExpression.Constant(obj));
        }
Ejemplo n.º 4
0
        private void AddTestData()
        {
            var table = Query.GetMutableTable(ObjectName.Parse("APP.test_table"));
            var row   = table.NewRow();

            row.SetValue("first_name", DataObject.String("John"));
            row.SetValue("last_name", DataObject.String("Doe"));
            row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1977, 01, 01)));
            row.SetValue("active", DataObject.Boolean(false));
            table.AddRow(row);

            row = table.NewRow();
            row.SetValue("first_name", DataObject.String("Jane"));
            row.SetValue("last_name", DataObject.String("Doe"));
            row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1978, 11, 01)));
            row.SetValue("active", DataObject.Boolean(true));
            table.AddRow(row);

            row = table.NewRow();
            row.SetValue("first_name", DataObject.String("Roger"));
            row.SetValue("last_name", DataObject.String("Rabbit"));
            row.SetValue("birth_date", DataObject.Date(new SqlDateTime(1985, 05, 05)));
            row.SetValue("active", DataObject.Boolean(true));
            table.AddRow(row);
        }
Ejemplo n.º 5
0
        public static DataObject Contains(DataObject geometry, DataObject other)
        {
            var g1     = (SqlGeometry)geometry.Value;
            var g2     = (SqlGeometry)other.Value;
            var result = Contains(g1, g2);

            return(DataObject.Boolean(result));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 7
0
        public void CastBooleanTrueToString()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(DataObject.Boolean(true)), PrimitiveTypes.String());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <StringType>(value.Type);
            Assert.AreEqual(SqlTypeCode.String, value.Type.TypeCode);
            Assert.AreEqual(new SqlString("True"), value.Value);
        }
Ejemplo n.º 8
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            bool?b1 = ob1.ToBoolean();
            bool?b2 = ob2.ToBoolean();

            // If either ob1 or ob2 are null
            if (!b1.HasValue)
            {
                return(b2.HasValue && b2.Equals(false) ? DataObject.BooleanFalse : DataObject.BooleanNull);
            }
            if (!b2.HasValue)
            {
                return(b1.Equals(false) ? DataObject.BooleanFalse : DataObject.BooleanNull);
            }

            // If both true.
            return(DataObject.Boolean(b1.Equals(true) && b2.Equals(true)));
        }
Ejemplo n.º 9
0
        public void NumericAndBooleanSubtract()
        {
            var exp1        = SqlExpression.Constant(DataObject.Number(new SqlNumber(325778.32)));
            var exp2        = SqlExpression.Constant(DataObject.Boolean(true));
            var subtractExp = SqlExpression.Subtract(exp1, exp2);

            SqlExpression resultExp = null;

            Assert.DoesNotThrow(() => resultExp = subtractExp.Evaluate());
            Assert.IsNotNull(resultExp);
            Assert.IsInstanceOf <SqlConstantExpression>(resultExp);

            var constExp = (SqlConstantExpression)resultExp;

            Assert.IsNotNull(constExp.Value.Value);
            Assert.IsInstanceOf <NumericType>(constExp.Value.Type);
            Assert.IsInstanceOf <SqlNumber>(constExp.Value.Value);
            Assert.AreEqual(new SqlNumber(325777.32), (SqlNumber)constExp.Value.Value);
        }
Ejemplo n.º 10
0
        public void BooleanNegate(bool a, bool expected)
        {
            var exp1   = SqlExpression.Constant(DataObject.Boolean(a));
            var negExp = SqlExpression.Negate(exp1);

            SqlExpression resultExp = null;

            Assert.DoesNotThrow(() => resultExp = negExp.Evaluate());
            Assert.IsNotNull(resultExp);
            Assert.IsInstanceOf <SqlConstantExpression>(resultExp);

            var constExp = (SqlConstantExpression)resultExp;

            Assert.IsNotNull(constExp.Value.Value);
            Assert.IsInstanceOf <BooleanType>(constExp.Value.Type);
            Assert.IsInstanceOf <SqlBoolean>(constExp.Value.Value);

            var actual         = ((SqlBoolean)constExp.Value.Value);
            var expectedResult = new SqlBoolean(expected);

            Assert.AreEqual(expectedResult, actual);
        }
Ejemplo n.º 11
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));
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        public void NumericAndBooleanAdd()
        {
            var exp1   = SqlExpression.Constant(DataObject.Number(new SqlNumber(4566)));
            var exp2   = SqlExpression.Constant(DataObject.Boolean(true));
            var addExp = SqlExpression.Add(exp1, exp2);

            SqlExpression resultExp = null;

            Assert.DoesNotThrow(() => resultExp = addExp.Evaluate());
            Assert.IsNotNull(resultExp);
            Assert.IsInstanceOf <SqlConstantExpression>(resultExp);

            var constExp = (SqlConstantExpression)resultExp;

            Assert.IsNotNull(constExp.Value.Value);
            Assert.IsInstanceOf <NumericType>(constExp.Value.Type);
            Assert.IsInstanceOf <SqlNumber>(constExp.Value.Value);

            var actual   = ((SqlNumber)constExp.Value.Value).Round(2);
            var expected = new SqlNumber(4567, 2);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 14
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            var op = Operator;

            if (ob2.DataType is QueryType)
            {
                // The sub-query plan
                IQueryPlanNode plan = (IQueryPlanNode)ob2.Value;
                // Discover the correlated variables for this plan.
                IList <CorrelatedVariable> list = plan.DiscoverCorrelatedVariables(1, new List <CorrelatedVariable>());

                if (list.Count > 0)
                {
                    // Set the correlated variables from the IVariableResolver
                    foreach (CorrelatedVariable variable in list)
                    {
                        variable.SetFromResolver(context.VariableResolver);
                    }
                    // Clear the cache in the context
                    context.QueryContext.ClearCache();
                }

                // Evaluate the plan,
                ITable t = plan.Evaluate(context.QueryContext);

                // The ANY operation
                Operator revPlainOp = op.Plain().Reverse();
                return(DataObject.Boolean(t.ColumnMatchesValue(0, revPlainOp, ob1)));
            }

            if (ob2.DataType is ArrayType)
            {
                Operator plainOp = op.Plain();
                var      expList = (IEnumerable <Expression>)ob2.Value;
                // Assume there are no matches
                DataObject retVal = DataObject.BooleanFalse;
                foreach (Expression exp in expList)
                {
                    DataObject expItem = exp.Evaluate(context.GroupResolver, context.VariableResolver, context.QueryContext);
                    // If null value, return null if there isn't otherwise a match found.
                    if (expItem.IsNull)
                    {
                        retVal = DataObject.BooleanNull;
                    }
                    else
                    {
                        var opExp = Expression.Operator(ob1, plainOp, expItem);
                        if (IsTrue(opExp.Evaluate()))
                        {
                            // If there is a match, the ANY set test is true
                            return(DataObject.BooleanTrue);
                        }
                    }
                }
                // No matches, so return either false or NULL.  If there are no matches
                // and no nulls, return false.  If there are no matches and there are
                // nulls present, return null.
                return(retVal);
            }

            throw new ApplicationException("Unknown RHS of ANY.");
        }
Ejemplo n.º 15
0
 public void SetValue(int columnIndex, bool value)
 {
     SetValue(columnIndex, DataObject.Boolean(value));
 }
Ejemplo n.º 16
0
 public static void SetBooleanVariable(this IVariableScope transaction, string name, bool value)
 {
     transaction.SetVariable(name, DataObject.Boolean(value));
 }