/// <summary>
        /// Basically call the standard data provider.
        /// Queries the fast.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="context">The context.</param>
        /// <returns>A list of IDs.</returns>
        protected override IDList QueryFast(string query, CallContext context)
        {
            var baseIdList = this.SelectIDs(query, context);
            if (baseIdList != null && baseIdList.Count > 0) return baseIdList;

            if (!this.IsTraceEnabled(query)) return base.QueryFast(query, context);

            var parameters = new ParametersList();
            var sql = this.Translator.TranslateQuery(query, context, parameters);

            Log.Debug(string.Format("FastQuery: {0}", query), this);
            Log.Debug(string.Format("SQL Query: {0}", this.FormatSqlQuery(sql, parameters)), this);

            if (sql == null) return null;

            var stopwatch = Stopwatch.StartNew();
            using (var reader = this.Api.CreateReader(sql, parameters.ToArray()))
            {
                var idList = new IDList();
                while (reader.Read())
                {
                    idList.Add(this.Api.GetId(0, reader));
                }

                context.CurrentResult = idList;
            }

            Log.Debug(string.Format("Query Time: {0}ms", stopwatch.ElapsedMilliseconds), this);
            return null;
        }
示例#2
0
        protected virtual string TranslateQuery([NotNull] Opcode opcode, [NotNull] Database database, [NotNull] ParametersList parameters)
        {
            Assert.ArgumentNotNull(opcode, nameof(opcode));
            Assert.ArgumentNotNull(database, nameof(database));
            Assert.ArgumentNotNull(parameters, nameof(parameters));

            var step = opcode as Step;

            if (step == null || !(step is Root))
            {
                return(null);
            }

            step = step.NextStep;
            var sql = string.Empty;

            while (step != null)
            {
                if (!(step is Children) && !(step is Descendant) && !(step is Ancestor) && !(step is Parent))
                {
                    throw new Exception("Can navigate only child, parent, descendant, and ancestor axes.");
                }

                ITranslationContext context = new BasicTranslationContext(_factory, _api, database, parameters);
                var predicate = GetPredicate(step);
                var name      = GetName(step);

                // Dirty hack to overcome problems with numbers in Query
                if (name.StartsWith("_."))
                {
                    name = name.Substring(2);
                }

                Opcode expression = null;
                if (predicate != null)
                {
                    expression = predicate.Expression;
                }

                var where = string.Empty;
                if (expression != null)
                {
                    where = context.Factory.GetTranslator(expression).Translate(expression, context);
                }

                var builder = new StringBuilder();

                AddInitialStatement(builder);
                AddFieldJoins(context, builder);

                if (!string.IsNullOrEmpty(sql))
                {
                    AddNestedQuery(step, sql, builder);
                }

                AddExtraJoins(context, builder);

                // Add WHERE statement
                where = where.Trim();
                var whereAppended = false;
                if (where.Length > 0)
                {
                    whereAppended = AddConditionJoint(false, builder);
                    builder.Append(where);
                }

                // Add Name filter
                if (name.Length > 0 && name != "*")
                {
                    whereAppended = AddConditionJoint(whereAppended, builder);
                    AddNameFilter(name, builder);
                }

                if (step is Children && string.IsNullOrEmpty(sql))
                {
                    AddConditionJoint(whereAppended, builder);
                    AddRootItemFilter(parameters, builder);
                }

                sql  = builder.ToString();
                step = step.NextStep;
            }

            return(sql);
        }
示例#3
0
        static void Main(string[] args)
        {
            var boolType = SolidityType.Bool;
            var intType  = SolidityType.Int;

            var propertyName  = "PropertyName1";
            var propertyName2 = "PropertyName2";
            var propertyName3 = "PropertyName3";

            var pr1 = new ContractProperty()
            {
                Variable   = PrepareVariable(propertyName, boolType),
                Visibility = Visibility.Public
            };

            var pr2 = new ContractProperty()
            {
                Variable   = PrepareVariable(propertyName2, intType),
                Visibility = Visibility.Private
            };

            var pr3 = new ContractProperty()
            {
                Variable   = PrepareVariable(propertyName3, boolType),
                Visibility = Visibility.Public
            };

            var properties = new List <ContractProperty>()
            {
                pr1, pr2, pr3
            };

            var eventName  = "EventName1";
            var eventName2 = "EventName2";
            var eventName3 = "EventName3";

            var epl = new ParametersList()
            {
                Parameters = new List <Variable>()
                {
                    PrepareVariable("ep1", boolType), PrepareVariable("ep2", intType)
                }
            };

            var epl2 = new ParametersList()
            {
                Parameters = new List <Variable>()
                {
                    PrepareVariable("ep1", boolType)
                }
            };

            var epl3 = new ParametersList()
            {
                Parameters = new List <Variable>()
                {
                    PrepareVariable("ep1", intType)
                }
            };

            var e1 = new ContractEvent()
            {
                Name       = eventName,
                Parameters = epl
            };

            var e2 = new ContractEvent()
            {
                Name       = eventName2,
                Parameters = epl2
            };

            var e3 = new ContractEvent()
            {
                Name       = eventName3,
                Parameters = epl3
            };

            var events = new List <ContractEvent>()
            {
                e1, e2, e3
            };

            var name1 = "_p";
            var name2 = "_q";
            var name3 = "_r";
            var name4 = "v";
            var p1    = PrepareVariable(name1, boolType);
            var p2    = PrepareVariable(name2, boolType);
            var p3    = PrepareVariable(name3, boolType);
            var v     = PrepareVariable(name4, boolType);

            var pl = new ParametersList()
            {
                Parameters = new List <Variable>()
                {
                    p1, p2
                }
            };

            var pl2 = new ParametersList()
            {
                Parameters = new List <Variable>()
                {
                    p1, p2, p3
                }
            };

            var cpl = new CallingParametersList()
            {
                Parameters = new List <IAssignable>()
                {
                    p1, p2
                }
            };

            var cpl2 = new CallingParametersList()
            {
                Parameters = new List <IAssignable>()
                {
                    p1, p2, p3
                }
            };

            var d = new Declaration()
            {
                Variable = v
            };

            var op = new Operation()
            {
                LeftSide  = p1,
                Operator  = OperationOperator.OR,
                RightSide = p2
            };

            var instruction = new Assignment()
            {
                Destination = d,
                Source      = op
            };

            var ao = new Operation()
            {
                LeftSide = v,
                Operator = OperationOperator.Negation
            };

            var instruction2 = new Assignment()
            {
                Destination = v,
                Source      = ao
            };

            var instructions = new InstructionsList();

            instructions.AppendInstruction(instruction);
            instructions.AppendInstruction(instruction2);

            var c = new Constructor()
            {
                Visibility   = Visibility.Public,
                Parameters   = pl,
                Instructions = instructions
            };

            var functionInstructions = new InstructionsList();

            functionInstructions.AppendInstruction(instruction);

            var aof = new Operation()
            {
                LeftSide  = v,
                Operator  = OperationOperator.AND,
                RightSide = p3
            };

            var instruction3 = new Assignment()
            {
                Destination = v,
                Source      = aof
            };

            functionInstructions.AppendInstruction(instruction3);

            var function = new ContractFunction()
            {
                Name         = "TestFunction",
                Visibility   = Visibility.Public,
                Parameters   = pl2,
                Instructions = functionInstructions
            };

            var fp1 = PrepareVariable(name1, intType);
            var fp2 = PrepareVariable(name2, intType);
            var fp3 = PrepareVariable(name3, intType);
            var fv  = PrepareVariable(name4, intType);

            var fpl = new ParametersList()
            {
                Parameters = new List <Variable>()
                {
                    fp1, fp2, fp3
                }
            };

            var fd = new Declaration()
            {
                Variable = fv
            };

            var fop = new Operation()
            {
                LeftSide  = p1,
                Operator  = OperationOperator.Plus,
                RightSide = p2
            };

            var finstruction = new Assignment()
            {
                Destination = fd,
                Source      = fop
            };

            var fao = new Operation()
            {
                LeftSide  = v,
                Operator  = OperationOperator.Multiply,
                RightSide = fp3
            };

            var finstruction2 = new Assignment()
            {
                Destination = pr2,
                Source      = fao
            };

            var finstructions = new InstructionsList();

            finstructions.AppendInstruction(finstruction);
            finstructions.AppendInstruction(finstruction2);

            var function2 = new ContractFunction()
            {
                Name         = "TestFunction2",
                Visibility   = Visibility.External,
                Parameters   = fpl,
                Instructions = finstructions
            };

            var trueInstructions  = new InstructionsList();
            var falseInstructions = new InstructionsList();

            trueInstructions.AppendInstruction(
                new FunctionCall()
            {
                FunctionToCall = function, Parameters = cpl2
            }
                );

            var ecpl = new CallingParametersList()
            {
                Parameters = new List <IAssignable>()
                {
                    p1
                }
            };

            falseInstructions.AppendInstruction(
                new EventCall()
            {
                EventToCall = e2, Parameters = ecpl
            }
                );

            var newFInstructions = new InstructionsList();
            var condOp           = new Operation()
            {
                LeftSide = p1,
                Operator = OperationOperator.Negation
            };
            var cond = new Condition()
            {
                ConditionOperation = condOp
            };
            var ifStatement = new IfStatement()
            {
                Condition         = cond,
                TrueInstructions  = trueInstructions,
                FalseInstructions = falseInstructions
            };

            newFInstructions.AppendInstruction(ifStatement);

            var loopVariable = PrepareVariable("loopVariable", intType);
            var declaration  = new Declaration()
            {
                Variable = loopVariable
            };
            var assignment = new Assignment()
            {
                Destination = declaration,
                Source      = new ConstantValue()
                {
                    Value = "0"
                }
            };

            var condOperation = new Operation()
            {
                LeftSide  = loopVariable,
                Operator  = OperationOperator.NotEquals,
                RightSide = new ConstantValue()
                {
                    Value = "1"
                }
            };

            var breakCondition = new Condition()
            {
                ConditionOperation = condOperation
            };

            var stepOp = new Operation()
            {
                LeftSide  = loopVariable,
                Operator  = OperationOperator.Plus,
                RightSide = new ConstantValue()
                {
                    Value = "1"
                }
            };

            var stepInstruction = new Assignment()
            {
                Destination = loopVariable,
                Source      = stepOp
            };

            var loopInstructions = new InstructionsList();
            var cple             = new CallingParametersList()
            {
                Parameters = new List <IAssignable>()
                {
                    loopVariable
                }
            };

            loopInstructions.AppendInstruction(
                new EventCall()
            {
                EventToCall = e3,
                Parameters  = cple
            }
                );

            var loop = new ContractLoop()
            {
                InitialAssignment = assignment,
                BreakCondition    = breakCondition,
                StepInstruction   = stepInstruction,
                Instructions      = loopInstructions
            };

            newFInstructions.AppendInstruction(loop);

            var function3 = new ContractFunction()
            {
                Name         = "NewFunction",
                Visibility   = Visibility.Public,
                Parameters   = pl2,
                Instructions = newFInstructions
            };

            var functions = new List <ContractFunction>()
            {
                function, function2, function3
            };

            string   contractName = "TestContract";
            Contract contract     = new Contract()
            {
                Name        = contractName,
                Constructor = c,
                Functions   = functions,
                Events      = events,
                Properties  = properties
            };

            Console.WriteLine(contract.GenerateCode(new Indentation()));
        }
 public BaseSqlCommand(string connection_string, RequestInfo request)
 {
     ConnectionString = connection_string;
     Request          = request;
     Parameters       = new ParametersList();
 }
        /// <summary>
        /// Formats the SQL query by replacing the parameters with the actual values.
        /// </summary>
        /// <param name="sql">The SQL.</param>
        /// <param name="parameterList">The parameter list.</param>
        /// <returns>The formatted query.</returns>
        private string FormatSqlQuery(string sql, ParametersList parameterList)
        {
            var parameters = parameterList.ToArray();
            for (var i = 0; i < parameters.Length; i = i + 2)
            {
                var parameterName = string.Format("@{0}", parameters[i]);
                var parameterValue = string.Format("'{0}'", parameters[i + 1]);

                sql = sql.Replace(parameterName, parameterValue);
            }

            return sql;
        }
示例#6
0
        public virtual string TranslateQuery(Opcode opcode, Database database, ParametersList parameters)
        {
            var step = opcode as Step;

            if (!(step is Root))
            {
                return(null);
            }

            step = step.NextStep;
            var sql = string.Empty;

            while (step != null)
            {
                if (!(step is Children) && !(step is Descendant) && !(step is Ancestor) && !(step is Parent))
                {
                    throw new Exception("Can navigate only child, parent, descendant, and ancestor axes.");
                }

                var context = new BasicTranslationContext(_factory, _api, database, parameters);


                ((BasicTranslatorFactory)context.Factory).Register(typeof(LinqToSitecoreFunction), new LinqToSitecoreFunctionTranslator());

                var predicate = GetPredicate(step);
                var name      = GetName(step);

                if (name.StartsWith("_."))
                {
                    name = name.Substring(2);
                }

                Opcode expression = null;
                if (predicate != null)
                {
                    expression = predicate.Expression;
                }

                var where = string.Empty;
                if (expression != null)
                {
                    where = context.Factory.GetTranslator(expression).Translate(expression, context);
                }

                var builder = new StringBuilder();

                AddInitialStatement(builder);


                AddFieldJoins(context, builder);

                if (!string.IsNullOrEmpty(sql))
                {
                    AddNestedQuery(step, sql, builder);
                }

                AddExtraJoins(context, builder);

                where = where.Trim();
                var whereAppended = false;
                if (where.Length > 0)
                {
                    whereAppended = AddConditionJoint(false, builder);
                    builder.Append(where);
                }

                if (name.Length > 0 && name != "*")
                {
                    whereAppended = AddConditionJoint(whereAppended, builder);
                    AddNameFilter(name, builder);
                }

                if (step is Children && string.IsNullOrEmpty(sql))
                {
                    AddConditionJoint(whereAppended, builder);
                    AddRootItemFilter(parameters, builder);
                }

                sql  = builder.ToString();
                step = step.NextStep;
            }

            return(NormalizeQuery(sql));
        }
示例#7
0
 public void GenerateCodeTest(ParametersList parametersList, string expected)
 {
     System.Diagnostics.Contracts.Contract.Requires(parametersList != null);
     Assert.AreEqual(expected, parametersList.GenerateCode(true));
 }
 public SpellInfo(int newSpellId, string newSpellName)
 {
     spellId    = newSpellId;
     spellName  = newSpellName;
     parameters = new ParametersList();
 }