Handles processing of a query + parameters into something that can be run. Extracts special parameter types (CommandBehavior, Buffered, Transaction); turns a query that's a field + a single parameter into a select x=y query; turns a query that's a single field + a numeric parameter into a primary key selector
Пример #1
0
        public void CaseSensitivity()
        {
            IQ.Config.IgnoreCaseParameters = false;
            var sql="select * FROM table where col = @Parm1";

            var pp = new ParameterParser(sql, "p1value");
            Assert.AreEqual(pp.Parameters[0].ParameterName, "@Parm1");
            Assert.AreEqual(pp.Parameters[0].Value, "p1value");

            Assert.Throws<ArgumentException>(() =>
            {
                pp = new ParameterParser(sql, "@parm1",123);
            });

            Assert.Throws<ArgumentException>(() =>
            {
                pp = new ParameterParser(sql, new
                    {
                        parm1=123
                    });
            });

            IQ.Config.IgnoreCaseParameters = true;
            pp = new ParameterParser(sql, "@parm1",123);
            Assert.AreEqual(pp.Parameters[0].ParameterName, "@Parm1");
            Assert.AreEqual(pp.Parameters[0].Value, 123);
        }
Пример #2
0
        public void NullValues()
        {
            ParameterParser pp = new ParameterParser("select * FROM table where col = @parm1", 1);

            Assert.AreEqual(1, pp.Parameters.Count);
            Assert.AreEqual(1, pp.Parameters[0].Value);
            // need 2 parms - null values after the first parameter are considered.
            Assert.Throws<ArgumentException>(() =>
            {
                pp = new ParameterParser("select * FROM animals where age = @parm1 and age = @parm2", 1);
            });
        }
Пример #3
0
        public void ExtraData()
        {
            string sql = "select * FROM table where col = @parm1, col2 = @parm2";

            Assert.Throws<ArgumentException>(() => {
                var pp = new ParameterParser(sql, new {parm2="p2value", parm1="p1value"},null);
            });

            var pp2= new ParameterParser(sql, new { parm2 = "p2value", parm1 = "p1value" });

            Assert.AreEqual(2, pp2.Parameters.Count);
        }
Пример #4
0
        /// <summary>
        /// When passing type=0, it's never going to try to build a query - only fully formed query SQl will work.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="query"></param>
        /// <param name="parameters"></param>
        protected void ParseQuery(object query, IEnumerable <object> parameters)
        {
            ISqlQuery outputQuery = null;

            if (query == null)
            {
                throw new IQMissingQueryException();
            }
            else if (query is SqlQueryMaker)
            {
                ExpectNoParameters(parameters);
                if (QueryType != 0 && ((SqlQueryMaker)query).QueryType != QueryType)
                {
                    throw new IQException("The query passed was not of type '" + QueryType.ToString() + "' as required for this operation.");
                }
                outputQuery = (SqlQueryMaker)query;
            }
            else if (QueryType != 0)
            {
                outputQuery = ParseComplexQuery(query, parameters);
            }
            else if (query is string)
            {
                string queryString = (string)query;

                ParameterParser pp = new ParameterParser(queryString, parameters);

                outputQuery = new SqlQueryDef(queryString, pp.Parameters);
            }
            else
            {
                throw new IQException("The type of object '" + query.GetType().ToString() + "' passed as a query isn't something I know how to handle.");
            }

            if (outputQuery == null)
            {
                throw new IQMissingQueryException();
            }

            if (outputQuery.QueryType == QueryType.Invalid && QueryType != 0)
            {
                throw new IQException("Couldn't make a valid query out of the mess you passed me. Check the SQL and the parameters.");
            }

            Query = outputQuery;
        }
Пример #5
0
        public SqlQueryDef(string sql, IEnumerable<object> parameters)
        {
            IEnumerable<IDataParameter> parms;
            if (parameters is IEnumerable<IDataParameter>)
            {
                parms = (IEnumerable<IDataParameter>)parameters;
                QuerySql = sql;
            }
            else
            {
                ParameterParser pp = new ParameterParser(sql, parameters);
                QuerySql = pp.GetQuery();
                parms = pp.Parameters;

            }

            QueryParameters.AddRange(parms);
        }
Пример #6
0
        public void SimpleQueries()
        {
            string sql = "select * FROM table where col = @parm1, col2 = @parm2";
            var pp = new ParameterParser(sql, 1,"something");

            Action sameTest = () =>
            {
                var p1 = pp.Parameters.FirstOrDefault(item => item.ParameterName == "@parm1");
                var p2 = pp.Parameters.FirstOrDefault(item => item.ParameterName == "@parm2");

                Assert.AreEqual(2, pp.Parameters.Count);
                Assert.AreEqual(1, p1.Value);
                Assert.AreEqual("something", p2.Value);
            };
            sameTest();

            pp = new ParameterParser(sql, "parm2", "something", "parm1", 1);
            sameTest();

            pp = new ParameterParser(sql, new
            {
                parm1= 1,
                parm2= "something"
            });
            sameTest();

            pp = new ParameterParser(sql, new
            {
                parm1= 1,
            },new
            {
                parm2= "something"
            });

            sameTest();
        }
Пример #7
0
        protected ISqlQuery ParseComplexQuery(object query, IEnumerable <object> parms)
        {
            ISqlQuery outputQuery = null;

            // We always want to parse the parameters. But if the thing passed to us as "query" is not a string, then
            // just assume that all the parms are option type parameters and don't pass a query to ParameterParser
            string querySource = query is string?
                                 (string)query:
                                 "";

            ParameterParser pp = new ParameterParser(querySource, parms);

            if (Types.IsNumericType(query))
            {
                // It's a single numeric value - assume it's a primary key

                ExpectNoParameters(pp.Parameters);

                var classInfo = IQ.ClassInfo <T>();

                ISqlQueryMaker queryPK = classInfo.GetQuery();
                queryPK.Where.Add(classInfo.PrimaryKeyField.Name, query);
                outputQuery = queryPK;
            }
            else if (query is string)
            {
                bool isMappable = Types.IsMappable(typeof(T));

                // First check if its a single named field

                if (isMappable)
                {
                    var classInfo = IQ.ClassInfo <T>();


                    // Try to create a valid raw query.. if it's not valid, assume it's a where
                    if (QueryType == QueryType.Where || pp.QueryType == QueryType.Invalid)
                    {
                        ISqlQueryMaker queryPK = classInfo.GetQuery();

                        //var whereString = new WhereString(pp.Query,
                        //    pp.Parameters.Count > 0 ?
                        //        pp.Parameters.ToArray():
                        //        null);
                        queryPK.Where.Add(pp.GetWhereClause());

                        outputQuery = queryPK;
                    }
                    else
                    {
                        outputQuery = new SqlQueryDef(pp.GetQuery(QueryType), pp.Parameters);
                    }
                }
                else
                {
                    // it's mapped to a primitive type -
                    outputQuery = new SqlQueryDef(pp.GetQuery(QueryType), pp.Parameters);
                }
            }
            if (outputQuery.QueryType != QueryType)
            {
                throw new IQException("Wrong type of query passed to method: was " + outputQuery.ToString() + ", expected " + QueryType.ToString());
            }

            return(outputQuery);
        }
Пример #8
0
 /// <summary>
 /// Build a where clause from a string
 /// </summary>
 /// <param name="clause"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public static IWhere WhereParse(string clause, params object[] parameters)
 {
     ParameterParser parser = new ParameterParser(clause, parameters);
     return new WhereString(clause, parser.Parameters.ToArray());
 }