Skip to content

Commit a4dcad5

Browse files
committed
more refactoring
1 parent 4dc9269 commit a4dcad5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3223
-2916
lines changed

ActiveRecord/Entity/Columns.vb

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Imports System.Dynamic
2+
3+
Namespace ActiveRecord
4+
Partial Public MustInherit Class Entity
5+
Inherits DynamicObject
6+
7+
Public Shared Function Columns(
8+
tableIndex As Integer,
9+
Optional separator As String = ","
10+
) As String
11+
Return String.Join(separator, ProviderResource.ColumnsArray(
12+
Tools.GetEntryClassType(), tableIndex
13+
))
14+
End Function
15+
16+
Public Shared Function Columns(Of TActiveRecord)(
17+
tableIndex As Integer,
18+
Optional separator As String = ","
19+
) As String
20+
Return String.Join(
21+
separator,
22+
ProviderResource.ColumnsArray(GetType(TActiveRecord),
23+
tableIndex
24+
))
25+
End Function
26+
27+
Public Shared Function Columns(
28+
Optional separator As String = ",",
29+
Optional tableIndex As Integer = 0
30+
) As String
31+
Return String.Join(separator, ProviderResource.ColumnsArray(
32+
Tools.GetEntryClassType(), tableIndex
33+
))
34+
End Function
35+
36+
Public Shared Function Columns(Of TActiveRecord)(
37+
Optional separator As String = ",",
38+
Optional tableIndex As Integer = 0
39+
) As String
40+
Return String.Join(
41+
separator,
42+
ProviderResource.ColumnsArray(GetType(TActiveRecord),
43+
tableIndex
44+
))
45+
End Function
46+
47+
End Class
48+
End Namespace
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Imports System.Dynamic
2+
3+
Namespace ActiveRecord
4+
Partial Public MustInherit Class Entity
5+
Inherits DynamicObject
6+
7+
Public Shared Function ColumnsExcept(
8+
exceptColumns As String(),
9+
Optional tableIndex As Short = 0,
10+
Optional separator As String = ","
11+
) As String
12+
Dim source As List(Of String) = ProviderResource.ColumnsList(
13+
Tools.GetEntryClassType(), tableIndex
14+
)
15+
For Each exceptColumn As String In exceptColumns
16+
If source.Contains(exceptColumn) Then source.Remove(exceptColumn)
17+
Next
18+
Return String.Join(separator, Enumerable.ToArray(Of String)(source))
19+
End Function
20+
21+
Public Shared Function ColumnsExcept(Of TResource)(
22+
exceptColumns As String(),
23+
Optional tableIndex As Short = 0,
24+
Optional separator As String = ","
25+
) As String
26+
Dim source As List(Of String) = ProviderResource.ColumnsList(
27+
GetType(TResource), tableIndex
28+
)
29+
For Each exceptColumn As String In exceptColumns
30+
If source.Contains(exceptColumn) Then source.Remove(exceptColumn)
31+
Next
32+
Return String.Join(separator, Enumerable.ToArray(Of String)(source))
33+
End Function
34+
35+
Public Shared Function ColumnsExcept(
36+
exceptColumns As String(),
37+
separator As String,
38+
Optional tableIndex As Short = 0
39+
) As String
40+
Dim source As List(Of String) = ProviderResource.ColumnsList(
41+
Tools.GetEntryClassType(), tableIndex
42+
)
43+
For Each exceptColumn As String In exceptColumns
44+
If source.Contains(exceptColumn) Then source.Remove(exceptColumn)
45+
Next
46+
Return String.Join(separator, Enumerable.ToArray(Of String)(source))
47+
End Function
48+
49+
Public Shared Function ColumnsExcept(Of TResource)(
50+
exceptColumns As String(),
51+
separator As String,
52+
Optional tableIndex As Short = 0
53+
) As String
54+
Dim source As List(Of String) = ProviderResource.ColumnsList(
55+
GetType(TResource), tableIndex
56+
)
57+
For Each exceptColumn As String In exceptColumns
58+
If source.Contains(exceptColumn) Then source.Remove(exceptColumn)
59+
Next
60+
Return String.Join(separator, Enumerable.ToArray(Of String)(source))
61+
End Function
62+
63+
End Class
64+
End Namespace

ActiveRecord/Entity/DatabaseGetters/GetById.vb

Lines changed: 85 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,94 @@
22
Imports System.Dynamic
33

44
Namespace ActiveRecord
5-
Partial Public MustInherit Class Entity
6-
Inherits DynamicObject
5+
Partial Public MustInherit Class Entity
6+
Inherits DynamicObject
77

8-
''' <summary>
9-
''' Get active record entity instance by autoincrement column, there will be loaded all table columns.
10-
''' </summary>
11-
''' <typeparam name="TValue">Model class type, inherited from ActiveRecord.</typeparam>
12-
''' <param name="connectionIndex">Config connection index to use different database, default by 0 to use first connection in &lt;connectionStrings&gt; list.</param>
13-
''' <returns></returns>
14-
Public Shared Function GetById(Of TValue)(id As Int64, Optional connectionIndex As Int32 = Databasic.Defaults.CONNECTION_INDEX) As TValue
15-
Return Entity.GetById(Of TValue)(
16-
id, Connection.Get(connectionIndex)
17-
)
18-
End Function
8+
''' <summary>
9+
''' Get active record entity instance by autoincrement column, there will be loaded all table columns.
10+
''' </summary>
11+
''' <typeparam name="TValue">Model class type, inherited from ActiveRecord.</typeparam>
12+
''' <param name="connectionIndex">Config connection index to use different database, default by 0 to use first connection in &lt;connectionStrings&gt; list.</param>
13+
''' <returns></returns>
14+
Public Shared Function GetById(Of TValue)(
15+
id As Int64,
16+
Optional connectionIndex As Int32? = Nothing
17+
) As TValue
18+
If Not connectionIndex.HasValue Then
19+
connectionIndex = Tools.GetConnectionIndexByClassAttr(
20+
GetType(TValue), True
21+
)
22+
End If
23+
Return Entity.GetById(Of TValue)(
24+
id, Connection.Get(connectionIndex.Value)
25+
)
26+
End Function
1927

20-
''' <summary>
21-
''' Get active record entity instance by autoincrement column, there will be loaded all table columns.
22-
''' </summary>
23-
''' <typeparam name="TValue">Model class type, inherited from ActiveRecord.</typeparam>
24-
''' <param name="connectionName">Config connection name to use different database, default by Databasic.Defaults.CONNECTION_INDEX to use first connection in &lt;connectionStrings&gt; list.</param>
25-
''' <returns></returns>
26-
Public Shared Function GetById(Of TValue)(id As Int64, connectionName As String) As TValue
27-
Return Entity.GetById(Of TValue)(
28-
id, Connection.Get(If(
29-
String.IsNullOrEmpty(connectionName),
30-
Databasic.Defaults.CONNECTION_INDEX,
31-
connectionName
32-
))
33-
)
34-
End Function
28+
''' <summary>
29+
''' Get active record entity instance by autoincrement column, there will be loaded all table columns.
30+
''' </summary>
31+
''' <typeparam name="TValue">Model class type, inherited from ActiveRecord.</typeparam>
32+
''' <param name="connectionName">Config connection name to use different database, default by Databasic.Defaults.CONNECTION_INDEX to use first connection in &lt;connectionStrings&gt; list.</param>
33+
''' <returns></returns>
34+
Public Shared Function GetById(Of TValue)(
35+
id As Int64,
36+
connectionName As String
37+
) As TValue
38+
Return Entity.GetById(Of TValue)(
39+
id, Connection.Get(If(
40+
String.IsNullOrEmpty(connectionName),
41+
Tools.GetConnectionIndexByClassAttr(
42+
GetType(TValue), True
43+
),
44+
connectionName
45+
))
46+
)
47+
End Function
3548

36-
''' <summary>
37-
''' Get active record entity instance by autoincrement column, there will be loaded all table columns.
38-
''' </summary>
39-
''' <typeparam name="TValue">Model class type, inherited from ActiveRecord.</typeparam>
40-
''' <param name="connection">Connection intance.</param>
41-
''' <exception cref="Exception">TODO</exception>
42-
''' <returns></returns>
43-
Public Shared Function GetById(Of TValue)(id As Int64, connection As Connection) As TValue
44-
Dim instanceType As Type = GetType(TValue)
45-
Dim classMetaDescription As MetaDescription = MetaDescriptor.GetClassDescription(instanceType)
46-
If Not classMetaDescription.AutoIncrementColumn.HasValue Then
47-
Events.RaiseError(New Exception(String.Format(
48-
"Class '{0}' has no whole number member with 'AutoIncrement' attribute.",
49-
classMetaDescription.ClassType
50-
)))
51-
End If
52-
Dim statement As Statement = connection.GetProviderResource().GetById(
53-
id, connection, classMetaDescription
54-
)
55-
Return ActiveRecord.Entity.ToInstance(Of TValue)(
56-
statement.Reader, classMetaDescription.ColumnsByDatabaseNames
57-
)
58-
End Function
49+
''' <summary>
50+
''' Get active record entity instance by autoincrement column, there will be loaded all table columns.
51+
''' </summary>
52+
''' <typeparam name="TValue">Model class type, inherited from ActiveRecord.</typeparam>
53+
''' <param name="connection">Connection intance.</param>
54+
''' <exception cref="Exception">TODO</exception>
55+
''' <returns></returns>
56+
Public Shared Function GetById(Of TValue)(
57+
id As Int64,
58+
connection As Connection
59+
) As TValue
60+
Dim instanceType As Type = GetType(TValue)
61+
Dim classMetaDescription As MetaDescription = MetaDescriptor.GetClassDescription(instanceType)
62+
If Not classMetaDescription.AutoIncrementColumn.HasValue Then
63+
Events.RaiseError(New Exception(
64+
$"Class '{classMetaDescription.ClassType}' has no whole number member with 'AutoIncrement' attribute."
65+
))
66+
End If
67+
Dim statement As Statement = connection.GetProviderResource().GetById(
68+
id, connection, classMetaDescription
69+
)
70+
Return ActiveRecord.Entity.ToInstance(Of TValue)(
71+
statement.Reader, classMetaDescription.ColumnsByDatabaseNames
72+
)
73+
End Function
5974

60-
Public Shared Function GetById(Of TValue)(id As Int64, transaction As Transaction) As TValue
61-
Dim instanceType As Type = GetType(TValue)
62-
Dim classMetaDescription As MetaDescription = MetaDescriptor.GetClassDescription(instanceType)
63-
If Not classMetaDescription.AutoIncrementColumn.HasValue Then
64-
Events.RaiseError(New Exception(String.Format(
65-
"Class '{0}' has no whole number member with 'AutoIncrement' attribute.",
66-
classMetaDescription.ClassType
67-
)))
68-
End If
69-
Dim statement As Statement = transaction.ConnectionWrapper.GetProviderResource().GetById(
70-
id, transaction, classMetaDescription
71-
)
72-
Return ActiveRecord.Entity.ToInstance(Of TValue)(
73-
statement.Reader, classMetaDescription.ColumnsByDatabaseNames
74-
)
75-
End Function
75+
Public Shared Function GetById(Of TValue)(
76+
id As Int64,
77+
transaction As Transaction
78+
) As TValue
79+
Dim instanceType As Type = GetType(TValue)
80+
Dim classMetaDescription As MetaDescription = MetaDescriptor.GetClassDescription(instanceType)
81+
If Not classMetaDescription.AutoIncrementColumn.HasValue Then
82+
Events.RaiseError(New Exception(
83+
$"Class '{classMetaDescription.ClassType}' has no whole number member with 'AutoIncrement' attribute."
84+
))
85+
End If
86+
Dim statement As Statement = transaction.ConnectionWrapper.GetProviderResource().GetById(
87+
id, transaction, classMetaDescription
88+
)
89+
Return ActiveRecord.Entity.ToInstance(Of TValue)(
90+
statement.Reader, classMetaDescription.ColumnsByDatabaseNames
91+
)
92+
End Function
7693

77-
End Class
94+
End Class
7895
End Namespace

0 commit comments

Comments
 (0)