In this article, I describe pseudo-objects and illustrate how to program with them.
Tools & ResourcesThe following tools and resources are recommended for testing the example programs that are presented in this article:
Note: Having access to a database is not absolutely necessary. With just a little work, the sample programs that I present can be modified so that they don’t access a database. Basic Pseudo-ObjectsMany ASP programs are written in VBScript, which does not provide a way to create user-defined objects (unlike VB). That is, in VBScript you can’t define your own classes then create objects from those classes. However, ASP has an object called Dictionary that is similar to an Associative array in Perl or a Hashtable in Java. It provides a mechanism by which you can store key-value pairs. Here [“DictionaryObj.asp”] is a simple program that demonstrates using the Dictionary object. It creates a Dictionary object, adds some items to it, then displays the contents of the object. With the Dictionary object you can create “objects” with dynamically defined fields. They are not real objects, because they don’t have methods, but are useful nonetheless. Thus, I call them “pseudo-objects”. A simple example program that illustrates a basic pseudo-object called “Car” is here [“BasicObj.asp”]. It creates a new Car pseudo-object, sets the fields, then displays the object. Even though methods cannot be defined for pseudo-objects, functions and subroutines can be written that manipulate pseudo-objects in a similar manner. For example, the Car_New() function in “BasicObj.asp” creates and returns a New Car object. The functions and subroutines for a pseudo-object can be packaged together in an include file to create an encapsulated unit that is similar a complete user-defined object. You will see an example of this in the next section. Database ObjectsDatabase objects provide a layer of separation between the application and the database, because programs only have to deal with the objects, not the database. One way to implement database pseudo-objects is to write functions and subroutines that retrieve, create, update, and delete objects that are stored in a database. A program that illustrates a database version of the Car pseudo-object shown in the previous section is here [“DatabaseObj.asp”]. It creates a few Car pseudo-objects in the database, loads a list of the cars that match given criteria, shows the list, sets the color of each car in the list to blue, shows the list again, deletes one car, loads a list of all cars, then shows the list one final time. Note that the “DatabaseObj.asp” program assumes the following:
The include file that contains the implementation of the Car pseudo-object is here here [“Car.inc.txt”]. An include file that contains some database functions is here [“db.inc.txt”]. Entity ObjectsAfter using pseudo-objects a couple of times, I realized that I had redundant code in each object that could be abstracted. So, I created a generic pseudo-object, which I called an Entity. Then, I implemented other pseudo-objects in my applications as compositions of the Entity pseudo-object. Essentially, this is one way to achieve the equivalent of inheritance in a programming language that is not object-oriented. (You can do something similar in VB to fake inheritance.)
The code for the Entity pseudo-object is here [“Entity.inc”]. An implementation of the Car pseudo-object that has been re-written as a composition of the Entity pseudo-object is here [“Car2.inc.”]. Notice that each function or subroutine is merely a wrapper for the corresponding function or subroutine of the Entity pseudo-object. If you modify “DatabaseObj.asp” so that it includes “Entity.inc” and “Car2.inc” instead of “Car.inc”, it should run correctly without any other changes. (Note: Be sure “Entity.inc” is included before “Car2.inc”.) Field InspectionSince pseudo-objects are based on the ASP Dictionary object, inspection of the fields is possible. First, the field names are just the keys in the Dictionary and can be retrieved with the Keys() method. Second, the field types can be determined with the TypeName() function. Here [“FieldInspection.asp”] is a simple example. With field inspection, more advanced operations are possible, such as serialization and simple cloning. These are presented in the next two sections. SerializationAs I used pseudo-objects for more applications, I discovered that I needed to pass them from one program to another, or create them from an input form. So, I implemented a couple of routines to serialize and deserialize pseudo-objects. The code for them can be seen in “Entity.inc”. Basically, Entity_URLEncode() serializes an object to a string that is URL encoded, so that it can be passed as a series of URL parameters. Entity_URLDecode() reverses the process, decoding data from a given Request object (an ASP object that contains HTTP request data) into an empty pseudo-object. The fields of the pseudo-object are determined by using field inspection to examine the empty pseudo-object. A program that illustrates serialization and deserialization of a pseudo-object is here [“SerialObj.asp.”]. It creates a pseudo-object, presents a form for changing the fields. When the user clicks the “Submit” button, the Car object will be updated and show in the form again. When the user clicks on the “Revert” link, any changes will be discarded and the previous car object will be displayed in the form. CloningAt some point in development of an application, I determined that I needed to make a copy of a pseudo-object. So, I wrote Entity_Clone(), a function that returns a copy of a given object. Note, though, that Entity_Clone() only makes a shallow copy. If the given object contains references to other objects, those objects will not be cloned. Entity_Clone() uses field inspection to examine the given object and Determine its field names, then uses the field names to copy the field values to a new object. You can see the code for it in “Entity.inc”. Also, here [“CloneObj.asp”] is a simple program that creates a pseudo-object, clones it, then displays both objects. SummaryIn this article, I have described pseudo-objects, illustrated how to use them, and shown some advanced operations that can be performed with them. Hopefully, you have seen how they can provide a good layer of separation between ASP programs and a database.
About the author:Thornton Rose is a software developer who lives and works in Atlanta, Georgia. He can be reached via e-mail at trose@avana.net.
|
%
‘ DictionaryObj.asp – Simple program to show usage of Dictionary object.
‘ Force variables to be declared.
option explicit
‘ Declare variables.
dim objFoo
‘ Create empty Dictionary object.
set objFoo = Server.CreateObject(“Scripting.Dictionary”)
‘ Add items.
‘ Note: The item keys are case-sensitive. If you add an item with the key
‘ “color”, you cannot retrieve it with the key “Color”.
objFoo.Add “color”, “blue”
objFoo.Add “size”, 1
‘ Show object.
Response.Write “
” & vbCrLf” & vbCrLf
Response.Write “objFoo.color = ” & objFoo(“color”) & vbCrLf
Response.Write “objFoo.size = ” & objFoo(“size”) & vbCrLf
Response.Write “
%>
<%
' BasicObj.asp - Simple program to show pseudo-object called "Car".
' Force variables to be declared.
option explicit
' Declare variables.
dim objCar
' Create a Car pseudo-object.
set objCar = Car_New()
objCar("make") = "Toyota"
objCar("model") = "Corolla"
objCar("year") = 1991
' Show the car.
Response.Write "objCar: " & objCar("year") & " " & objCar("make") & " " & _
objCar("model") & vbCrLf
' -----------------------------------------------------------------------------
' Car_New(): Creates a new, empty Car object.
'
function Car_New()
dim objCar
' Create Dictionary object as base.
set objCar = Server.CreateObject("Scripting.Dictionary")
' Add fields.
objCar.Add "make", ""
objCar.Add "model", ""
objCar.Add "year", 0
' Return the new, empty Car.
set Car_New = objCar
end function
%>
<%
' DatabaseObj.asp - Program to show database version of pseudo-object called
' "Car".
option explicit
%>
NOTE: include file=”db.inc”
NOTE: include file=”car.inc”
<% dim strConn ' Connection string dim carList ' List of cars dim car ' Car object dim items ' Array of car objects dim result ' Result code dim i ' Counter ' Specify database connection. strConn = "DSN=CarDB;UID=;PWD=" ' Put some car objects in the database. CreateCars strConn Response.Write vbCrLf ' Load and show a list of Cars that are newer than 1990, ordered by make. set carList = Car_Load(strConn, "year > 1990″, “make, model, year”)Response.Write “Cars newer than 1990:” & vbCrLf
ShowCarList carList
Response.Write vbCrLf‘ Make the 1990 and later cars blue.
items = carList.Items()
for i = 0 to UBound(items)
set car = items(i)
car(“color”) = “blue”
result = Car_Update(strConn, car)
next‘ Show the cars again.
Response.Write “Blue cars newer than 1990:” & vbCrLf
ShowCarList carList
Response.Write vbCrLf‘ Delete the 1997 Geo Prizm.
set car = carList(“1003″)
result = Car_Delete(strConn, car)ShowCar car
Response.Write ” deleted.” & vbCrLf & vbCrLf‘ Load the list of all cars, then show it.
set carList = Car_Load(strConn, “”, “make, model, year”)
Response.Write “All cars that are left:” & vbCrLf
ShowCarList carList‘ —————————————————————————–
‘ CreateCars(): Populate the database with some cars.
‘
sub CreateCars(strConn)
dim objCar‘ Car 1
set objCar = Car_New()
objCar(“VIN”) = “1001”
objCar(“make”) = “Toyota”
objCar(“model”) = “Corolla”
objCar(“color”) = “white”
objCar(“year”) = 1991Car_Insert strConn, objCar
ShowCar objCar
Response.Write ” inserted.” & vbCrLf‘ Car 2
set objCar = Car_New()
objCar(“VIN”) = “1002”
objCar(“make”) = “Volkswagen”
objCar(“model”) = “Beetle”
objCar(“color”) = “red”
objCar(“year”) = 1965Car_Insert strConn, objCar
ShowCar objCar
Response.Write ” inserted.” & vbCrLf‘ Car 3
set objCar = Car_New()
objCar(“VIN”) = “1003”
objCar(“make”) = “Geo”
objCar(“model”) = “Prizm”
objCar(“color”) = “green”
objCar(“year”) = 1997Car_Insert strConn, objCar
ShowCar objCar
Response.Write ” inserted.” & vbCrLf
end sub‘ ShowCarList(): Show the given list of cars.
‘
sub ShowCarList(list)
dim i
dim items
dim car‘ Get an array of the items.
items = list.Items()
‘ Loop through the array.
for i = 0 to UBound(items)
‘ Get the car.set car = items(i)
‘ Show the car.
Response.Write “car ” & (i + 1) & ” = ”
ShowCar car
Response.Write vbCrLf
next
end sub‘ ShowCar(): Shows the given car.
‘
sub ShowCar(car)
Response.Write “[” & car(“year”) & ” ” & car(“make”) & ” ” & _
car(“model”) & “, ” & car(“color”) & “]”
end sub
%>
<%
' 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
%>
<%
' 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
%>
<%
' Car2.inc -- Functions and subroutines for Car pseudo-object.
' (Modified version of Car.inc to use Entity as base object.)
' Car_New(): Creates an empty Car object.
'
function Car_New()
dim objCar
' Create empty Entity.
set objCar = Entity_New()
' Set "hidden" fields.
objCar("_Table") = "Cars"
objCar("_PrimaryKey") = "VIN"
' Add Car fields.
objCar.Add "VIN", ""
objCar.Add "make", ""
objCar.Add "model", ""
objCar.Add "color", ""
objCar.Add "year", 0
' Return object.
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)
set Car_Load = Entity_Load(strConn, Car_New(), "*", where, orderBy)
end function
' Car_Insert(): Inserts the given Car object into the database.
'
function Car_Insert(strConn, objCar)
Car_Insert = Entity_Insert(strConn, objCar)
end function
' Car_Update(): Updates the given Car object in the database.
'
function Car_Update(strConn, objCar)
Car_Update = Entity_Update(strConn, objCar)
end function
' Car_Delete(): Deletes the given Car object from the database.
'
function Car_Delete(strConn, objCar)
Car_Delete = Entity_Delete(strConn, objCar)
end function
%>
<%
' FieldInspection.asp - Example program to show inspection of the fields of a
' pseudo-object.
option explicit
%>
NOTE: include file=”db.inc”
NOTE: include file=”Entity.inc”
NOTE: include file=”Car2.inc”
<% dim car dim fieldNames dim field dim i ' Create Car and get array of fields. set car = Car_New() fieldNames = car.Keys() ' Show fields and field types. Response.Write "” & vbCrLf” & vbCrLf
Response.Write “Fields of Car pseudo-object:” & vbCrLf & vbCrLffor i = 0 to UBound(fieldNames)
field = car(fieldNames(i))
Response.Write fieldNames(i) & “: ” & TypeName(field) & vbCrLf
nextResponse.Write “
%>
<%
' SerialObj.asp - Program to illustrate serialization/deserialization of a
' pseudo-object.
option explicit
%>
NOTE: include file=”db.inc”
NOTE: include file=”entity.inc”
NOTE: include file=”car2.inc”
<%
dim car
dim fieldNames
dim field
dim i
dim thisPage
thisPage = Request.ServerVariables("SCRIPT_NAME")
set car = Car_New()
if Request("event") <> “” then
Entity_URLDecode Request, “car”, car
end if
%>
<%
' CloneObj.asp - Example program to show cloning of a pseudo-object.
option explicit
%>
NOTE: include file=”db.inc”
NOTE: include file=”Entity.inc”
NOTE: include file=”Car2.inc”
<% dim car1 dim car2 ' Create a Car. set car1 = Car_New() car1("make") = "Volkswagen" car1("model") = "Beetle" car1("year") = 1965 car1("color") = "yellow" ' Clone the car. set car2 = Entity_Clone(car1) ' Show the cars. Response.Write "” & vbCrLf” & vbCrLfResponse.Write “car 1: [” & car1(“year”) & ” ” & car1(“make”) & ” ” & _
car1(“model”) & “, ” & car1(“color”) & “]” & vbCrLf
Response.Write “car 2: [” & car2(“year”) & ” ” & car2(“make”) & ” ” & _
car2(“model”) & “, ” & car2(“color”) & “]” & vbCrLfResponse.Write “
%>
<%
' db.inc -- Database functions and procedures.
' DB_NullVal() is equivalent to the Oracle nvl() function. It returns the
' given value if the given variable is null. Otherwise, it returns the
' variable.
'
function DB_NullVal(var, val)
if IsNull(var) then
DB_NullVal = val
else
DB_NullVal = var
end if
end function
' DB_NextVal() returns the next values of the given sequence.
'
function DB_NextVal(connString, sequenceName)
dim conn
dim rs
dim nextVal
nextVal = 0
set conn = Server.CreateObject("ADODB.Connection")
conn.Open connString
set rs = conn.Execute("select " & sequenceName & ".NextVal from dual")
nextVal = rs("NextVal")
rs.Close
DB_NextVal = nextVal
end function
' DB_Query() runs a query and returns the resulting recordset.
'
function DB_Query(connString, fields, from, where, orderBy)
dim recordset
dim sql
sql = "select " & fields & " from " & from
if where <> “” then
sql = sql & ” where ” & where
end if
if orderBy <> “” then
sql = sql & ” order by ” & orderBy
end if
‘ debug
‘ Response.Write “
” & sql & “”
set recordset = Server.CreateObject(“ADODB.Recordset”)
recordset.Open sql, connString
set DB_Query = recordset
end function
‘ DB_Insert() executes an SQL command to insert records. If the insert
‘ statement completes successfully, a commit is executed.
‘
function DB_Insert(connString, table, fields, values)
dim conn
dim rs
dim sql
dim recordsAffected
DB_Insert = 0
set conn = Server.CreateObject(“ADODB.Connection”)
conn.Open connString
sql = “insert into ” & table & ” (” & fields & “) values(” & values & “)”
conn.Execute sql, recordsAffected
DB_Insert = recordsAffected
if recordsAffected > 0 then
‘ Uncomment for Oracle, if autocommit is not on.
‘ conn.Execute “commit”, recordsAffected
end if
conn.Close
end function
‘ DB_Update() executes SQL to perform a database update. If the update
‘ statement completes successfully, a commit is executed.
‘
function DB_Update(connString, tables, setStatements, where)
dim conn
dim sql
dim recordsAffected
sql = “update ” & tables & ” set ” & setStatements
if where <> “” then
sql = sql & ” where ” & where
end if
‘ debug
‘ Response.Write “
sql = ” & sql & “”
set conn = Server.CreateObject(“ADODB.Connection”)
conn.Open connString
conn.Execute sql, recordsAffected
DB_Update = recordsAffected
if recordsAffected > 0 then
‘ Uncomment for Oracle, it autocommit is not on.
‘ conn.Execute “commit”, recordsAffected
end if
conn.Close
end function
‘ DB_Delete() executes an SQL command to delete records. If the delete
‘ statement completes successfully, a commit is executed.
‘
function DB_Delete(connString, from, where)
dim conn
dim sql
dim recordsAffected
sql = “delete from ” & from
if where <> “” then
sql = sql & ” where ” & where
end if
set conn = Server.CreateObject(“ADODB.Connection”)
conn.Open connString
conn.Execute sql, recordsAffected
DB_Delete = recordsAffected
if recordsAffected > 0 then
‘ Uncomment for Oracle, if autocommit is not on.
‘ conn.Execute “commit”, recordsAffected
end if
conn.Close
end function
‘ DB_BeginTransaction() begins a transaction. (SQL Server)
‘
function DB_BeginTransaction(connString)
dim conn
set conn = Server.CreateObject(“ADODB.Connection”)
conn.Open connString
conn.BeginTrans
set DB_BeginTransaction = conn
end function
%>