Example #1
0
        void CheckAdd(string databaseProvider, string from, string to)
        {
            if (databaseProvider == null)
            {
                throw new ArgumentNullException("databaseProvider");
            }

            if (from == null)
            {
                throw new ArgumentNullException("from");
            }

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            GlazeFactory factory = new GlazeFactory(databaseProvider);
            DbCommand    command = factory.CreateCommand();

            DbParameter dbParameter = command.CreateParameter();

            dbParameter.ParameterName = from;
            dbParameter.Value         = "value";
            command.Parameters.Add(dbParameter);

            Assert.AreEqual(1, command.Parameters.Count);
            Assert.AreEqual(to, command.Parameters[0].ParameterName);
        }
Example #2
0
        void CheckGetParameter(string databaseProvider, string name)
        {
            if (databaseProvider == null)
            {
                throw new ArgumentNullException("databaseProvider");
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            GlazeFactory factory = new GlazeFactory(databaseProvider);
            DbCommand    command = factory.CreateCommand();

            DbParameter dbParameter = command.CreateParameter();

            dbParameter.ParameterName = name;
            dbParameter.Value         = "value";
            command.Parameters.Add(dbParameter);

            DbParameter found = command.Parameters[name];

            Assert.IsNotNull(found);
            Assert.AreEqual("value", found.Value);

            DbParameter first = command.Parameters[0];

            Assert.IsNotNull(first);
            Assert.AreEqual("value", first.Value);
        }
Example #3
0
        void Test()
        {
            AppSettingsReader configuration    = new AppSettingsReader();
            string            databaseProvider = configuration.GetValue(
                "DatabaseProvider", typeof(string)) as string;
            string connectionString = configuration.GetValue(
                "ConnectionString", typeof(string)) as string;

            GlazeFactory provider   = new GlazeFactory(databaseProvider);
            DbConnection connection = provider.CreateConnection();

            connection.ConnectionString = connectionString;
            connection.Open();
            try
            {
                DbCommand command = connection.CreateCommand();
                command.CommandText = "SELECT count(*) FROM table_name";
                object total = command.ExecuteScalar();
                Console.WriteLine("{0}", total);
            }
            finally
            {
                connection.Close();
            }
        }
Example #4
0
        void CheckExecuteScalar(GlazeFactory glazeFactory, DbConnection connection)
        {
            CheckIntExpression(connection, "1+2", 3);
            CheckIntExpression(connection, "5 % 3", 2);
            CheckIntExpression(connection, "mod(-15, 4)", -3);

            CheckDoubleExpression(connection, ".2", 0.2);
            CheckDoubleExpression(connection, "0.2", 0.2);
            CheckDoubleExpression(connection, "-1.2", -1.2);
            CheckDoubleExpression(connection, "1.2E2", 120);
            CheckDoubleExpression(connection, "0E-10", 0);

            if (!glazeFactory.DatabaseProvider.Equals(Factory.MySqlProvider))
            {
                // MySQL just returns NULL on division by zero, and there doesn't seem
                // to be a way to make it fail...
                CheckFailingExpression(connection, "select 0/0");

                // ditto for invalid date operations
                CheckFailingExpression(connection,
                                       "select 2003-07-31T00:00:00 - interval '1' month");
            }

            CheckDateTime(glazeFactory, connection);
            CheckDateTimeInterval(connection);

            CheckCurrentTime(connection, "select getdate()");
            CheckCurrentTime(connection, "select now()");
            CheckCurrentTime(connection, "select sysdate from dual");

            CheckStringExpression(connection, "''''", "'");
            CheckStringExpression(connection, "'\"'", "\"");
            CheckStringExpression(connection, "'&'", "&");
            CheckStringExpression(connection, "N'kως'", "kως");
            CheckStringExpression(connection, "'A2|45'", "A2|45");
            CheckStringExpression(connection, "'a' || 'b'", "ab");
            CheckStringExpression(connection, "'a' 'b'\n'c'", "abc");
            CheckStringExpression(connection, "N'žluouθkύ' || N' ' || N'kως'",
                                  "žluouθkύ kως");
            CheckStringExpression(connection, "N'žluouθkύ' \r\n ' kun'",
                                  "žluouθkύ kun");
            CheckStringExpression(connection, "'zlutoucky ' \r\n N'kως'",
                                  "zlutoucky kως");

            CheckStringExpression(connection, "substring('abcdefgh' from 1 for 2)", "ab");
            CheckStringExpression(connection, "substring(N'kως' from 2 for 2)", "ως");
            CheckStringExpression(connection, "substring(null from 2)", null);
            CheckStringExpression(connection, "substring('abc' from null)", null);
            CheckStringExpression(connection, "substring('abc' from 1 for null)", null);
            CheckStringExpression(connection, "substring('abc' from 4)", "");
            CheckStringExpression(connection, "substring('abc' from 1 for 5)", "abc");
            CheckStringExpression(connection, "substring('a' || 'b' || 'c' from 1 for 5)", "abc");

            CheckStringFunctions(connection);
            CheckCoalesce(connection);

            CheckCrossJoin(connection);
        }
Example #5
0
        void CheckDateTime(GlazeFactory glazeFactory, DbConnection connection)
        {
            if (glazeFactory == null)
            {
                throw new ArgumentNullException("glazeFactory");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            DbCommand command = glazeFactory.CreateCommand();

            command.Connection  = connection;
            command.CommandText = "SELECT 2007-07-18T16:30:00";
            object c = command.ExecuteScalar();

            Assert.IsNotNull(c);

            DateTime dt = ResultToDate(c);

            Assert.AreEqual(2007, dt.Year);
            Assert.AreEqual(7, dt.Month);
            Assert.AreEqual(18, dt.Day);
            Assert.AreEqual(16, dt.Hour);
            Assert.AreEqual(30, dt.Minute);
            Assert.AreEqual(0, dt.Second);

            command.CommandText = "SELECT EXTRACT(year FROM 2007-07-18T16:30:00)";
            c = command.ExecuteScalar();
            Assert.AreEqual(2007, c);

            command.CommandText = "SELECT EXTRACT(month FROM 2007-07-18T16:30:00)";
            c = command.ExecuteScalar();
            Assert.AreEqual(7, c);

            command.CommandText = "SELECT EXTRACT(day FROM 2007-07-18T16:30:00)";
            c = command.ExecuteScalar();
            Assert.AreEqual(18, c);

            command.CommandText = "SELECT EXTRACT(hour FROM 2007-07-18T16:30:00)";
            c = command.ExecuteScalar();
            Assert.AreEqual(16, c);

            command.CommandText = "SELECT EXTRACT(minute FROM 2007-07-18T16:30:00)";
            c = command.ExecuteScalar();
            Assert.AreEqual(30, c);

            command.CommandText = "SELECT EXTRACT(second FROM 2007-07-18T16:30:04)";
            c = command.ExecuteScalar();
            Assert.AreEqual(4, c);

            command.CommandText = "SELECT EXTRACT(second FROM null)";
            c = command.ExecuteScalar();
            Assert.AreEqual(DBNull.Value, c);
        }
Example #6
0
 public void TestDatabaseProvider()
 {
     string[] providers = TestUtil.Providers;
     for (int i = 0; i < providers.Length; ++i)
     {
         GlazeFactory glazeFactory = new GlazeFactory(providers[i]);
         GlazeCommand glazeCommand = (GlazeCommand)(glazeFactory.CreateCommand());
         Assert.AreEqual(providers[i], glazeCommand.DatabaseProvider);
     }
 }
Example #7
0
        void Test()
        {
            AppSettingsReader configuration    = new AppSettingsReader();
            string            databaseProvider = configuration.GetValue(
                "DatabaseProvider", typeof(string)) as string;

            GlazeFactory provider   = new GlazeFactory(databaseProvider);
            DbConnection connection = provider.CreateConnection();
            DbCommand    command    = connection.CreateCommand();
        }
Example #8
0
 public void TestCreateCommand()
 {
     string[] providers = TestUtil.Providers;
     for (int i = 0; i < providers.Length; ++i)
     {
         GlazeFactory glazeFactory = new GlazeFactory(providers[i]);
         DbCommand    command      = glazeFactory.CreateCommand();
         GlazeCommand glazeCommand = command as GlazeCommand;
         Assert.IsNotNull(glazeCommand);
     }
 }
Example #9
0
        static void Run(string databaseProvider)
        {
            if (databaseProvider == null)
            {
                throw new ArgumentNullException("databaseProvider");
            }

            GlazeFactory glazeFactory = new GlazeFactory(databaseProvider);
            DbConnection connection   = glazeFactory.CreateConnection();

            Assert.IsNotNull(connection);
        }
Example #10
0
 public void TestCreateDataSourceEnumerator()
 {
     string[] providers = TestUtil.Providers;
     for (int i = 0; i < providers.Length; ++i)
     {
         GlazeFactory glazeFactory = new GlazeFactory(providers[i]);
         if (glazeFactory.CanCreateDataSourceEnumerator)
         {
             DbDataSourceEnumerator enumerator =
                 glazeFactory.CreateDataSourceEnumerator();
             Assert.IsNotNull(enumerator);
         }
     }
 }
Example #11
0
        void CheckExecuteReader(GlazeFactory glazeFactory, DbConnection connection)
        {
            CheckQuoting(connection);
            CheckCase(connection);

            string[] conditions = { "null is null",         "'a' is not null",
                                    "not null is not null", "not 2 < 1",
                                    "'abcd' like '%c%'",    "'abcd' not like '%e%'","'a' like '_'",
                                    "not ('a' like '__')",  "'_' like '_'",         };
            // FIXME: standard (and Oracle) '[' pattern doesn't work
            // for MS engines, which use brackets for ranges. It could be
            // replaced by '[[]', but it isn't clear when (as the change
            // isn't idempotent).
            for (int i = 0; i < conditions.Length; ++i)
            {
                CheckCondition(connection, conditions[i]);
            }

            CheckTop(connection);

            CreateCountedRows(connection);

            int etalon      = CheckParameters(connection, '@', true);
            int alternative = CheckParameters(connection, '@', false);

            Assert.AreEqual(etalon, alternative);

            alternative = CheckParameters(connection, ':', true);
            Assert.AreEqual(etalon, alternative);

            alternative = CheckParameters(connection, ':', false);
            Assert.AreEqual(etalon, alternative);

            alternative = CheckUnnamedParameters(connection);
            Assert.AreEqual(etalon, alternative);

            alternative = CheckTableAlias(connection);
            Assert.AreEqual(etalon, alternative);

            alternative = CheckTableAliasWithRepeat(connection, false);
            Assert.AreEqual(etalon, alternative);

            alternative = CheckTableAliasWithRepeat(connection, true);
            Assert.AreEqual(etalon, alternative);

            CheckJoin(connection);
            CheckAlias(connection);
            CheckNoSpace(connection);
        }
Example #12
0
        void CheckStatement(GlazeFactory glazeFactory, DbConnection connection)
        {
            QueryExpression queryExpression = new QueryExpression();

            queryExpression.SelectItems = new AliasedItem(
                new Expression(new IntegerValue(1),
                               ExpressionOperator.Minus,
                               new IntegerValue(2)));

            GlazeCommand command = (GlazeCommand)(connection.CreateCommand());

            command.Statement = new SelectStatement(queryExpression);

            object c = command.ExecuteScalar();

            Assert.IsNotNull(c);
            int d = Convert.ToInt32(c);

            Assert.AreEqual(-1, d);

            InsertStatement insertStatement = new InsertStatement();

            insertStatement.Table       = new DbObject(new Identifier("no_table"));
            insertStatement.ColumnNames = new AliasedItem(
                new Identifier("test_column"));

            Expression expr = new Expression();

            expr.Left = new IntegerValue(42);
            insertStatement.ColumnValues = new ExpressionItem(expr);
            command.Statement            = insertStatement;

            bool changed = false;

            try
            {
                command.ExecuteNonQuery();
            }
            catch
            {
                changed = true;
            }

            Assert.IsTrue(changed);

            string text = command.CommandText;

            Assert.IsTrue(text.StartsWith("INSERT"));
        }
Example #13
0
        void CheckDbTransaction(GlazeFactory glazeFactory, DbConnection connection)
        {
            if (glazeFactory == null)
            {
                throw new ArgumentNullException("glazeFactory");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            CheckTransactionCommit(connection);
            CheckTransactionRollback(connection);
        }
Example #14
0
        public void TestState()
        {
            string[] providers = TestUtil.Providers;

            if (providers.Length == 0)
            {
                Console.WriteLine("No database tested. Consider creating TestGlaze.exe.config and configuring some providers.");
            }

            for (int i = 0; i < providers.Length; ++i)
            {
                GlazeFactory glazeFactory = new GlazeFactory(providers[i]);
                DbConnection connection   = glazeFactory.CreateConnection();
                Assert.AreEqual(ConnectionState.Closed, connection.State);
            }
        }
Example #15
0
        void CheckExecuteNonQuery(GlazeFactory glazeFactory, DbConnection connection)
        {
            if (glazeFactory == null)
            {
                throw new ArgumentNullException("glazeFactory");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            CheckDelete(connection);
            CheckInsertDate(connection);
            CheckInsertDefault(connection);
        }
Example #16
0
        void CheckCommandText(GlazeFactory glazeFactory, DbConnection connection)
        {
            if (glazeFactory == null)
            {
                throw new ArgumentNullException("glazeFactory");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            DbCommand command = glazeFactory.CreateCommand();

            Assert.IsNotNull(command);
            Assert.AreEqual("", command.CommandText);
        }
Example #17
0
        public void TestCreateDataAdapter()
        {
            string[] providers = TestUtil.Providers;
            for (int i = 0; i < providers.Length; ++i)
            {
                GlazeFactory glazeFactory = new GlazeFactory(providers[i]);
                DbCommand    command      = glazeFactory.CreateCommand();
                Assert.IsNotNull(command);

                DbDataAdapter dataAdapter = glazeFactory.CreateDataAdapter();

                // can't be encapsulated because SelectCommand isn't virtual
                dataAdapter.SelectCommand = ((GlazeCommand)command).Inner;

                Assert.IsNotNull(dataAdapter.SelectCommand);
            }
        }
Example #18
0
        void CheckBindByName(GlazeFactory glazeFactory, DbConnection connection)
        {
            if (glazeFactory == null)
            {
                throw new ArgumentNullException("glazeFactory");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            DbCommand command = glazeFactory.CreateCommand();

            Assert.IsNotNull(command);
            GlazeCommand glazeCommand = (GlazeCommand)command;

            Assert.IsFalse(glazeCommand.BindByName);

            glazeCommand.BindByName = true;
            Assert.IsTrue(glazeCommand.BindByName);
        }
Example #19
0
        static void Run(string databaseProvider, ConnectedTest test)
        {
            if (databaseProvider == null)
            {
                throw new ArgumentNullException("databaseProvider");
            }

            AppSettingsReader config           = new AppSettingsReader();
            string            connectionString = config.GetValue(databaseProvider,
                                                                 typeof(string)) as string;
            GlazeFactory glazeFactory = new GlazeFactory(databaseProvider);
            DbConnection connection   = glazeFactory.CreateConnection();

            connection.ConnectionString = connectionString;
            connection.Open();
            try
            {
                test(glazeFactory, connection);
            }
            finally
            {
                connection.Close();
            }
        }