Ejemplo n.º 1
0
        public void Constructor_DbCommand_UpdatesPropertyValues()
        {
            const string commandText = "CommandText";
            var parameter = new SqlParameter("parameterName1", "value1");

            var command = new SqlCommand(commandText);
            command.Parameters.Add(parameter);

            var sqlGeneratorResult = new SqlGeneratorResult(command);

            Assert.That(sqlGeneratorResult.SqlString, Is.EqualTo(commandText));
            Assert.That(sqlGeneratorResult.Parameters.Count, Is.EqualTo(1));
            Assert.That(sqlGeneratorResult.Parameters.First(), Is.EqualTo(parameter));
        }
Ejemplo n.º 2
0
        public void GetUnparameterizedSql_NamedParameters_UnparameterizesCorrectly()
        {
            var sqlGeneratorResult = new SqlGeneratorResult
            {
                SqlString = "SELECT column1, column2 FROM table1 WHERE column3 < @p1 and column1 > @p11",
                Parameters = new List<IDataParameter>
                {
                    new SqlParameter("p1", "value1"),
                    new SqlParameter("p11", 3)
                }
            };

            var unparameterizedSql = sqlGeneratorResult.GetUnparameterizedSql();

            Assert.That(unparameterizedSql, Is.EqualTo("SELECT column1, column2 FROM table1 WHERE column3 < 'value1' and column1 > 3"));
        }
        private SqlGeneratorResult GenerateSql(Expression expression, bool unalias, bool isStarSelect)
        {
            SqlGeneratorResult result;

            // TODO: Try to reuse an existing session, if one exists
            using (var session = SessionFactory.OpenSession()) // SessionFactory.GetCurrentSession()
            {
                var sessionImplementor = session.GetSessionImplementation();

                var queryExpressionAndParameters = GetQueryExpressionWithParameters(sessionImplementor, expression);

                var queryExpression = queryExpressionAndParameters.Item1;
                var queryParameters = queryExpressionAndParameters.Item2;

                var dbCommand = CreateDbCommand(sessionImplementor, queryExpression, queryParameters, unalias, isStarSelect);

                result = new SqlGeneratorResult(dbCommand);

                session.Close();
            }

            return result;
        }
Ejemplo n.º 4
0
        public void GetUnparameterizedSql_NoParameters_UnparameterizesCorrectly()
        {
            var sqlGeneratorResult = new SqlGeneratorResult
            {
                SqlString = "SELECT 10",
                Parameters = new List<IDataParameter>()
            };

            var unparameterizedSql = sqlGeneratorResult.GetUnparameterizedSql();

            Assert.That(unparameterizedSql, Is.EqualTo("SELECT 10"));
        }
Ejemplo n.º 5
0
        public void GetUnparameterizedSql_UnknownParameterType_Throws()
        {
            var sqlGeneratorResult = new SqlGeneratorResult
            {
                SqlString = "SELECT column1, column2 FROM table1 WHERE column3 < ? and column1 > ?",
                Parameters = new List<IDataParameter>
                {
                    new SqlParameter("parameterName1", "value1"),
                    new SqlParameter("parameterName3", 3) { DbType = DbType.Object }
                }
            };

            Assert.Throws<ArgumentException>(() => sqlGeneratorResult.GetUnparameterizedSql());
        }
Ejemplo n.º 6
0
        public void GetUnparameterizedSql_TooManyPositionalParameters_Throws()
        {
            var sqlGeneratorResult = new SqlGeneratorResult
            {
                SqlString = "SELECT column1, column2 FROM table1 WHERE column3 < ? and column1 > ?",
                Parameters = new List<IDataParameter>
                {
                    new SqlParameter("parameterName1", "value1"),
                    new SqlParameter("parameterName2", 3),
                    new SqlParameter("parameterName3", 4)
                }
            };

            Assert.Throws<ArgumentException>(() => sqlGeneratorResult.GetUnparameterizedSql());
        }
Ejemplo n.º 7
0
        public void GetUnparameterizedSql_PositionalParametersWithNull_UnparameterizesCorrectly()
        {
            var sqlGeneratorResult = new SqlGeneratorResult
            {
                SqlString = "SELECT column1, column2 FROM table1 WHERE column3 < ? and column1 IS NOT ?",
                Parameters = new List<IDataParameter>
                {
                    new SqlParameter("parameterName1", "value1"),
                    new SqlParameter("parameterName2", null)
                }
            };

            var unparameterizedSql = sqlGeneratorResult.GetUnparameterizedSql();

            Assert.That(unparameterizedSql, Is.EqualTo("SELECT column1, column2 FROM table1 WHERE column3 < 'value1' and column1 IS NOT NULL"));
        }
Ejemplo n.º 8
0
        public void GetUnparameterizedSql_NotEnoughNamedParameters_Throws()
        {
            var sqlGeneratorResult = new SqlGeneratorResult
            {
                SqlString = "SELECT column1, column2 FROM table1 WHERE column3 < @p1 and column1 in (@p2, @p3)",
                Parameters = new List<IDataParameter>
                {
                    new SqlParameter("p1", "value1"),
                    new SqlParameter("p2", 3)
                }
            };

            Assert.Throws<ArgumentException>(() => sqlGeneratorResult.GetUnparameterizedSql());
        }