1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
'============================================================================================================================
'=== Prerequisites: ===
'=== You should have SQLXML 3.0 SP1 installed on the machine running the script.
'=== SQLXML 3.0 SP1 is available for download at:
'=== http://www.microsoft.com/downloads/details.aspx?FamilyID=4023deea-f179-45de-b41d-84e4ff655a3b
'============================================================================================================================
Option Explicit
Dim strURL, sDatasourceName, sSavedReportUserAlias, sSavedReportName, sMaxNumRows, sArguments, sDSN, sXML, fso, fFile
Dim DBName, DBServerName, UserName, Password, oPoster, objDBConn, objBL, objDocument, sDBName
Dim XMLFile, XSDFile, XMLOk, BulkLoadErrLogPath, sTableName, RootDir
Set objDocument = CreateObject("msxml2.DOMDocument")
Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad.3.0")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objDBConn = CreateObject("ADODB.Connection")
'=================================================================
'Sub: InitVariables ==
' This procedure sets values for the variables that will be ==
' used later by the script. Make sure you set the values ==
' as per your working environment. ==
'=================================================================
Sub InitVariables()
wscript.echo "Number of arguments supplied: " & wscript.arguments.count
If wscript.arguments.count > 6 Then
sDatasourceName = wscript.arguments(0)'"UTS - TicketDiary"
sSavedReportUserAlias = wscript.arguments(1)'"siggib"
sSavedReportName = wscript.arguments(2)'"DiaryBodyKeyword"
sMaxNumRows = wscript.arguments(3)'0
sArguments = wscript.arguments(4)'"Netscalar"
sTableName = wscript.arguments(5)'"NetscalarDiaries"
sDBName = wscript.arguments(6)
Else
wscript.echo "Supply all arguments"
wscript.quit(1)
End If
'--- Initialize Script variables
DBServerName = "by2netsql01" ' -- Replace with db server name
DBName = sDBName ' -- Replace with db name
UserName = "ScriptRW" ' -- Replace with user name
Password = "thisbites2." ' -- Replace with pwd
RootDir = "C:\~"
'--- Make sure you are posting the data to the correct URL
strURL = "http://XMLInterface/XMLPullRS.asp"
sDSN = "provider=SQLOLEDB.1;data source=" & DBServerName & ";database=" & DBName & ";uid=" & UserName & ";pwd=" & Password
BulkLoadErrLogPath = RootDir & "XMLPullError.xml"
XMLFile = RootDir & sTableName & ".xml"
XSDFile = RootDir & sTableName & "Schema.xsd"
sXML = ""
End Sub
'================================================================
'Script Main Processing -- Begins here =====
'================================================================
InitVariables
wscript.echo "Starting processing at " & now
wscript.echo "sDatasourceName=" & sDatasourceName
wscript.echo "sSavedReportUserAlias=" & sSavedReportUserAlias
wscript.echo "sSavedReportName=" & sSavedReportName
wscript.echo "sArguments=" & sArguments
wscript.echo "sMaxNumRows=" & sMaxNumRows
wscript.echo "sTableName=" & sTableName
wscript.echo "XMLFile=" & XMLFile
'--- Request the XSD Schema
Set fFile = fso.CreateTextFile(XMLFile, True, True)
Set oPoster = CreateObject("Microsoft.XMLHTTP")
GetPosterData "GetElementBasedXSDSchema2", URLEncode(sDatasourceName), URLEncode(sSavedReportUserAlias), URLEncode(sSavedReportName), "[dbo]." & URLEncode(sTableName), "", "", "", ""
CheckPosterStatus
sXML = oPoster.responseXML.xml
'--- oPoster.responseXML is an XMLDocument object containing the query results
If oPoster.responseXML.xml <> "" Then
'--- Write the schema to the output file
sXML = Replace(sXML, "unsignedbyte", "unsignedByte")
Set fFile = fso.CreateTextFile(XSDFile, True, True)
fFile.Write (sXML)
fFile.Close
Set fFile = Nothing
Else
ExitWithErrors("Unable to output Schema to file. XML results to follow." & Chr(13) & Chr(10) & oPoster.responseXML.xml & Chr(13) & Chr(10) & oPoster.responseText)
End If
'--- Get the data
GetPosterData "ScriptedPull2", URLEncode(sDatasourceName), URLEncode(sSavedReportUserAlias), URLEncode(sSavedReportName), "[dbo]." & URLEncode(sTableName), CInt(sMaxNumRows), URLEncode(sArguments), True, False
CheckPosterStatus
'--- Write the XML to the output file
Set fFile = fso.CreateTextFile(XMLFile, True, True)
sXML = oPoster.responseXML.xml
sXML = Replace(sXML, "<?xml<version>1.0</version>?>", "<?xml version=""1.0""?>")
sXML = Replace(sXML, "<DATASET>", "<DATASET xmlns:od=""urn:schemas-microsoft-com:officedata"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:noNamespaceSchemaLocation=""~XMLPull.xsd"">")
fFile.Write(Replace(sXML, "<?xml version=""1.0""?>", "<?xml version=""1.0"" encoding=""UTF-16""?>"))
fFile.Close()
'--- Check for errors
If Err.Number > 0 Then
ExitWithErrors("Unable to output XML to file. XML results to follow." & Chr(13) & Chr(10) & oPoster.responseXML.xml & Chr(13) & Chr(10) & oPoster.responseText)
Else
ValidateXMLDocument XMLFile
BulkLoadXML XMLFile, XSDFile
If Err.Number <> 0 Then
ExitWithErrors("There were errors while processing the job. Please try running the job later or contact OSSG for more info.")
Else
WScript.Echo "Results successfully written to database."
End If
End If
CleanUpObjects
'=================================================================
'End of Script ==============================================
'=================================================================
'=================================================================
'Sub: ValidateXMLDocument(XMLFile) ==
' This subroutine loads and parse the XML document ==
'=================================================================
Sub ValidateXMLDocument(XMLFile)
On Error Resume Next
objDocument.async = False
objDocument.validateOnParse = True
XMLOk = objDocument.load(XMLFile)
If Not XMLOk Then
XMLErrorMsgDesc = "XML Document is invalid: " & objDocument.parseError.reason
iXMLTagBegPos = InStrRev(objDocument.parseError.srctext, "<",(objDocument.parseError.linepos - 1))
If iXMLTagBegPos = 0 Then
If objDocument.parseError.line > 1 Then
XMLErrorMsgDesc = XMLErrorMsgDesc & "XML syntax on line " & (objDocument.parseError.line - 1)
Else
XMLErrorMsgDesc = XMLErrorMsgDesc & "Cannot determine source text in error."
End If
Else
XMLErrorMsgDesc = XMLErrorMsgDesc & " XML:" & Mid(objDocument.parseError.srctext, iXMLTagBegPos, (objDocument.parseError.linepos - iXMLTagBegPos))
End If
ExitWithErrors(XMLErrorMsgDesc)
End If
End Sub
'=================================================================
'Sub: BulkLoadXML (XMLFile, XSDFile) ==
' This subroutine sets values for the SQLXML bulkload object ==
' and uses the content in XSD & XML documents to populate ==
' a SQL database table. ==
'=================================================================
Sub BulkLoadXML(XMLFile, XSDFile)
On Error Resume Next
objDBConn.Open sDSN
If Err.Number <> 0 Then
ExitWithErrors("Database connection error: Cannot connect to destination database [" & DBName & "] on server [" & DBServerName & "]." & Chr(13) & Chr(10) & Err.Description)
End If
objBL.SGDropTables = True
objBL.SchemaGen = True
objBL.ForceTableLock = True
objBL.ConnectionString = sDSN
objBL.ErrorLogFile = BulkLoadErrLogPath
objBL.Execute XSDFile, XMLFile
If Err.Number <> 0 Then
ExitWithErrors("Error while updating the database." & Chr(13) & Chr(10) & Err.Description & Chr(13) & Chr(10) & "Check the file named ~XMLPullError.xml on the root of Drive C for more information.")
End If
End Sub
'=================================================================
'Sub: GetPosterData(p0, p1, p2, p3, p4, p5, p6, p7, p8) ==
' This procedure posts request through the poster object ==
' to a particular subroutine in the server script ==
'=================================================================
Sub GetPosterData (strAction, strDSN, strSavedRptUser, strSavedRptName, strTableName, strRowCount, strArguments, bElementBasedSchema, bXMLDateFormat)
oPoster.Open "POST", strURL, 0
oPoster.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Select Case UCase(strAction)
Case "SCRIPTEDPULL2"
oPoster.Send "sAction=" & strAction & "&p1=" & strDSN & "&p2=" & strSavedRptUser & "&p3=" & strSavedRptName & "&p4=" & strRowCount & "&p5=" & strArguments & "&p6=" & bElementBasedSchema & "&p7=" & bXMLDateFormat
Case "GETELEMENTBASEDXSDSCHEMA2"
oPoster.Send "sAction=" & strAction & "&p1=" & strDSN & "&p2=" & strSavedRptUser & "&p3=" & strSavedRptName & "&p4=" & strTableName
End Select
End Sub
'=================================================================
'Sub: CheckPosterStatus ==
' This subroutine checks the return status of the ==
' poster object ==
'=================================================================
Sub CheckPosterStatus
'--- Check the return status
Select Case Left(oPoster.Status, 3)
Case "200"
If oPoster.responseXML.xml = "" Then
ExitWithErrors("No XML results were returned." & Chr(13) & Chr(10) & oPoster.responseText )
End If
Case "602"
ExitWithErrors("Processing was unsuccessful. Invalid request. " & Chr(13) & Chr(10) & oPoster.responseText)
Case "604"
ExitWithErrors("Processing was unsuccessful. Database error. " & Chr(13) & Chr(10) & oPoster.responseText)
Case Else
ExitWithErrors("Processing returned an unexpected HTTP status code - " & oPoster.status)
End Select
End Sub
'=================================================================
'Sub: ExitWithErrors(XMLErrorMsgDesc) ==
' This subroutine writes an error message to the console ==
' and terminates the script execution ==
'=================================================================
Sub ExitWithErrors(XMLErrorMsgDesc)
On Error Resume Next
WScript.Echo XMLErrorMsgDesc
wscript.echo sdsn
CleanUpObjects
WScript.Quit(1)
End Sub
'=================================================================
'Sub: CleanUpObjects ==
' This subroutine cleans up all objects and temp files ==
' created by this script ==
'=================================================================
Sub CleanUpObjects
On Error Resume Next
fso.DeleteFile (XMLFile)
fso.DeleteFile (XSDFile)
Set fFile = Nothing
Set fso = Nothing
Set oPoster = Nothing
Set objDBConn = Nothing
Set objBL = Nothing
Set objDocument = Nothing
wscript.echo
wscript.echo "script completed at " & now
End Sub
'================================================================
'Function: URLEncode =
' This function simply encodes all characters that are not =
' valid in a URL. It is a direct implementation of ASP's =
' built-in Server.URLEncode function =
'================================================================
Function URLEncode(tmpStr)
Dim temp, onechar, j
Const URLComponent_SET_OF_VALID_UNESCAPED_CHARACTERS = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ;/:@=$-_.!*'(), "
For j = 1 To Len(tmpStr)
onechar = Mid(tmpStr, j, 1)
If InStr(URLComponent_SET_OF_VALID_UNESCAPED_CHARACTERS, onechar) = 0 Then
' Encode this character
temp = temp + "%" + Hex(AscB(onechar))
Else
' Good character
temp = temp + onechar
End If
Next
URLEncode = Replace(temp, " ", "+")
End Function
|