home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2000 March / VPR0003B.ISO / nec98 / build.vbs < prev    next >
Text File  |  1999-10-14  |  9KB  |  258 lines

  1. Option Explicit
  2.  
  3. ' Shell object
  4. Dim m_wshShell
  5. Set m_wshShell=WScript.CreateObject("Wscript.Shell")
  6.  
  7. ' Some defaults
  8. Dim m_strServername, m_strDir, m_strLoginID, m_strPassword
  9. Dim m_strErrLogFile, m_strBadArgument
  10.  
  11. ' Argument prefix length
  12. Const ARG_PREFIX_LEN = 3
  13.  
  14. Call GetCommandLineArgs
  15.  
  16. ' Name of the script and welcome message
  17. Dim m_strSource, m_strWelcome
  18. m_strSource = "Exploration Air Database Install v1.04.1003"
  19.  
  20. If m_strBadArgument = "True" Then
  21.     m_strWelcome = vbTab & vbTab & "Incorrect Parameter Entered" & vbCRLF & _
  22.                     vbTab & vbTab & "Unable to continue" & vbCRLF & vbCRLF
  23. Else
  24.     m_strWelcome = ""
  25. End If
  26.  
  27. m_strWelcome =     m_strWelcome & vbTab & vbTab & "**** PLEASE READ ****" & vbCRLF & vbCRLF & _
  28.                 "This script builds up the SQL Server databases for Exploration Air." & vbCRLF & vbCRLF & _
  29.                 "Usage is: BuildExAir.cmd [-S:server] [-U:login ID] [-P:password] [-D:dir]" & vbCRLF & vbCRLF & _ 
  30.                 "Where: " & vbCRLF & _
  31.                 vbTab & "[server] name of the server where SQL Server resides (default is '" & m_strServername & "')." & vbCRLF & _
  32.                 vbTab & "[login ID] is the SQL Server admin login (default is 'sa')." & vbCRLF & _
  33.                 vbTab & "[password] is the login ID password (default is no password)." & vbCRLF & _ 
  34.                 vbTab & "[dir] is the location of SQL Server (default is 'c:\mssql')." & vbCRLF & vbCRLF & _ 
  35.                 "Note: Any existing database named ExAir or ExAirBenefits will be deleted." & vbCRLF & vbCRLF
  36.  
  37. If m_strBadArgument = "True" Then
  38.     m_strWelcome = m_strWelcome & "To build up the SQL Server databases for Exploration Air, rerun with correct parameters."
  39.     Msgbox m_strWelcome, vbOKOnly, m_strSource 
  40. Else
  41.     m_strWelcome = m_strWelcome & "Do you wish to continue?"
  42.     If Msgbox(m_strWelcome,vbYesNo + vbQuestion, m_strSource) = vbNo Then
  43.         Err.Raise 1,m_strSource,"Execution halted by the user."
  44.     End If
  45.     Call BuildDatabases
  46.     Call BuildDSN
  47.  
  48.     MsgBox "Database Setup is completed!",vbInformation+vbOK,m_strSource
  49. End If
  50.  
  51. '''''''''''''''''''''''''''''''''''''''''''''''''
  52. ' GetCommandLineArges parses the command line
  53. Sub GetCommandLineArgs
  54.     ' Set defaults
  55.     m_strDir="c:\mssql"
  56.     m_strLoginID="sa"
  57.     m_strPassword=""
  58.     m_strErrLogFile = "ExAirLog."
  59.     m_strBadArgument = "False"
  60.  
  61.     Dim wshNet
  62.     set wshNet = WScript.CreateObject("Wscript.Network")
  63.     m_strServername = wshNet.ComputerName
  64.  
  65.     ' Get argument object
  66.     Dim wshArg, str
  67.     'Set wshArg=Wscript.Arguments
  68.     For each str in Wscript.Arguments
  69.         Select Case UCase(Left(str,ARG_PREFIX_LEN))
  70.             Case "-S:"
  71.                 m_strServerName = PruneOffPrefix(str)
  72.  
  73.             Case "-P:"
  74.                 m_strPassword = PruneOffPrefix(str)
  75.  
  76.             Case "-D:"
  77.                 m_strDir = PruneOffPrefix(str)
  78.  
  79.             Case "-U:"
  80.                 m_strLoginID = PruneOffPrefix(str)
  81.  
  82.             Case Else
  83.                 m_strBadArgument = "True"
  84.                 Exit Sub
  85.         End Select
  86.     Next
  87.  
  88. End Sub
  89.  
  90. '''''''''''''''''''''''''''''''''''''''''''''''''
  91. ' PruneOffPrefix removes the -X: from a 
  92. ' command-line option
  93. Function PruneOffPrefix(str)
  94.     If Len(str) > ARG_PREFIX_LEN Then 
  95.         PruneOffPrefix = Mid(str, ARG_PREFIX_LEN+1)
  96.     Else
  97.         PruneOffPrefix = ""
  98.     End If
  99. End Function
  100.  
  101. '''''''''''''''''''''''''''''''''''''''''''''''''
  102. ' ExecuteBCP executes a .BCP file 
  103. ' Throws an exception on failure
  104. Sub ExecuteBCP(strDatabase,strTable, strSwitch)
  105.     Dim strComment
  106.     strComment = "   Populating " & strTable
  107.     WScript.echo strComment
  108.  
  109.     dim strCommand
  110.     strCommand = "bcp " & strDatabase & ".." & strTable    & _ 
  111.                  " in " & strTable & ".bcp" & _
  112.                  " -S" & m_strServerName & _
  113.                  " -U" & m_strLoginID & _
  114.                  " -P" & m_strPassword & _ 
  115.                  " -o " & m_strErrLogFile & _
  116.                  " -n"
  117.     If strSwitch <> "" Then
  118.         strCommand = strCommand & strSwitch
  119.     End If
  120.  
  121.     Dim ret
  122.     ret=m_wshShell.Run(strCommand,2,True)
  123.     If ret <> 0 Then
  124.         Err.Raise 1,m_strSource,"BCP failed, please look at the " & m_strErrLogFile & " file for any errors."
  125.     End If
  126. End Sub
  127.  
  128. '''''''''''''''''''''''''''''''''''''''''''''''''
  129. ' ExecuteSQLScript executes a .SQL file 
  130. ' Throws an exception on failure
  131. Sub ExecuteSQLScript(strWhich, strComment, strSQLScript, strOption)
  132.     
  133.     WScript.echo vbCRLF & strComment
  134.  
  135.     Dim strCommand
  136.     strCommand = "isql -U" & m_strLoginID & _ 
  137.                  " -P" & m_strPassword & _
  138.                  " -S" & m_strServerName & _
  139.                  " -i " & strSQLScript & _
  140.                  " -o " & m_strErrLogFile & strWhich & " " & strOption
  141.  
  142.     Dim ret
  143.     ret=m_wshShell.Run(strCommand,2,True)
  144.     If ret <> 0 Then
  145.         Err.Raise 1,m_strSource,"ISQL failed, please look at the " & m_strErrLogFile & strWhich & " file for any errors."
  146.     End If
  147.      
  148. End Sub
  149.  
  150. '''''''''''''''''''''''''''''''''''''''''''''''''
  151. ' ExecuteISQL executes the SQL statement in strSQL 
  152. ' Throws an exception on failure
  153. Sub ExecuteISQL(strWhich, strComment, strSQL, strOption)
  154.     
  155.     WScript.echo vbCRLF & strComment
  156.  
  157.     Dim strCommand
  158.     strCommand = "isql -U" & m_strLoginID & _ 
  159.                  " -P" & m_strPassword & _
  160.                  " -S" & m_strServerName & _
  161.                  " /Q " & chr(34) & strSQL & chr(34) & _
  162.                  " -o " & m_strErrLogFile & strWhich & " " & strOption
  163.  
  164.     Dim ret
  165.     ret=m_wshShell.Run(strCommand,2,True)
  166.     If ret <> 0 Then
  167.         Err.Raise 1,m_strSource,"ISQL failed, please look at the " & m_strErrLogFile & strWhich & " file for any errors."
  168.     End If
  169.      
  170. End Sub
  171. '''''''''''''''''''''''''''''''''''''''''''''''''
  172. ' BuildDatabases builds and populates all the data
  173. Sub BuildDatabases
  174.     ' Nuke old stuff
  175.     ExecuteSQLScript "1", "Clearing old data", "NukeDev.sql", ""                         
  176.  
  177.     ' Build main ExAir database
  178.     ExecuteISQL "2", "Creating ExAir database device", "disk init name='ExAirDev', physname='" & m_strDir & "\Data\ExAirDev.Dat', vdevno=42, size=2048", "-b"
  179.     ExecuteSQLScript "2", "Building ExAir database (this can be time-consuming!)", "ExAir.sql", "-b"
  180.     ExecuteBCP "ExAir", "Ads", ""
  181.     ExecuteBCP "ExAir", "AdsInterests", ""
  182.     ExecuteBCP "ExAir", "Company", ""
  183.     ExecuteBCP "ExAir", "Destination", ""
  184.     ExecuteBCP "ExAir", "FlightSchedule", ""
  185.     ExecuteBCP "ExAir", "InterestCategories", ""
  186.     ExecuteBCP "ExAir", "Interests", ""
  187.     ExecuteBCP "ExAir", "Member", ""
  188.     ExecuteBCP "ExAir", "Membership", ""
  189.     ExecuteBCP "ExAir", "MembershipType", ""
  190.     ExecuteBCP "ExAir", "MembersInterests", ""
  191.     ExecuteBCP "ExAir", "Promotions", ""
  192.     ExecuteBCP "ExAir", "PromotionsInterests", ""
  193.     ExecuteBCP "ExAir", "Special", ""
  194.     ExecuteBCP "ExAir", "TakeANumber", ""
  195.     ExecuteBCP "ExAir", "Transactions", ""
  196.     ExecuteBCP "ExAir", "TransactionType", ""
  197.  
  198.     ' Build Benefits database
  199.     ExecuteISQL "3", "Creating Benefits database device", "disk init name='ExAirBenefitsDev', physname='" & m_strDir & "\Data\ExAirBenefitsDev.Dat', vdevno=43, size=2048", "-b"
  200.     ExecuteSQLScript "3", "Building Benefits database (this can be time-consuming!)", "ExAirBenefits.sql", "-b"
  201.     ExecuteBCP "ExAirBenefits", "BenefitStatus", " -E"
  202.     ExecuteBCP "ExAirBenefits", "EBDStatus", " -E"
  203.     ExecuteBCP "ExAirBenefits", "EDStatus", " -E"
  204.     ExecuteBCP "ExAirBenefits", "EmployeeStatus", " -E"
  205.     ExecuteBCP "ExAirBenefits", "PlanStatus", " -E"
  206.     ExecuteBCP "ExAirBenefits", "QualifierStatus", " -E"
  207.     ExecuteBCP "ExAirBenefits", "Benefit", " -E"
  208.     ExecuteBCP "ExAirBenefits", "DependentType", " -E"
  209.     ExecuteBCP "ExAirBenefits", "Field", " -E"
  210.     ExecuteBCP "ExAirBenefits", "Gender", " -E"
  211.     ExecuteBCP "ExAirBenefits", "GeoArea", " -E"
  212.     ExecuteBCP "ExAirBenefits", "Physician", " -E"
  213.     ExecuteBCP "ExAirBenefits", "Plans", " -E"
  214.     ExecuteBCP "ExAirBenefits", "QualifierClass", " -E"
  215.     ExecuteBCP "ExAirBenefits", "TaxStatus", " -E"
  216.     ExecuteBCP "ExAirBenefits", "BenefitPlan", ""
  217.     ExecuteBCP "ExAirBenefits", "BenefitTaxStatus", ""
  218.     ExecuteBCP "ExAirBenefits", "Dependent", " -E"
  219.     ExecuteBCP "ExAirBenefits", "Employee", " -E"
  220.     ExecuteBCP "ExAirBenefits", "PlanField", ""
  221.     ExecuteBCP "ExAirBenefits", "PlanGeoArea", ""
  222.     ExecuteBCP "ExAirBenefits", "Qualifier", " -E"
  223.     ExecuteBCP "ExAirBenefits", "BenefitQualifier", ""
  224.     ExecuteBCP "ExAirBenefits", "EmployeeBenefit", ""
  225.     ExecuteBCP "ExAirBenefits", "EmployeeDependent", ""
  226.     ExecuteBCP "ExAirBenefits", "EmployeeQualifier", " -E"
  227.     ExecuteBCP "ExAirBenefits", "EmployeeBenefitDependent", ""
  228. End Sub
  229.  
  230. '''''''''''''''''''''''''''''''''''''''''''''''''
  231. ' BuildDSN builds the two File DSN's
  232. Sub BuildDSN
  233.     WScript.echo vbCRLF & "Building ODBC data sources (DSNs)"
  234.     WriteOneDSN "SQLFreq.dsn", "ExAir"
  235.     WriteOneDSN "SQLBenefits.dsn", "ExAirBenefits"
  236. End Sub
  237.  
  238. Sub WriteOneDSN(strDSNName, strDatabase)
  239.  
  240.     Dim obFileSys, obDSN, WSHShell, DefaultODBCDir
  241.  
  242.     Set WSHShell = WScript.CreateObject("WScript.Shell")
  243.     DefaultODBCDir = WSHShell.RegRead("HKLM\Software\ODBC\ODBC.INI\ODBC File DSN\DefaultDSNDir")
  244.  
  245.     Set obFileSys = WScript.CreateObject("Scripting.FileSystemObject")
  246.     Set obDSN = obFileSys.CreateTextFile(DefaultODBCDir & "\" & strDSNName)
  247.  
  248.     obDSN.WriteLine("[ODBC]")
  249.     obDSN.WriteLine("DRIVER=SQL Server")
  250.     obDSN.WriteLine("UID=" & m_strLoginID)
  251.     obDSN.WriteLine("PWD=" & m_strPassword)
  252.     obDSN.WriteLine("DATABASE=" & strDatabase)
  253.     obDSN.WriteLine("WSID=" & m_strServername)
  254.     obDSN.WriteLine("APP=Microsoft Win32")
  255.     obDSN.WriteLine("SERVER=" & m_strServername)
  256.     obDSN.Close
  257.  
  258. End Sub