-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
52 lines (48 loc) · 2.26 KB
/
Program.cs
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
using System;
using System.Data;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using System.Windows.Forms;
using DevExpress.Xpo.Metadata;
using DXSample;
using System.Data.SQLite;
namespace MapArbitraryDatabaseExample {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string connectionString = SQLiteConnectionProvider.GetConnectionString(@"nwind.sqlite");
using (IDbConnection connection = new SQLiteConnection(connectionString)) {
InitializeDataLayer(connection);
Application.Run(new Form1());
}
}
static void InitializeDataLayer(IDbConnection connection) {
ConnectionProviderSql prov = (ConnectionProviderSql)XpoDefault.GetConnectionProvider(connection, AutoCreateOption.None);
XPDictionary dict = new ReflectionDictionary();
DBTable[] tables = prov.GetStorageTables("Category");
AddClass(dict, tables[0]);
XpoDefault.DataLayer = new SimpleDataLayer(dict, prov);
}
public static XPClassInfo AddClass(XPDictionary dict, DBTable table) {
if (table.PrimaryKey.Columns.Count > 1)
throw new NotSupportedException("Compound primary keys are not supported");
XPClassInfo classInfo = dict.CreateClass(dict.GetClassInfo(typeof(BasePersistentClass)), table.Name.Replace('.', '_'));
classInfo.AddAttribute(new PersistentAttribute(table.Name));
DBColumnType primaryColumnType = table.GetColumn(table.PrimaryKey.Columns[0]).ColumnType;
classInfo.CreateMember(table.PrimaryKey.Columns[0], DBColumn.GetType(primaryColumnType),
new KeyAttribute(IsAutoGenerationSupported(primaryColumnType)));
foreach (DBColumn col in table.Columns)
if (!col.IsKey && col.ColumnType != DBColumnType.Unknown)
classInfo.CreateMember(col.Name, DBColumn.GetType(col.ColumnType));
return classInfo;
}
static bool IsAutoGenerationSupported(DBColumnType type) {
return type == DBColumnType.Guid || type == DBColumnType.Int16 || type == DBColumnType.Int32;
}
}
}