VCS_Optimizer.bas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. ' *** activates the optimizer
  9. Private p_optimizer As Boolean
  10. Public Sub activate_optimizer()
  11. p_optimizer = True
  12. End Sub
  13. Public Function optimizer_activated()
  14. optimizer_activated = p_optimizer
  15. End Function
  16. '***
  17. '*** main methods
  18. Public Function is_dirty(acType As Integer, name As String)
  19. ' has the object been modified since last export?
  20. is_dirty = (get_last_update_date(acType, name) > get_sources_date)
  21. End Function
  22. Public Function get_last_update_date(ByVal acType As Integer, ByVal name As String)
  23. On Error GoTo err
  24. 'get the date of the last update of an object
  25. Select Case acType
  26. 'case table or query: get [DateUpdate] in MSysObjects
  27. Case acTable
  28. get_last_update_date = DFirst("DateUpdate", "MSysObjects", "([Type]=1 or [Type]=4 or [Type]=6) and [name]='" & name & "'")
  29. Case acQuery
  30. get_last_update_date = DFirst("DateUpdate", "MSysObjects", "[Type]=5 and [name]='" & name & "'")
  31. 'MSysObjects is not reliable for other objects,
  32. 'So we used the DateModified property:
  33. Case acForm
  34. get_last_update_date = CurrentProject.AllForms(name).DateModified
  35. Case acReport
  36. get_last_update_date = CurrentProject.AllReports(name).DateModified
  37. Case acMacro
  38. get_last_update_date = CurrentProject.AllMacros(name).DateModified
  39. Case acModule
  40. get_last_update_date = CurrentProject.AllModules(name).DateModified
  41. End Select
  42. Exit Function
  43. err:
  44. Debug.Print "get_last_update_date - erreur - " & acType & ", " & name & ": " & err.Description
  45. get_last_update_date = #1/1/1900#
  46. End Function
  47. '*** displays modified (dirties) objects
  48. Public Function list_modified(acType As Integer)
  49. ' returns a list (string with ';' separator) of the objects wich were updated since last export
  50. Dim sources_date As Date
  51. list_modified = ""
  52. sources_date = get_sources_date()
  53. Dim rs As DAO.Recordset
  54. Set rs = CurrentDb.OpenRecordset("SELECT * FROM MSysObjects WHERE " & msys_type_filter(acType) & ";", _
  55. dbOpenSnapshot)
  56. If rs.RecordCount = 0 Then GoTo emptylist
  57. rs.MoveFirst
  58. Do Until rs.EOF
  59. If Left$(rs![name], 4) <> "MSys" And _
  60. Left$(rs![name], 1) <> "~" Then
  61. If get_last_update_date(acType, rs![name]) > sources_date Then
  62. If Len(list_modified) > 0 Then
  63. list_modified = list_modified & ";" & rs![name]
  64. Else
  65. list_modified = rs![name]
  66. End If
  67. End If
  68. End If
  69. rs.MoveNext
  70. Loop
  71. Exit Function
  72. emptylist:
  73. End Function
  74. Public Function msg_list_modified() As String
  75. 'returns a formatted text listing all of the objects which were updated since last export of the sources
  76. Dim lstmod, obj_type_split, obj_type_label, obj_type_num As String
  77. Dim obj_type, objname As Variant
  78. msg_list_modified = ""
  79. For Each obj_type In Split( _
  80. "tables|" & acTable & "," & _
  81. "queries|" & acQuery & "," & _
  82. "forms|" & acForm & "," & _
  83. "reports|" & acReport & "," & _
  84. "macros|" & acMacro & "," & _
  85. "modules|" & acModule _
  86. , "," _
  87. )
  88. obj_type_split = Split(obj_type, "|")
  89. obj_type_label = obj_type_split(0)
  90. obj_type_num = obj_type_split(1)
  91. lstmod = list_modified(CInt(obj_type_num))
  92. If Len(lstmod) > 0 Then
  93. msg_list_modified = msg_list_modified & "** " & UCase(obj_type_label) & " **" & vbNewLine
  94. For Each objname In Split(lstmod, ";")
  95. msg_list_modified = msg_list_modified & " - " & objname & vbNewLine
  96. Next objname
  97. End If
  98. Next obj_type
  99. End Function
  100. '******
  101. '*** sources_date is the date of the last export of the sources files
  102. Public Function get_sources_date() As Date
  103. ' get the registered sources date
  104. get_sources_date = CDate(vcs_param("sources_date", "01/01/1900 00:00:00"))
  105. End Function
  106. Public Sub update_sources_date()
  107. ' update the sources date with Now()
  108. Call update_vcs_param("sources_date", CStr(Now))
  109. End Sub
  110. '*****
  111. '**** cleans sources or objects after differential import/export
  112. Public Function CleanDirs(Optional ByVal sim As Boolean = False)
  113. ' cleans the directories after a differential export
  114. ' returns a list of the deleted relative file paths (string with '|' separator)
  115. ' if 'sim' is set to True, doesn't process to the delete but still return the list
  116. CleanDirs = ""
  117. Dim source_path As String
  118. source_path = VCS_Dir.ProjectPath() & "source\"
  119. Dim rsSys As DAO.Recordset
  120. Dim sql As String
  121. sql = "SELECT name, type FROM MSysObjects;"
  122. Set rsSys = CurrentDb.OpenRecordset(sql, dbOpenSnapshot)
  123. Dim subdir, filename, objectname, obj_type_label As String
  124. Dim obj_type, obj_type_split As Variant
  125. Dim obj_type_num As Integer
  126. Dim oFSO As Scripting.FileSystemObject
  127. Dim oFld As Scripting.folder
  128. Dim file As Scripting.file
  129. 'Instanciation du FSO
  130. Set oFSO = New Scripting.FileSystemObject
  131. For Each obj_type In Split( _
  132. "forms|" & acForm & "," & _
  133. "reports|" & acReport & "," & _
  134. "macros|" & acMacro & "," & _
  135. "modules|" & acModule _
  136. , "," _
  137. )
  138. obj_type_split = Split(obj_type, "|")
  139. obj_type_label = obj_type_split(0)
  140. obj_type_num = obj_type_split(1)
  141. subdir = source_path & obj_type_label
  142. If Not oFSO.FolderExists(subdir) Then GoTo next_obj_type
  143. Set oFld = oFSO.GetFolder(subdir)
  144. For Each file In oFld.Files
  145. objectname = remove_ext(file.name)
  146. rsSys.FindFirst (msys_type_filter(obj_type_num) & " AND [name]='" & objectname & "'")
  147. If rsSys.NoMatch Then
  148. 'object doesn't exist anymore
  149. If Len(CleanDirs) > 0 Then CleanDirs = CleanDirs & "|"
  150. CleanDirs = CleanDirs & (Replace(file.Path, CurrentProject.Path, "."))
  151. If Not sim Then
  152. oFSO.DeleteFile file
  153. End If
  154. End If
  155. Next file
  156. next_obj_type:
  157. Next obj_type
  158. End Function