/
Model.cs
99 lines (77 loc) · 3.43 KB
/
Model.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using MySql.Data.MySqlClient;
namespace AltairStudios.Core.Orm {
public class Model : AltairStudios.Core.Mvc.Model {
public List<T> getBy<T>() {
Type type = this.GetType();
PropertyInfo[] properties = this.GetType().GetProperties();
StringBuilder sql = new StringBuilder();
List<PropertyInfo> parameters = new List<PropertyInfo>();
List<string> fields = this.getFields(properties);
sql.Append("SELECT " + string.Join(",", fields.ToArray()) + " FROM " + type.Name + " WHERE ");
for(int i = 0; i < properties.Length; i++) {
if(properties[i].GetValue(this, null) != null && properties[i].PropertyType.ToString() == "System.String") {
//sql.Append(properties[i].Name + " = '" + properties[i].GetValue(this, null) + "' AND " );
TemplatizeAttribute[] attributes = (TemplatizeAttribute[])properties[i].GetCustomAttributes(typeof(TemplatizeAttribute), true);
if(attributes.Length > 0 && attributes[0].Templatize) {
sql.Append(properties[i].Name + " = @" + properties[i].Name + " AND " );
parameters.Add(properties[i]);
}
}
}
sql.Append("1 = 1");
MySqlCommand command = ConnectionFactory.createCommand();
command.CommandText = sql.ToString();
for(int i = 0; i < parameters.Count; i++) {
command.Parameters.Add(parameters[i].Name, ConnectionFactory.resolveType(properties[i].PropertyType)).Value = parameters[i].GetValue(this, null);
}
MySqlDataReader reader = command.ExecuteReader();
List<T> models = new List<T>();
ConstructorInfo constructor = type.GetConstructor(new Type[0]);
int counter = 0;
while(reader.Read()) {
object instance = constructor.Invoke(new Object[0]);
counter = 0;
for(int i = 0; i < properties.Length; i++) {
TemplatizeAttribute[] attributes = (TemplatizeAttribute[])properties[i].GetCustomAttributes(typeof(TemplatizeAttribute), true);
if(attributes.Length > 0 && attributes[0].Templatize) {
type.GetProperty(properties[i].Name).SetValue(instance, reader[counter], null);
counter++;
}
}
models.Add(this.cast<T>(instance));
}
reader.Close();
command.Connection.Close();
return models;
}
public string save() {
Type type = this.GetType();
PropertyInfo[] properties = this.GetType().GetProperties();
StringBuilder sql = new StringBuilder();
List<PropertyInfo> parameters = new List<PropertyInfo>();
sql.Append("UPDATE " + type.Name + " SET ");
for(int i = 0; i < properties.Length; i++) {
if(properties[i].PropertyType.ToString() == "System.String") {
TemplatizeAttribute[] attributes = (TemplatizeAttribute[])properties[i].GetCustomAttributes(typeof(TemplatizeAttribute), true);
if(attributes.Length > 0 && attributes[0].Templatize) {
sql.Append(properties[i].Name + " = @" + properties[i].Name + ", " );
parameters.Add(properties[i]);
}
}
}
sql.Append("1 = 1");
sql.Append(" WHERE id = @Id");
MySqlCommand command = ConnectionFactory.createCommand();
command.CommandText = sql.ToString();
for(int i = 0; i < parameters.Count; i++) {
command.Parameters.Add(parameters[i].Name, ConnectionFactory.resolveType(properties[i].PropertyType)).Value = parameters[i].GetValue(this, null);
}
//MySqlDataReader reader = command.ExecuteReader();
return sql.ToString();
}
}
}