subsonic 3.0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/lib/LICENSE.txt +41 -0
  2. data/lib/SubSonic.Core.dll +0 -0
  3. data/lib/T4 Templates/ActiveRecord/ActiveRecord.tt +620 -0
  4. data/lib/T4 Templates/ActiveRecord/Context.tt +295 -0
  5. data/lib/T4 Templates/ActiveRecord/SQLServer.ttinclude +347 -0
  6. data/lib/T4 Templates/ActiveRecord/Settings.ttinclude +552 -0
  7. data/lib/T4 Templates/ActiveRecord/StoredProcedures.tt +30 -0
  8. data/lib/T4 Templates/ActiveRecord/Structs.tt +67 -0
  9. data/lib/T4 Templates/LinqTemplates/Classes.tt +117 -0
  10. data/lib/T4 Templates/LinqTemplates/Context.tt +276 -0
  11. data/lib/T4 Templates/LinqTemplates/SQLServer.ttinclude +349 -0
  12. data/lib/T4 Templates/LinqTemplates/Settings.ttinclude +551 -0
  13. data/lib/T4 Templates/LinqTemplates/StoredProcedures.tt +30 -0
  14. data/lib/T4 Templates/LinqTemplates/Structs.tt +67 -0
  15. data/lib/T4 Templates/README.txt +7 -0
  16. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/ActiveRecord.tt +560 -0
  17. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/ActiveRecord.vb +6447 -0
  18. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Context.tt +258 -0
  19. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Context.vb +349 -0
  20. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/SQLServer.ttinclude +332 -0
  21. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Settings.ttinclude +550 -0
  22. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/StoredProcedures.tt +30 -0
  23. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/StoredProcedures.vb +55 -0
  24. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Structs.tt +57 -0
  25. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Structs.vb +1478 -0
  26. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Tests/FetchTests.vb +52 -0
  27. data/lib/T4 Templates/SubSonic.TemplatesVB/App.config +9 -0
  28. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Application.Designer.vb +13 -0
  29. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Application.myapp +10 -0
  30. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/AssemblyInfo.vb +35 -0
  31. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Resources.Designer.vb +63 -0
  32. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Resources.resx +117 -0
  33. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Settings.Designer.vb +73 -0
  34. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Settings.settings +7 -0
  35. data/lib/T4 Templates/SubSonic.TemplatesVB/SubSonic.TemplatesVB.vbproj +171 -0
  36. data/lib/T4 Templates/SubSonic.TemplatesVB/SubSonic.TemplatesVB.vbproj.user +5 -0
  37. data/lib/T4 Templates/TemplateProviders/MySQL.ttinclude +278 -0
  38. data/lib/T4 Templates/TemplateProviders/MySQL.ttinclude.orig +303 -0
  39. data/lib/T4 Templates/TemplateProviders/MySQLTest.tt +27 -0
  40. data/lib/T4 Templates/TemplateProviders/SQLite.ttinclude +194 -0
  41. data/lib/T4 Templates/TemplateProviders/SQLite.ttinclude.orig +236 -0
  42. data/lib/T4 Templates/TemplateProviders/SQLiteTest.tt +27 -0
  43. data/lib/T4 Templates/TemplateProviders/Settings.ttinclude +551 -0
  44. metadata +112 -0
@@ -0,0 +1,30 @@
1
+ <#@ template language="C#v3.5" debug="False" hostspecific="True" #>
2
+ <#@ output extension=".cs" #>
3
+ <#@ include file="SQLServer.ttinclude" #>
4
+ <#
5
+ var sps = GetSPs();
6
+ if(sps.Count>0){
7
+ #>
8
+ using System;
9
+ using SubSonic;
10
+ using SubSonic.Schema;
11
+ using SubSonic.DataProviders;
12
+ using System.Data;
13
+
14
+ namespace <#=Namespace#>{
15
+ public partial class <#=DatabaseName#>DB{
16
+
17
+ <# foreach(var sp in sps){#>
18
+ public StoredProcedure <#=sp.CleanName#>(<#=sp.ArgList#>){
19
+ StoredProcedure sp=new StoredProcedure("<#=sp.Name#>",this.Provider);
20
+ <# foreach(var par in sp.Parameters){#>
21
+ sp.Command.AddParameter("<#=par.Name#>",<#=par.CleanName#>,DbType.<#=par.DbType#>);
22
+ <# }#>
23
+ return sp;
24
+ }
25
+ <# }#>
26
+
27
+ }
28
+
29
+ }
30
+ <# }#>
@@ -0,0 +1,67 @@
1
+ <#@ template language="C#v3.5" debug="False" hostspecific="True" #>
2
+ <#@ output extension=".cs" #>
3
+ <#@ include file="SQLServer.ttinclude" #>
4
+ <#
5
+ var tables = LoadTables();
6
+ #>
7
+ using System;
8
+ using SubSonic.Schema;
9
+ using System.Collections.Generic;
10
+ using SubSonic.DataProviders;
11
+ using System.Data;
12
+
13
+ namespace <#=Namespace#> {
14
+
15
+ <# foreach(var tbl in tables){
16
+ if(!ExcludeTables.Contains(tbl.Name))
17
+ {
18
+ #>
19
+ /// <summary>
20
+ /// Table: <#=tbl.Name#>
21
+ /// Primary Key: <#=tbl.PrimaryKey#>
22
+ /// </summary>
23
+
24
+ public class <#=tbl.CleanName#>Table: DatabaseTable {
25
+
26
+ public <#=tbl.CleanName#>Table(IDataProvider provider):base("<#=tbl.Name#>",provider){
27
+ ClassName = "<#=tbl.ClassName#>";
28
+ SchemaName = "<#=tbl.Schema ?? ""#>";
29
+
30
+ <# foreach(var col in tbl.Columns){#>
31
+
32
+ Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
33
+ {
34
+ IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
35
+ DataType = DbType.<#=col.DbType.ToString()#>,
36
+ IsNullable = <#=col.IsNullable.ToString().ToLower()#>,
37
+ AutoIncrement = <#=col.AutoIncrement.ToString().ToLower()#>,
38
+ IsForeignKey = <#=col.IsForeignKey.ToString().ToLower()#>,
39
+ MaxLength = <#=col.MaxLength#>
40
+ });
41
+ <# }#>
42
+
43
+
44
+ }
45
+
46
+ <# foreach(var col in tbl.Columns){#>
47
+ public IColumn <#=col.CleanName#>{
48
+ get{
49
+ return this.GetColumn("<#=col.Name#>");
50
+ }
51
+ }
52
+
53
+ public static string <#= col.CleanName #>Column{
54
+ get{
55
+ return "<#= col.Name #>";
56
+ }
57
+ }
58
+
59
+ <# }#>
60
+ }
61
+
62
+ <#
63
+ }
64
+
65
+ }
66
+ #>
67
+ }
@@ -0,0 +1,7 @@
1
+ Open up _Settings.tt
2
+ Set the Namespace, ConnectionString, and DatabaseName accordingly
3
+ Save.
4
+ Drag into project in VS 2008.
5
+ You're done.
6
+
7
+ If you have other DB (SQLite or MySQL) use the appropriate file in TemplateProviders directory.
@@ -0,0 +1,560 @@
1
+ <#@ template language="C#v3.5" debug="True" hostspecific="True" #>
2
+ <#@ output extension=".vb" #>
3
+ <#@ include file="SQLServer.ttinclude" #>
4
+ Imports System
5
+ Imports System.Collections.Generic
6
+ Imports System.Linq
7
+ Imports System.Text
8
+ Imports System.Data
9
+ Imports SubSonic.DataProviders
10
+ Imports SubSonic.Extensions
11
+ Imports System.Linq.Expressions
12
+ Imports SubSonic.Schema
13
+ Imports System.Collections
14
+ Imports SubSonic
15
+ Imports SubSonic.Repository
16
+ Imports System.ComponentModel
17
+ Imports System.Data.Common
18
+
19
+ NameSpace <#=Namespace #>
20
+ <#
21
+
22
+ var tables = LoadTables();
23
+
24
+ foreach(Table tbl in tables)
25
+ {
26
+ if(!ExcludeTables.Contains(tbl.Name))
27
+ {
28
+ #>
29
+
30
+
31
+ ''' <summary>
32
+ ''' A class which represents the <#=tbl.Name #> table in the <#=DatabaseName#> Database.
33
+ ''' </summary>
34
+ Public Partial Class <#=tbl.ClassName#>
35
+ Implements IActiveRecord
36
+
37
+ ' Built-in testing
38
+ Shared TestItems As IList(Of <#=tbl.ClassName#>)
39
+ Shared _testRepo As TestRepository(Of <#=tbl.ClassName#>)
40
+ Public Sub SetIsLoaded(isLoaded As Boolean) Implements IActiveRecord.SetIsLoaded
41
+ _isLoaded = isLoaded
42
+ End Sub
43
+ Private Shared Sub SetTestRepo()
44
+ iF _testRepo Is Nothing Then _testRepo = New TestRepository(Of <#=tbl.ClassName#>)(New <#=Namespace#>.<#=DatabaseName#>DB())
45
+ End Sub
46
+ Public Shared Sub ResetTestRepo()
47
+ _testRepo = Nothing
48
+ SetTestRepo()
49
+ End Sub
50
+ Public Shared Sub Setup(testlist As List(Of <#=tbl.ClassName#>))
51
+ SetTestRepo()
52
+ _testRepo._items = testlist
53
+ End Sub
54
+ Public Shared Sub Setup(item As <#=tbl.ClassName#>)
55
+ SetTestRepo()
56
+ _testRepo._items.Add(item)
57
+ End Sub
58
+ Public Shared Sub Setup(testItems As Integer)
59
+ SetTestRepo()
60
+ For i As Integer = 0 To testItems - 1
61
+ Dim item As New <#=tbl.ClassName#>()
62
+ _testRepo._items.Add(item)
63
+ Next i
64
+ End Sub
65
+
66
+ Public TestMode As Boolean = False
67
+
68
+
69
+
70
+ Private _repo As IRepository(Of <#=tbl.ClassName#>)
71
+ Private tbl As ITable
72
+ Private _isNew As Boolean
73
+ Public Function IsNew() As Boolean Implements IActiveRecord.IsNew
74
+ Return _isNew
75
+ End Function
76
+ Public Sub SetIsNew(isNew As Boolean) Implements IActiveRecord.SetIsNew
77
+ _isNew=isNew
78
+ End Sub
79
+ Private _isLoaded As Boolean
80
+ Public Function IsLoaded() As Boolean Implements IActiveRecord.IsLoaded
81
+ Return _isLoaded
82
+ End Function
83
+
84
+ Private _dirtyColumns As List(Of IColumn)
85
+ Public Function IsDirty() As Boolean Implements IActiveRecord.IsDirty
86
+ Return _dirtyColumns.Count > 0
87
+ End Function
88
+
89
+ Public Function GetDirtyColumns() As List(Of IColumn) Implements IActiveRecord.GetDirtyColumns
90
+ Return _dirtyColumns
91
+ End Function
92
+
93
+ Private _db As <#=Namespace#>.<#=DatabaseName#>DB
94
+ Public Sub New(connectionString As String, providerName As String)
95
+ _db = New <#=Namespace#>.<#=DatabaseName#>DB(connectionString, providerName)
96
+ Init()
97
+ End Sub
98
+ Private Sub Init()
99
+ TestMode = Me._db.Provider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase)
100
+ _dirtyColumns = New List(Of IColumn)()
101
+ If TestMode Then
102
+ <#=tbl.ClassName#>.SetTestRepo()
103
+ _repo=_testRepo
104
+ Else
105
+ _repo = New SubSonicRepository(Of <#=tbl.ClassName#>)(_db)
106
+ End If
107
+ tbl=_repo.GetTable()
108
+ _isNew = True
109
+ OnCreated()
110
+
111
+ End Sub
112
+
113
+ Public Sub New()
114
+ _db = New <#=Namespace#>.<#=DatabaseName#>DB()
115
+ Init()
116
+ End Sub
117
+
118
+
119
+ Private Partial Sub OnCreated()
120
+ End Sub
121
+
122
+ Private Partial Sub OnLoaded()
123
+ End Sub
124
+
125
+ Private Partial Sub OnSaved()
126
+ End Sub
127
+
128
+ Private Partial Sub OnChanged()
129
+ End Sub
130
+
131
+ Public ReadOnly Property Columns As IList(Of IColumn)
132
+ Get
133
+ Return tbl.Columns
134
+ End Get
135
+ End Property
136
+
137
+ Public Sub New(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean)))
138
+ MyBase.New()
139
+ _isLoaded=_repo.Load(Me,expression)
140
+ If _isLoaded Then OnLoaded()
141
+ End Sub
142
+
143
+
144
+
145
+ Friend Shared Function GetRepo(connectionString As String, providerName As String) As IRepository(Of <#=tbl.ClassName#>)
146
+ Dim db As <#=Namespace#>.<#=DatabaseName#>DB
147
+ If String.IsNullOrEmpty(connectionString)
148
+ db = New <#=Namespace#>.<#=DatabaseName#>DB()
149
+ Else
150
+ db = New <#=Namespace#>.<#=DatabaseName#>DB(connectionString, providerName)
151
+ End If
152
+ Dim _repo As IRepository(Of <#=tbl.ClassName#>)
153
+
154
+ If db.TestMode Then
155
+ <#=tbl.ClassName#>.SetTestRepo()
156
+ _repo = _testRepo
157
+ Else
158
+ _repo = New SubSonicRepository(Of <#=tbl.ClassName#>)(db)
159
+ End If
160
+ Return _repo
161
+ End Function
162
+
163
+ Friend Shared Function GetRepo() As IRepository(Of <#=tbl.ClassName#>)
164
+ Return GetRepo(String.Empty,String.Empty)
165
+ End Function
166
+
167
+ Public Shared Function SingleOrDefault(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean))) As <#=tbl.ClassName#>
168
+
169
+ Dim repo = GetRepo()
170
+ Dim results = repo.Find(expression)
171
+ Dim singleItem As <#=tbl.ClassName#> = Nothing
172
+ If results.Count() > 0 Then
173
+ singleItem = results.ToList()(0)
174
+ singleItem.OnLoaded()
175
+ singleItem.SetIsLoaded(true)
176
+ End If
177
+
178
+ Return singleItem
179
+ End Function
180
+
181
+ Public Shared Function SingleOrDefault(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean)), _
182
+ connectionString As String, _
183
+ providerName As String) As <#=tbl.ClassName#>
184
+ Dim repo = GetRepo(connectionString,providerName)
185
+ Dim results = repo.Find(expression)
186
+ Dim singleItem As <#=tbl.ClassName#> = Nothing
187
+ If results.Count() > 0 Then
188
+ singleItem = results.ToList()(0)
189
+ singleItem.OnLoaded()
190
+ singleItem.SetIsLoaded(true)
191
+ End If
192
+
193
+ Return singleItem
194
+ End Function
195
+
196
+
197
+ Public Shared Function Exists(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean)), connectionString As String, providerName As String) As Boolean
198
+ Return All(connectionString,providerName).Any(expression)
199
+ End Function
200
+
201
+ Public Shared Function Exists(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean))) As Boolean
202
+ Return All().Any(expression)
203
+ End Function
204
+
205
+ Public Shared Function Find(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean))) As IList(Of <#=tbl.ClassName#>)
206
+ Dim repo = GetRepo()
207
+ Return repo.Find(expression).ToList()
208
+ End Function
209
+
210
+ Public Shared Function Find(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean)), connectionString As String, providerName As String) As IList(Of <#=tbl.ClassName#>)
211
+ Dim repo = GetRepo(connectionString,providerName)
212
+ Return repo.Find(expression).ToList()
213
+ End Function
214
+ Public Shared Function All(connectionString As String, providerName As String) As IQueryable(Of <#=tbl.ClassName#>)
215
+ Return GetRepo(connectionString,providerName).GetAll()
216
+ End Function
217
+ Public Shared Function All() As IQueryable(Of <#=tbl.ClassName#>)
218
+ Return GetRepo().GetAll()
219
+ End Function
220
+
221
+ Public Shared Function GetPaged(sortBy As String, pageIndex As Integer, pageSize As Integer, connectionString As String, providerName As String) As PagedList(Of <#=tbl.ClassName#>)
222
+ Return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize)
223
+ End Function
224
+
225
+ Public Shared Function GetPaged(sortBy As String, pageIndex As Integer, pageSize As Integer) As PagedList(Of <#=tbl.ClassName#>)
226
+ Return GetRepo().GetPaged(sortBy, pageIndex, pageSize)
227
+ End Function
228
+
229
+ Public Shared Function GetPaged(pageIndex As Integer, pageSize As Integer, connectionString As String, providerName As String) As PagedList(Of <#=tbl.ClassName#>)
230
+ Return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize)
231
+ End Function
232
+
233
+ Public Shared Function GetPaged(pageIndex As Integer, pageSize As Integer) As PagedList(Of <#=tbl.ClassName#>)
234
+ Return GetRepo().GetPaged(pageIndex, pageSize)
235
+ End Function
236
+
237
+ Public Function KeyName() As String Implements IActiveRecord.KeyName
238
+ Return "<#=tbl.PK.CleanName #>"
239
+ End Function
240
+
241
+ Public Function KeyValue() As Object Implements IActiveRecord.KeyValue
242
+ Return Me.<#=tbl.PK.CleanName#>
243
+ End Function
244
+
245
+ Public Sub SetKeyValue(value As Object) Implements IActiveRecord.SetKeyValue
246
+ If value IsNot Nothing AndAlso value IsNot DBNull.Value Then
247
+ Dim settable = value.ChangeTypeTo(Of <#=tbl.PK.SysType#>)()
248
+ Me.GetType.GetProperty(Me.KeyName()).SetValue(Me, settable, Nothing)
249
+ End If
250
+ End Sub
251
+
252
+ Public Overrides Function ToString() As String
253
+ Return Me.<#=tbl.Descriptor.CleanName #>.ToString()
254
+ End Function
255
+
256
+ Public Overrides Function Equals(obj As Object) As Boolean
257
+ If obj.GetType() Is GetType(<#=tbl.ClassName#>) Then
258
+ Dim compareItem As <#=tbl.ClassName#> = obj
259
+ Return compareItem.KeyValue() = Me.KeyValue()
260
+ Else
261
+ Return MyBase.Equals(obj)
262
+ End If
263
+ End Function
264
+
265
+ Public Function DescriptorValue() As String Implements IActiveRecord.DescriptorValue
266
+ Return Me.<#=tbl.Descriptor.CleanName #>.ToString()
267
+ End Function
268
+
269
+ Public Function DescriptorColumn() As String Implements IActiveRecord.DescriptorColumn
270
+ Return "<#=tbl.Descriptor.CleanName #>"
271
+ End Function
272
+ Public Shared Function GetKeyColumn() As String
273
+ Return "<#=tbl.PK.CleanName #>"
274
+ End Function
275
+ Public Shared Function GetDescriptorColumn() As String
276
+ Return "<#=tbl.Descriptor.CleanName #>"
277
+ End Function
278
+
279
+ #Region " Foreign Keys "
280
+ <#
281
+ List<string> fkCreated = new List<string>();
282
+ foreach(FKTable fk in tbl.FKTables)
283
+ {
284
+
285
+ if(!ExcludeTables.Contains(fk.OtherTable)){
286
+ string propName=fk.OtherQueryable;
287
+ if(fkCreated.Contains(propName))
288
+ {
289
+ propName=fk.OtherQueryable+fkCreated.Count.ToString();
290
+ }
291
+
292
+ fkCreated.Add(fk.OtherQueryable);
293
+
294
+
295
+ #>
296
+ Public ReadOnly Property [<#=propName #>] As IQueryable(Of <#=fk.OtherClass #>)
297
+ Get
298
+ Dim repo = <#=Namespace #>.<#=fk.OtherClass#>.GetRepo()
299
+ Return From items In repo.GetAll() _
300
+ Where items.<#=fk.OtherColumn#> = _<#=fk.ThisColumn#> _
301
+ Select items
302
+ End Get
303
+ End Property
304
+
305
+ <#
306
+ }
307
+ }
308
+
309
+ #>
310
+ #End Region
311
+
312
+ <#
313
+ foreach(Column col in tbl.Columns)
314
+ {
315
+
316
+ if (tbl.ClassName == col.CleanName)
317
+ {
318
+ col.CleanName += ColumnSuffix;
319
+ }
320
+ #>
321
+ Private _<#=col.CleanName #> As <#=col.SysType #><#=CheckNullable(col)#>
322
+ Public Property [<#=col.CleanName #>] As <#=col.SysType #><#=CheckNullable(col)#>
323
+ Get
324
+ Return _<#=col.CleanName #>
325
+ End Get
326
+ Set(value As <#=col.SysType #><#=CheckNullable(col)#>)
327
+ _<#=col.CleanName #> = value
328
+ Dim col = tbl.Columns.SingleOrDefault(Function(x) x.Name = "<#=col.Name #>")
329
+ If col IsNot Nothing Then
330
+ If Not _dirtyColumns.Any(Function(x) x.Name = col.Name) AndAlso _isLoaded Then
331
+ _dirtyColumns.Add(col)
332
+ End If
333
+ End If
334
+ OnChanged()
335
+ End Set
336
+ End Property
337
+
338
+ <#
339
+ }
340
+ #>
341
+
342
+
343
+ Public Function GetUpdateCommand() As DbCommand Implements IActiveRecord.GetUpdateCommand
344
+ <#if(tbl.Columns.Any(x=>x.Name.ToLower()=="modifiedon")){#>
345
+ If Not _dirtyColumns.Any(Function(x) x.Name.ToLower() = "modifiedon") Then
346
+ Me.<#=tbl.Columns.Single(x=>x.Name.ToLower()=="modifiedon").CleanName#> = DateTime.Now
347
+ End If
348
+ <#}#>
349
+ If TestMode Then
350
+ Return _db.Provider.CreateCommand()
351
+ Else
352
+ Return Me.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand()
353
+ End If
354
+ End Function
355
+ Public Function GetInsertCommand() As DbCommand Implements IActiveRecord.GetInsertCommand
356
+ If TestMode Then
357
+ Return _db.Provider.CreateCommand()
358
+ Else
359
+ Return Me.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand()
360
+ End If
361
+ End Function
362
+
363
+ Public Function GetDeleteCommand() As DbCommand Implements IActiveRecord.GetDeleteCommand
364
+ If TestMode Then
365
+ Return _db.Provider.CreateCommand()
366
+ Else
367
+ Return Me.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand()
368
+ End If
369
+ End Function
370
+
371
+
372
+ Public Sub Update() Implements IActiveRecord.Update
373
+ Update(_db.Provider)
374
+ End Sub
375
+
376
+ Public Sub Update(provider As IDataProvider) Implements IActiveRecord.Update
377
+
378
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
379
+ Me.ModifiedBy = Environment.UserName
380
+ <#}#>
381
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedOn")){#>
382
+ Me.ModifiedOn = DateTime.Now
383
+ <#}#>
384
+ If Me._dirtyColumns.Count > 0 Then _repo.Update(Me,provider)
385
+
386
+ OnSaved()
387
+ End Sub
388
+
389
+ Public Sub Add() Implements IActiveRecord.Add
390
+ Add(_db.Provider)
391
+ End Sub
392
+
393
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
394
+ Public Sub Update(username As String)
395
+
396
+ Me.ModifiedBy = username
397
+ Update()
398
+
399
+ End Sub
400
+ Public Sub Update(username As String, provider As IDataProvider)
401
+
402
+ Me.ModifiedBy = username
403
+ Update(provider)
404
+ End Sub
405
+ <#}#>
406
+
407
+
408
+ Public Sub Add(provider As IDataProvider) Implements IActiveRecord.Add
409
+
410
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedOn")){#>
411
+ Me.CreatedOn = DateTime.Now
412
+ <#}#>
413
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#>
414
+ Me.CreatedBy = Environment.UserName
415
+ <#}#>
416
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedOn")){#>
417
+ Me.ModifiedOn = DateTime.Now
418
+ <#}#>
419
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
420
+ Me.ModifiedBy = Environment.UserName
421
+ <#}#>
422
+ Me.SetKeyValue(_repo.Add(Me,provider))
423
+ OnSaved()
424
+ End Sub
425
+
426
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#>
427
+ Public Sub Add(username As String)
428
+
429
+ Me.CreatedBy = username
430
+ Add()
431
+
432
+ End Sub
433
+ Public Sub Add(username As String, provider As IDataProvider)
434
+
435
+ Me.CreatedBy = username
436
+ Add(provider)
437
+ End Sub
438
+ <#}#>
439
+
440
+
441
+ Public Sub Save() Implements IActiveRecord.Save
442
+ Save(_db.Provider)
443
+ End Sub
444
+ Public Sub Save(provider As IDataProvider) Implements IActiveRecord.Save
445
+ If _isNew Then
446
+ Add(provider)
447
+ Else
448
+ Update(provider)
449
+ End If
450
+ End Sub
451
+
452
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy" || x.Name=="ModifiedBy")){#>
453
+ Public Sub Save(username As String, provider As IDataProvider)
454
+ If _isNew Then
455
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#>
456
+ Add(username,provider)
457
+ <#}else{#>
458
+ Add(provider)
459
+ <#}#>
460
+ Else
461
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
462
+ Update(username,provider)
463
+ <#}else{#>
464
+ Update(provider)
465
+ <#}#>
466
+
467
+ End If
468
+
469
+ End Sub
470
+ <#}#>
471
+
472
+
473
+ Public Sub Delete(provider As IDataProvider)
474
+ <#if(tbl.HasLogicalDelete()){#>
475
+
476
+ Me.<#=tbl.DeleteColumn.CleanName#> = True
477
+ _repo.Update(Me,provider)
478
+
479
+ <#}else{#>
480
+
481
+ _repo.Delete(KeyValue())
482
+
483
+ <#}#>
484
+ End Sub
485
+
486
+
487
+ Public Sub Delete() Implements IActiveRecord.Delete
488
+ Delete(_db.Provider)
489
+ End Sub
490
+
491
+
492
+ Public Shared Sub Delete(expression As Expression(Of Func(Of <#=tbl.ClassName#>, Boolean)))
493
+ Dim repo = GetRepo()
494
+
495
+ <#if(tbl.HasLogicalDelete()){#>
496
+
497
+ Dim items As List(Of <#=tbl.ClassName#>) = repo.GetAll().Where(expression).ToList()
498
+ items.ForEach(Function(x) x.<#=tbl.DeleteColumn.CleanName#> = True)
499
+ repo.Update(items)
500
+
501
+ <#}else{#>
502
+
503
+ repo.DeleteMany(expression)
504
+
505
+ <#}#>
506
+ End Sub
507
+
508
+ <#if(tbl.HasLogicalDelete()){#>
509
+
510
+ Public Shared Sub Destroy(expression As Func(Of <#=tbl.ClassName#>, Boolean))
511
+ Dim repo = GetRepo()
512
+ repo.Delete(expression)
513
+ End Sub
514
+
515
+ Public Shared Sub Destroy(key As Object)
516
+ Dim repo = GetRepo()
517
+ repo.Delete(key)
518
+ End Sub
519
+
520
+ Public Shared Sub Destroy(key As Object, provider As IDataProvider)
521
+
522
+ Dim repo = GetRepo()
523
+ repo.Delete(key,provider)
524
+
525
+ End Sub
526
+
527
+ Public Sub Destroy()
528
+ _repo.Delete(KeyValue())
529
+ End Sub
530
+ Public Sub Destroy(provider As IDataProvider)
531
+ _repo.Delete(KeyValue(), provider)
532
+ End Sub
533
+ <#}#>
534
+
535
+
536
+ Public Sub Load(rdr As IDataReader) Implements IActiveRecord.Load
537
+ Load(rdr, true)
538
+ End Sub
539
+ Public Sub Load(rdr As IDataReader, closeReader As Boolean) Implements IActiveRecord.Load
540
+ If rdr.Read() Then
541
+ Try
542
+ rdr.Load(Me)
543
+ _isNew = False
544
+ _isLoaded = True
545
+ Catch
546
+ _isLoaded = False
547
+ Throw
548
+ End Try
549
+ Else
550
+ _isLoaded = False
551
+ End If
552
+
553
+ If closeReader Then rdr.Dispose()
554
+
555
+ End Sub
556
+ End Class
557
+ <# }
558
+ }
559
+ #>
560
+ End NameSpace