Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SQLiteLoader.cs « SQLiteHelper « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 53c3364b447172e1e0e3552db5a3bf28c63d77f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;

namespace Duplicati.Library.SQLiteHelper
{
    public static class SQLiteLoader
    {
        /// <summary>
        /// A cached copy of the type
        /// </summary>
        private static Type m_type = null;

        /// <summary>
        /// Returns the SQLiteCommand type for the current architecture
        /// </summary>
        public static Type SQLiteConnectionType
        {
            get
            {
                if (m_type == null)
                {
                    string filename = "System.Data.SQLite.dll";
                    string basePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "SQLite");

                    //Default is to use the pinvoke version which requires a native .dll/.so
                    string assemblyPath = System.IO.Path.Combine(basePath, "pinvoke");

                    if (!Duplicati.Library.Utility.Utility.IsMono)
                    {
                        //If we run with MS.Net we can use the mixed mode assemblies
                        if (Library.Utility.Utility.Is64BitProcess)
                        {
                            if (System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(basePath, "win64"), filename)))
                                assemblyPath = System.IO.Path.Combine(basePath, "win64");
                        }
                        else
                        {
                            if (System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(basePath, "win32"), filename)))
                                assemblyPath = System.IO.Path.Combine(basePath, "win32");
                        }
                    } else {
                        //On Mono, we try to find the Mono version of SQLite
                        
                        //This secret environment variable can be used to support older installations
                        var envvalue = System.Environment.GetEnvironmentVariable("DISABLE_MONO_DATA_SQLITE");
                        if (!Utility.Utility.ParseBool(envvalue, envvalue != null))
                        {
                            foreach(var asmversion in new string[] {"4.0.0.0", "2.0.0.0"})
                            {
                                try 
                                {
                                    Type t = System.Reflection.Assembly.Load(string.Format("Mono.Data.Sqlite, Version={0}, Culture=neutral, PublicKeyToken=0738eb9f132ed756", asmversion)).GetType("Mono.Data.Sqlite.SqliteConnection");
                                    if (t != null && t.GetInterface("System.Data.IDbConnection", false) != null)
                                    {
                                        Version v = new Version((string)t.GetProperty("SQLiteVersion").GetValue(null, null));
                                        if (v >= new Version(3, 6, 3))
                                        {
                                            m_type = t;
                                            return m_type;
                                        }
                                    }
                                    
                                } catch {
                                }
                            }

                            Console.WriteLine("Failed to load Mono.Data.Sqlite.SqliteConnection, reverting to built-in.");
                        }
                    }

                    m_type = System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(assemblyPath, filename)).GetType("System.Data.SQLite.SQLiteConnection");
                }

                return m_type;
            }
        }
    }
}