-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathOSqlCreateClass.cs
More file actions
173 lines (140 loc) · 4.97 KB
/
OSqlCreateClass.cs
File metadata and controls
173 lines (140 loc) · 4.97 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using Orient.Client.API.Types;
using Orient.Client.Protocol;
using Orient.Client.Protocol.Operations;
using Orient.Client.Protocol.Operations.Command;
// syntax:
// CREATE CLASS <class>
// [EXTENDS <super-class>]
// [CLUSTER <clusterId>*]
namespace Orient.Client
{
public class OSqlCreateClass
{
private SqlQuery _sqlQuery;
private Connection _connection;
private string _className;
private Type _type;
private bool _autoProperties;
public OSqlCreateClass()
{
_sqlQuery = new SqlQuery(null);
}
internal OSqlCreateClass(Connection connection)
{
_connection = connection;
_sqlQuery = new SqlQuery(connection);
}
#region Class
public OSqlCreateClass Class(string className)
{
_className = className;
_sqlQuery.Class(_className);
return this;
}
public OSqlCreateClass Class<T>()
{
_type = typeof(T);
_className = typeof(T).Name;
return Class(_className);
}
public OSqlCreateClass Class<T>(string className)
{
_type = typeof(T);
_className = className;
return Class(_className);
}
#endregion
#region Extends
public OSqlCreateClass Extends(string superClass)
{
_sqlQuery.Extends(superClass);
return this;
}
public OSqlCreateClass CreateProperties()
{
if (_type == null)
throw new InvalidOperationException("Can only create properties automatically when a generic type parameter has been specified");
_autoProperties = true;
return this;
}
public OSqlCreateClass CreateProperties<T>()
{
if (_type != null && _type != typeof(T))
throw new InvalidOperationException("Inconsistent type specified - type for CreateProperties<T> must match type for Class<T>");
_type = typeof(T);
_autoProperties = true;
return this;
}
public OSqlCreateClass Extends<T>()
{
return Extends(typeof(T).Name);
}
#endregion
public OSqlCreateClass Cluster(short clusterId)
{
_sqlQuery.Cluster(clusterId.ToString());
return this;
}
public string Run()
{
OCluster cluster;
var defaultClusterId = _connection.Database.GetClusterIdFor(_className);
if (defaultClusterId == -1)
{
CommandPayloadCommand payload = new CommandPayloadCommand();
payload.Text = ToString();
Command operation = new Command(_connection.Database);
operation.OperationMode = OperationMode.Synchronous;
operation.CommandPayload = payload;
var doc = _connection.ExecuteOperation(operation);
OCommandResult result = new OCommandResult(doc);
// var clusterId = short.Parse(result.ToDocument().GetField<string>("Content"));
cluster = _connection.Database.AddCluster(new OCluster { Name = _className });
}
else
{
cluster = _connection.Database.GetClusters().FirstOrDefault(c => c.Name.Equals(_className, StringComparison.OrdinalIgnoreCase));
}
if (_autoProperties)
{
CreateAutoProperties();
}
return cluster.Name;
}
private void CreateAutoProperties()
{
foreach (var pi in _type.GetTypeInfo().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
{
if (pi.CanRead && pi.CanWrite)
{
var oprop = pi.GetOPropertyAttribute();
if (oprop != null && !oprop.Deserializable && !oprop.Serializable)
continue;
CreateProperty(pi);
}
}
}
private void CreateProperty(PropertyInfo pi)
{
var propType = ConvertPropertyType(pi.PropertyType);
var @class = _className;
var propid = _connection.Database
.Create
.Property(pi.Name, propType)
.Class(@class)
.Run();
}
private OType ConvertPropertyType(Type propertyType)
{
return TypeConverter.TypeToDbName(propertyType);
}
public override string ToString()
{
return _sqlQuery.ToString(QueryType.CreateClass);
}
}
}