Data Access Object
- Code : Tout sélectionner
Classe LotusScrpit permettant plusieurs type de recherche :
methode search
methode getdocumentbykey
methode @dblookup....
Dites moi ce que vous en penser...
'OpenNotesDAO:
Option Public
Option Declare
' See Declarations for some documentation
' See Initialize to enable / disable auto initialization
' //////////// OpenNotesDAO
'' - added function setDocument
' - changed class name to ensure the name space is somewhat unique
' - added auto initialization structures and flags - allows auto instatiaiton as OpenNotesDao unless flag set false
'
' What: a simple database wrapper that combines many basic Notes lookup methods in one object, and adds a few that don't exist
' Just dim myDAO as new OpenNotesDAO(notesDatabaseHandle) and call the method you need.
' Methods:
' getDocuments(query As Variant, cutoff As Variant, rowlimit As Variant) As notesdocumentcollection
' - wraps db.search(queryFormula, cutoffDate, numberOfResults)
' getDocument(query As Variant, cutoff As Variant, rowlimit As Variant,nth As Variant) As NotesDocument
' - wraps db.search as above but then returns the nth document from the resulting document collection
' getDocumentByKey(viewname As Variant,key As Variant, strict as Boolean) As NotesDocument
' - wraps view.getDocumentByKey
' dbLookup(viewname As String,key As String, fieldname As String, failSilent As Integer) as String
' - functions just like @dblookup
' getColumnFromView(column As Integer, viewName As String) As Variant
' - gets a coulmn from the specified view, returns a list of strings
' setDocument(doc as NotesDocument) as Variant
' - creates an image of the document (all items are passed through) in the database (as instantiated) and saves it
'//////////// Auto Instantiation ////////////
' IMPORTANT - see the Initialize sub if you want to instatiate this yourself
Dim autoInit As Boolean
Dim OpenNotesDao As OpenNotesDAO
'//////////////////////////////////////// OpenNotesDAO (Data Access Object) /////////////////////////////////////////////
Public Class OpenNotesDAO
Private dc As notesdocumentcollection
Private db As NotesDatabase
Sub New(dataSource As NotesDatabase)
On Error Goto eh
Set db = dataSource
Exit Sub
eh:
Dim myErr As New Throwable(Me)
Resume ends
ends:
End Sub
'/////////// Function getDocs ////////////
Function getDocuments(query As Variant, cutoff As Variant, rowlimit As Variant) As notesdocumentcollection
On Error Goto eh
Dim dc As notesdocumentcollection
' input QC
Dim tmpq As String
Dim tmpdate As NotesDateTime
Dim tmplimit As Integer
tmpq = Cstr(query)
Set tmpdate = cutoff
tmplimit = Cint(rowlimit)
Set dc = Me.db.Search(tmpq, tmpdate, tmplimit)
Set getDocuments = dc
Exit Function
eh:
Dim myErr As New Throwable(Me)
Resume ends
ends:
End Function
'/////////// Function getDocument ////////////
Function getDocument(query As Variant, cutoff As Variant, rowlimit As Variant,nth As Variant) As NotesDocument
On Error Goto eh
Dim dc As notesdocumentcollection
' input QC
Dim tmpq As String
Dim tmpdate As NotesDateTime
Dim tmplimit As Integer
Dim tmpnth As Integer
tmpq = Cstr(query)
Set tmpdate = cutoff
tmplimit = Cint(rowlimit)
tmpnth = Cint(nth)
Set dc = Me.db.Search(tmpq, tmpdate, tmplimit)
Set getDocument = dc.GetNthDocument(tmpnth)
Exit Function
eh:
Dim myErr As New Throwable(Me)
Resume ends
ends:
End Function
'/////////// Function getDocument ByKey////////////
Function getDocumentByKey(viewname As Variant,key As Variant, strict As Boolean) As NotesDocument
On Error Goto eh
Dim view As notesview
' input QC
Dim tmpview As String
Dim tmpkey As String
tmpview = Cstr(viewname)
tmpkey = Cstr(key)
Set view = Me.db.getview(tmpview)
If view Is Nothing Then
Error 7002, "Can't get requested view: " + tmpview
Dim viewError As New Throwable(Me)
Exit Function
End If
Set getDocumentByKey = view.GetDocumentByKey(tmpkey,strict)
Exit Function
eh:
Dim myErr As New Throwable(Me)
Resume ends
ends:
End Function
'/////////// Function dbLookup - similar to above but returns a field////////////
Function dbLookup(viewname As String,key As String, fieldname As String, failSilent As Integer) As String
On Error Goto eh
Dim view As notesview
' input QC
Dim tmpdoc As notesdocument
Dim tmpDc As NotesDocumentCollection
Dim returnList List As Variant
Dim x As Integer
Set view = Me.db.getview(viewname)
If view Is Nothing Then
Error 7002, "Can't get requested view: " + viewname
Dim viewError As New Throwable(Me)
Exit Function
End If
Set tmpdoc = view.GetDocumentByKey(key,True)
If tmpdoc Is Nothing Then
If Not(failSilent=1) Then
Error 7009, {Error: Could not find the requested document with a lookup key of "} + key + {"}
Else
returnList(0) = ""
dbLookup = returnList(0)
Exit Function
End If
Else
If (tmpdoc.HasItem(fieldname)) Then
returnList(0) = Cstr(tmpdoc.GetFirstItem(fieldname).values(0))
Else
Error 7010, {Error: Returned document did not contain specified item, "} + fieldname + {". }
End If
dbLookup = returnList(0)
End If
Exit Function
eh:
Dim myErr As New Throwable(Me)
Resume ends
ends:
End Function
'/////////// Function getColumn ////////////
' returns a list of strings
Function getColumnFromView(column As Integer, viewName As String) As Variant
On Error Goto eh
Dim tmpVar List As String
Dim view As NotesView
Dim viewNav As NotesViewNavigator
Set view = Me.db.getView(viewName)
Set viewNav = view.CreateViewNav
Dim pointer As Integer
pointer = 0
Dim cursor As NotesViewEntry
Set cursor = viewNav.GetFirst()
While Not (cursor Is Nothing)
tmpVar(pointer) = Cstr( cursor.ColumnValues(column) )
pointer = pointer +1
Set cursor = viewNav.GetNext(cursor)
Wend
getColumnFromView = tmpVar
Exit Function
eh:
Dim myErr As New Throwable(Me)
Resume ends
ends:
End Function
'/////////// Function setDocument ////////////
' creates an image of the document in the database
Function setDocument(doc As notesDocument) As Variant
On Error Goto eh
Dim tmpdoc As notesdocument
Set tmpdoc = Me.db.createDocument()
' remapping the document allows us to esure we write the document to the proper database (the parent of this class, not the passed in document)
Forall item In doc.Items
Call tmpdoc.AppendItemValue( item.name, item.values)
End Forall
Call tmpdoc.Save (True,True)
setDocument = True
Exit Function
eh:
Dim myErr As New Throwable(Me)
setDocument = False
Resume ends
ends:
End Function
End Class
' Centralized error throwing
Class Throwable
'
Sub New(o As Variant)
If Isobject(o) Then
Error Err(), Typename(o) + "." + Lsi_info(12) + ":" + Erl() + "|" + Error
Else
Error Err(), Lsi_info(12) + ":" + Erl() + "|" + Error
End If
End Sub
End Class
Sub Initialize
' Set this to False if you would like to specify a database manually via the constructor (dim myDAO as New OpenNotesDOA(myDb) )
autoInit = True
' Automatically instatiates the class to the local database.
If autoInit Then
Dim s As New NotesSession
Dim db As notesdatabase
Set db = s.CurrentDatabase
Set OpenNotesDAO = New OpenNotesDAO(db)
End If
End Sub