-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemaBuilder.cs
124 lines (89 loc) · 2.31 KB
/
SchemaBuilder.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NuoTest
{
// SchemaBuilder
// Establish the schema in the database.
//
class SchemaBuilder : Writer
{
public SchemaBuilder(DbCommand command)
: base(command)
{
}
internal override void Write()
{
try {
Run();
}
catch (Exception e) {
Console.WriteLine(e.Message);
throw e;
}
}
protected override void Run()
{
Command.Parameters.Clear();
Command.CommandType = CommandType.Text;
foreach (String ddl in ddlCommands) {
String pattern = @"[\t]";
Command.CommandText = Regex.Replace(ddl, pattern, "");
Command.ExecuteNonQuery();
}
}
private String[] ddlCommands = new String[] {
@"DROP PROCEDURE INSERTAUDIT IF EXISTS",
@"DROP TABLE IF EXISTS AUDIT",
@"DROP PROCEDURE IF EXISTS INSERTDATA",
@"DROP TABLE IF EXISTS T_DATA",
@"DROP PROCEDURE IF EXISTS INSERTGROUP",
@"DROP TABLE IF EXISTS T_GROUP",
@"DROP PROCEDURE IF EXISTS INSERTEVENT",
@"DROP TABLE IF EXISTS T_EVENT",
@"DROP PROCEDURE IF EXISTS INSERTOWNER",
@"DROP TABLE IF EXISTS T_OWNER",
@"CREATE TABLE T_OWNER (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name STRING NOT NULL,
masterAlias BIGINT NOT NULL
)",
@"CREATE TABLE T_EVENT(
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
ownerId BIGINT NOT NULL,
name STRING NOT NULL,
description STRING NOT NULL,
date DATE NOT NULL
)",
@"CREATE TABLE T_GROUP (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
eventId BIGINT NOT NULL,
name STRING NOT NULL,
description STRING NOT NULL,
dataCount INTEGER NOT NULL,
date DATE NOT NULL
)",
@"CREATE TABLE T_DATA (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
groupId BIGINT NOT NULL,
instanceUID STRING NOT NULL,
name STRING NOT NULL,
description STRING NOT NULL,
path STRING NOT NULL,
active SMALLINT NOT NULL
)",
@"CREATE TABLE T_AUDIT (
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
date DATE NOT NULL,
targetTable STRING NOT NULL,
targetId BIGINT NOT NULL,
action STRING NOT NULL
)",
};
}
}