ProjectArcade

Форк
0
91 строка · 2.8 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.IO;
6
using System.Data.Common;
7
using System.Data;
8

9
namespace EmulatorLauncher.Common
10
{
11
    public static class SQLiteInteropManager
12
    {
13
        public static void InstallSQLiteInteropDll()
14
        {
15
            string dllName = Path.Combine(Path.GetDirectoryName(typeof(SQLiteInteropManager).Assembly.Location), "SQLite.Interop.dll");
16
            int platform = IntPtr.Size;
17

18
            if (File.Exists(dllName) && Kernel32.IsX64(dllName) == (IntPtr.Size == 8))
19
                return;
20

21
            if (File.Exists(dllName))
22
            {
23
                try { File.Delete(dllName); }
24
                catch { }
25
            }
26

27
            FileTools.ExtractGZipBytes(IntPtr.Size == 8 ? Properties.Resources.SQLite_Interop_x64 : Properties.Resources.SQLite_Interop_x86, dllName);
28
        }
29

30
        public static T[] ReadObjects<T>(this IDataReader reader) where T : new()
31
        {
32
            var cols = GetColumnIndices(reader);
33

34
            var properties = new Dictionary<int,System.Reflection.PropertyInfo>();
35

36
            foreach (var prop in typeof(T).GetProperties())
37
            {
38
                int index;
39
                if (cols.TryGetValue(prop.Name, out index))
40
                    properties.Add(index, prop);
41
            }
42

43
            List<T> ret = new List<T>();
44

45
            if (properties.Count == 0)
46
                return ret.ToArray();
47

48
            while (reader.Read())
49
            {
50
                T instance = new T();
51

52
                foreach (var prop in properties)
53
                {
54
                    if (prop.Value.PropertyType == typeof(string))
55
                    {
56
                        var str = reader.GetString(prop.Key);
57
                        prop.Value.SetValue(instance, str, null);
58
                    }
59
                    else if (prop.Value.PropertyType == typeof(int))
60
                    {
61
                        var intVal = reader.GetInt32(prop.Key);
62
                        prop.Value.SetValue(instance, intVal, null);
63
                    }
64
                    else if (prop.Value.PropertyType == typeof(long))
65
                    {
66
                        var intVal = reader.GetInt64(prop.Key);
67
                        prop.Value.SetValue(instance, intVal, null);
68
                    }
69
                }
70

71
                ret.Add(instance);
72
            }
73

74
            return ret.ToArray();
75
        }
76

77
        static Dictionary<string, int> GetColumnIndices(IDataReader reader)
78
        {
79
            Dictionary<string, int> columnIndices = new Dictionary<string, int>();
80

81
            for (int i = 0; i < reader.FieldCount; i++)
82
            {
83
                string columnName = reader.GetName(i);
84
                columnIndices.Add(columnName, i);
85
            }
86

87
            return columnIndices;
88
        }
89

90
    }
91
}
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.