<% ' +---------------------------------------------------------------------- ' | 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> ' +---------------------------------------------------------------------- Class POPASP_MODEL Private Function dateCompare( date1,date2 ) dateCompare = DateDiff("s",date2,date1) End Function '创建一个Model文件 Function CreateModelFile( db_type,db_path,tableName,prikey,ignoreExists ) dim dirName,filePath,model_str,bool,modelPath CreateModelFile = False dirName = POP_MVC.File.baseName( array( db_path,1 ) ) if dirName = "" Then dirName = "db" filePath = POP_MVC.appPath & "/Model/" & LCase(db_type) & "/" & dirName & "/" & POP_MVC.String.UCFirst( LCase(tableName) ) & ".asp" '如./home/Model/excel/15251成绩/1学期.asp bool = false if not is_empty(ignoreExists) then '忽略文件的存在性 bool = true elseif POP_MVC.file.isExists( filePath ) then '如果目标文件不存在 bool = true end if if bool then '创建目标文件 model_str = POP_MVC.asp_get_contents( POP_MVC.mvc_dir & "Tpl/model2.html" ) model_str = Replace(model_str,"TABLENAME" ,tableName ,1,1,0) model_str = Replace(model_str,"TABLENAME" ,tableName ,1,1,0) model_str = Replace(model_str,"PRIKEY" ,prikey ,1,1,0) call POP_MVC.file_put_contents( filePath, "<" & "%" & trim(model_str) & "%" & ">" ) end if if POP_MVC.file.isExists( filePath ) then '如果目标文件不存在 CreateModelFile = true end if End Function '创建Model对象,可以用 M_("表名") 来创建 Function Create(ByVal arg) on error resume next Dim model_path,dst_path,model_str,bReload,key,tableName,runtime_model,runtime_arg Dim db_type,access_type,db_path,db_host,db_user,db_name,db_pwd,bnd '分配数据库相关的配置项 db_type = C_("DB_TYPE") access_type = C_("ACCESS_TYPE") db_path = C_("DB_PATH") db_name = C_("DB_NAME") db_host = C_("DB_HOST") db_user = C_("DB_USER") db_pwd = C_("DB_PWD") if not isArray( arg ) then tableName = arg else tableName = arg(0) '如果是数组的话,则按如下参数分配 'access: tableName,db_type,db_path,access_type 'excel: tableName,db_type,db_path 'sqlite3: tableName,db_type,db_path 'mysql: tableName,db_type,db_name,db_host,db_user,db_pwd 'sql server: tableName,db_type,db_name,db_host,db_user,db_pwd if ubound( arg ) > 0 then db_type = trim(lcase(arg(1))) bnd = ubound( arg ) if db_type = "access" then if bnd > 1 then db_path = arg(2) if bnd > 2 then access_type = arg(3) ElseIf db_type = "excel" or db_type = "sqlite3" then if bnd > 1 then db_path = arg(2) ElseIf db_type = "mysql" or db_type = "sqlserver" then if bnd > 1 then db_name = arg(2) if bnd > 2 then db_host = arg(3) if bnd > 3 then db_user = arg(4) if bnd > 4 then db_pwd = arg(5) end if end if end if if db_type = "access" or db_type = "excel" or db_type = "sqlite3" then runtime_arg = array( tableName , db_type , db_path ) else runtime_arg = array( tableName , db_type , db_name ) end if runtime_model = POP_MVC.getModelRunName( runtime_arg ) key = runtime_model & "Model" if POP_MVC.dModel.Exists(key) then if typeName( POP_MVC.dModel(key) ) = key Then set Create = POP_MVC.dModel(key) Exit Function end if end if bReload = false model_path = POP_MVC.getModelPath( runtime_arg ) dst_path = POP_MVC.getModelDstPath( runtime_arg ) IF model_path <> "" Then '如果已经定义了model If Not POP_MVC.file.isFile(dst_path) Then '如果目标文件不存在 bReload = True ElseIf dateCompare(POP_MVC.file.mtime(POP_MVC.mvc_dir & "popasp_model.class.asp"),POP_MVC.file.mtime(dst_path)) > 0 Then ' 如果popasp_model.class.asp进行了最新修改 bReload = True ElseIf dateCompare(POP_MVC.file.mtime(model_path),POP_MVC.file.mtime(dst_path)) > 0 Then ' 如果 模型文件 进行了最新修改 bReload = True Else '获取文件内容 bReload = false model_str = POP_MVC.asp_get_contents( dst_path ) End If if bReload Then model_str = POP_MVC.asp_get_contents( model_path ) model_str = POP_MVC.String.reg_replace( model_str, "Class [" & runtime_model & "Model]" & vbCrLf & vbTab & "Public db", "^\s*Class\s+\[?" & tableName&"\]?","i" ) call POP_MVC.file_put_contents( dst_path, "<" & "%" & vbCrLf & trim(model_str) & vbCrLf & "%" & ">" ) End If Else '没有定义model If POP_MVC.file.isFile( dst_path ) Then '如果解析后的文件存在 '获取文件内容 model_str = POP_MVC.asp_get_contents( dst_path ) Else '创建目标文件 model_str = POP_MVC.asp_get_contents( POP_MVC.mvc_dir & "Tpl/model1.html" ) model_str = Replace(model_str,"TABLENAME" ,runtime_model ,1,1,0) model_str = Replace(model_str,"TABLENAME" ,tableName ,1,1,0) call POP_MVC.file_put_contents( dst_path, "<" & "%" & vbCrLf & trim(model_str) & vbCrLf & "%" & ">" ) end If End If '实例化类 Execute model_str if err.number <> 0 then if is_empty(C_("APP_DEBUG")) then call L_("") else call L_( POP_MVC.UCFirst(tableName) & "Model 类出错:" ) call POP_MVC.quit end if end if Execute "set POP_MVC.dModel(""" & key & """) = new " & key '给对象中的db赋值 if db_type = "access" or db_type = "excel" or db_type = "sqlite3" then db_path = POP_MVC.realPath( db_path ) set POP_MVC.dModel(key).db = P_( array("POPASP_" & UCase(db_type) , db_path & "#" & tableName) ) else set POP_MVC.dModel(key).db = P_( array("POPASP_" & UCase(db_type) , db_name & "#" & tableName) ) end if POP_MVC.dModel(key).db.db_type = db_type POP_MVC.dModel(key).db.access_type = access_type POP_MVC.dModel(key).db.db_path = db_path POP_MVC.dModel(key).db.db_name = db_name POP_MVC.dModel(key).db.db_host = db_host POP_MVC.dModel(key).db.db_user = db_user POP_MVC.dModel(key).db.db_pwd = db_pwd Call POP_MVC.dModel(key).db.assignTool( db_type,access_type,db_path,db_host,db_user,db_name,db_pwd ) Call POP_MVC.dModel(key).init if isEmpty(POP_MVC.dModel(key).db.tableName) then POP_MVC.dModel(key).db.tableName = tableName end if set Create = POP_MVC.dModel(key) Call L_("") End Function End Class %>