@@ -15,69 +15,119 @@ Install this package only with specific database package like:
15
15
Imports Databasic
16
16
17
17
' create active record class extending Databasic.ActiveRecord.Entity:
18
- Public Class Dealer
19
- Inherits ActiveRecord.Entity
18
+ <Connection( "ConfigConnectionName" )>
19
+ Public Class Person
20
20
Public Id As Int32?
21
21
Public Property Firstname As String
22
22
Public Property Secondname As String
23
23
End Class
24
24
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"
28
28
).FetchOne( New With {
29
29
.id = 5
30
- }).ToInstance( Of Dealer )()
30
+ }).ToInstance( Of Person )()
31
31
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"
35
35
).FetchAll( New With {
36
36
.id = 5
37
- }).ToList( Of Dealer )()
37
+ }).ToList( Of Person )()
38
38
39
- ' load all dealers with id higher than 5 into dictionary
39
+ ' load all persons with id higher than 5 into dictionary
40
40
' 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"
43
43
).FetchAll( New With {
44
44
.id = 5
45
- }).ToDictionary( Of Int32, Dealer )("Id" )
45
+ }).ToDictionary( Of Int32, Person )("Id" )
46
46
```
47
47
48
48
``` cs
49
49
using Databasic ;
50
50
51
51
// create active record class extending Databasic.ActiveRecord.Entity:
52
- public class Dealer : ActiveRecord .Entity {
52
+ [Connection (" ConfigConnectionName" )]
53
+ public class Person {
53
54
public int ? Id ;
54
55
public string Firstname { get ; set ; }
55
56
public string Secondname { get ; set ; }
56
57
}
57
58
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"
61
62
).FetchOne (new {
62
63
id = 5
63
- }).ToInstance <Dealer >()
64
+ }).ToInstance <Person >()
64
65
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"
68
69
).FetchAll (new {
69
70
id = 5
70
- }).ToList <Dealer >()
71
+ }).ToList <Person >()
71
72
72
- // load all dealers with id higher than 5 into dictionary
73
+ // load all persons with id higher than 5 into dictionary
73
74
// 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"
76
77
).FetchAll (new {
77
78
id = 5
78
- }).ToDictionary <Int32 , Dealer >(" Id" )
79
+ }).ToDictionary <Int32 , Person >(" Id" )
79
80
```
80
81
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
+ ```
81
131
82
132
## Features
83
133
- choosing connection by config index or name as another param of Databasic.Statement.Prepare()
0 commit comments