Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Public Function GetById(ByVal id As Long) As SqlResultRow
  4. 'because, all my tables have an Id primary key.
  5. End Function
  6.  
  7. Public Function GetAll() As SqlResult
  8. End Function
  9.  
  10. Public Function Count() As Long
  11. End Function
  12.  
  13. Public Sub Add(ByVal value As SqlResultRow)
  14. End Sub
  15.  
  16. Public Sub Remove(ByVal id As Long)
  17. End Sub
  18.  
  19. Public Sub Update(ByVal id As Long, ByVal value As SqlResultRow)
  20. End Sub
  21.  
  22. Public Function NewItem(ByVal model As SqlResult, ByVal values As Dictionary) As SqlResultRow
  23. 'creates a new item, out of a Scripting.Dictionary containing.
  24. 'model: contains the field names.
  25. 'values: field names as key, field values for values.
  26. End Function
  27.  
  28. Dim description As String
  29. description = row("description")
  30.  
  31. sql = "SELECT Foo, DateInserted FROM FooBar WHERE DateInserted > ?;"
  32. Set result = cmd.QuickExecute(sql, Now - 30)
  33. For Each row In result
  34. Debug.Print row.ToString ' prints a CSV list of the row's values
  35. Next
  36.  
  37. Option Explicit
  38. Private cmd As New SqlCommand
  39. Implements IRepository
  40.  
  41. Public Function NewItem(ByVal model As SqlResult, ByVal values As Dictionary) As SqlResultRow
  42.  
  43. Dim result As New SqlResultRow
  44.  
  45. Dim items() As String
  46. ReDim items(LBound(values.items) To UBound(values.items))
  47.  
  48. Dim i As Integer
  49. For i = LBound(values.items) To UBound(values.items)
  50. items(i) = values.items(i)
  51. Next
  52.  
  53. Set NewItem = result.Mock(model, items)
  54.  
  55. End Function
  56.  
  57. Public Sub Add(ByVal value As SqlResultRow)
  58.  
  59. Dim sql As String
  60. sql = "INSERT INTO Planning.CustomerGroups (Description, DateInserted) VALUES (?, ?);"
  61.  
  62. cmd.QuickExecuteNonQuery sql, value("description"), Now
  63.  
  64. End Sub
  65.  
  66. Public Function GetAll() As SqlResult
  67.  
  68. Dim sql As String
  69. sql = "SELECT Id, Description FROM Planning.CustomerGroups ORDER BY Id;"
  70.  
  71. Set GetAll = cmd.QuickExecute(sql)
  72.  
  73. End Function
  74.  
  75. Public Function GetById(ByVal id As Long) As SqlResultRow
  76.  
  77. Dim sql As String
  78. sql = "SELECT Id, Description FROM Planning.CustomerGroups WHERE Id = ?;"
  79.  
  80. Set GetById = cmd.QuickSelectFirstRow(sql, id)
  81.  
  82. End Function
  83.  
  84. Public Sub Remove(ByVal id As Long)
  85.  
  86. Dim sql As String
  87. sql = "DELETE FROM Planning.CustomerGroups WHERE Id = ?;"
  88.  
  89. cmd.QuickExecuteNonQuery sql, id
  90.  
  91. End Sub
  92.  
  93. Public Sub Update(ByVal id As Long, ByVal value As SqlResultRow)
  94.  
  95. Dim sql As String
  96. sql = "UPDATE Planning.CustomerGroups SET Description = ?, DateUpdated = ? WHERE Id = ?;"
  97.  
  98. cmd.QuickExecuteNonQuery sql, value("description"), Now, value("id")
  99.  
  100. End Sub
  101.  
  102. Public Function Count() As Long
  103.  
  104. Dim sql As String
  105. sql = "SELECT COUNT(*) FROM Planning.CustomerGroups;"
  106.  
  107. Count = cmd.QuickSelectSingleValue(sql)
  108.  
  109. End Function
  110.  
  111. Private Sub IRepository_Add(ByVal value As SqlResultRow)
  112. Add value
  113. End Sub
  114.  
  115. Private Function IRepository_Count() As Long
  116. IRepository_Count = Count
  117. End Function
  118.  
  119. Private Function IRepository_GetAll() As SqlResult
  120. Set IRepository_GetAll = GetAll
  121. End Function
  122.  
  123. Private Function IRepository_GetById(ByVal id As Long) As SqlResultRow
  124. Set IRepository_GetById = GetById(id)
  125. End Function
  126.  
  127. Private Function IRepository_NewItem(ByVal model As SqlResult, ByVal values As Scripting.IDictionary) As SqlResultRow
  128. Set IRepository_NewItem = NewItem(model, values)
  129. End Function
  130.  
  131. Private Sub IRepository_Remove(ByVal id As Long)
  132. Remove id
  133. End Sub
  134.  
  135. Private Sub IRepository_Update(ByVal id As Long, ByVal value As SqlResultRow)
  136. Update id, value
  137. End Sub
  138.  
  139. Option Explicit
  140. Private FieldNames As List
  141. Private Items As List
  142. Implements IRepository
  143. Implements IStringRepresentable
  144.  
  145. Public Sub SetModel(model As SqlResult)
  146.  
  147. Set FieldNames = model.FieldNames
  148. Set Items = New List
  149.  
  150. Dim row As SqlResultRow
  151. For Each row In model
  152. Items.Add row
  153. Next
  154.  
  155. End Sub
  156.  
  157. Private Function MockSqlResult() As SqlResult
  158.  
  159. Dim result As New SqlResult
  160.  
  161. Dim name As Variant
  162. For Each name In FieldNames
  163. result.AddFieldName name
  164. Next
  165.  
  166. Dim row As SqlResultRow
  167. For Each row In Items
  168. result.AddValue row
  169. Next
  170.  
  171. Set MockSqlResult = result
  172.  
  173. End Function
  174.  
  175. Public Function Create(model As SqlResult) As MockRepository
  176.  
  177. Dim result As New MockRepository
  178. result.SetModel model
  179.  
  180. Set Create = result
  181.  
  182. End Function
  183.  
  184. Public Sub Add(ByVal value As SqlResultRow)
  185.  
  186. Dim newId As Long
  187. newId = Items.Count + 1
  188. If Items.Last("id") <= newId Then
  189. newId = Items.Last("id") + 1
  190. End If
  191.  
  192. value("id") = newId
  193. Items.Add value
  194.  
  195. End Sub
  196.  
  197. Public Function GetAll() As SqlResult
  198. Set GetAll = MockSqlResult
  199. End Function
  200.  
  201. Public Function GetById(ByVal id As Long) As SqlResultRow
  202. Set GetById = Items(id)
  203. Dim row As SqlResultRow
  204. For Each row In Items
  205. If row("id") = id Then
  206. Set GetById = row
  207. Exit Function
  208. End If
  209. Next
  210. End Function
  211.  
  212. Public Sub Remove(ByVal id As Long)
  213. Items.Remove GetById(id)
  214. End Sub
  215.  
  216. Public Sub Update(ByVal id As Long, ByVal value As SqlResultRow)
  217.  
  218. Dim row As SqlResultRow
  219. Set row = GetById(id)
  220.  
  221. Dim i As Integer
  222. For i = 1 To value.FieldCount
  223. If LCase(FieldNames(i)) <> "id" Then
  224. row(i - 1) = value(i - 1)
  225. End If
  226. Next
  227.  
  228. End Sub
  229.  
  230. Public Function Count() As Long
  231. Count = Items.Count
  232. End Function
  233.  
  234. Public Function NewItem(ByVal values As Dictionary) As SqlResultRow
  235.  
  236. Dim result As New SqlResultRow
  237. Dim i As Integer
  238.  
  239. Dim model As New SqlResult
  240. For i = LBound(values.Keys) To UBound(values.Keys)
  241. model.AddFieldName values.Keys(i)
  242. Next
  243.  
  244. For i = LBound(values.Items) To UBound(values.Items)
  245. result.AddValue values.Items(i)
  246. Next
  247.  
  248. Set result.ParentResult = model
  249. Set NewItem = result
  250.  
  251. End Function
  252.  
  253. Public Function ToString() As String
  254. Dim result As String
  255.  
  256. Dim Item As IStringRepresentable
  257. For Each Item In Items
  258. result = result & Item.ToString & vbNewLine
  259. Next
  260.  
  261. ToString = result
  262. End Function
  263.  
  264. Private Sub IRepository_Add(ByVal value As SqlResultRow)
  265. Add value
  266. End Sub
  267.  
  268. Private Function IRepository_Count() As Long
  269. IRepository_Count = Count
  270. End Function
  271.  
  272. Private Function IRepository_GetAll() As SqlResult
  273. Set IRepository_GetAll = GetAll
  274. End Function
  275.  
  276. Private Function IRepository_GetById(ByVal id As Long) As SqlResultRow
  277. Set IRepository_GetById = Items(id)
  278. End Function
  279.  
  280. Private Sub IRepository_Remove(ByVal id As Long)
  281. Remove id
  282. End Sub
  283.  
  284. Private Sub IRepository_Update(ByVal id As Long, ByVal value As SqlResultRow)
  285. Update id, value
  286. End Sub
  287.  
  288. Private Function IRepository_NewItem(ByVal model As SqlResult, ByVal values As Dictionary) As SqlResultRow
  289. Set IRepository_NewItem = NewItem(values)
  290. End Function
  291.  
  292. Private Function IStringRepresentable_ToString() As String
  293. IStringRepresentable_ToString = ToString
  294. End Function
  295.  
  296. Option Explicit
  297.  
  298. Public Sub TestMaintainCustomerGroups()
  299.  
  300. Dim CustomerGroups As New MockRepository
  301. Set CustomerGroups = CustomerGroups.Create(GetMockCustomerGroupsModel)
  302.  
  303. Dim Customers As New MockRepository
  304. Set Customers = Customers.Create(GetMockCustomersModel)
  305.  
  306. Dim presenter As New CustomerGroupsPresenter
  307. Set presenter.CustomerGroupsRepo = CustomerGroups
  308. Set presenter.CustomersRepo = Customers
  309.  
  310. presenter.Show
  311.  
  312. End Sub
  313.  
  314. Private Function GetMockCustomerGroupsModel() As SqlResult
  315.  
  316. Dim model As New SqlResult
  317. model.AddFieldName "Id"
  318. model.AddFieldName "Description"
  319.  
  320. Dim row As SqlResultRow
  321. Dim i As Integer
  322. For i = 1 To 10
  323.  
  324. Set row = New SqlResultRow
  325. Set row.ParentResult = model
  326.  
  327. row.AddValue i
  328. row.AddValue "Test" & i
  329.  
  330. model.AddValue row
  331.  
  332. Next
  333.  
  334. Set GetMockCustomerGroupsModel = model
  335.  
  336. End Function
  337.  
  338. Private Function GetMockCustomersModel() As SqlResult
  339.  
  340. Dim model As New SqlResult
  341. model.AddFieldName "Id"
  342. model.AddFieldName "Code"
  343. model.AddFieldName "Name"
  344. model.AddFieldName "CustomerGroupId"
  345.  
  346. Dim row As SqlResultRow
  347. Dim i As Integer
  348. For i = 1 To 10
  349.  
  350. Set row = New SqlResultRow
  351. Set row.ParentResult = model
  352.  
  353. row.AddValue i
  354. row.AddValue 1000 + i
  355. row.AddValue "Customer" & i
  356. row.AddValue 1
  357.  
  358. model.AddValue row
  359.  
  360. Next
  361.  
  362. Set GetMockCustomersModel = model
  363.  
  364. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement