public function update(optional byval index as variant) as variant
on error goto errorhandler
dim errornum as long
dim odaleng as iobpda.iobpconnection
dim rs as ador.recordset
dim ocuserinfo as cuserinfo
dim vparam() as variant
dim lowerlimit as long
dim upperlimit as long
dim inx as long
dim spname as string
update = false
if me.count = 0 then '没有要更新的项目
update = true
goto cleanexit
end if
'如果提供了索引,则只更新已提供的记录
if not ismissing(index) then
if index < 1 or index > me.count then
err.raise err_userinfoindexoutofrange, _
"coluserinfo.update proc", "index out of range"
goto cleanexit
else
lowerlimit = index
upperlimit = index
end if
else
lowerlimit = 1
upperlimit = me.count
end if
if not safecreateobject(odaleng, obp_da_connection, errornum) then
err.raise err_userinfosafecreatefailed, _
"coluserinfo.update proc", "unable to create " & _
obp_da_connection & ". return code was: " & errornum
goto cleanexit
end if
for inx = lowerlimit to upperlimit
set ocuserinfo = me.item(inx)
if ocuserinfo.dirty then
redim vparam(parmubound, 16)
with ocuserinfo
.classstorage = true
'填充参数数组
vparam(parmname, 0) = parmnamesp_userinfousernumber
vparam(parmtype, 0) = parmtypesp_userinfousernumber
vparam(parmlength, 0) = 0
vparam(parmdir, 0) = adinput
vparam(parmvalue, 0) = .usernumber
<...>
.classstorage = false
end with
'检查是否将现有的记录更新到数据库中
if ocuserinfo.isnew = false then
'更新此记录
spname = sp_u_userinfo
else
'插入此记录
spname = sp_i_userinfo
end if
if not odaleng.execute(securitytoken, spname, vparam, _
rs) then
err.raise err_userinfodalupdatefailed, _
"coluserinfo.update proc", _
"update to database failed. spname was: " & _
spname
goto cleanexit
elseif (not rs is nothing) then
'如果 db 中没有返回任何数据,则设置为 true
if rs.recordcount > 1 then
err.raise err_returntoomanyrecords, _
"coluserinfo.update proc", _
"update to database failed. returned more " & _
"than one record. spname was: " & spname
goto cleanexit
else
'用返回的数据更新 collection
with ocuserinfo
.classstorage =
.usernumber = rs.fields(fn_userinfousernumber)
.userid = rs.fields(fn_userinfouserid)
.usertypeid = rs.fields(fn_userinfousertypeid)
<...>
.isnew = false
.securitytoken = securitytoken
.classstorage = false
.dirty = false
end with
end if
end if
end if 'dirty
set ocuserinfo = nothing
next inx
update = true
<...>
end function
update() 与 delete() 类似,因为它可以更新一个或多个项目。另一个关键功能是能够在插入和更新之间作出选择。再次用到了 isnew 特性,但是在这种情况下,true 表示此方法应该执行插入,而 false 则表示它应该执行更新。class 的信息被打包,然后发送给数据访问对象,并在那里被插入或更新。一旦进行了远程更新或插入,就会更新内部 class 来反映可能已对数据库作了更改的那些变化。例如,recordtimestamp 将发生改变或被创建,因此该 class 需要反映出这个新值。
有关其它函数,请参阅与本文相关的代码样例。
engine 的设计
engine 是位于 collection 和 class 之上的一个层,它是 class 和 collection 的单点入口。engine 成为安全和对象创建控制的绝缘层。它的关键责任如下:
- 充当创建 collection 和 class 的代理
- 在创建对象之前检查安全性
- 将多个 class 打包成一个 collection,并将它们返回给客户
- 为访问 collection 和 class 提供一个标准接口