<% ' Entity.inc -- Functions and subroutines for Entity pseudo-objects. ' Entity_New() creates a new Entity. ' function Entity_New() dim obj dim fields dim defaults dim i ' Create the empty object. set obj = Server.CreateObject("Scripting.Dictionary") ' Add the base fields. obj.Add "_Table", "" obj.Add "_PrimaryKey", "" set Entity_New = obj end function ' Entity_Load() loads a list of objects from the database. ' function Entity_Load(strConn, emptyObj, fields, where, orderBy) dim list dim recordset dim obj dim fieldNames dim value dim key dim i ' Get field names. fieldNames = emptyObj.Keys() ' Create the list and run the query. set list = Server.CreateObject("Scripting.Dictionary") set recordset = DB_Query(strConn, fields, emptyObj("_Table"), _ where, orderBy) ' Load the records into the list. do while not recordset.EOF set obj = Entity_Clone(emptyObj) for i = 0 to UBound(fieldNames) if Left(fieldNames(i), 1) <> "_" then value = Entity_ConvertType( _ DB_NullVal( _ recordset(fieldNames(i)).Value, emptyObj(fieldNames(i))), _ TypeName(obj(fieldNames(i))) ) obj(fieldNames(i)) = value end if next ' Add the object to the list. key = recordset(emptyObj("_PrimaryKey")).Value if TypeName(key) <> "String" then key = CStr(key) end if list.Add key, obj ' Move to the next record. recordset.MoveNext loop ' Close the recordset. recordset.Close ' Return the list. set Entity_Load = list end function ' Entity_Insert() inserts the given object into the database. ' function Entity_Insert(strConn, obj) dim fields dim values dim keys dim data dim i ' Build the fields and values clauses. fields = "" values = "" keys = obj.Keys() for i = 0 to UBound(keys) ' If not an internal key ... if Left(keys(i), 1) <> "_" then if fields <> "" then fields = fields & "," end if fields = fields & "`" & keys(i) & "`" ' Add value. data = obj(keys(i)) if values <> "" then values = values & "," end if if (TypeName(data) = "String") or _ (TypeName(data) = "Date") then values = values & "'" & data & "'" else values = values & data end if end if next ' Insert the record. Entity_Insert = DB_Insert(strConn, obj("_Table"), fields, values) end function ' Entity_Update() updates the given object in the database. ' function Entity_Update(strConn, obj) dim assignments dim items dim keys dim data dim where dim i assignments = "" keys = obj.Keys() items = obj.Items() for i = 0 to UBound(keys) if (Left(keys(i), 1) <> "_") and _ (keys(i) <> obj("_PrimaryKey")) then ' Add assignment. data = items(i) if assignments <> "" then assignments = assignments & "," end if assignments = assignments & "`" & keys(i) & "` = " if (TypeName(data) = "String") or _ (TypeName(data) = "Date") then assignments = assignments & "'" & data & "'" else assignments = assignments & data end if end if next ' Build where clause. where = obj("_PrimaryKey") & " = " data = obj(obj("_PrimaryKey")) if (TypeName(data) = "String") or _ (TypeName(data) = "Date") then where = where & "'" & data & "'" else where = where & data end if ' Update database. Entity_Update = DB_Update(strConn, obj("_Table"), assignments, where) end function ' Entity_Delete() deletes the given object from the database. ' function Entity_Delete(strConn, obj) dim data dim where ' Build where clause. where = obj("_PrimaryKey") & " = " data = obj(obj("_PrimaryKey")) if (TypeName(data) = "String") or _ (TypeName(data) = "Date") then where = where & "'" & data & "'" else where = where & data end if ' Delete record. Entity_Delete = DB_Delete(strConn, obj("_Table"), where) end function ' Entity_Clone() clones the given object. ' function Entity_Clone(obj) dim newObj dim keys dim i ' Create an empty object. set newObj = Server.CreateObject("Scripting.Dictionary") keys = obj.Keys() for i = 0 to UBound(keys) newObj.Add keys(i), obj(keys(i)) next set Entity_Clone = newObj end function ' Entity_URLEncode() encodes the given Dictionary object into query ' string format (e.g. a=1&b=2) using the given name as a tag for the object ' fields. ' function Entity_URLEncode(name, obj) dim data dim keys dim i ' Get the keys (field names) of the object. data = "" keys = obj.Keys() ' Loop through the keys. For each, add the key name and the key value to ' the query string data. The given object name is prepended to the key ' name, so that it can be decoded later and so that multiple objects can ' be encoded without field name collisions. (Basically, this allows each ' object to have its own "name space" in the query string.) ' ' Here is an example: ' ' name = "article" ' key = "title" ' article("title") = "foo" ' ' => data = "article_title=foo" for i = 0 to UBound(keys) data = data & _ Server.URLEncode(name & "_" & keys(i)) & "=" & _ Server.URLEncode(CStr(obj(keys(i)))) ' If there is more than one keys, append "&", which is the field ' separator in a query string. if i < UBound(keys) then data = data & "&" end if next ' Return the query string data. Entity_URLEncode = data end function ' Entity_URLDecode() decodes a object from a query string. When it is ' done the given object contains the decoded data. The fields defined in the ' given object are extracted from the query string of the given request ' object and stored in the given object. The given object name is used as ' a tag for the object fields and must the name (case-sensitive) that was ' used to encode the object originally. ' sub Entity_URLDecode(req, objectName, obj) dim keys dim fieldName dim fieldData dim fieldValue dim i ' Get the keys (field names) from the object. keys = obj.Keys() ' Loop through the fields. For each, decode the field and its data from ' the given request object and store the data in the given object. for i = 0 to UBound(keys) ' Get the field name. fieldName = keys(i) ' Get the field data from the request object. fieldData = req(objectName & "_" & fieldName) ' Convert the field data from string to the type of the object field. fieldValue = Entity_ConvertType(fieldData, TypeName(obj(fieldName))) ' If the type conversion did not work (null was returned), then just ' store the data as-is. if IsNull(fieldValue) then fieldValue = fieldData end if ' Store the field in the given object. obj(fieldName) = fieldValue next end sub ' Entity_WriteInternalAttributes() writes all attributes that start with "_" as ' hidden form fields. ' sub Entity_WriteInternalAttributes(obj, prefix) dim keys dim i keys = obj.Keys() for i = 0 to UBound(keys) if Left(keys(i), 1) = "_" then Response.Write _ "" & vbCrLf end if next end sub ' Entity_ConvertType() converts the given value to the given type. ' function Entity_ConvertType(value, toType) dim result select case toType case "String" result = CStr(value) case "Date" result = CDate(value) case "Integer" result = CInt(value) case "Long" result = CLng(value) case "Double" result = CDbl(value) end select Entity_ConvertType = result end function %>