1
- Imports System.Data.Common
2
- Imports System.Reflection
1
+ Imports System.Reflection
3
2
Imports System.Runtime.CompilerServices
4
- Imports Databasic.Connections
3
+ Imports System.Threading
5
4
6
5
Namespace ActiveRecord
7
- Partial Public Class Resource
8
-
9
- ''' <summary>
10
- ''' Resource class primary database table unique column name, usually "Id".
11
- ''' </summary>
12
- <CompilerGenerated>
13
- Protected uniqueColumnName As String = Database.DEFAUT_UNIQUE_COLUMN_NAME
14
-
15
-
16
-
17
- <CompilerGenerated>
18
- Private Shared _columns As New Dictionary( Of Int16, Dictionary( Of String , List( Of String )))
19
- <CompilerGenerated>
20
- Private Shared _columnsLocks As New Dictionary( Of Int16, Object )
21
-
22
-
23
-
24
-
25
- Public Shared Function Columns(resourceType As Type, Optional tableIndex As Int16 = 0 ) As List( Of String )
26
- Dim result As List( Of String ) = Nothing
27
- Dim table As String = Resource.Table(resourceType, tableIndex)
28
- Dim instance As Object = Activator.CreateInstance(resourceType)
29
- Dim connectionIndex As Int16 = Resource._getConnectionIndex(instance)
30
- Dim tablesAndColumns As Dictionary( Of String , List( Of String ))
31
- SyncLock Resource._columnsLocks(connectionIndex)
32
- If Not ActiveRecord.Resource._columns.ContainsKey(connectionIndex) Then
33
- ActiveRecord.Resource._columns.Add(connectionIndex, New Dictionary( Of String , List( Of String )))
34
- End If
35
- tablesAndColumns = ActiveRecord.Resource._columns(connectionIndex)
36
- If tablesAndColumns.ContainsKey(table) Then
37
- result = tablesAndColumns(table)
38
- End If
39
- End SyncLock
40
- If Not TypeOf result Is List( Of String ) Then
41
- Dim connection As Connection = Connection.Get(connectionIndex)
42
- Dim resource As Provider.Resource = Activator.CreateInstance(connection.ResourceType)
43
- result = resource.GetTableColumns(connection, table)
44
- If result.Count = 0 Then
45
- Events.RaiseError( New Exception(
46
- $"No columns found for table: '{table}'. Is the table name correct?"
47
- ))
48
- End If
49
- End If
50
- SyncLock Resource._columnsLocks(connectionIndex)
51
- If TypeOf result Is List( Of String ) Then
52
- If Not tablesAndColumns.ContainsKey(table) Then
53
- tablesAndColumns.Add(table, result)
54
- End If
55
- End If
56
- End SyncLock
57
- Return result
58
- End Function
59
- Public Shared Function Columns( Of TResource)(separator As String , Optional tableIndex As Int16 = 0 ) As String
60
- Return String .Join(
61
- separator,
62
- Enumerable.ToArray( Of String )(
63
- Resource.Columns( GetType (TResource), tableIndex)
64
- )
65
- )
66
- End Function
67
- Public Shared Function Columns( Of TResource)( Optional tableIndex As Int16 = 0 , Optional separator As String = "," ) As String
68
- Return String .Join(
69
- separator,
70
- Enumerable.ToArray( Of String )(
71
- Resource.Columns( GetType (TResource), tableIndex)
72
- )
73
- )
74
- End Function
75
-
76
- Public Function Columns(separator As String , Optional tableIndex As Int16 = 0 ) As String
77
- Return String .Join(
78
- separator,
79
- Enumerable.ToArray( Of String )(
80
- Resource.Columns( Me .GetType(), tableIndex)
81
- )
82
- )
83
- End Function
84
-
85
- Public Function Columns( Optional tableIndex As Int16 = 0 , Optional separator As String = "," ) As String
86
- Return String .Join(
87
- separator,
88
- Enumerable.ToArray( Of String )(
89
- Resource.Columns( Me .GetType(), tableIndex)
90
- )
91
- )
92
- End Function
93
-
94
-
95
-
96
-
97
- Public Shared Function ColumnsExcept( Of TResource)(exceptColumns As String (), separator As String , Optional tableIndex As Int16 = 0 ) As String
98
- Dim result As List( Of String ) = Resource.Columns( GetType (TResource), tableIndex)
99
- For Each exceptCol As String In exceptColumns
100
- If result.Contains(exceptCol) Then result.Remove(exceptCol)
101
- Next
102
- Return String .Join(
103
- separator,
104
- Enumerable.ToArray( Of String )(result)
105
- )
106
- End Function
107
- Public Shared Function ColumnsExcept( Of TResource)(exceptColumns As String (), Optional tableIndex As Int16 = 0 , Optional separator As String = "," ) As String
108
- Dim result As List( Of String ) = Resource.Columns( GetType (TResource), tableIndex)
109
- For Each exceptCol As String In exceptColumns
110
- If result.Contains(exceptCol) Then result.Remove(exceptCol)
111
- Next
112
- Return String .Join(
113
- separator,
114
- Enumerable.ToArray( Of String )(result)
115
- )
116
- End Function
117
-
118
- Public Function ColumnsExcept(exceptColumns As String (), separator As String , Optional tableIndex As Int16 = 0 ) As String
119
- Dim result As List( Of String ) = Resource.Columns( Me .GetType(), tableIndex)
120
- For Each exceptCol As String In exceptColumns
121
- If result.Contains(exceptCol) Then result.Remove(exceptCol)
122
- Next
123
- Return String .Join(
124
- separator,
125
- Enumerable.ToArray( Of String )(result)
126
- )
127
- End Function
128
-
129
-
130
-
131
-
132
- ''' <summary>
133
- ''' Get declared identifier table column name by 'resourceType' argument.
134
- ''' </summary>
135
- ''' <param name="resourceType">Class type, inherited from Resource class with declared protected static field 'idColumn' as string.</param>
136
- ''' <returns>Declared database table id column name from resource class.</returns>
137
- Public Shared Function UniqueColumn(resourceType As Type) As String
138
- Dim result As String = ""
139
- Dim instance As Object = Activator.CreateInstance(resourceType)
140
- Dim fieldInfo As FieldInfo = resourceType.GetField( "uniqueColumnName" , BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.NonPublic)
141
- If Not TypeOf fieldInfo Is FieldInfo Then
142
- Throw New Exception( $"Class '{resourceType.FullName}' has no field 'uniqueColumnName'. Please define this field as string." )
143
- End If
144
- result = DirectCast (fieldInfo.GetValue(instance), String )
145
- Return If ( String .IsNullOrEmpty(result), Database.DEFAUT_UNIQUE_COLUMN_NAME, result)
146
- End Function
147
- ''' <summary>
148
- ''' Get declared identifier table column name from generic type 'TResource'.
149
- ''' </summary>
150
- ''' <typeparam name="TResource">Class name, inherited from Resource class with declared protected static field 'idColumn' as string.</typeparam>
151
- ''' <returns>Declared database table id column name from resource class.</returns>
152
- Public Shared Function UniqueColumn( Of TResource)() As String
153
- Return Resource.UniqueColumn( GetType (TResource))
154
- End Function
155
- Public Function UniqueColumn() As String
156
- Return Resource.UniqueColumn( Me .GetType())
157
- End Function
158
-
159
-
160
-
161
- End Class
6
+ Partial Public Class Resource
7
+
8
+
9
+
10
+ <CompilerGenerated>
11
+ Private Shared _columns As New Dictionary( Of Int32, Dictionary( Of String , List( Of String )))
12
+ <CompilerGenerated>
13
+ Private Shared _columnsLocks As New Dictionary( Of Int32, ReaderWriterLockSlim)
14
+
15
+
16
+
17
+
18
+ Friend Shared Sub StaticInit(connectionsConfigLength As Int32)
19
+ For index As Int32 = 0 To connectionsConfigLength - 1
20
+ Databasic.ActiveRecord.Resource._columnsLocks.Add(index, New ReaderWriterLockSlim())
21
+ Next
22
+ End Sub
23
+
24
+
25
+
26
+
27
+
28
+ Public Shared Function Columns(resourceType As Type, Optional tableIndex As Int16 = 0 ) As List( Of String )
29
+ Dim result As List( Of String ) = Nothing
30
+ Dim table As String = Resource.Table(resourceType, tableIndex)
31
+ Dim connectionIndex As Int16 = Tools.GetConnectionIndexByClassAttr(resourceType)
32
+ Dim tablesAndColumns As Dictionary( Of String , List( Of String ))
33
+ Dim loadingColumnsFromDb As Boolean = True
34
+ Resource._columnsLocks(connectionIndex).EnterUpgradeableReadLock()
35
+
36
+ If ActiveRecord.Resource._columns.ContainsKey(connectionIndex) Then
37
+ tablesAndColumns = ActiveRecord.Resource._columns(connectionIndex)
38
+ If tablesAndColumns.ContainsKey(table) Then
39
+ result = tablesAndColumns(table)
40
+ loadingColumnsFromDb = False
41
+ End If
42
+ Resource._columnsLocks(connectionIndex).ExitUpgradeableReadLock()
43
+ Else
44
+ Resource._columnsLocks(connectionIndex).EnterWriteLock()
45
+ Resource._columnsLocks(connectionIndex).ExitUpgradeableReadLock()
46
+ ActiveRecord.Resource._columns.Add(connectionIndex, New Dictionary( Of String , List( Of String )))
47
+ Resource._columnsLocks(connectionIndex).ExitWriteLock()
48
+ End If
49
+ If loadingColumnsFromDb Then
50
+ Dim connection As Connection = Connection.Get(connectionIndex)
51
+ Dim resource As Provider.Resource = Activator.CreateInstance(connection.ResourceType)
52
+ result = resource.GetTableColumns(connection, table)
53
+ If result.Count = 0 Then
54
+ Events.RaiseError( New Exception(
55
+ $"No columns found for table: '{table}'. Is the table name correct?"
56
+ ))
57
+ End If
58
+ If TypeOf result Is List( Of String ) Then
59
+ Databasic.ActiveRecord.Resource._columnsLocks(connectionIndex).EnterWriteLock()
60
+ tablesAndColumns = ActiveRecord.Resource._columns(connectionIndex)
61
+ If Not tablesAndColumns.ContainsKey(table) Then
62
+ tablesAndColumns.Add(table, result)
63
+ End If
64
+ Databasic.ActiveRecord.Resource._columnsLocks(connectionIndex).ExitWriteLock()
65
+ End If
66
+ End If
67
+ Return result
68
+ End Function
69
+ Public Shared Function Columns( Of TResource)(separator As String , Optional tableIndex As Int16 = 0 ) As String
70
+ Return String .Join(
71
+ separator,
72
+ Enumerable.ToArray( Of String )(
73
+ Resource.Columns( GetType (TResource), tableIndex)
74
+ )
75
+ )
76
+ End Function
77
+ Public Shared Function Columns( Of TResource)( Optional tableIndex As Int16 = 0 , Optional separator As String = "," ) As String
78
+ Return String .Join(
79
+ separator,
80
+ Enumerable.ToArray( Of String )(
81
+ Resource.Columns( GetType (TResource), tableIndex)
82
+ )
83
+ )
84
+ End Function
85
+
86
+ Public Shared Function Columns(separator As String , Optional tableIndex As Int16 = 0 ) As String
87
+ Return String .Join(
88
+ separator,
89
+ Enumerable.ToArray( Of String )(
90
+ Resource.Columns(Tools.GetEntryClassType(), tableIndex)
91
+ )
92
+ )
93
+ End Function
94
+
95
+ Public Shared Function Columns( Optional tableIndex As Int16 = 0 , Optional separator As String = "," ) As String
96
+ Return String .Join(
97
+ separator,
98
+ Enumerable.ToArray( Of String )(
99
+ Resource.Columns(Tools.GetEntryClassType(), tableIndex)
100
+ )
101
+ )
102
+ End Function
103
+
104
+
105
+
106
+
107
+ Public Shared Function ColumnsExcept( Of TResource)(exceptColumns As String (), separator As String , Optional tableIndex As Int16 = Database.DEFAUT_CONNECTION_INDEX) As String
108
+ Dim result As List( Of String ) = Resource.Columns( GetType (TResource), tableIndex)
109
+ For Each exceptCol As String In exceptColumns
110
+ If result.Contains(exceptCol) Then result.Remove(exceptCol)
111
+ Next
112
+ Return String .Join(
113
+ separator,
114
+ Enumerable.ToArray( Of String )(result)
115
+ )
116
+ End Function
117
+ Public Shared Function ColumnsExcept( Of TResource)(exceptColumns As String (), Optional tableIndex As Int16 = Database.DEFAUT_CONNECTION_INDEX, Optional separator As String = "," ) As String
118
+ Dim result As List( Of String ) = Resource.Columns( GetType (TResource), tableIndex)
119
+ For Each exceptCol As String In exceptColumns
120
+ If result.Contains(exceptCol) Then result.Remove(exceptCol)
121
+ Next
122
+ Return String .Join(
123
+ separator,
124
+ Enumerable.ToArray( Of String )(result)
125
+ )
126
+ End Function
127
+
128
+ Public Shared Function ColumnsExcept(exceptColumns As String (), separator As String , Optional tableIndex As Int16 = Database.DEFAUT_CONNECTION_INDEX) As String
129
+ Dim result As List( Of String ) = Resource.Columns(Tools.GetEntryClassType(), tableIndex)
130
+ For Each exceptCol As String In exceptColumns
131
+ If result.Contains(exceptCol) Then result.Remove(exceptCol)
132
+ Next
133
+ Return String .Join(
134
+ separator,
135
+ Enumerable.ToArray( Of String )(result)
136
+ )
137
+ End Function
138
+
139
+ Public Shared Function ColumnsExcept(exceptColumns As String (), Optional tableIndex As Int16 = Database.DEFAUT_CONNECTION_INDEX, Optional separator As String = "," ) As String
140
+ Dim result As List( Of String ) = Resource.Columns(Tools.GetEntryClassType(), tableIndex)
141
+ For Each exceptCol As String In exceptColumns
142
+ If result.Contains(exceptCol) Then result.Remove(exceptCol)
143
+ Next
144
+ Return String .Join(
145
+ separator,
146
+ Enumerable.ToArray( Of String )(result)
147
+ )
148
+ End Function
149
+
150
+
151
+
152
+
153
+ ''' <summary>
154
+ ''' Get declared identifier table column name by 'resourceType' argument.
155
+ ''' </summary>
156
+ ''' <param name="resourceType">Class type, inherited from Resource class with declared protected static field 'idColumn' as string.</param>
157
+ ''' <returns>Declared database table id column name from resource class.</returns>
158
+ Public Shared Function UniqueColumn(resourceType As Type) As String
159
+ Dim result As String = ""
160
+ Dim instance As Object = Activator.CreateInstance(resourceType)
161
+ Dim fieldInfo As FieldInfo = resourceType.GetField( "uniqueColumnName" , BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.NonPublic)
162
+ If Not TypeOf fieldInfo Is FieldInfo Then
163
+ Throw New Exception( $"Class '{resourceType.FullName}' has no field 'uniqueColumnName'. Please define this field as string." )
164
+ End If
165
+ result = DirectCast (fieldInfo.GetValue(instance), String )
166
+ Return If ( String .IsNullOrEmpty(result), Database.DEFAUT_UNIQUE_COLUMN_NAME, result)
167
+ End Function
168
+ ''' <summary>
169
+ ''' Get declared identifier table column name from generic type 'TResource'.
170
+ ''' </summary>
171
+ ''' <typeparam name="TResource">Class name, inherited from Resource class with declared protected static field 'idColumn' as string.</typeparam>
172
+ ''' <returns>Declared database table id column name from resource class.</returns>
173
+ Public Shared Function UniqueColumn( Of TResource)() As String
174
+ Return Resource.UniqueColumn( GetType (TResource))
175
+ End Function
176
+ Public Function UniqueColumn() As String
177
+ Return Resource.UniqueColumn( Me .GetType())
178
+ End Function
179
+
180
+
181
+
182
+ End Class
162
183
End Namespace
0 commit comments