コード例 #1
0
        public static SqliteConnection AddEFCoreExtensions(this SqliteConnection connection)
        {
            // TimeSpan
            connection.CreateFunction("days", (TimeSpan value) => value.TotalDays);
            connection.CreateFunction("timespan", (double value) => TimeSpan.FromDays(value));

            // NetTopologySuite
            using (var command = connection.CreateCommand())
            {
                // Workaround ericsink/SQLitePCL.raw#225
                var file = "mod_spatialite";
                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    file += ".dylib";
                }
                else if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    file += ".so";
                }

                command.CommandText = "SELECT load_extension('" + file + "');";

                // Let the CLR load from NuGet package assets. SQLite won't look there
                var temp = spatialite_version();

                connection.EnableExtensions();
                command.ExecuteNonQuery();
                connection.EnableExtensions(false);
            }

            return(connection);
        }
コード例 #2
0
ファイル: SqliteConnectionTest.cs プロジェクト: ymf1/efcore
        public void EnableExtensions_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var sql = "SELECT load_extension('unknown');";

                var ex            = Assert.Throws <SqliteException>(() => connection.ExecuteNonQuery(sql));
                var originalError = ex.Message;

                connection.EnableExtensions();

                ex = Assert.Throws <SqliteException>(() => connection.ExecuteNonQuery(sql));
                var enabledError = ex.Message;

                connection.EnableExtensions(enable: false);

                ex = Assert.Throws <SqliteException>(() => connection.ExecuteNonQuery(sql));
                var disabledError = ex.Message;

                Assert.NotEqual(originalError, enabledError);
                Assert.Equal(originalError, disabledError);
            }
        }
コード例 #3
0
        public void EnableExtensions_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var loadExtensionOmitted = connection.ExecuteScalar <long>(
                    "SELECT COUNT(*) FROM pragma_compile_options WHERE compile_options = 'OMIT_LOAD_EXTENSION';");
                if (loadExtensionOmitted != 0L)
                {
                    return;
                }

                var sql = "SELECT load_extension('unknown');";

                var ex            = Assert.Throws <SqliteException>(() => connection.ExecuteNonQuery(sql));
                var originalError = ex.Message;

                connection.EnableExtensions();

                ex = Assert.Throws <SqliteException>(() => connection.ExecuteNonQuery(sql));
                var enabledError = ex.Message;

                connection.EnableExtensions(enable: false);

                ex = Assert.Throws <SqliteException>(() => connection.ExecuteNonQuery(sql));
                var disabledError = ex.Message;

                Assert.NotEqual(originalError, enabledError);
                Assert.Equal(originalError, disabledError);
            }
        }
コード例 #4
0
        public void LoadExtension_works_when_closed()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var loadExtensionOmitted = connection.ExecuteScalar <long>(
                    "SELECT COUNT(*) FROM pragma_compile_options WHERE compile_options = 'OMIT_LOAD_EXTENSION';");
                if (loadExtensionOmitted != 0L)
                {
                    return;
                }

                connection.Close();
                connection.EnableExtensions(false);
                connection.Open();

                var ex = Assert.Throws <SqliteException>(
                    () => connection.ExecuteNonQuery("SELECT load_extension('unknown');"));
                var extensionsDisabledError = ex.Message;

                connection.Close();

                connection.LoadExtension("unknown");

                ex = Assert.Throws <SqliteException>(() => connection.Open());

                Assert.NotEqual(extensionsDisabledError, ex.Message);
            }
        }
コード例 #5
0
        public void EnableExtensions_doesnt_bleed_across_connections()
        {
            sqlite3         db;
            SqliteException ex;
            string          disabledMessage;

            using (var connection1 = new SqliteConnection(ConnectionString))
            {
                connection1.Open();
                db = connection1.Handle !;

                var loadExtensionOmitted = connection1.ExecuteScalar <long>(
                    "SELECT COUNT(*) FROM pragma_compile_options WHERE compile_options = 'OMIT_LOAD_EXTENSION';");
                if (loadExtensionOmitted != 0L)
                {
                    return;
                }

                ex = Assert.Throws <SqliteException>(
                    () => connection1.ExecuteNonQuery("SELECT load_extension('unknown');"));
                disabledMessage = ex.Message;

                connection1.EnableExtensions();
            }

            using var connection2 = new SqliteConnection(ConnectionString);
            connection2.Open();
            Assert.Same(db, connection2.Handle);

            ex = Assert.Throws <SqliteException>(
                () => connection2.ExecuteNonQuery("SELECT load_extension('unknown');"));
            Assert.Equal(disabledMessage, ex.Message);
        }
コード例 #6
0
 private SqliteConnection Open(string connectionString)
 {
     var connection = new SqliteConnection(connectionString);
     connection.Open();
     connection.EnableExtensions();
     connection.LoadExtension(_spatialiteExtensionName);
     return connection;
 }
コード例 #7
0
 public void EnableExtensions_throws_when_closed()
 {
     using (var connection = new SqliteConnection("Data Source=:memory:"))
     {
         var ex = Assert.Throws <InvalidOperationException>(() => connection.EnableExtensions());
         Assert.Equal(Resources.CallRequiresOpenConnection("EnableExtensions"), ex.Message);
     }
 }
コード例 #8
0
ファイル: SqliteConnectionTest.cs プロジェクト: ymf1/efcore
        public void LoadExtension_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.EnableExtensions(false);
                connection.Open();

                var ex = Assert.Throws <SqliteException>(
                    () => connection.ExecuteNonQuery("SELECT load_extension('unknown');"));
                var extensionsDisabledError = ex.Message;

                ex = Assert.Throws <SqliteException>(() => connection.LoadExtension("unknown"));

                Assert.NotEqual(extensionsDisabledError, ex.Message);
            }
        }
コード例 #9
0
 public void EnableExtensions_throws_when_closed()
 {
     using (var connection = new SqliteConnection("Data Source=:memory:"))
     {
         var ex = Assert.Throws<InvalidOperationException>(() => connection.EnableExtensions());
         Assert.Equal(Strings.FormatCallRequiresOpenConnection("EnableExtensions"), ex.Message);
     }
 }
コード例 #10
0
        public void EnableExtensions_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var sql = "SELECT load_extension('unknown');";

                var ex = Assert.Throws<SqliteException>(() => connection.ExecuteNonQuery(sql));
                var originalError = ex.Message;

                connection.EnableExtensions();

                ex = Assert.Throws<SqliteException>(() => connection.ExecuteNonQuery(sql));
                var enabledError = ex.Message;

                connection.EnableExtensions(enable: false);

                ex = Assert.Throws<SqliteException>(() => connection.ExecuteNonQuery(sql));
                var disabledError = ex.Message;

                Assert.NotEqual(originalError, enabledError);
                Assert.Equal(originalError, disabledError);
            }
        }