📄 db.asp
字号:
<%
Class ImplMocomUtilDB
'--------------------------------------------------------------------
' DB.cls - Database Class
'
' Copyright (c) 2006 - 2008 MOEx Group.
'
'
' last update: 2008/06/16
'
'--------------------------------------------------------------------
' explain 数据库类
'--------------------------------------------------------------------
' Property List
'--------------------------------------------------------------------
' ConnectionString [get|let] - 设置/获取数据库的连接字符串
' - arguments[0] = 连接字符串(type: String)
' ActiveConnection [get] - 获取连接对象(type: Connection)
' DataType [get|let] - 设置/获取数据库类型
' - arguments[0] = 数据库类型(type: Integer),参考WEB-INF/include/declare.asp
' Count [get] - 获取总共执行SQL语句的条数
'--------------------------------------------------------------------
' Method List
'--------------------------------------------------------------------
' Exec - 执行SQL语句并返回影响的行数
' - 返回类型:Long
' - arguments[0] = SQL语句(type: String)
' SafeExec - 安全执行SQL语句,不抛出异常
' - 返回类型:Boolean
' - arguments[0] = SQL语句(type: String)
' MultiExec - 按换行分割一个字符串,并一一执行每行SQL语句
' - 返回类型:
' - arguments[0] = 字符串(type: String)
' Exec2 - 执行SQL语句并返回数据集对象
' - 返回类型:Recordset
' - arguments[0] = SQL语句(type: String)
' Exec3 - 执行SQL语句并返回数据集对象
' - 返回类型:Recordset
' - arguments[0] = SQL语句(type: String)
' - arguments[1] = 游标类型(type: CursorTypeEnum)
' - arguments[2] = 锁定类型(type: LockTypeEnum)
' - arguments[3] = 属性(type: Long)
' HasRow - 执行SQL语句并判断是否存在数据集
' - 返回类型:Boolean
' - arguments[0] = SQL语句(type: String)
' GetRow - 执行SQL语句并返回数据集第一个集合的第一个元素值
' - 返回类型:Variant
' - arguments[0] = SQL语句(type: String)
' GetRows - 执行SQL语句并返回数据集的二维数组
' - 返回类型:Variant(?, ?)
' - arguments[0] = SQL语句(type: String)
' GetLimitSQL - 格式化查询指定条数的SQL语句
' - 返回类型:String
' - arguments[0] = 查询条数(type: Long)
' - arguments[1] = 字段名列表(type: String)
' - arguments[2] = 表名(type: String)
' - arguments[3] = 查询条件(type: String)
' - arguments[4] = 分组条件(type: String
' - arguments[5] = 排序条件(type: String)
' GetIdentity - 获取刚插入行的表中新序列的值
' - 返回类型:Long
' - arguments[0] = 表名(type: String)
'--------------------------------------------------------------------
Private objConn
Private strConn
Private intCount
Private intDataType
Private Sub Class_Initialize()
Set objConn = Nothing
intCount = 0
intDataType = adAccess
End Sub
Private Sub Class_Terminate()
If objConn Is Nothing Then Exit Sub
If objConn.State = adStateOpen Then
objConn.Close
End If
Set objConn = Nothing
End Sub
Public Property Let ConnectionString(ByVal strValue)
strConn = strValue
End Property
Public Property Get ConnectionString()
ConnectionString = strConn
End Property
Public Property Get ActiveConnection()
Call CheckConnect
Set ActiveConnection = objConn
End Property
Public Property Let DataType(ByVal intValue)
intDataType = intValue
End Property
Public Property Get DataType()
DataType = intDataType
End Property
Public Function Exec(ByVal strSQL)
Call CheckConnect
If objConn.State <> adStateOpen Then
Err.Raise vbObjectError + 1, "DB.Exec", "Connect database failed: " & Err.Description
End If
'Response.Write strSQL & vbCrLf
If intDataType = adAccess Then
objConn.Execute JPEncode(strSQL), Exec, adCmdText + adExecuteNoRecords
Else
objConn.Execute strSQL, Exec, adCmdText + adExecuteNoRecords
End If
intCount = intCount + 1
End Function
Public Function SafeExec(ByVal strSQL)
On Error Resume Next
Exec strSQL
SafeExec = CBool(Err.Number = 0)
End Function
Public Sub MultiExec(ByVal strData)
Dim arr, ptr
arr = Split(strData, vbCrLf)
For Each ptr In arr
If Trim(ptr) <> "" Then
Exec ptr
End If
Next
End Sub
Public Function Exec2(ByVal strSQL)
Call CheckConnect
If objConn.State <> adStateOpen Then
Err.Raise vbObjectError + 1, "DB.Exec2", "Connect database failed: " & Err.Description
End If
'Response.Write strSQL & vbCrLf
If intDataType = adAccess Then
Set Exec2 = objConn.Execute(JPEncode(strSQL))
Else
Set Exec2 = objConn.Execute(strSQL)
End If
intCount = intCount + 1
End Function
Public Function Exec3(ByVal strSQL, ByVal intCursor, ByVal intLock, ByVal intOption)
Call CheckConnect
If objConn.State <> adStateOpen Then
Err.Raise vbObjectError + 1, "DB.Exec3", "Connect database failed: " & Err.Description
End If
Set Exec3 = Server.CreateObject("ADODB.RecordSet")
If intDataType = adMySQL Then
Exec3.CursorLocation = adUseClient
End If
'Response.Write strSQL & vbCrLf
If intDataType = adAccess Then
Exec3.Open JPEncode(strSQL), objConn, intCursor, intLock, intOption
Else
Exec3.Open strSQL, objConn, intCursor, intLock, intOption
End If
intCount = intCount + 1
End Function
Public Function HasRow(ByVal strSQL)
Dim rs
Set rs = Exec2(strSQL)
HasRow = CBool(Not rs.EOF)
rs.Close
Set rs = Nothing
End Function
Public Function GetRow(ByVal strSQL)
Dim rs
Set rs = Exec2(strSQL)
If Not rs.EOF Then
GetRow = rs(0)
End If
rs.Close
Set rs = Nothing
End Function
Public Function GetRows(ByVal strSQL)
Dim rs
Set rs = Exec2(strSQL)
If Not rs.EOF Then
GetRows = rs.GetRows()
End If
rs.Close
Set rs = Nothing
End Function
Public Property Get Count()
Count = intCount
End Property
Private Sub CheckConnect()
If objConn Is Nothing Then
Set objConn = Server.CreateObject("ADODB.Connection")
End If
If objConn.State = adStateOpen Then Exit Sub
If strConn = "" Then
Err.Raise vbObjectError + 1, "DB.CheckConnect", "Missing connection string"
End If
objConn.Open strConn
End Sub
Public Function GetLimitSQL(ByVal intCount, ByVal strColumn, ByVal strTable, ByVal strWhere, ByVal strGroup, ByVal strOrder)
Dim strSQL
Select Case intDataType
Case adAccess
strSQL = "SELECT TOP $0 $1 FROM $2$3$4$5"
Case adSQLServer
strSQL = "SELECT TOP $0 $1 FROM $2$3$4$5"
Case adOracle
strSQL = "SELECT $1 FROM (SELECT $1 FROM $2$3$4$5) WHERE ROWNUM<=$0"
Case adMySQL
strSQL = "SELECT $1 FROM $2$3$4$5 LIMIT $0"
End Select
If strWhere <> "" Then strWhere = " WHERE " & strWhere
If strGroup <> "" Then strGroup = " GROUP BY " & strGroup
If strOrder <> "" Then strOrder = " ORDER BY " & strOrder
GetLimitSQL = str_format(strSQL, Array(intCount, strColumn, strTable, strWhere, strGroup, strOrder))
End Function
Public Function Random(ByVal strIndex)
Dim ret
Select Case intDataType
Case adAccess
ret = "RND(" & strIndex & ")"
Case adSQLServer
ret = "NEWID()"
Case adOracle
ret = "SYS_GUID()"
Case adMySQL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -