Skip to content

Commit 18a193e

Browse files
committed
MySql Provider update
1 parent ab9bce5 commit 18a193e

11 files changed

Lines changed: 234 additions & 195 deletions

Provider for MySQL/DbObject.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ public class ColumnInfo
1515
}
1616
public class DbObject<T> where T : DbObject<T>, new()
1717
{
18-
public string db_TableName;
19-
public List<ColumnInfo> db_Columns = new List<ColumnInfo>();
18+
// ReSharper disable once StaticMemberInGenericType
19+
public static string DbTableName;
20+
21+
22+
public List<ColumnInfo> DBColumns = new List<ColumnInfo>();
2023

2124
public virtual object GetValue(string key)
2225
{
@@ -34,11 +37,11 @@ public virtual int Insert(MySqlConnection connection)
3437
using (var command = connection.CreateCommand())
3538
{
3639
command.CommandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})",
37-
db_TableName,
38-
String.Join(",", db_Columns.Select(c=> string.Format("`{0}`", c.Name) )),
39-
String.Join(",", db_Columns.Select(c=> "@" + c.Name)));
40+
DbTableName,
41+
String.Join(",", DBColumns.Select(c=> string.Format("`{0}`", c.Name) )),
42+
String.Join(",", DBColumns.Select(c=> "@" + c.Name)));
4043

41-
command.Parameters.AddRange(db_Columns.Select(c=> CreateParameter(c)).ToArray());
44+
command.Parameters.AddRange(DBColumns.Select(c=> CreateParameter(c)).ToArray());
4245
command.CommandType = CommandType.Text;
4346
int cnt = command.ExecuteNonQuery();
4447
return cnt;
@@ -48,11 +51,11 @@ public virtual int Insert(MySqlConnection connection)
4851
public int Update(MySqlConnection connection)
4952
{
5053
string command = string.Format(@"UPDATE {0} SET {1} WHERE {2}",
51-
db_TableName,
52-
String.Join(",", db_Columns.Where(c => !c.IsKey).Select(c => string.Format("`{0}` = @{0}", c.Name))),
53-
String.Join(" AND ", db_Columns.Where(c => c.IsKey).Select(c => string.Format("`{0}` = @{0}", c.Name))));
54+
DbTableName,
55+
String.Join(",", DBColumns.Where(c => !c.IsKey).Select(c => string.Format("`{0}` = @{0}", c.Name))),
56+
String.Join(" AND ", DBColumns.Where(c => c.IsKey).Select(c => string.Format("`{0}` = @{0}", c.Name))));
5457

55-
var parameters = db_Columns.Select(c =>
58+
var parameters = DBColumns.Select(c =>
5659
CreateParameter(c)).ToArray();
5760

5861
return ExecuteCommand(connection, command, parameters);
@@ -63,31 +66,29 @@ public static T SelectByKey(MySqlConnection connection, object id)
6366
{
6467
var t = new T();
6568

66-
var key = t.db_Columns.Where(c => c.IsKey).FirstOrDefault();
69+
var key = t.DBColumns.FirstOrDefault(c => c.IsKey);
6770
if(key == null)
6871
{
69-
throw new Exception(string.Format("Key for table {0} isn't defined.", t.db_TableName));
72+
throw new Exception(string.Format("Key for table {0} isn't defined.", DbTableName));
7073
}
7174

72-
string selectText = string.Format("SELECT * FROM {0} WHERE `{1}` = @p_id", t.db_TableName, key.Name.ToUpper());
73-
var p_id = new MySqlParameter("p_id", key.Type);
74-
p_id.Value = ConvertToDBCompatibilityType(id);
75+
string selectText = string.Format("SELECT * FROM {0} WHERE `{1}` = @p_id", DbTableName, key.Name.ToUpper());
76+
var pId = new MySqlParameter("p_id", key.Type) {Value = ConvertToDBCompatibilityType(id)};
7577

76-
return Select(connection, selectText, p_id).FirstOrDefault();
78+
return Select(connection, selectText, pId).FirstOrDefault();
7779
}
7880

7981
public static int Delete(MySqlConnection connection, object id, MySqlTransaction transaction = null)
8082
{
8183
var t = new T();
82-
var key = t.db_Columns.Where(c => c.IsKey).FirstOrDefault();
84+
var key = t.DBColumns.FirstOrDefault(c => c.IsKey);
8385
if (key == null)
84-
throw new Exception(string.Format("Key for table {0} isn't defined.", t.db_TableName));
86+
throw new Exception(string.Format("Key for table {0} isn't defined.", DbTableName));
8587

86-
var p_id = new MySqlParameter("p_id", key.Type);
87-
p_id.Value = ConvertToDBCompatibilityType(id);
88+
var pId = new MySqlParameter("p_id", key.Type) {Value = ConvertToDBCompatibilityType(id)};
8889

8990
return ExecuteCommand(connection,
90-
string.Format("DELETE FROM {0} WHERE `{1}` = @p_id", t.db_TableName, key.Name.ToUpper()),transaction, p_id);
91+
string.Format("DELETE FROM {0} WHERE `{1}` = @p_id", DbTableName, key.Name.ToUpper()), transaction, pId);
9192
}
9293
public static int ExecuteCommand(MySqlConnection connection, string commandText,
9394
params MySqlParameter[] parameters)
@@ -131,7 +132,7 @@ public static T[] Select(MySqlConnection connection, string commandText, params
131132
command.CommandType = CommandType.Text;
132133
command.Parameters.AddRange(parameters);
133134

134-
DataTable dt = new DataTable();
135+
var dt = new DataTable();
135136
using (var oda = new MySqlDataAdapter(command))
136137
{
137138
oda.Fill(dt);
@@ -142,7 +143,7 @@ public static T[] Select(MySqlConnection connection, string commandText, params
142143
foreach (DataRow row in dt.Rows)
143144
{
144145
T item = new T();
145-
foreach (var c in item.db_Columns)
146+
foreach (var c in item.DBColumns)
146147
item.SetValue(c.Name, row[c.Name.ToUpper()]);
147148
res.Add(item);
148149
}
@@ -161,8 +162,7 @@ public static object ConvertToDBCompatibilityType(object obj)
161162

162163
public virtual MySqlParameter CreateParameter(ColumnInfo c)
163164
{
164-
var p = new MySqlParameter(c.Name, c.Type);
165-
p.Value = GetValue(c.Name);
165+
var p = new MySqlParameter(c.Name, c.Type) {Value = GetValue(c.Name)};
166166
return p;
167167
}
168168
}

Provider for MySQL/Models/WorkflowGlobalParameter.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using MySql.Data.MySqlClient;
33

4+
// ReSharper disable once CheckNamespace
45
namespace OptimaJet.Workflow.MySQL
56
{
67
public class WorkflowGlobalParameter : DbObject<WorkflowGlobalParameter>
@@ -13,18 +14,19 @@ public class WorkflowGlobalParameter : DbObject<WorkflowGlobalParameter>
1314

1415
public string Value { get; set; }
1516

16-
private const string _tableName = "workflowglobalparameter";
17-
17+
static WorkflowGlobalParameter()
18+
{
19+
DbTableName = "workflowglobalparameter";
20+
}
21+
1822
public WorkflowGlobalParameter()
19-
: base()
2023
{
21-
db_TableName = _tableName;
22-
db_Columns.AddRange(new ColumnInfo[]
24+
DBColumns.AddRange(new[]
2325
{
24-
new ColumnInfo() {Name = "Id", IsKey = true, Type = MySqlDbType.Binary},
25-
new ColumnInfo() {Name = "Type"},
26-
new ColumnInfo() {Name = "Name"},
27-
new ColumnInfo() {Name = "Value"}
26+
new ColumnInfo {Name = "Id", IsKey = true, Type = MySqlDbType.Binary},
27+
new ColumnInfo {Name = "Type"},
28+
new ColumnInfo {Name = "Name"},
29+
new ColumnInfo {Name = "Value"}
2830
});
2931
}
3032

@@ -68,7 +70,7 @@ public override void SetValue(string key, object value)
6870

6971
public static WorkflowGlobalParameter[] SelectByTypeAndName(MySqlConnection connection, string type, string name = null)
7072
{
71-
string selectText = string.Format("SELECT * FROM {0} WHERE `Type` = @type", _tableName);
73+
string selectText = string.Format("SELECT * FROM {0} WHERE `Type` = @type", DbTableName);
7274

7375
if (!string.IsNullOrEmpty(name))
7476
selectText = selectText + " AND `Name` = @name";
@@ -85,7 +87,7 @@ public static WorkflowGlobalParameter[] SelectByTypeAndName(MySqlConnection conn
8587

8688
public static int DeleteByTypeAndName(MySqlConnection connection, string type, string name = null)
8789
{
88-
string selectText = string.Format("DELETE FROM {0} WHERE `Type` = @type", _tableName);
90+
string selectText = string.Format("DELETE FROM {0} WHERE `Type` = @type", DbTableName);
8991

9092
if (!string.IsNullOrEmpty(name))
9193
selectText = selectText + " AND `Name` = @name";

Provider for MySQL/Models/WorkflowProcessInstance.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using MySql.Data.MySqlClient;
33

4+
// ReSharper disable once CheckNamespace
45
namespace OptimaJet.Workflow.MySQL
56
{
67
public class WorkflowProcessInstance: DbObject<WorkflowProcessInstance>
@@ -18,24 +19,29 @@ public class WorkflowProcessInstance: DbObject<WorkflowProcessInstance>
1819
public string StateName { get; set; }
1920
public Guid? ParentProcessId { get; set; }
2021
public Guid RootProcessId { get; set; }
21-
public WorkflowProcessInstance(): base()
22+
23+
static WorkflowProcessInstance()
24+
{
25+
DbTableName = "workflowprocessinstance";
26+
}
27+
28+
public WorkflowProcessInstance()
2229
{
23-
db_TableName = "WorkflowProcessInstance";
24-
db_Columns.AddRange(new ColumnInfo[]
30+
DBColumns.AddRange(new[]
2531
{
26-
new ColumnInfo() {Name = "Id", IsKey = true, Type = MySqlDbType.Binary},
27-
new ColumnInfo() {Name = "ActivityName"},
28-
new ColumnInfo() {Name = "IsDeterminingParametersChanged", Type = MySqlDbType.Bit},
29-
new ColumnInfo() {Name = "PreviousActivity"},
30-
new ColumnInfo() {Name = "PreviousActivityForDirect"},
31-
new ColumnInfo() {Name = "PreviousActivityForReverse"},
32-
new ColumnInfo() {Name = "PreviousState"},
33-
new ColumnInfo() {Name = "PreviousStateForDirect"},
34-
new ColumnInfo() {Name = "PreviousStateForReverse"},
35-
new ColumnInfo() {Name = "SchemeId", Type = MySqlDbType.Binary},
36-
new ColumnInfo() {Name = "StateName"},
37-
new ColumnInfo() {Name = "ParentProcessId", Type = MySqlDbType.Binary},
38-
new ColumnInfo() {Name = "RootProcessId", Type = MySqlDbType.Binary},
32+
new ColumnInfo {Name = "Id", IsKey = true, Type = MySqlDbType.Binary},
33+
new ColumnInfo {Name = "ActivityName"},
34+
new ColumnInfo {Name = "IsDeterminingParametersChanged", Type = MySqlDbType.Bit},
35+
new ColumnInfo {Name = "PreviousActivity"},
36+
new ColumnInfo {Name = "PreviousActivityForDirect"},
37+
new ColumnInfo {Name = "PreviousActivityForReverse"},
38+
new ColumnInfo {Name = "PreviousState"},
39+
new ColumnInfo {Name = "PreviousStateForDirect"},
40+
new ColumnInfo {Name = "PreviousStateForReverse"},
41+
new ColumnInfo {Name = "SchemeId", Type = MySqlDbType.Binary},
42+
new ColumnInfo {Name = "StateName"},
43+
new ColumnInfo {Name = "ParentProcessId", Type = MySqlDbType.Binary},
44+
new ColumnInfo {Name = "RootProcessId", Type = MySqlDbType.Binary},
3945
});
4046
}
4147

Provider for MySQL/Models/WorkflowProcessInstancePersistence.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using MySql.Data.MySqlClient;
33

4+
// ReSharper disable once CheckNamespace
45
namespace OptimaJet.Workflow.MySQL
56
{
67
public class WorkflowProcessInstancePersistence : DbObject<WorkflowProcessInstancePersistence>
@@ -10,17 +11,18 @@ public class WorkflowProcessInstancePersistence : DbObject<WorkflowProcessInstan
1011
public string ParameterName { get; set; }
1112
public string Value { get; set; }
1213

13-
private static string _tableName = "WorkflowProcessInstancePersistence";
14+
static WorkflowProcessInstancePersistence()
15+
{
16+
DbTableName = "workflowprocessinstancepersistence";
17+
}
1418

1519
public WorkflowProcessInstancePersistence()
16-
: base()
1720
{
18-
db_TableName = _tableName;
19-
db_Columns.AddRange(new ColumnInfo[]{
20-
new ColumnInfo(){Name="Id", IsKey = true, Type = MySqlDbType.Binary},
21-
new ColumnInfo(){Name="ProcessId", Type = MySqlDbType.Binary},
22-
new ColumnInfo(){Name="ParameterName"},
23-
new ColumnInfo(){Name="Value", Type = MySqlDbType.LongText }
21+
DBColumns.AddRange(new[]{
22+
new ColumnInfo {Name="Id", IsKey = true, Type = MySqlDbType.Binary},
23+
new ColumnInfo {Name="ProcessId", Type = MySqlDbType.Binary},
24+
new ColumnInfo {Name="ParameterName"},
25+
new ColumnInfo {Name="Value", Type = MySqlDbType.LongText }
2426
});
2527
}
2628

@@ -64,19 +66,17 @@ public override void SetValue(string key, object value)
6466

6567
public static WorkflowProcessInstancePersistence[] SelectByProcessId(MySqlConnection connection, Guid processId)
6668
{
67-
string selectText = string.Format("SELECT * FROM {0} WHERE `ProcessId` = @processid", _tableName);
68-
var p = new MySqlParameter("processId", MySqlDbType.Binary);
69-
p.Value = processId.ToByteArray();
69+
string selectText = string.Format("SELECT * FROM {0} WHERE `ProcessId` = @processid", DbTableName);
70+
var p = new MySqlParameter("processId", MySqlDbType.Binary) {Value = processId.ToByteArray()};
7071
return Select(connection, selectText, p);
7172
}
7273

7374
public static int DeleteByProcessId(MySqlConnection connection, Guid processId, MySqlTransaction transaction = null)
7475
{
75-
var p = new MySqlParameter("processId", MySqlDbType.Binary);
76-
p.Value = processId.ToByteArray();
76+
var p = new MySqlParameter("processId", MySqlDbType.Binary) {Value = processId.ToByteArray()};
7777

7878
return ExecuteCommand(connection,
79-
string.Format("DELETE FROM {0} WHERE `ProcessId` = @processid", _tableName),transaction, p);
79+
string.Format("DELETE FROM {0} WHERE `ProcessId` = @processid", DbTableName), transaction, p);
8080
}
8181
}
8282
}

Provider for MySQL/Models/WorkflowProcessInstanceStatus.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using MySql.Data.MySqlClient;
33

4+
// ReSharper disable once CheckNamespace
45
namespace OptimaJet.Workflow.MySQL
56
{
67
public class WorkflowProcessInstanceStatus : DbObject<WorkflowProcessInstanceStatus>
@@ -9,15 +10,17 @@ public class WorkflowProcessInstanceStatus : DbObject<WorkflowProcessInstanceSta
910
public Guid Lock { get; set; }
1011
public byte Status { get; set; }
1112

12-
private const string TableName = "WorkflowProcessInstanceStatus";
13+
static WorkflowProcessInstanceStatus()
14+
{
15+
DbTableName = "workflowprocessinstancestatus";
16+
}
1317

1418
public WorkflowProcessInstanceStatus()
1519
{
16-
db_TableName = TableName;
17-
db_Columns.AddRange(new ColumnInfo[]{
18-
new ColumnInfo(){Name="Id", IsKey = true, Type = MySqlDbType.Binary},
19-
new ColumnInfo(){Name="Lock", Type = MySqlDbType.Binary},
20-
new ColumnInfo(){Name="Status", Type = MySqlDbType.Byte}
20+
DBColumns.AddRange(new[]{
21+
new ColumnInfo {Name="Id", IsKey = true, Type = MySqlDbType.Binary},
22+
new ColumnInfo {Name="Lock", Type = MySqlDbType.Binary},
23+
new ColumnInfo {Name="Status", Type = MySqlDbType.Byte}
2124
});
2225
}
2326

@@ -57,7 +60,7 @@ public override void SetValue(string key, object value)
5760

5861
public static int MassChangeStatus(MySqlConnection connection, byte stateFrom, byte stateTo)
5962
{
60-
string command = string.Format("UPDATE {0} SET `Status` = @stateto WHERE `Status` = @statefrom", TableName);
63+
var command = string.Format("UPDATE {0} SET `Status` = @stateto WHERE `Status` = @statefrom", DbTableName);
6164
var p1 = new MySqlParameter("statefrom", MySqlDbType.Byte) {Value = stateFrom};
6265
var p2 = new MySqlParameter("stateto", MySqlDbType.Byte) {Value = stateTo};
6366

@@ -66,7 +69,7 @@ public static int MassChangeStatus(MySqlConnection connection, byte stateFrom, b
6669

6770
public static int ChangeStatus(MySqlConnection connection, WorkflowProcessInstanceStatus status, Guid oldLock)
6871
{
69-
string command = string.Format("UPDATE {0} SET `Status` = @newstatus, `Lock` = @newlock WHERE `Id` = @id AND `Lock` = @oldlock", TableName);
72+
var command = string.Format("UPDATE {0} SET `Status` = @newstatus, `Lock` = @newlock WHERE `Id` = @id AND `Lock` = @oldlock", DbTableName);
7073
var p1 = new MySqlParameter("newstatus", MySqlDbType.Byte) { Value = status.Status };
7174
var p2 = new MySqlParameter("newlock", MySqlDbType.Binary) { Value = status.Lock.ToByteArray() };
7275
var p3 = new MySqlParameter("id", MySqlDbType.Binary) { Value = status.Id.ToByteArray() };

0 commit comments

Comments
 (0)