Pseudo-Objects in Active Server Pages
<%
' Car.inc -- Functions and subroutines for Car pseudo-object.
' Car_New(): Creates an empty Car object.
'
function Car_New()
dim objCar
set objCar = Server.CreateObject("Scripting.Dictionary")
objCar.Add "VIN", ""
objCar.Add "make", ""
objCar.Add "model", ""
objCar.Add "color", ""
objCar.Add "year", 0
set Car_New = objCar
end function
' Car_Load(): Loads a list of Car objects from the database for the given
' condition in the given order.
'
function Car_Load(strConn, where, orderBy)
dim recordset
dim list
dim car
' Create the list.
set list = Server.CreateObject("Scripting.Dictionary")
' Run the database query.
set recordset = DB_Query(strConn, "*", "Cars", where, orderBy)
' Loop through the recordset to load the list.
do while not recordset.EOF
' Create an empty car.
set car = Car_New()
' Get the field values.
car("VIN") = recordset("VIN").Value
car("make") = recordset("make").Value
car("model") = recordset("model").Value
car("color") = recordset("color").Value
car("year") = CInt(recordset("year").Value)
' Add the car to the list.
list.Add car("VIN"), car
' Move to the next record.
recordset.MoveNext
loop
' Close the recordset.
recordset.Close
' Return the list.
set Car_Load = list
end function
' Car_Insert(): Inserts the given Car object into the database.
'
function Car_Insert(strConn, objCar)
dim fields
dim values
fields = "VIN, make, model, color, year"
values = _
"'" & objCar("VIN") & "', " & _
"'" & objCar("make") & "', " & _
"'" & objCar("model") & "', " & _
"'" & objCar("color") & "', " & _
objCar("year")
Car_Insert = DB_Insert(strConn, "Cars", fields, values)
end function
' Car_Update(): Updates the given Car object in the database.
'
function Car_Update(strConn, objCar)
dim values
values = _
"make = '" & objCar("make") & "', " & _
"model = '" & objCar("model") & "', " & _
"color = '" & objCar("color") & "', " & _
"year = " & objCar("year")
Car_Update = DB_Update(strConn, "Cars", values, _
"VIN = '" & objCar("VIN") & "'")
end function
' Car_Delete(): Deletes the given Car object from the database.
'
function Car_Delete(strConn, objCar)
Car_Delete = DB_Delete(strConn, "Cars", _
"VIN = '" & objCar("VIN") & "'")
end function
%>
Page 5 of 11
This article was originally published on October 14, 1999