<!--
     VBScript used to generate wival.inc from a Windows Installer schema.msi

        wival.inc is used by the candle compiler for the _Validation table
        data

      2000/02/17 - created - robmen@microsoft.com
-->
<job id='wi_compile'>
   <!-- main -->
   <script Language='VBScript'>
   'Option Explicit

	Set fso = WScript.CreateObject("Scripting.FileSystemObject")
	Set ts = fso.CreateTextFile("wival.inc", True)
	Set Installer = WScript.CreateObject("WindowsInstaller.Installer")
	Set db = Installer.OpenDatabase("schema.msi", 0)

	Set vw = db.OpenView("SELECT * FROM `_Validation` ORDER BY `Table`")
	vw.Execute

	ts.WriteLine "'<script language='VBScript'>"
	ts.WriteLine "' wival.inc - Windows Installer _Validation table definitions"
	ts.WriteLine "'"
	ts.WriteLine "'  " & Now & " - created - val.wsf (tool by robmen@microsoft.com)"
	ts.WriteLine "'"
	ts.WriteLine "' requires:"
	ts.WriteLine "'    none"
	ts.WriteLine "'"
	ts.WriteLine "' entrypoints:"
	ts.WriteLine "'    AddValidation"
	ts.WriteLine "'" & vbCrLf

	ts.WriteLine "Dim val_dicValidation : Set val_dicValidation = CreateObject(""Scripting.Dictionary"")"

	i = 0
	sTable = Empty
	Set rec = vw.Fetch
	Do Until rec Is Nothing
		If sTable <> rec.StringData(1) Then
			If Not IsEmpty(sTable) Then 
				ts.WriteLine "Dim val_" & sTable & "(" & i - 1 & ")"
				ts.Write s
				ts.WriteLine "val_dicValidation.Add """ & sTable & """, val_" & sTable & vbCrLf
			End If

			i = 0
			s = ""
			sTable = rec.StringData(1)
		End If

'		s = s & "val_" & sTable & "(" & i & ") = ""INSERT INTO `_Validation`(`Table`, `Column`, `Nullable`, `MinValue`, `MaxValue`, `KeyTable`, `KeyColumn`, `Category`, `Set`, `Description`) VALUES("
'		s = s & "'" & rec.StringData(1) & "', '" & rec.StringData(2) & "', '" & rec.StringData(3) & "', " & rec.IntegerData(4) & ", " & rec.IntegerData(5) & ", '" 
'		s = s & rec.StringData(6) & "', " & rec.IntegerData(7) & ", '" & rec.StringData(8) & "', '" & rec.StringData(9) & "', '" & Replace(Replace(rec.StringData(10), """", """"""), "'", "") & "')""" & vbCrLf

		s = s & "val_" & sTable & "(" & i & ") = ""'" & rec.StringData(1) & "'&&'" & rec.StringData(2) & "'&&'" & rec.StringData(3) & "'&&'" & rec.StringData(4) & "'&&'" & rec.StringData(5)
		s = s  & "'&&'" & rec.StringData(6)  & "'&&'" & rec.StringData(7)  & "'&&'" & rec.StringData(8)  & "'&&'" & rec.StringData(9)  & "'&&'" & Replace(rec.StringData(10), """", """""") &  "'""" & vbCrLf

'		s = s & "val_" & sTable & "_Table(" & i & ") = """ & rec.StringData(1) & """" & vbCrLf
'		s = s & "val_" & sTable & "_Column(" & i & ") = """ & rec.StringData(2) & """" & vbCrLf
'		s = s & "val_" & sTable & "_Nullable(" & i & ") = """ & rec.StringData(3) & """" & vbCrLf
'		s = s & "val_" & sTable & "_MinValue(" & i & ") = """ & rec.StringData(4) & """" & vbCrLf
'		s = s & "val_" & sTable & "_MaxValue(" & i & ") = """ & rec.StringData(5) & """" & vbCrLf
'		s = s & "val_" & sTable & "_KeyTable(" & i & ") = """ & rec.StringData(6) & """" & vbCrLf
'		s = s & "val_" & sTable & "_KeyColumn(" & i & ") = """ & rec.StringData(7) & """" & vbCrLf
'		s = s & "val_" & sTable & "_Category(" & i & ") = """ & rec.StringData(8) & """" & vbCrLf
'		s = s & "val_" & sTable & "_Set(" & i & ") = """ & rec.StringData(9) & """" & vbCrLf
'		s = s & "val_" & sTable & "_Description(" & i & ") = """ & Replace(rec.StringData(10), """", """""")  & """"' & vbCrLf


		i = i + 1

		Set rec = vw.Fetch
	Loop

	If Not IsEmpty(sTable) Then 
		ts.WriteLine "Dim val_" & sTable & "(" & i - 1 & ")"
		ts.WriteLine s
		ts.WriteLine "val_dicValidation.Add """ & sTable & """, val_" & sTable
	End If

	ts.WriteLine vbCrLf & "Sub AddValidation(installer, db, sTable, fVerbose)"
	ts.WriteLine vbTab & "Dim vw, rec, arrSplit"
	ts.WriteLine vbTab & "' create the validation table if necessary"
	ts.WriteLine vbTab & "If db.TablePersistent(""_Validation"") = 2 Then"
	ts.WriteLine vbTab & vbTab & "Set vw = db.OpenView(""CREATE TABLE _Validation(`Table` CHAR(32) NOT NULL, `Column` CHAR(32) NOT NULL, `Nullable` CHAR(4) NOT NULL, `MinValue` LONG, `MaxValue` LONG, `KeyTable` CHAR(255), `KeyColumn` SHORT, `Category` CHAR(32), `Set` CHAR(255), `Description` CHAR(255) PRIMARY KEY `Table`, `Column`)"")"
	ts.WriteLine vbTab & vbTab & "vw.Execute"
	ts.WriteLine vbTab & vbTab & "AddValidation installer, db, ""_Validation"", fVerbose"
	ts.WriteLine vbTab & "End If"
	ts.WriteLine vbTab & "If val_dicValidation.Exists(sTable) Then"
	ts.WriteLine vbTab & vbTab & "Dim i, arTable : arTable = val_dicValidation.Item(sTable)"
	ts.WriteLine vbTab & vbTab & "Set vw = db.OpenView(""SELECT `Table`, `Column`, `Nullable`, `MinValue`, `MaxValue`, `KeyTable`, `KeyColumn`, `Category`, `Set`, `Description` FROM `_Validation`"")"
	ts.WriteLine vbTab & vbTab & "vw.Execute"
	ts.WriteLine vbTab & vbTab & "Set rec = installer.CreateRecord(10)"
	ts.WriteLine vbTab & vbTab & "For i = 0 To UBound(arTable)"
	ts.WriteLine vbTab & vbTab & vbTab & "If fVerbose Then WScript.Echo arTable(i)"
	ts.WriteLine vbTab & vbTab & vbTab & "arrSplit = Split(arTable(i), ""&&"")"
	ts.WriteLine vbTab & vbTab & vbTab & "For j = 0 To UBound(arrSplit)"
'	ts.WriteLine vbTab & vbTab & vbTab & "db.OpenView(arTable(i)).Execute"
	ts.WriteLine vbTab & vbTab & vbTab & vbTab & "rec.StringData(j + 1) = Mid(arrSplit(j), 2, Len(arrSplit(j)) - 2)"
	ts.WriteLine vbTab & vbTab & vbTab & "Next"
	ts.WriteLine vbTab & vbTab & vbTab & "vw.Modify 1, rec"
	ts.WriteLine vbTab & vbTab & "Next"
	ts.WriteLine vbTab & "End If"
	ts.WriteLine "End Sub"

	</script>
</job>