for i=0 to Ubound(KeysArray)-1
Key=KeysArray(i,0)
Item=Request(md5(Key))
objDict.Add Key,Item
next
Call System_Initialize()
Dim objUser ‘新建對象
Set objUser = new TUser
objUser.Table="Comm_User" ‘指定數(shù)據(jù)表
If objUser.Creat Then ‘建立表的Dictionary對象
objUser.ValidAndTransfer() ‘獲取數(shù)據(jù)、驗證并轉(zhuǎn)入最終的數(shù)據(jù)驅(qū)動層
objUser.Update() ‘更新該表數(shù)據(jù)
End IF
Set objUser = nothing
Call System_Terminate()
SELECT Table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE (TABLE_TYPE <> ‘VIEW‘)
SELECT Column_name,IS_NULLABLE,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = ‘myTableName‘)
strSQL="SELECT Table_name FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE<>‘VIEW‘)"
Set objRS_table= objConn.Execute(strSQL)
do while not objRS_table.EOF
strSQL="SELECT Column_name,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT,CHARACTER_OCTET_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = ‘"& objRS_table("Table_name") &"‘)"
set objRS_column=server.createobject("adodb.recordset")
objRS_column.open strSQL,objConn,1,1
Dim i,TableTemp
i=0
ReDim TableTemp(objRS_column.RecordCount,4)
do while not objRS_column.EOF
TableTemp(i,0)=objRS_column(0)
TableTemp(i,1)=objRS_column(1)
TableTemp(i,2)=objRS_column(2)
TableTemp(i,3)=objRS_column(3)
TableTemp(i,4)=objRS_column(4)
i=i+1
objRS_column.movenext
loop
Set objRS_column= nothing
application(objRS_table(0))=TableTemp
objRS_table.movenext
loop
<%
‘******************************************************
‘底層A:對DataBase操作的封裝。
‘返回錯誤代碼頭:DB
‘******************************************************
Class TDBDriver
Public objDict
Private Table
Private KeysArray
Private ItemArray
Private objRS
Private strSQL
Private SQL_Start
Private SQL_Main
Private SQL_Join
Private SQL_Where
Private SQL_Order
Private SQL_Group
Public PageSize
Private ErrorMsg
‘插入一條數(shù)據(jù)或者更新指定的條目
Public Function Update()
If objDict.Item(KeysArray(0,0))="" Then
SQL_Start="Insert into "& Table
SQL_Main=" ("
‘從有數(shù)據(jù)的部分對應(yīng)更新入數(shù)據(jù)庫
For i=1 to objDict.Count-1
If Not ItemArray(i)="" Then
SQL_Main= SQL_Main & KeysArray(i,0) &","
End If
Next
‘去掉最后一個“,”
SQL_Main=Left(SQL_Main,Len(SQL_Main)-1)
SQL_Main= SQL_Main &") Values("
For i=1 to objDict.Count-1
If Not ItemArray(i)="" Then
If KeysArray(i,1)="int" Then
SQL_Main= SQL_Main & objDict.Item(KeysArray(i,0))
Else
SQL_Main= SQL_Main &"‘"& objDict.Item(KeysArray(i,0)) &"‘"
End if
SQL_Main= SQL_Main &","
End If
Next
‘去掉最后一個“,”
SQL_Main=Left(SQL_Main,Len(SQL_Main)-1)
SQL_Main= SQL_Main &")"
Else
SQL_Start="Update "& Table &" Set "
For i=1 To objDict.Count-1
If Not ItemArray(i)="" Then
SQL_Main= SQL_Main &" ("& KeysArray(i,0) &"="
If KeysArray(i,1)="int" Then
SQL_Main= SQL_Main & objDict.Item(KeysArray(i,0))
Else
SQL_Main= SQL_Main &"‘"& objDict.Item(KeysArray(i,0)) &"‘"
End if
SQL_Main= SQL_Main &"),"
End IF
Next
‘去掉最后一個“,”
SQL_Main=Left(SQL_Main,Len(SQL_Main)-1)
SQL_Where=" Where "& KeysArray(0,0) &"="& objDict.Item(KeysArray(0,0))
End if
strSQL=SQL_Start & SQL_Main & SQL_Where
‘objConn.Execute(strSQL)
Response.Write strSQL
Update=True
End Function
‘刪除數(shù)據(jù)
Public Function Delete()
If objDict.Item(KeysArray(0,0))="" Then Call ShowError(ErrorMsg,01) :Exit Function
SQL_Start="Delete From "& Table
SQL_Where=" Where "& KeysArray(0,0) &"="& objDict.Item(KeysArray(0,0))
strSQL=SQL_Start & SQL_Where
objConn.Execute(strSQL)
Delete=True
End Function
‘返回單條數(shù)據(jù)
Public Function ReturnSingle()
If objDict.Item(KeysArray(0,0))="" Then Call ShowError(ErrorMsg,02) :Exit Function
SQL_Start="Select * From "& Table
SQL_Where=" Where "& KeysArray(0,0) &"="& objDict.Item(KeysArray(0,0))
strSQL=SQL_Start & SQL_Where
Set objRS =objConn.Execute(strSQL)
If objRS.EOF or objRS.BOF Then
Call ShowError(ErrorMsg,03)
Else
For i=1 To Ubound(objDict.Items)-1
objDict.Item(KeysArray(i,0)) = objRS(i)
Next
End IF
Set objRS =Nothing
ReturnSingle=objDict
End Function
‘返回分頁數(shù)據(jù)
‘需要繼承回傳的數(shù)據(jù)
Public Function ReturnList()
‘必須指定頁長、頁數(shù)
If PageSize="" Then Call ShowError(ErrorMsg,0) :Exit Function
End Function
Public Function Transfer(ExternalDict)
Set objDict=ExternalDict.objDict
‘參數(shù)導(dǎo)入,只是為了書寫方便
Table=ExternalDict.Table
KeysArray=ExternalDict.KeysArray
ItemArray=objDict.Items
End Function
Private Sub Class_Initialize()
ErrorMsg="DB"
‘傳入FormDriver處理過的Dictionary
End Sub
Private Sub Class_Terminate()
‘返回FormDriver處理過的Dictionary
End Sub
End Class
‘******************************************************
‘******************************************************
‘底層B:對Dictionary操作的封裝。
‘返回錯誤代碼頭:DC
‘******************************************************
Class TDictionaryDriver
Public Table
Public KeysArray
Public objDict
Private Key
Private Item
Private ErrorMsg
‘根據(jù)系統(tǒng)緩存導(dǎo)入框架
‘報錯信息
Public Function Creat()
‘必須指定數(shù)據(jù)表
If Table="" Then Call ShowError(ErrorMsg,01) :Exit Function
‘必須是系統(tǒng)緩存中已經(jīng)存在的數(shù)據(jù)表
If IsNull(application(Table)) Then Call ShowError(ErrorMsg,02) :Exit Function
‘導(dǎo)入數(shù)據(jù)表框架
KeysArray=application(Table)
End Function
‘根據(jù)數(shù)據(jù)表框架-獲取[過濾]數(shù)據(jù)
‘通過加密的方案解決表單字段和數(shù)據(jù)庫字段的異步問題
Public Function GetRequest()
for i=0 to Ubound(KeysArray)-1
Key=KeysArray(i,0)
Item=Request(md5(Key))
‘Item=Request.QueryString(md5(Key))
‘Item=Request.Form(Key)
objDict.Add Key,Item
next
‘形成Dictionary對象
GetRequest=True
End Function
‘根據(jù)數(shù)據(jù)表框架-效驗獲取的數(shù)據(jù)
Public Function Valid()
‘注入轉(zhuǎn)換和過濾
‘必填項
If objDict.Item(KeysArray(0,0))="" Then
‘對于新增數(shù)據(jù),必填項目不能為空
‘允許更新操作跳過必填項目遍歷進行部分更新
for i=1 to Ubound(KeysArray)-1 ‘跳過主鍵遍歷
If Trim(KeysArray(i,2))="No" And objDict.Item(KeysArray(i,0))="" Then Call ShowError(ErrorMsg,03) :Exit Function
next
End IF
‘字段類型要求
for i=0 to Ubound(KeysArray)-1
‘效驗數(shù)據(jù)類型要求
If KeysArray(i,1)="int" Then
If IsNumeric(objDict.Item(KeysArray(i,0)))=0 Then Call ShowError(ErrorMsg,04) :Exit Function
If objDict.Item(KeysArray(i,0))>2147483647 or objDict.Item(KeysArray(i,0))<-2147483647 Then Call ShowError(ErrorMsg,05) :Exit Function
ElseIf objDict.Item(KeysArray(i,0))<>"" And KeysArray(i,1) ="datetime" Then
If IsDate(objDict.Item(KeysArray(i,0)))=0 Then Call ShowError(ErrorMsg,06) :Exit Function
ElseIF KeysArray(i,1)="bit" Then
If (Not objDict.Item(KeysArray(i,0))=0) And (Not objDict.Item(KeysArray(i,0))=1) Then Call ShowError(ErrorMsg,07) :Exit Function
End If
‘效驗字段長度
If IsNumeric(KeysArray(i,4)) And Len(objDict.Item(KeysArray(i,0)))>=KeysArray(i,4) Then Call ShowError(ErrorMsg,08) :Exit Function
‘默認值
next
Valid=True
End Function
‘從數(shù)據(jù)庫框架和Dictionary還原出完整的表單,該表單需要具體對象類根據(jù)實際需求進行進一步處理
‘通過加密的方案解決表單字段和數(shù)據(jù)庫字段的異步問題
Public Function ReturnForm()
Dim strForm
strForm="<form action="""" method=""post"" id="""& MD5(Table) &"""><ul>" &chr(10)
strForm=strForm &"<li><input id="""& MD5(KeysArray(0,0)) &""" type=""hiddden"" value="""& objDict.Item(KeysArray(0,0)) &"""></li>" &chr(10)
for i=0 to Ubound(KeysArray)-1
‘效驗數(shù)據(jù)類型
If KeysArray(i,1)="int" Then
‘Radio
‘Select
‘Checkbox
strForm=strForm &"<li><input id="""& MD5(KeysArray(i,0)) &""" type=""text"" value="""& objDict.Item(KeysArray(i,0)) &"""></li>"&chr(10)
ElseIf KeysArray(i,1) ="datetime" Then
strForm=strForm &"<li><input id="""& MD5(KeysArray(i,0)) &""" type=""text"" value="""& objDict.Item(KeysArray(i,0)) &"""></li>"&chr(10)
ElseIF KeysArray(i,1)="bit" Then
strForm=strForm &"<li><input id="""& MD5(KeysArray(i,0)) &""" type=""text"" value="""& objDict.Item(KeysArray(i,0)) &"""></li>"&chr(10)
ElseIF KeysArray(i,1)="text" Then
strForm=strForm &"<li><textarea id="""& MD5(KeysArray(i,0)) &""" type=""text"">"& objDict.Item(KeysArray(i,0)) &"</textarea></li>"&chr(10)
End If
next
strForm=strForm &"</ul><form>" &chr(10)
Response.Write strForm
ReturnForm=True
End Function
Private Sub Class_Initialize()
‘創(chuàng)建Dictionary對象
Set objDict=CreateObject("Scripting.Dictionary")
ErrorMsg="DC"
End Sub
Private Sub Class_Terminate()
‘釋放Dictionary對象
Set objDict=nothing
End Sub
End Class
‘******************************************************
‘交互層,根據(jù)實際需要定制結(jié)合對Dictionary和DataBase的操作,定義針對對象的直接操作。
‘返回錯誤代碼頭:UR
‘******************************************************
Class TUser
Public Table
Private objDictionary
Private objDataBase
Private ErrorMsg
Public Function Update()
iF objDataBase.Update Then Update=True
End Function
Public Function Delete()
iF objDataBase.Delete Then Delete=True
End Function
Public Function DrawForm()
iF objDictionary.ReturnForm Then DrawForm=True
End Function
Public Function ValidAndTransfer()
objDictionary.GetRequest()
If objDictionary.Valid Then
Call objDataBase.Transfer(objDictionary)
Else
Call ShowError(ErrorMsg,01) :Exit Function
End If
End Function
Public Function Creat()
If Not Table="" Then
objDictionary.Table=Table
objDictionary.Creat()
End If
Creat=True
End Function
Private Sub Class_Initialize()
ErrorMsg="UR"
Set objDictionary= new TDictionaryDriver
Set objDataBase = new TDBDriver
End Sub
Private Sub Class_Terminate()
Set objDictionary= Nothing
Set objDataBase = nothing
End Sub
End Class
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
Call System_Initialize()
Dim objUser
Set objUser = new TUser
objUser.Table="Comm_User"
If objUser.Creat Then
‘objUser.ValidAndTransfer()
‘objUser.Update()
Call objUser.DrawForm()
End IF
Set objUser = nothing
Call System_Terminate()