ProjectArcade
91 строка · 2.8 Кб
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using System.Data.Common;
7using System.Data;
8
9namespace EmulatorLauncher.Common
10{
11public static class SQLiteInteropManager
12{
13public static void InstallSQLiteInteropDll()
14{
15string dllName = Path.Combine(Path.GetDirectoryName(typeof(SQLiteInteropManager).Assembly.Location), "SQLite.Interop.dll");
16int platform = IntPtr.Size;
17
18if (File.Exists(dllName) && Kernel32.IsX64(dllName) == (IntPtr.Size == 8))
19return;
20
21if (File.Exists(dllName))
22{
23try { File.Delete(dllName); }
24catch { }
25}
26
27FileTools.ExtractGZipBytes(IntPtr.Size == 8 ? Properties.Resources.SQLite_Interop_x64 : Properties.Resources.SQLite_Interop_x86, dllName);
28}
29
30public static T[] ReadObjects<T>(this IDataReader reader) where T : new()
31{
32var cols = GetColumnIndices(reader);
33
34var properties = new Dictionary<int,System.Reflection.PropertyInfo>();
35
36foreach (var prop in typeof(T).GetProperties())
37{
38int index;
39if (cols.TryGetValue(prop.Name, out index))
40properties.Add(index, prop);
41}
42
43List<T> ret = new List<T>();
44
45if (properties.Count == 0)
46return ret.ToArray();
47
48while (reader.Read())
49{
50T instance = new T();
51
52foreach (var prop in properties)
53{
54if (prop.Value.PropertyType == typeof(string))
55{
56var str = reader.GetString(prop.Key);
57prop.Value.SetValue(instance, str, null);
58}
59else if (prop.Value.PropertyType == typeof(int))
60{
61var intVal = reader.GetInt32(prop.Key);
62prop.Value.SetValue(instance, intVal, null);
63}
64else if (prop.Value.PropertyType == typeof(long))
65{
66var intVal = reader.GetInt64(prop.Key);
67prop.Value.SetValue(instance, intVal, null);
68}
69}
70
71ret.Add(instance);
72}
73
74return ret.ToArray();
75}
76
77static Dictionary<string, int> GetColumnIndices(IDataReader reader)
78{
79Dictionary<string, int> columnIndices = new Dictionary<string, int>();
80
81for (int i = 0; i < reader.FieldCount; i++)
82{
83string columnName = reader.GetName(i);
84columnIndices.Add(columnName, i);
85}
86
87return columnIndices;
88}
89
90}
91}
92