<%@LANGUAGE="VBSCRIPT" CodePage=65001%>
<% option explicit %>
<!--#include file="adovbs.inc" -->
<!--#include file="connection.inc" -->
<%
'*********************************************
' Code by Philippe Nomail, http://philflash.inway.fr/
'*********************************************
Dim saveUpdatePacket, aspdebug
' For debug
saveUpdatePacket = False
' saveUpdatePacket = True
aspdebug = request.querystring("debug")
' ----- DB Connection -----
' use DB_connection declared in connection.inc
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open DB_connection
' ----- Define Result Doc: UpdatePacket -----
Dim resultdoc
Set resultdoc = Server.CreateObject("Microsoft.XMLDOM")
resultdoc.async = False
resultdoc.loadXML( "<?xml version=""1.0"" encoding=""UTF-8"" ?><update_packet></update_packet>")
' ----- Read XML -----
Dim xmldoc
Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.async = False
If aspdebug = "1" Then
xmldoc.load(Server.MapPath("updatePacket.xml"))
Else
If Request.TotalBytes = 0 Then
xmldoc.loadXML( "<?xml version=""1.0"" encoding=""UTF-8"" ?><update_packet tableName=""TEST"" nullValue=""_NULL_"" transID=""""></update_packet>")
Else
xmldoc.load Request
End If
If saveUpdatePacket Then
xmldoc.save(Server.MapPath("updatePacket.xml"))
End If
End If
' ----- Parse UpdatePacket -----
Dim rootNode, table, nullValue, transID
Set rootNode = xmldoc.documentElement
table = rootNode.getAttribute("tableName")
nullValue = rootNode.getAttribute("nullValue")
transID = rootNode.getAttribute("transID")
' ----- Prepare resultDoc -----
Dim resultRootNode
Set resultRootNode = xmldoc.createElement("update_packet")
Set resultdoc.documentElement = resultRootNode
resultRootNode.setAttribute "transID", transID
' ----- process "update" nodes -----
Dim nodes, node, fields, field
Dim kname, ktype, kvalue, stmt
Dim fname, ftype, fvalue
Dim objRS, strSQL, newId
Dim operationId
Set nodes = rootNode.getElementsByTagName("update")
For Each node In nodes
operationId = node.getAttribute("id")
Set fields = node.getElementsByTagName("field")
' -- find key --
For Each field in fields
If field.getAttribute("key") = "true" Then
kname = field.getAttribute("name")
ktype = field.getAttribute("type")
kvalue = field.getAttribute("oldValue")
stmt = kvalue
If ktype = "Integer" Or ktype = "Number" Then
stmt = kname & "=" & kvalue
Else
kvalue = Replace(kvalue, "'", "''")
stmt = kname & "= """ & kvalue & """"
End If
Exit For
End If
Next
strSQL = "SELECT * FROM " & table & " WHERE " & stmt
Set objRS = Server.CreateObject("ADODB.RecordSet")
objRS.Open strSQL, objConn, adOpenKeySet, adLockPessimistic
If objRS.EOF Then
' Cas record supprim?
Else
' -- update record --
For Each field in fields
If field.getAttribute("key") = "false" Then
fname = field.getAttribute("name")
ftype = field.getAttribute("type")
fvalue = field.getAttribute("newValue")
' -- check NullValue --
If fvalue = nullValue Then
fvalue = Null
End If
objRS.Fields(fname) = fvalue
End If
Next
objRS.Update
End If
objRS.Close
Set objRS = Nothing
Next
' ----- process "delete" nodes -----
Set nodes = rootNode.getElementsByTagName("delete")
For Each node In nodes
operationId = node.getAttribute("id")
Set fields = node.getElementsByTagName("field")
' -- find key --
For Each field in fields
If field.getAttribute("key") = "true" Then
kname = field.getAttribute("name")
ktype = field.getAttribute("type")
kvalue = field.getAttribute("oldValue")
stmt = kvalue
If ktype = "Integer" Or ktype = "Number" Then
stmt = kname & "=" & kvalue
Else
kvalue = Replace(kvalue, "'", "''")
stmt = kname & "= '" & kvalue & "'"
End If
Exit For
End If
Next
strSQL = "DELETE FROM " & table & " WHERE " & stmt
objConn.Execute(strSQL)
Next
' ----- process "insert" nodes -----
Set nodes = rootNode.getElementsByTagName("insert")
For Each node In nodes
operationId = node.getAttribute("id")
Set fields = node.getElementsByTagName("field")
' -- find key --
For Each field in fields
If field.getAttribute("key") = "true" Then
kname = field.getAttribute("name")
ktype = field.getAttribute("type")
Exit For
End If
Next
Set objRS = Server.CreateObject("ADODB.RecordSet")
objRS.Open table, objConn, adOpenKeySet, adLockPessimistic, adCmdTable
objRS.AddNew
' -- insert record --
For Each field in fields
If field.getAttribute("key") = "false" Then
fname = field.getAttribute("name")
ftype = field.getAttribute("type")
fvalue = field.getAttribute("newValue")
' -- check NullValue --
If fvalue = nullValue Then
fvalue = Null
End If
objRS.Fields(fname) = fvalue
End If
Next
objRS.Update
newId = objRS.Fields(kname)
' -- append result doc for newId --
Dim roperationNode, rfieldNode
Set roperationNode = resultdoc.createElement("operation")
roperationNode.setAttribute "op", "update"
roperationNode.setAttribute "id", operationId
Set rfieldNode = resultdoc.createElement("field")
rfieldNode.setAttribute "name", kname
rfieldNode.setAttribute "curValue", newId
roperationNode.appendChild(rfieldNode)
resultRootNode.appendChild(roperationNode)
objRS.Close
Set objRS = Nothing
Next
' Send XML to Response
resultdoc.save(Response)
objConn.Close
Set objConn = Nothing
Set xmldoc=Nothing
Set resultdoc=Nothing
%>