Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.9 KiB

  1. ' Windows Installer utility to execute SQL statements against an installer database
  2. ' For use with Windows Scripting Host, CScript.exe or WScript.exe
  3. ' Copyright (c) 1999, Microsoft Corporation
  4. ' Demonstrates the script-driven database queries and updates
  5. '
  6. Option Explicit
  7. Const msiOpenDatabaseModeReadOnly = 0
  8. Const msiOpenDatabaseModeTransact = 1
  9. Dim argNum, argCount:argCount = Wscript.Arguments.Count
  10. If (argCount < 2) Then
  11. Wscript.Echo "Windows Installer utility to execute SQL queries against an installer database." &_
  12. vbLf & " The 1st argument specifies the path to the MSI database, relative or full path" &_
  13. vbLf & " Subsequent arguments specify SQL queries to execute - must be in double quotes" &_
  14. vbLf & " SELECT queries will display the rows of the result list specified in the query" &_
  15. vbLf & " Binary data columns selected by a query will not be displayed"
  16. Wscript.Quit 1
  17. End If
  18. ' Scan arguments for valid SQL keyword and to determine if any update operations
  19. Dim openMode : openMode = msiOpenDatabaseModeReadOnly
  20. For argNum = 1 To argCount - 1
  21. Dim keyword : keyword = Wscript.Arguments(argNum)
  22. Dim keywordLen : keywordLen = InStr(1, keyword, " ", vbTextCompare)
  23. If (keywordLen) Then keyword = UCase(Left(keyword, keywordLen - 1))
  24. If InStr(1, "UPDATE INSERT DELETE CREATE ALTER DROP", keyword, vbTextCompare) Then
  25. openMode = msiOpenDatabaseModeTransact
  26. ElseIf keyword <> "SELECT" Then
  27. Fail "Invalid SQL statement type: " & keyword
  28. End If
  29. Next
  30. ' Connect to Windows installer object
  31. On Error Resume Next
  32. Dim installer : Set installer = Nothing
  33. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  34. ' Open database
  35. Dim databasePath:databasePath = Wscript.Arguments(0)
  36. Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  37. ' Process SQL statements
  38. Dim query, view, record, message, rowData, columnCount, delim, column
  39. For argNum = 1 To argCount - 1
  40. query = Wscript.Arguments(argNum)
  41. Set view = database.OpenView(query) : CheckError
  42. view.Execute : CheckError
  43. If Ucase(Left(query, 6)) = "SELECT" Then
  44. Do
  45. Set record = view.Fetch
  46. If record Is Nothing Then Exit Do
  47. columnCount = record.FieldCount
  48. rowData = Empty
  49. delim = " "
  50. For column = 1 To columnCount
  51. If column = columnCount Then delim = vbLf
  52. rowData = rowData & record.StringData(column) & delim
  53. Next
  54. message = message & rowData
  55. Loop
  56. End If
  57. Next
  58. If openMode = msiOpenDatabaseModeTransact Then database.Commit
  59. If Not IsEmpty(message) Then Wscript.Echo message
  60. Wscript.Quit 0
  61. Sub CheckError
  62. Dim message, errRec
  63. If Err = 0 Then Exit Sub
  64. message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  65. If Not installer Is Nothing Then
  66. Set errRec = installer.LastErrorRecord
  67. If Not errRec Is Nothing Then message = message & vbLf & errRec.FormatText
  68. End If
  69. Fail message
  70. End Sub
  71. Sub Fail(message)
  72. Wscript.Echo message
  73. Wscript.Quit 2
  74. End Sub