<% ' Auto-generated Repository for table [Posts] ' Generated on 5/2/2026 9:48:11 PM ' 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 PostsRepository_Class Public Function FindByID(id) Dim sql : sql = "Select [Body], [CategoryID], [CreatedDate], [IsPublished], [PostID], [PublishedDate], [Slug], [Summary], [Title], [UpdatedDate] FROM [Posts] WHERE [PostID] = ?" Dim rs : Set rs = DAL.Query(sql, Array(id)) If rs.EOF Then Err.Raise 1, "PostsRepository_Class", RecordNotFoundException("PostID", id) Else Set FindByID = Automapper.AutoMap(rs, "POBO_Posts") End If Destroy rs End Function Public Function GetAll(orderBy) Set GetAll = Find(Empty, orderBy) End Function Public Function FindAllWhere(ByVal where_clause, ByVal order_by, ByVal offset, ByVal limit) Dim sql : sql = "Select [Body], [CategoryID], [CreatedDate], [IsPublished], [PostID], [PublishedDate], [Slug], [Summary], [Title], [UpdatedDate] FROM [Posts]" Dim whereText : whereText = "" If Not IsEmpty(where_clause) Then If Not IsNull(where_clause) Then whereText = Trim(CStr(where_clause)) End If If Len(whereText) > 0 Then sql = sql & " WHERE " & whereText End If sql = sql & BuildOrderBy(order_by, "[PostID]") Dim offsetCount, limitCount If IsNumeric(offset) Then offsetCount = CLng(offset) Else offsetCount = 0 End If If offsetCount < 0 Then offsetCount = 0 If IsNumeric(limit) Then limitCount = CLng(limit) Else limitCount = 0 End If Dim rs : Set rs = DAL.Query(sql, Empty) Dim list : Set list = new LinkedList_Class Dim skipped, added skipped = 0 added = 0 Do Until rs.EOF If skipped < offsetCount Then skipped = skipped + 1 ElseIf limitCount <= 0 Or added < limitCount Then list.Push Automapper.AutoMap(rs, "POBO_Posts") added = added + 1 End If If limitCount > 0 And added >= limitCount Then Exit Do rs.MoveNext Loop Set FindAllWhere = list Destroy rs End Function Public Function Find(where_kvarray, order_string_or_array) Dim sql : sql = "Select [Body], [CategoryID], [CreatedDate], [IsPublished], [PostID], [PublishedDate], [Slug], [Summary], [Title], [UpdatedDate] FROM [Posts]" 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, "[PostID]") 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_Posts") 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 [Body], [CategoryID], [CreatedDate], [IsPublished], [PostID], [PublishedDate], [Slug], [Summary], [Title], [UpdatedDate] FROM [Posts]" 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, "[PostID]") 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 [Body], [CategoryID], [CreatedDate], [IsPublished], [PostID], [PublishedDate], [Slug], [Summary], [Title], [UpdatedDate] FROM [Posts]" 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, "[PostID]") 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 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_Posts") x = x + 1 rs.MoveNext Loop Set PagedList = list End Function Public Sub AddNew(ByRef model) Dim sql : sql = "INSERT INTO [Posts] ([Body], [CategoryID], [CreatedDate], [IsPublished], [PublishedDate], [Slug], [Summary], [Title], [UpdatedDate]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" DAL.Execute sql, Array(model.Body, model.CategoryID, model.CreatedDate, model.IsPublished, model.PublishedDate, model.Slug, model.Summary, model.Title, model.UpdatedDate) ' 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 [PostID] FROM [Posts] ORDER BY [PostID] DESC", Empty) End If On Error GoTo 0 If Not rsId.EOF Then If Not IsNull(rsId(0)) Then model.PostID = rsId(0) End If Destroy rsId End Sub Public Sub Update(model) Dim sql : sql = "UPDATE [Posts] SET [Body] = ?, [CategoryID] = ?, [CreatedDate] = ?, [IsPublished] = ?, [PublishedDate] = ?, [Slug] = ?, [Summary] = ?, [Title] = ?, [UpdatedDate] = ? WHERE [PostID] = ?" DAL.Execute sql, Array(model.Body, model.CategoryID, model.CreatedDate, model.IsPublished, model.PublishedDate, model.Slug, model.Summary, model.Title, model.UpdatedDate, model.PostID) End Sub Public Sub Delete(id) Dim sql : sql = "DELETE FROM [Posts] WHERE [PostID] = ?" DAL.Execute sql, Array(id) End Sub Private Function RecordNotFoundException(ByVal field_name, ByVal field_val) RecordNotFoundException = "Posts record was not found with " & field_name & " = '" & field_val & "'." End Function Private Function QI(name) QI = "[" & Replace(CStr(name), "]", "]]") & "]" End Function Private Function BuildOrderBy(ByVal orderArg, ByVal defaultCol) Dim s : s = "" If 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 ElseIf IsEmpty(orderArg) Then s = " ORDER BY " & defaultCol & " ASC" ElseIf IsNull(orderArg) Then s = " ORDER BY " & defaultCol & " ASC" ElseIf CStr(orderArg) = "" Then s = " ORDER BY " & defaultCol & " ASC" ElseIf IsSafeOrderExpression(orderArg) Then Dim parts : parts = Split(Trim(CStr(orderArg)), " ") s = " ORDER BY " & QI(parts(0)) If UBound(parts) = 1 Then s = s & " " & UCase(parts(1)) Else s = " ORDER BY " & QI(orderArg) End If BuildOrderBy = s End Function Private Function IsSafeOrderExpression(ByVal orderArg) Dim raw : raw = Trim(CStr(orderArg)) Dim parts : parts = Split(raw, " ") IsSafeOrderExpression = False If UBound(parts) < 0 Or UBound(parts) > 1 Then Exit Function If Not IsSafeIdentifier(parts(0)) Then Exit Function If UBound(parts) = 1 Then If UCase(parts(1)) <> "ASC" And UCase(parts(1)) <> "DESC" Then Exit Function End If IsSafeOrderExpression = True End Function Private Function IsSafeIdentifier(ByVal value) Dim i, ch value = CStr(value) If Len(value) = 0 Then IsSafeIdentifier = False Exit Function End If For i = 1 To Len(value) ch = Mid(value, i, 1) If Not ((ch >= "a" And ch <= "z") Or _ (ch >= "A" And ch <= "Z") Or _ (ch >= "0" And ch <= "9") Or _ ch = "_") Then IsSafeIdentifier = False Exit Function End If Next IsSafeIdentifier = True End Function End Class Dim PostsRepository__Singleton Function PostsRepository() If IsEmpty(PostsRepository__Singleton) Then Set PostsRepository__Singleton = new PostsRepository_Class End If Set PostsRepository = PostsRepository__Singleton End Function %>