<% ' +---------------------------------------------------------------------- ' | POPASP [ ASP MVC ] ' +---------------------------------------------------------------------- ' | Copyright (c) 2016 http://popasp.com All rights reserved. ' +---------------------------------------------------------------------- ' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) ' +---------------------------------------------------------------------- ' | Author: popasp <1737025626@qq.com> ' +---------------------------------------------------------------------- '在本类中已经使用了Replace作为方法名,所以在本类中不能再使用Replace函数了,请考虑使用正则的Replace方法吧。 Class POPASP_ACCESS '为了减轻数据库操作类的重量,特将相同代码写到了另外一个文件 'POPASP_REPLACE_CLASS_CODE_POPASP' ' 获取表的所有字段与字段类型 Function getTableFields( tableName ) set getTableFields = tool.getTableFields("SELECT TOP 1 * FROM [" & tableName & "]" ) End Function Function InsertByTable( table,data ) lastSql = tool.getInsertSql( table, data , getTableStructure( table ) ) InsertByTable = tool.InsertByTable( table , data , getPrikey( table ) ) End Function ' 向数据表中插入数据,data为Dictionary对象,其键名与字段名相对应,如果data含主键,须手动删除 Public Function Insert(ByVal table,ByVal data) table = getTableFromInput(table) call handlerData( table,data , 1 ) '剔除不存在的字段与主键 Insert = InsertByTable( table , data ) End Function ' 向数据表中添加数据 Public Property Get Add() call parseOptions '解析参数 if is_empty(parsedOptions("table")) then Call setPoptsTable( parsedOptions ) end if if isEmpty( parsedOptions("data") ) Then set parsedOptions("data") = POP_MVC.Form2Dict() end If if ( not is_empty( parsedOptions("data") ) ) Then '剔除不存在的字段与主键 call handlerData( parsedOptions("table"),parsedOptions("data") , 1 ) Add = InsertByTable( parsedOptions("table") , parsedOptions("data") ) else Call POP_MVC.error( "POPASP_ACCESS.Add" ) end If call ResumeOpts End Property ' Replace,无则添加,有则修改,此时where无效,返回结果是最后添加或修改的ID Public Property Get [Replace]() On Error Resume Next dim popts,pk,max,findCount call parseOptions '解析参数 set popts = parsedOptions if is_empty(popts("table")) then Call setPoptsTable( popts ) end if pk = getPrikey( popts("table") ) '得到主键 if popts("data").Exists( pk ) Then '如果数据中存在主键,则需要从最大ID与是否存在该ID两方面判断 set rs = getRS( "SELECT TOP 1 MAX([" & pk & "]) AS theResult FROM [" & popts("table") & "]") rs.moveFirst max = rs("theResult") '得到最大ID tool.closeRS rs set rs = getRS( "SELECT TOP 1 * FROM [" & popts("table") & "] WHERE " & pk & " = " & popts("data")(pk) ) findCount = rs.RecordCount '以该ID查找一行记录,找到为1,找不到为0 tool.closeRS rs if max >= popts("data")(pk) then '数据ID <= 最大ID if findCount > 0 then '如果能找到,则修改 options.Remove("where") [Replace] = popts("data")(pk) '返回实际修改的ID if Save() = 0 then [Replace] = 0 end if else '如果找不到,插入到数据ID的位置上。返回该数据ID [Replace] = Add() end if else ' 超出最大ID,则添加,如果数据表被全部删除,此时生成的ID并不是1 call options("data").Remove(pk) [Replace] = Add() end if else '不存在主键,直接添加 [Replace] = Add() end if call L_( Me.db_type & " Replace" ) End Property ' 得到数据库中的所有表名,返回一个对象 Public Function getTables() on error resume next dim shm,arr,content dim filename ' 如果已经保存了数据,直接返回 if not isEmpty(dTables) Then set getTables = dTables Exit Function End If '如果对应文件不存在,则须先将数据生成json保存到文件中 filename = tool.getDataFileName("_tables_") if Not is_empty( C_("APP_DEBUG") ) OR Not tool.file_exists( filename ) Then call tool.initConn Set shm = tool.conn.OpenSchema(20) lastSql = "Access OpenSchema" Call N_( array("db_query") ) shm.MoveFirst Do While Not shm.EOF If shm("TABLE_TYPE") = "TABLE" Then POP_MVC.Arr.Push arr, LCase(shm("TABLE_NAME")) End If shm.MoveNext Loop tool.closeRS shm call tool.file_put_contents(filename, join(arr,",") ) else '从文件中取出数据 content = POP_MVC.file_get_contents(tool.getFilePath(filename)) content = mid(content,2) arr = split(content,",") End If set dTables = POP_MVC.Arr.toDict(arr) set getTables = dTables Call L_("POPASP_ACCESS.getTables") End Function '获取数据表结构 Public Function getTableStructure( ByRef tableName ) call getTables set getTableStructure = tool.getTableStructure( tableName,dTables,dTS,"SELECT TOP 1 * FROM [" & tableName & "]") End Function ' Public Function getFindSql( byref popts ) popts("top") = 1 : getFindSql = getSelectSql(popts) End Function End Class %>