Skip to content

Commit 7de62bb

Browse files
Merge
2 parents 8cdf792 + 8c4611e commit 7de62bb

31 files changed

Lines changed: 1118 additions & 1049 deletions

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ namespace Sample
5454
public string Name { get; set; }
5555
public bool IsDriver { get; set; }
5656
57-
// required constructor with all properties in alphabetical order
5857
public Person(int id, bool isDriver, string name)
5958
{
6059
Id = id;

src/SQLiteAbstractCrud.Proxy/Field.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Field
66
{
77
public string NameOnDb { get; private set; }
88
internal string TypeCSharp { get; set; }
9-
public string TypeSQLite { get; private set; }
9+
public string SQLiteType { get; private set; }
1010
public string Quote { get; private set; }
1111
public bool IsPrimaryKey { get; }
1212
public bool IsAutoincrement { get; }
@@ -33,13 +33,13 @@ private void CtorConfig(string nameOnDb, string typeCSharp)
3333
{
3434
case "String":
3535
case "DateTime":
36-
TypeSQLite = "TEXT";
36+
SQLiteType = "TEXT";
3737
Quote = "'";
3838
break;
3939
case "Int32":
4040
case "Int16":
4141
case "Boolean":
42-
TypeSQLite = "INTEGER";
42+
SQLiteType = "INTEGER";
4343
Quote = "";
4444
break;
4545
default:

src/SQLiteAbstractCrud.Proxy/Fields.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Reflection;
34
using SQLiteAbstractCrud.Proxy.Queries;
45

56
namespace SQLiteAbstractCrud.Proxy
67
{
78
internal class Fields
89
{
9-
public List<Field> Items { get; } = new();
10-
11-
public void AddField(string name, string csharpType, bool isPrimaryKey, bool isAutoincrement)
10+
public Fields(List<ProxyPropertyInfo> proxyPropertiesInfos)
1211
{
13-
Items.Add(new Field(name, csharpType, isPrimaryKey, isAutoincrement));
12+
foreach (var p in proxyPropertiesInfos)
13+
{
14+
var field = new Field(p.OriginalName, p.CSharpType, p.IsPrimaryKey, p.IsAutoIncrement);
15+
Items.Add(field);
16+
}
1417
}
1518

19+
public List<Field> Items { get; } = new();
20+
21+
//private void AddField(string name, string csharpType)
22+
//{
23+
// Items.Add(new Field(name, csharpType));
24+
//}
25+
26+
//private void AddField(string name, string csharpType, bool isPrimaryKey, bool isAutoincrement)
27+
//{
28+
// Items.Add(new Field(name, csharpType, isPrimaryKey, isAutoincrement));
29+
//}
30+
1631
public string GetPrimaryKeyName()
1732
{
1833
return Items.First(x => x.IsPrimaryKey).NameOnDb;

src/SQLiteAbstractCrud.Proxy/OriginalPropertyInfo.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace SQLiteAbstractCrud.Proxy
5+
{
6+
public sealed class ProxyCtorParameterInfo
7+
{
8+
public string Name { get; set; }
9+
public string SQLiteType { get; set; }
10+
public string CSharpType { get; set; }
11+
12+
public static string GetSQLiteType(string csharpType)
13+
{
14+
switch (csharpType)
15+
{
16+
case "String":
17+
case "DateTime":
18+
return "TEXT";
19+
case "Int32":
20+
case "Int16":
21+
case "Boolean":
22+
return "INTEGER";
23+
default:
24+
throw new ArgumentOutOfRangeException(nameof(csharpType), "CsharpType not found");
25+
}
26+
}
27+
28+
public ProxyCtorParameterInfo(ParameterInfo parameterInfo)
29+
{
30+
this.Name = parameterInfo.Name;
31+
this.CSharpType = parameterInfo.ParameterType.Name;
32+
this.SQLiteType = GetSQLiteType(this.CSharpType);
33+
}
34+
}
35+
}

src/SQLiteAbstractCrud.Proxy/ProxyPropertyInfo.cs renamed to src/SQLiteAbstractCrud.Proxy/PropertyInfo.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public sealed class ProxyPropertyInfo
99
public int OriginalOrder { get; }
1010
private PropertyInfo _propertyInfo;
1111
public string OriginalName => _propertyInfo.Name;
12+
1213
public string CSharpType => _propertyInfo.PropertyType.Name;
14+
1315
public bool IsPrimaryKey
1416
{
1517
get
@@ -28,11 +30,16 @@ public bool IsAutoIncrement
2830
}
2931
public int ProxyOrder { get; }
3032

31-
public ProxyPropertyInfo(int originalOrder, PropertyInfo propertyInfo, int proxyOrder)
33+
//public ProxyPropertyInfo(int originalOrder, PropertyInfo propertyInfo, int proxyOrder)
34+
//{
35+
// this.OriginalOrder = originalOrder;
36+
// this._propertyInfo = propertyInfo;
37+
// this.ProxyOrder = proxyOrder;
38+
//}
39+
40+
public ProxyPropertyInfo(PropertyInfo propertyInfo)
3241
{
33-
this.OriginalOrder = originalOrder;
3442
this._propertyInfo = propertyInfo;
35-
this.ProxyOrder = proxyOrder;
3643
}
3744
}
3845
}

src/SQLiteAbstractCrud.Proxy/ProxyBase.cs

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,50 @@ namespace SQLiteAbstractCrud.Proxy
88
public class ProxyBase
99
{
1010
private Type _type;
11-
private readonly List<OriginalPropertyInfo> _originalPropertiesInfos = new();
1211
private readonly List<ProxyPropertyInfo> _proxyPropertiesInfos = new();
12+
private readonly List<ProxyCtorParameterInfo> _proxyCtorParametersInfos = new();
1313

14-
internal Fields Fields { get; } = new();
14+
internal Fields Fields;
1515

1616
public ProxyBase(Type type)
1717
{
1818
this._type = type;
19-
var properties = this._type.GetProperties().ToList();
20-
21-
int i = 0;
22-
properties.ForEach(x =>
23-
{
24-
_originalPropertiesInfos.Add(new OriginalPropertyInfo(i, x.Name));
25-
i++;
26-
});
27-
28-
int j = 0;
29-
properties.OrderBy(x => x.Name).ToList().ForEach(x =>
30-
{
31-
var originalOrder = _originalPropertiesInfos.First(y => y.Name == x.Name).Order;
32-
_proxyPropertiesInfos.Add(new ProxyPropertyInfo(originalOrder, x, j));
33-
j++;
34-
});
35-
36-
foreach (var item in _proxyPropertiesInfos)
37-
{
38-
Fields.AddField(item.OriginalName, item.CSharpType, item.IsPrimaryKey, item.IsAutoIncrement);
39-
}
19+
20+
var ctorParameters = _type.GetConstructors().Single().GetParameters().ToList();
21+
ctorParameters.ForEach(p => _proxyCtorParametersInfos.Add(new ProxyCtorParameterInfo(p)));
22+
23+
var propertiesInfos = this._type.GetProperties().ToList();
24+
propertiesInfos.ForEach(p => _proxyPropertiesInfos.Add(new ProxyPropertyInfo(p)));
25+
26+
Fields = new Fields(_proxyPropertiesInfos);
27+
28+
//ctorParameters.ForEach(ctorParameter =>Fields.AddField(new Field(ctorParameter)));
29+
//foreach (var item in _proxyPropertiesInfos)
30+
//{
31+
// Fields.AddField(item.OriginalName, item.CSharpType, item.IsPrimaryKey, item.IsAutoIncrement);
32+
//}
33+
34+
35+
//var originalProperties = this._type.GetProperties().ToList();
36+
37+
//int i = 0;
38+
//originalProperties.ForEach(x =>
39+
//{
40+
// _originalPropertiesInfos.Add(new OriginalPropertyInfo(i, x.Name));
41+
// i++;
42+
//});
43+
44+
//int h = 0;
45+
//originalProperties.ForEach(originalProperty =>
46+
//{
47+
// var originalOrder = _originalPropertiesInfos.First(y => y.Name == originalProperty.Name).Order;
48+
// _proxyPropertiesInfos.Add(new ProxyPropertyInfo(originalOrder, originalProperty, h));
49+
// h++;
50+
//});
51+
//foreach (var item in _proxyPropertiesInfos)
52+
//{
53+
// Fields.AddField(item.OriginalName, item.CSharpType, item.IsPrimaryKey, item.IsAutoIncrement);
54+
//}
4055
}
4156

4257
[Obsolete]
@@ -65,15 +80,44 @@ public int GetFieldsCount()
6580
{
6681
return this.Fields.Items.Count;
6782
}
83+
public int GetCtorParametersCount()
84+
{
85+
return this._proxyCtorParametersInfos.Count;
86+
}
6887

6988
public string GetFieldTypeSQLite(int index)
7089
{
71-
return this.Fields.Items[index].TypeSQLite;
90+
return this.Fields.Items[index].SQLiteType;
91+
}
92+
93+
public string GetCtorParameterSQLiteType(int index)
94+
{
95+
return this._proxyCtorParametersInfos[index].SQLiteType;
7296
}
7397

7498
public string GetFieldTypeCSharp(int index)
7599
{
76100
return this.Fields.Items[index].TypeCSharp;
77101
}
102+
103+
public List<ProxyCtorParameterInfo> GetCtorParemetersInfos()
104+
{
105+
return this._proxyCtorParametersInfos;
106+
}
107+
108+
public string GetCtorParemeterCSharpType(int index)
109+
{
110+
return this._proxyCtorParametersInfos[index].CSharpType;
111+
}
112+
113+
public string GetTableName()
114+
{
115+
return _type.Name;
116+
}
117+
118+
public bool FieldExists(string fieldName)
119+
{
120+
return Fields.Items.Any(x => x.NameOnDb.ToLower() == fieldName.ToLower());
121+
}
78122
}
79123
}

src/SQLiteAbstractCrud.Proxy/Queries/Query.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Query(T type)
2020

2121
public abstract string ToRaw();
2222

23-
public PropertyInfo GetPropertyInfo(string property)
23+
public System.Reflection.PropertyInfo GetPropertyInfo(string property)
2424
{
2525
return _type.GetType().GetProperty(property);
2626
}

src/SQLiteAbstractCrud.Proxy/Queries/QueryCreate.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ public QueryCreate() : base(default)
1414

1515
public override string ToRaw()
1616
{
17-
var fieldsQuery = _proxyBase.Fields.Items.Aggregate("", (current, property) => current + $"{property.NameOnDb} {property.TypeSQLite} NOT NULL,");
18-
var fieldPk = _proxyBase.Fields.Items.Where(x => x.IsPrimaryKey).Select(x => x.NameOnDb).ToList();
17+
18+
var fieldsQuery = _proxyBase.GetCtorParemetersInfos().Aggregate("", (current, property) => current + $"{property.Name} {property.SQLiteType} NOT NULL,");
19+
var fieldPk = _proxyBase.Fields.Items.Where(x => x.IsPrimaryKey).Select(x => x.NameOnDb).ToList();//todo: Proxy GetPkName
1920
var hasFieldAutoincrement = _proxyBase.Fields.Items.Any(x => x.IsAutoincrement);
2021

2122
if (fieldPk == null || !fieldPk.Any())
22-
throw new AggregateException("Can't find any primary key");
23+
throw new AggregateException("Cant find any primary key");
2324

2425
if (fieldPk.Count > 1 && hasFieldAutoincrement)
25-
throw new AggregateException("Can't create table with autoincrement field and composite primary key");
26+
throw new AggregateException("Cant create table with autoincrement field and composite primary key");
2627

27-
var queryCreate = $"CREATE TABLE if not exists {this.TableName} ( {fieldsQuery} PRIMARY KEY({GetFieldsCommas(fieldPk)} {(hasFieldAutoincrement ? "AUTOINCREMENT" : "")}))";
28+
var queryCreate = $"CREATE TABLE if not exists {this.TableName} " +
29+
$"( {fieldsQuery} PRIMARY KEY({GetFieldsCommas(fieldPk)} {(hasFieldAutoincrement ? "AUTOINCREMENT" : "")}))";
2830

2931
return queryCreate;
3032
}

src/SQLiteAbstractCrud.Proxy/Queries/QueryGet.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public QueryGet(object id) : base(default)
1717

1818
public override string ToRaw()
1919
{
20-
return $"SELECT {GetFieldsCommas(_proxyBase.GetFieldsNames())} " +
21-
$"FROM {this.TableName} " +
22-
$" WHERE {_proxyBase.Fields.GetPrimaryKeyName()} = {GetQueryWhere(_id)}";
20+
var ctorParametersNames = _proxyBase.GetCtorParemetersInfos().Select(x => x.Name).ToList();
21+
return $" SELECT {GetFieldsCommas(ctorParametersNames)} " +
22+
$" FROM {this.TableName} " +
23+
$" WHERE {_proxyBase.Fields.GetPrimaryKeyName()} = {GetQueryWhere(_id)}";
2324
}
2425

2526
private static string GetFieldsCommas(List<string> fields)

0 commit comments

Comments
 (0)