<% ' Auto-generated Repository for table [Households] ' Generator: GenerateRepo.vbs v1.0 ' ' Dependencies: ' - core/lib.DAL.asp (DAL singleton for database access) ' - core/lib.AutoMapper.asp (Automapper for object mapping) ' - core/lib.Collections.asp (LinkedList_Class) ' - core/lib.helpers.asp (KVUnzip, BuildOrderBy, QI, Destroy) Class HouseholdsRepository_Class Public Function FindByID(id) Dim sql : sql = "Select [Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [Id], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId] FROM [Households] WHERE [Id] = ?" Dim rs : Set rs = DAL.Query(sql, Array(id)) If rs.EOF Then Err.Raise 1, "HouseholdsRepository_Class", RecordNotFoundException("Id", id) Else Set FindByID = Automapper.AutoMap(rs, "POBO_Households") End If Destroy rs End Function Public Function GetAll(orderBy) Set GetAll = Find(Empty, orderBy) End Function Public Function Find(where_kvarray, order_string_or_array) Dim sql : sql = "Select [Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [Id], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId] FROM [Households]" Dim where_keys, where_values, i If Not IsEmpty(where_kvarray) Then KVUnzip where_kvarray, where_keys, where_values If Not IsEmpty(where_keys) Then sql = sql & " WHERE " For i = 0 To UBound(where_keys) If i > 0 Then sql = sql & " AND " sql = sql & " " & QI(where_keys(i)) & " = ?" Next End If End If sql = sql & BuildOrderBy(order_string_or_array, "[Id]") Dim rs : Set rs = DAL.Query(sql, where_values) Dim list : Set list = new LinkedList_Class Do Until rs.EOF list.Push Automapper.AutoMap(rs, "POBO_Households") rs.MoveNext Loop Set Find = list Destroy rs End Function Public Function FindPaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count) Dim sql : sql = "Select [Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [Id], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId] FROM [Households]" Dim where_keys, where_values, i If Not IsEmpty(where_kvarray) Then KVUnzip where_kvarray, where_keys, where_values If Not IsEmpty(where_keys) Then sql = sql & " WHERE " For i = 0 To UBound(where_keys) If i > 0 Then sql = sql & " AND " sql = sql & " " & QI(where_keys(i)) & " = ?" Next End If End If sql = sql & BuildOrderBy(order_string_or_array, "[Id]") Dim rs : Set rs = DAL.PagedQuery(sql, where_values, per_page, page_num) If Not rs.EOF Then rs.PageSize = per_page rs.AbsolutePage = page_num page_count = rs.PageCount record_count = rs.RecordCount End If Set FindPaged = PagedList(rs, per_page) Destroy rs End Function Public Function SearchTablePaged(columns_array, search_value, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count) Dim sql : sql = "Select [Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [Id], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId] FROM [Households]" Dim i, params() If IsArray(columns_array) And UBound(columns_array) >= 0 Then sql = sql & " WHERE " ReDim params(UBound(columns_array)) For i = 0 To UBound(columns_array) If i > 0 Then sql = sql & " OR " sql = sql & " " & QI(columns_array(i)) & " LIKE ?" params(i) = "%" & search_value & "%" Next End If sql = sql & BuildOrderBy(order_string_or_array, "[Id]") Dim rs : Set rs = DAL.PagedQuery(sql, params, per_page, page_num) If Not rs.EOF Then rs.PageSize = per_page rs.AbsolutePage = page_num page_count = rs.PageCount record_count = rs.RecordCount End If Set SearchTablePaged = PagedList(rs, per_page) Destroy rs End Function Public Function SearchTablePagedByDoNotCall(columns_array, search_value, doNotCallValue, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count) Dim sql : sql = "Select [Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [Id], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId] FROM [Households] WHERE [DoNotCall] = ?" Dim i, params() If IsArray(columns_array) And UBound(columns_array) >= 0 Then ReDim params(UBound(columns_array) + 1) params(0) = doNotCallValue sql = sql & " AND (" For i = 0 To UBound(columns_array) If i > 0 Then sql = sql & " OR " sql = sql & " " & QI(columns_array(i)) & " LIKE ?" params(i + 1) = "%" & search_value & "%" Next sql = sql & ")" Else ReDim params(0) params(0) = doNotCallValue End If sql = sql & BuildOrderBy(order_string_or_array, "[Id]") Dim rs : Set rs = DAL.PagedQuery(sql, params, per_page, page_num) If Not rs.EOF Then rs.PageSize = per_page rs.AbsolutePage = page_num page_count = rs.PageCount record_count = rs.RecordCount End If Set SearchTablePagedByDoNotCall = PagedList(rs, per_page) Destroy rs End Function Public Function FindPagedByTerritoryAndDoNotCall(territoryId, doNotCallValue, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count) Dim sql : sql = "Select [Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [Id], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId] FROM [Households] WHERE [TerritoryId] = ? AND [DoNotCall] = ?" Dim rs : Set rs = DAL.PagedQuery(sql & BuildOrderBy(order_string_or_array, "[Id]"), Array(territoryId, doNotCallValue), per_page, page_num) If Not rs.EOF Then rs.PageSize = per_page rs.AbsolutePage = page_num page_count = rs.PageCount record_count = rs.RecordCount End If Set FindPagedByTerritoryAndDoNotCall = PagedList(rs, per_page) Destroy rs End Function Private Function PagedList(rs, per_page) Dim list : Set list = new LinkedList_Class Dim x : x = 0 Do While (per_page <= 0 Or x < per_page) And Not rs.EOF list.Push Automapper.AutoMap(rs, "POBO_Households") x = x + 1 rs.MoveNext Loop Set PagedList = list End Function Public Function GetCountsByTerritory() Dim sql : sql = "SELECT [TerritoryId], COUNT(*) AS [HouseholdCount] FROM [Households] GROUP BY [TerritoryId]" Dim rs : Set rs = DAL.Query(sql, Empty) Dim dict : Set dict = Server.CreateObject("Scripting.Dictionary") Do Until rs.EOF If Not IsNull(rs("TerritoryId")) Then dict(CLng(rs("TerritoryId"))) = CLng(rs("HouseholdCount")) End If rs.MoveNext Loop Set GetCountsByTerritory = dict Destroy rs End Function Public Function GetDistinctStreetsByTerritory(territoryId) Dim sql : sql = "SELECT DISTINCT [StreetName] FROM [Households] WHERE [TerritoryId] = ? AND [StreetName] IS NOT NULL AND [StreetName] <> '' ORDER BY [StreetName]" Dim rs : Set rs = DAL.Query(sql, Array(territoryId)) Dim list : Set list = new LinkedList_Class Do Until rs.EOF list.Push rs("StreetName") & "" rs.MoveNext Loop Set GetDistinctStreetsByTerritory = list Destroy rs End Function Public Sub AddNew(ByRef model) Dim sql : sql = "INSERT INTO [Households] ([Address], [DoNotCall], [DoNotCallDate], [DoNotCallNotes], [DoNotCallPrivateNotes], [IsBusiness], [Latitude], [Longitude], [StreetName], [StreetNumber], [TerritoryId]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" DAL.[Execute] sql, Array(model.Address, model.DoNotCall, model.DoNotCallDate, model.DoNotCallNotes, model.DoNotCallPrivateNotes, model.IsBusiness, model.Latitude, model.Longitude, model.StreetName, model.StreetNumber, model.TerritoryId) ' Retrieve the newly inserted ID On Error Resume Next Dim rsId : Set rsId = DAL.Query("SELECT @@IDENTITY AS NewID", Empty) If Err.Number <> 0 Then ' Fallback for Access databases Err.Clear Set rsId = DAL.Query("SELECT TOP 1 [Id] FROM [Households] ORDER BY [Id] DESC", Empty) End If On Error GoTo 0 If Not rsId.EOF Then If Not IsNull(rsId(0)) Then model.Id = rsId(0) End If Destroy rsId End Sub Public Sub Update(model) Dim sql : sql = "UPDATE [Households] SET [Address] = ?, [DoNotCall] = ?, [DoNotCallDate] = ?, [DoNotCallNotes] = ?, [DoNotCallPrivateNotes] = ?, [IsBusiness] = ?, [Latitude] = ?, [Longitude] = ?, [StreetName] = ?, [StreetNumber] = ?, [TerritoryId] = ? WHERE [Id] = ?" DAL.[Execute] sql, Array(model.Address, model.DoNotCall, model.DoNotCallDate, model.DoNotCallNotes, model.DoNotCallPrivateNotes, model.IsBusiness, model.Latitude, model.Longitude, model.StreetName, model.StreetNumber, model.TerritoryId, model.Id) End Sub Public Sub Delete(id) Dim sql : sql = "DELETE FROM [Households] WHERE [Id] = ?" DAL.[Execute] sql, Array(id) End Sub Private Function RecordNotFoundException(ByVal field_name, ByVal field_val) RecordNotFoundException = "Households record was not found with " & field_name & " = '" & field_val & "'." End Function Private Function QI(name) QI = "[" & Replace(CStr(name), "]", "]]") & "]" End Function Private Function BuildOrderBy(orderArg, defaultCol) Dim s : s = "" If IsEmpty(orderArg) Or IsNull(orderArg) Or orderArg = "" Then s = " ORDER BY " & defaultCol & " ASC" ElseIf IsArray(orderArg) Then Dim i : s = " ORDER BY " For i = 0 To UBound(orderArg) If i > 0 Then s = s & ", " s = s & QI(orderArg(i)) Next Else s = " ORDER BY " & QI(orderArg) End If BuildOrderBy = s End Function End Class Dim HouseholdsRepository__Singleton Function HouseholdsRepository() If IsEmpty(HouseholdsRepository__Singleton) Then Set HouseholdsRepository__Singleton = new HouseholdsRepository_Class End If Set HouseholdsRepository = HouseholdsRepository__Singleton End Function %>