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

  1. ' Windows Installer utility to copy a file into a database text field
  2. ' For use with Windows Scripting Host, CScript.exe or WScript.exe
  3. ' Copyright (c) 1999-2000, Microsoft Corporation
  4. ' Demonstrates processing of primary key data
  5. '
  6. Option Explicit
  7.  
  8. Const msiOpenDatabaseModeReadOnly     = 0
  9. Const msiOpenDatabaseModeTransact     = 1
  10.  
  11. Const msiViewModifyUpdate  = 2
  12. Const msiReadStreamAnsi    = 2
  13.  
  14. Dim argCount:argCount = Wscript.Arguments.Count
  15. If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
  16. If (argCount < 4) Then
  17.     Wscript.Echo "Windows Installer utility to copy a file into a database text field." &_
  18.         vbNewLine & "The 1st argument is the path to the installation database" &_
  19.         vbNewLine & "The 2nd argument is the database table name" &_
  20.         vbNewLine & "The 3rd argument is the set of primary key values, concatenated with colons" &_
  21.         vbNewLine & "The 4th argument is non-key column name to receive the text data" &_
  22.         vbNewLine & "The 5th argument is the path to the text file to copy" &_
  23.         vbNewLine & "If the 5th argument is omitted, the existing data will be listed" &_
  24.         vbNewLine & "All primary keys values must be specified in order, separated by periods" &_
  25.         vbNewLine &_
  26.         vbNewLine & "Copyright (C) Microsoft Corporation, 1999-2000.  All rights reserved."
  27.     Wscript.Quit 1
  28. End If
  29.  
  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.  
  35.  
  36. ' Process input arguments and open database
  37. Dim databasePath: databasePath = Wscript.Arguments(0)
  38. Dim tableName   : tableName    = Wscript.Arguments(1)
  39. Dim rowKeyValues: rowKeyValues = Split(Wscript.Arguments(2),":",-1,vbTextCompare)
  40. Dim dataColumn  : dataColumn   = Wscript.Arguments(3)
  41. Dim openMode : If argCount >= 5 Then openMode = msiOpenDatabaseModeTransact Else openMode = msiOpenDatabaseModeReadOnly
  42. Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  43. Dim keyRecord : Set keyRecord = database.PrimaryKeys(tableName) : CheckError
  44. Dim keyCount : keyCount = keyRecord.FieldCount
  45. If UBound(rowKeyValues) + 1 <> keyCount Then Fail "Incorrect number of primary key values"
  46.  
  47. ' Generate and execute query
  48. Dim predicate, keyIndex
  49. For keyIndex = 1 To keyCount
  50.     If Not IsEmpty(predicate) Then predicate = predicate & " AND "
  51.     predicate = predicate & "`" & keyRecord.StringData(keyIndex) & "`='" & rowKeyValues(keyIndex-1) & "'"
  52. Next
  53. Dim query : query = "SELECT `" & dataColumn & "` FROM `" & tableName & "` WHERE " & predicate
  54. REM Wscript.Echo query 
  55. Dim view : Set view = database.OpenView(query) : CheckError
  56. view.Execute : CheckError
  57. Dim resultRecord : Set resultRecord = view.Fetch : CheckError
  58. If resultRecord Is Nothing Then Fail "Requested table row not present"
  59.  
  60. ' Update value if supplied. Cannot store stream object in string column, must convert stream to string
  61. If openMode = msiOpenDatabaseModeTransact Then
  62.     resultRecord.SetStream 1, Wscript.Arguments(4) : CheckError
  63.     resultRecord.StringData(1) = resultRecord.ReadStream(1, 100000, msiReadStreamAnsi) : CheckError
  64.     view.Modify msiViewModifyUpdate, resultRecord : CheckError
  65.     database.Commit : CheckError
  66. Else
  67.     Wscript.Echo resultRecord.StringData(1)
  68. End If
  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 & vbNewLine & 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.