Skip to content

Commit 92eae8d

Browse files
committed
readme updates, that is now fully possible to use
1 parent ccf772f commit 92eae8d

File tree

1 file changed

+77
-27
lines changed

1 file changed

+77
-27
lines changed

README.md

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,119 @@ Install this package only with specific database package like:
1515
Imports Databasic
1616

1717
' create active record class extending Databasic.ActiveRecord.Entity:
18-
Public Class Dealer
19-
Inherits ActiveRecord.Entity
18+
<Connection("ConfigConnectionName")>
19+
Public Class Person
2020
Public Id As Int32?
2121
Public Property Firstname As String
2222
Public Property Secondname As String
2323
End Class
2424

25-
' load dealer with id 5 and create it's instance:
26-
Dim instance As Dealer = Statement.Prepare(
27-
"SELECT * FROM Dealers WHERE Id = @id"
25+
' load person with id 5 and create it's instance:
26+
Dim instance As Person = Statement.Prepare(
27+
"SELECT * FROM Persons WHERE Id = @id"
2828
).FetchOne(New With {
2929
.id = 5
30-
}).ToInstance(Of Dealer)()
30+
}).ToInstance(Of Person)()
3131

32-
' load all dealers with id higher than 5 into list
33-
Dim list As List(Of Dealer) = Statement.Prepare(
34-
"SELECT * FROM Dealers WHERE Id > @id"
32+
' load all persons with id higher than 5 into list
33+
Dim list As List(Of Person) = Statement.Prepare(
34+
"SELECT * FROM Persons WHERE Id > @id"
3535
).FetchAll(New With {
3636
.id = 5
37-
}).ToList(Of Dealer)()
37+
}).ToList(Of Person)()
3838

39-
' load all dealers with id higher than 5 into dictionary
39+
' load all persons with id higher than 5 into dictionary
4040
' and complete dictionary keys by Id column
41-
Dim dct As Dictionary(Of Int32, Dealer) = Statement.Prepare(
42-
"SELECT * FROM Dealers WHERE Id > @id"
41+
Dim dct As Dictionary(Of Int32, Person) = Statement.Prepare(
42+
"SELECT * FROM Persons WHERE Id > @id"
4343
).FetchAll(New With {
4444
.id = 5
45-
}).ToDictionary(Of Int32, Dealer)("Id")
45+
}).ToDictionary(Of Int32, Person)("Id")
4646
```
4747

4848
```cs
4949
using Databasic;
5050

5151
// create active record class extending Databasic.ActiveRecord.Entity:
52-
public class Dealer: ActiveRecord.Entity {
52+
[Connection("ConfigConnectionName")]
53+
public class Person {
5354
public int? Id;
5455
public string Firstname { get; set; }
5556
public string Secondname { get; set; }
5657
}
5758

58-
// load dealer with id 5 and create it's instance:
59-
Dealer instance = Statement.Prepare(
60-
"SELECT * FROM Dealers WHERE Id = @id"
59+
// load person with id 5 and create it's instance:
60+
Person instance = Statement.Prepare(
61+
"SELECT * FROM Persons WHERE Id = @id"
6162
).FetchOne(new {
6263
id = 5
63-
}).ToInstance<Dealer>()
64+
}).ToInstance<Person>()
6465

65-
// load all dealers with id higher than 5 into list
66-
List<Dealer> list = Statement.Prepare(
67-
"SELECT * FROM Dealers WHERE Id > @id"
66+
// load all persons with id higher than 5 into list
67+
List<Person> list = Statement.Prepare(
68+
"SELECT * FROM Persons WHERE Id > @id"
6869
).FetchAll(new {
6970
id = 5
70-
}).ToList<Dealer>()
71+
}).ToList<Person>()
7172

72-
// load all dealers with id higher than 5 into dictionary
73+
// load all persons with id higher than 5 into dictionary
7374
// and complete dictionary keys by Id column
74-
Dictionary<Int32, Dealer> dct = Statement.Prepare(
75-
"SELECT * FROM Dealers WHERE Id > @id"
75+
Dictionary<Int32, Person> dct = Statement.Prepare(
76+
"SELECT * FROM Persons WHERE Id > @id"
7677
).FetchAll(new {
7778
id = 5
78-
}).ToDictionary<Int32, Dealer>("Id")
79+
}).ToDictionary<Int32, Person>("Id")
7980
```
8081

82+
## Advanced Examples
83+
```cs
84+
using Databasic;
85+
using System;
86+
using System.Collections.Generic;
87+
88+
[Connection("DefaultConnection"),Table("Dealers")]
89+
class Dealer: Person {
90+
public int? Id { get; set; }
91+
[Column("Firstname"), Trim]
92+
public new string FirstName { get; set; }
93+
[Column("Secondname")]
94+
public new string SecondName { get; set; }
95+
public double? TurnOver { get; set; }
96+
97+
public static Dealer GetById (int id) {
98+
return Statement.Prepare(
99+
$"SELECT {Columns()} FROM {Table()} WHERE Id = @idParam"
100+
).FetchOne(new {
101+
idParam = id
102+
}).ToInstance<Dealer>();
103+
}
104+
public static int GetCount () {
105+
return Statement.Prepare(
106+
$"SELECT COUNT(Id) FROM {Table()}"
107+
).FetchOne().ToInstance<Int32>();
108+
}
109+
public static Dictionary<object, Dealer> GetDictionary (Func<Dealer, object> keySelector = null) {
110+
return Statement.Prepare(
111+
$"SELECT {Columns()} FROM {Table()}"
112+
).FetchAll().ToDictionary<object, Dealer>(
113+
keySelector ?? (d => d.Id.Value)
114+
);
115+
}
116+
public static List<Dealer> GetList () {
117+
return Statement.Prepare(
118+
$"SELECT {Columns()} FROM {Table()}"
119+
).FetchAll().ToList<Dealer>();
120+
}
121+
}
122+
...
123+
Dictionary<object, Dealer> dct = Dealer.GetDictionary();
124+
125+
List<Dealer> list = Dealer.GetList();
126+
127+
Dealer dealer1 = Dealer.GetById(3);
128+
129+
int cnt = Dealer.GetCount();
130+
```
81131

82132
## Features
83133
- choosing connection by config index or name as another param of Databasic.Statement.Prepare()

0 commit comments

Comments
 (0)