home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / Chip_2001-06_cd1.bin / zkuste / vbasic / Data / Utility / MSISDK15.msi / WiRunSQL.vbs < prev    next >
Text File  |  2000-10-05  |  3KB  |  85 lines

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