Example #1
0
        public void CreateAggregate_works_when_null()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE dual (dummy); INSERT INTO dual (dummy) VALUES ('X');");
                connection.CreateAggregate("test", (string a) => "A");
                connection.CreateAggregate("test", default(Func <string, string>));

                var ex = Assert.Throws <SqliteException>(
                    () => connection.ExecuteScalar <long>("SELECT test() FROM dual;"));

                Assert.Equal(Resources.SqliteNativeError(SQLITE_ERROR, "no such function: test"), ex.Message);
            }
        }
Example #2
0
        public void Functions_dont_bleed_across_connections()
        {
            sqlite3 db;

            using (var connection1 = new SqliteConnection(ConnectionString))
            {
                if (new Version(connection1.ServerVersion) < new Version(3, 31, 0))
                {
                    // Skip. SQLite returns deleted functions
                    return;
                }

                connection1.CreateFunction("function1", () => 1L, isDeterministic: true);
                connection1.CreateAggregate("aggregate1", 0L, x => x, isDeterministic: true);
                connection1.Open();
                db = connection1.Handle !;
            }

            using var connection2 = new SqliteConnection(ConnectionString);
            connection2.Open();

            Assert.Same(db, connection2.Handle);
            var functions = connection2.ExecuteScalar <string>("SELECT group_concat(name) FROM pragma_function_list;")
                            .Split(",");

            Assert.DoesNotContain("function1", functions);
            Assert.DoesNotContain("aggregate1", functions);
        }
        public void CreateAggregate_throws_when_closed()
        {
            var connection = new SqliteConnection();

            var ex = Assert.Throws <InvalidOperationException>(() => connection.CreateAggregate("test", (string a) => "A"));

            Assert.Equal(Resources.CallRequiresOpenConnection("CreateAggregate"), ex.Message);
        }
Example #4
0
        public void CreateAggregate_throws_when_no_name()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                var ex = Assert.Throws <ArgumentNullException>(() => connection.CreateAggregate(null, (string a) => "A"));

                Assert.Equal("name", ex.ParamName);
            }
        }
Example #5
0
        public void CreateAggregate_works_when_params()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE dual (dummy); INSERT INTO dual (dummy) VALUES ('X');");
                connection.CreateAggregate("test", (string a, object[] args) => a + string.Join(", ", args) + "; ");

                var result = connection.ExecuteScalar <string>("SELECT test(dummy) FROM dual;");

                Assert.Equal("X; ", result);
            }
        }
Example #6
0
        public void CreateAggregate_works_when_sqlite_exception()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE dual (dummy); INSERT INTO dual (dummy) VALUES ('X');");
                connection.CreateAggregate("test", (string a) => throw new SqliteException("Test", 200));

                var ex = Assert.Throws <SqliteException>(
                    () => connection.ExecuteScalar <string>("SELECT test() FROM dual;"));

                Assert.Equal(Resources.SqliteNativeError(200, "Test"), ex.Message);
            }
        }
        public void CreateAggregate_works_when_exception_during_final()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE dual (dummy); INSERT INTO dual (dummy) VALUES ('X');");
                connection.CreateAggregate <string, string>("test", "A", a => "B", a => throw new Exception("Test"));

                var ex = Assert.Throws <SqliteException>(
                    () => connection.ExecuteScalar <string>("SELECT test() FROM dual;"));

                Assert.Equal(Resources.SqliteNativeError(raw.SQLITE_ERROR, "Test"), ex.Message);
                Assert.Equal(raw.SQLITE_ERROR, ex.SqliteErrorCode);
            }
        }
Example #8
0
        public void CreateAggregate_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE dual2 (dummy1, dummy2); INSERT INTO dual2 (dummy1, dummy2) VALUES ('X', 1);");
                connection.CreateAggregate(
                    "test",
                    "A",
                    (string a, string x, int y) => a + x + y,
                    a => a + "Z");

                var result = connection.ExecuteScalar <string>("SELECT test(dummy1, dummy2) FROM dual2;");

                Assert.Equal("AX1Z", result);
            }
        }