miércoles, 26 de abril de 2017

Join de Searches

Parametros:
  • pDom: El dom de busqueda sobre el cual se hace el join
  • pDomKey: El campo de clave foranea dentro del dom
  • pFolder: El Folder ID a joinear
  • pFolderKey: El campo de clave del folder, contra el cual se matchea el foreign key
  • pFolderFields: La lista de campos de la tabla relacionada que se van a agregar al dom


Sub JoinSearches(ByRef pDom, ByRef pDomKey, ByRef pFolder, ByRef pFolderKey, ByRef pFolderFields)
 Dim node, sIDs, sID, fld, domJoin, attr, nodeJoin

 sIDs = "," 
 For Each node In pDom.documentElement.childNodes
  sID = node.getAttribute(pDomKey) & ""
  If sID <> "" Then
   If InStr(sIDs, "," & sID & ",") = 0 Then
    sIDs = sIDs & sID & ","
   End If
  End If
 Next
 
 If sIDs <> "" Then
  If TypeName(pFolder) = "Folder" Then
   Set fld = pFolder
  Else
   Set fld = dSession.FoldersGetFromId(CLng(pFolder))
  End If
  Set domJoin = fld.Search(pFolderKey & ", " & pFolderFields, pFolderKey & " in (" & _
    Mid(sIDs, 2, Len(sIDs) - 2) & ")", CStr(pFolderKey))

  For Each node In pDom.documentElement.childNodes
   Set nodeJoin = BinarySearchNode(domJoin, pFolderKey, node.getAttribute(pDomKey))
   If Not nodeJoin Is Nothing Then
    For Each attr In nodeJoin.Attributes
     node.setAttribute attr.Name, attr.Value
    Next
   End If
  Next
 End If
End Sub

Function BinarySearchNode(pDom, pField, pValue)
 'El dom debe venir ordenado por pField
 'todo: ver si se puede ordenar rapido mediante XSL o algo asi

 Dim L, R, m, node, mValue
  
 Set BinarySearchNode = Nothing 
 If IsNumeric(pValue) Then pValue = CLng(pValue)
 L = 0
 R = pDom.documentElement.childNodes.length - 1
 
 Do While L <= R
  m = Int((L + R) / 2)
  Set node = pDom.documentElement.childNodes(m)
  mValue = node.getAttribute(pField) & ""
  If IsNumeric(mValue) Then mValue = CLng(mValue)
  If mValue < pValue Then
   L = m + 1
  ElseIf mValue > pValue Then
   R = m - 1
  Else
   Set BinarySearchNode = node
   Exit Function
  End If
 Loop
End Function

No hay comentarios:

Publicar un comentario