optimizer.bas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. Option Compare Database
  2. Option Explicit
  3. '****
  4. '*
  5. '* Optimizer for VCS: only import/export objects which were updated since last import/export
  6. '*
  7. '****
  8. Public Function is_dirty(acType As Integer, name As String)
  9. ' has the object been modified since last export?
  10. is_dirty = (get_last_update_date(acType, name) > get_sources_date)
  11. End Function
  12. Public Function get_last_update_date(ByVal acType As Integer, ByVal name As String)
  13. On Error GoTo err
  14. 'get the date of the last update of an object
  15. Select Case acType
  16. 'case table or query: get [DateUpdate] in MSysObjects
  17. Case acTable
  18. get_last_update_date = DFirst("DateUpdate", "MSysObjects", "([Type]=1 or [Type]=4 or [Type]=6) and [name]='" & name & "'")
  19. Case acQuery
  20. get_last_update_date = DFirst("DateUpdate", "MSysObjects", "[Type]=5 and [name]='" & name & "'")
  21. 'MSysObjects is not reliable for other objects,
  22. 'So we used the DateModified property:
  23. Case acForm
  24. get_last_update_date = CurrentProject.AllForms(name).DateModified
  25. Case acReport
  26. get_last_update_date = CurrentProject.AllReports(name).DateModified
  27. Case acMacro
  28. get_last_update_date = CurrentProject.AllMacros(name).DateModified
  29. Case acModule
  30. get_last_update_date = CurrentProject.AllModules(name).DateModified
  31. End Select
  32. Exit Function
  33. err:
  34. Debug.Print "get_last_update_date - erreur - " & acType & ", " & name & ": " & err.Description
  35. get_last_update_date = #1/1/1900#
  36. End Function
  37. '*** displays modified (dirties) objects
  38. Public Function list_modified(acType As Integer)
  39. ' returns a list (string with ';' separator) of the objects wich were updated since last export
  40. Dim sources_date As Date
  41. list_modified = ""
  42. sources_date = get_sources_date()
  43. Dim rs As DAO.Recordset
  44. Set rs = CurrentDb.OpenRecordset("SELECT * FROM MSysObjects WHERE " & typefilter(acType) & ";", _
  45. dbOpenSnapshot)
  46. If rs.RecordCount = 0 Then GoTo emptylist
  47. rs.MoveFirst
  48. Do Until rs.EOF
  49. If Left$(rs![name], 4) <> "MSys" And _
  50. Left$(rs![name], 1) <> "~" Then
  51. If rs![dateupdate] > sources_date Then
  52. If Len(list_modified) > 0 Then
  53. list_modified = list_modified & ";" & rs![name]
  54. Else
  55. list_modified = rs![name]
  56. End If
  57. End If
  58. End If
  59. rs.MoveNext
  60. Loop
  61. Exit Function
  62. emptylist:
  63. End Function
  64. Public Function msg_list_modified() As String
  65. 'returns a formatted text listing all of the objects which were updated since last export of the sources
  66. Dim lstmod, obj_type_split, obj_type_label, obj_type_num As String
  67. Dim obj_type, objname As Variant
  68. msg_list_modified = ""
  69. For Each obj_type In Split( _
  70. "tables|" & acTable & "," & _
  71. "queries|" & acQuery & "," & _
  72. "forms|" & acForm & "," & _
  73. "reports|" & acReport & "," & _
  74. "macros|" & acMacro & "," & _
  75. "modules|" & acModule _
  76. , "," _
  77. )
  78. obj_type_split = Split(obj_type, "|")
  79. obj_type_label = obj_type_split(0)
  80. obj_type_num = obj_type_split(1)
  81. lstmod = list_modified(CInt(obj_type_num))
  82. If Len(lstmod) > 0 Then
  83. msg_list_modified = msg_list_modified & "** " & UCase(obj_type_label) & " **" & vbNewLine
  84. For Each objname In Split(lstmod, ";")
  85. msg_list_modified = msg_list_modified & " " & objname & vbNewLine
  86. Next objname
  87. End If
  88. Next obj_type
  89. End Function
  90. '******
  91. '*** sources_date is the date of the last export of the sources files
  92. Public Function get_sources_date() As Date
  93. get_sources_date = CDate(vcs_param("sources_date", "01/01/1900 00:00:00"))
  94. End Function
  95. Public Sub update_sources_date()
  96. If Not vcs_tbl_exists() Then
  97. Call create_vcs_tbl
  98. End If
  99. Call update_vcs_param("sources_date", CStr(Now))
  100. End Sub
  101. '*****
  102. '**** cleans sources or objects after differential import/export
  103. Public Function CleanDirs(Optional ByVal sim As Boolean = False)
  104. ' cleans the directories after a differential export
  105. ' returns a list of the deleted relative file paths (string with '|' separator)
  106. ' if 'sim' is set to True, doesn't process to the delete but still return the list
  107. CleanDirs = ""
  108. Dim source_path As String
  109. source_path = VCS_Dir.ProjectPath() & "source\"
  110. Dim rsSys As DAO.Recordset
  111. Dim sql As String
  112. sql = "SELECT name, type FROM MSysObjects;"
  113. Set rsSys = CurrentDb.OpenRecordset(sql, dbOpenSnapshot)
  114. Dim subdir, filename, objectname, obj_type_label As String
  115. Dim obj_type, obj_type_split As Variant
  116. Dim obj_type_num As Integer
  117. Dim oFSO As Scripting.FileSystemObject
  118. Dim oFld As Scripting.folder
  119. Dim file As Scripting.file
  120. 'Instanciation du FSO
  121. Set oFSO = New Scripting.FileSystemObject
  122. For Each obj_type In Split( _
  123. "forms|" & acForm & "," & _
  124. "reports|" & acReport & "," & _
  125. "macros|" & acMacro & "," & _
  126. "modules|" & acModule _
  127. , "," _
  128. )
  129. obj_type_split = Split(obj_type, "|")
  130. obj_type_label = obj_type_split(0)
  131. obj_type_num = obj_type_split(1)
  132. subdir = source_path & obj_type_label
  133. If Not oFSO.FolderExists(subdir) Then GoTo next_obj_type
  134. Set oFld = oFSO.GetFolder(subdir)
  135. For Each file In oFld.Files
  136. objectname = remove_ext(file.name)
  137. rsSys.FindFirst (typefilter(obj_type_num) & " AND [name]='" & objectname & "'")
  138. If rsSys.NoMatch Then
  139. 'object doesn't exist anymore
  140. If Len(CleanDirs) > 0 Then CleanDirs = CleanDirs & "|"
  141. CleanDirs = CleanDirs & (Replace(file.Path, CurrentProject.Path, "."))
  142. If Not sim Then
  143. oFSO.DeleteFile file
  144. End If
  145. End If
  146. Next file
  147. next_obj_type:
  148. Next obj_type
  149. End Function
  150. Public Sub CleanObjects()
  151. End Sub
  152. '*** utilities
  153. Private Function typefilter(acType) As String
  154. 'returns a sql filter string for the object type
  155. 'NB: types in msysobjects table
  156. '-32768 = Form
  157. '-32766 = Macro
  158. '-32764 = Report
  159. '-32761 = Module
  160. '-32758 Users
  161. '-32757 Database Document
  162. '-32756 Data Access Pages
  163. '1 Table - Local Access Tables
  164. '2 Access Object - Database
  165. '3 Access Object - Containers
  166. '4 Table - Linked ODBC Tables
  167. '5 Queries
  168. '6 Table - Linked Access Tables
  169. '8 SubDataSheets
  170. Select Case acType
  171. Case acTable
  172. typefilter = "([Type]=1 or [Type]=4 or [Type]=6)"
  173. Case acQuery
  174. typefilter = "[Type]=5"
  175. Case acForm
  176. typefilter = "[Type]=-32768"
  177. Case acReport
  178. typefilter = "[Type]=-32764"
  179. Case acModule
  180. typefilter = "[Type]=-32761"
  181. Case acMacro
  182. typefilter = "[Type]=-32766"
  183. Case Else
  184. GoTo typerror
  185. End Select
  186. Exit Function
  187. typerror:
  188. MsgBox "typerror:" & acType & " is not a valid object type"
  189. typefilter = ""
  190. End Function
  191. Public Function remove_ext(ByVal filename As String) As String
  192. ' removes the extension of a file name
  193. If Not InStr(filename, ".") > 0 Then
  194. remove_ext = filename
  195. Exit Function
  196. End If
  197. Dim splitted_name As Variant
  198. splitted_name = Split(filename, ".")
  199. Dim i As Integer
  200. remove_ext = ""
  201. For i = 0 To (UBound(Split(filename, ".")) - 1)
  202. If Len(remove_ext) > 0 Then remove_ext = remove_ext & "."
  203. remove_ext = remove_ext & Split(filename, ".")(i)
  204. Next i
  205. End Function