使用Jscript之代码生成工具篇

zyug 2009-09-21 11:31:43
之前是学习ASP之后才学习ASP.NET
后来人变的越来越懒,后来看到ASP.NET能用生成工具生成相应的代码.ASP可以吗?
于是有了下面的内容

现在只能生成一些简单的,如有人感兴趣...放出代码
生成的实体类代码


function JCModels() {
this.ID = 0;
this.ModelName = "";
this.CreateDate = formatDate(new Date());
this.Url = "";
this.keyWord = "";
this.pagesize = 20;
this.pageno = 1;
this.recordscount = 0;

this.insert = function() {
var sql = " insert into [Models]([ModelName],[CreateDate],[Url]) values(?,?,?)";
var connection = Server.CreateObject("adodb.connection");
connection.ConnectionString = MDBCONNECTIONSTRING;
connection.Open();
var command = Server.CreateObject("adodb.command");
command.ActiveConnection = connection;
command.CommandText = sql;
var par_ModelName = command.CreateParameter("ModelName", 202, 1, 50, this.ModelName);
command.Parameters.Append(par_ModelName);
var par_CreateDate = command.CreateParameter("CreateDate", 202, 1, 20, this.CreateDate);
command.Parameters.Append(par_CreateDate);
var par_Url = command.CreateParameter("Url", 202, 1, 100, this.Url);
command.Parameters.Append(par_Url);
command.Execute();
connection.Close();
}

this.update = function() {
var sql = "update [Models] set [ModelName]=?,[CreateDate]=?,[Url]=? where [ID] = ? ";
var connection = Server.CreateObject("adodb.connection");
connection.ConnectionString = MDBCONNECTIONSTRING;
connection.Open();
var command = Server.CreateObject("adodb.command");
command.ActiveConnection = connection;
command.CommandText = sql;
var par_ModelName = command.CreateParameter("ModelName", 202, 1, 50, this.ModelName);
command.Parameters.Append(par_ModelName);
var par_CreateDate = command.CreateParameter("CreateDate", 202, 1, 20, this.CreateDate);
command.Parameters.Append(par_CreateDate);
var par_Url = command.CreateParameter("Url", 202, 1, 100, this.Url);
command.Parameters.Append(par_Url);
var par_ID = command.CreateParameter("ID", 3, 1, 4, this.ID);
command.Parameters.Append(par_ID)
command.Execute();
connection.Close();
}

this.deleteentity = function() {
var sql = "delete from [Models] where [ID] = ?";
var connection = Server.CreateObject("adodb.connection");
connection.ConnectionString = MDBCONNECTIONSTRING;
connection.Open();
var command = Server.CreateObject("adodb.command");
command.ActiveConnection = connection;
command.CommandText = sql;
var par_ID = command.CreateParameter("ID", 3, 1, 4, this.ID);
command.Parameters.Append(par_ID);
command.Execute();
command.CommandText = "delete from [ModelContent] where [ModelID]=?";
//var par_ModelID = command.CreateParameter("ModelID", 3, 1, 4, this.ID);
//command.Parameters.Append(par_ModelID);
command.Execute();

connection.Close();
}

this.getentity = function(entityid) {
var sql = "select * from [Models] where [ID] = ?";
var connection = Server.CreateObject("adodb.connection");
connection.ConnectionString = MDBCONNECTIONSTRING;
connection.Open();
var command = Server.CreateObject("adodb.command");
command.ActiveConnection = connection;
command.CommandText = sql;
var par_ID = command.CreateParameter("ID", 3, 1, 4, entityid);
command.Parameters.Append(par_ID);
var rsEntity = command.Execute();
if (!rsEntity.Eof) {
this.ID = new String(rsEntity("ID")).replace(/(^[\s]*)|([\s]*$)/g, "");
this.ModelName = new String(rsEntity("ModelName")).replace(/(^[\s]*)|([\s]*$)/g, "");
this.CreateDate = formatDate(new Date(rsEntity("CreateDate")));
this.Url = new String(rsEntity("Url")).replace(/(^[\s]*)|([\s]*$)/g, "");
}
connection.Close();
}

this.doQuery = function() {
var connection = Server.CreateObject("adodb.connection");
connection.Open(MDBCONNECTIONSTRING);
var order = " order by [ID] desc ";
var condition = " where 1=1 ";
var sql = "select top " + this.pagesize + " * from [Models] ";
sql += condition;
var rsCount = connection.Execute("select count(1) from [Models] " + condition);
this.recordscount = new Number(rsCount(0));
if (this.pageno > 1) {
var notIncludeCount = (this.pageno - 1) * this.pagesize;
var notInclude = " select top " + notIncludeCount + " [ID] from [Models] " + condition + order;
sql += " and [ID] not in ( " + notInclude + " ) ";
}
sql += order;
var rsContent = connection.Execute(sql);
var arrayResult = new Array();
if (rsContent.Eof) {
;
}
while (!rsContent.Eof) {
var newentity = new JCModels();
newentity.ID = new String(rsContent("ID")).replace(/(^[\s]*)|([\s]*$)/g, "");
newentity.ModelName = new String(rsContent("ModelName")).replace(/(^[\s]*)|([\s]*$)/g, "");
newentity.CreateDate = formatDate(new Date(rsContent("CreateDate")));
newentity.Url = new String(rsContent("Url")).replace(/(^[\s]*)|([\s]*$)/g, "");
arrayResult.push(newentity);
rsContent.MoveNext();
}
connection.Close();
return arrayResult;
}

function formatDate(s) {
s = new Date(s);
var Format = "YYYY-MM-DD hh:mm:ss";
Format = Format.replace("YYYY", s.getYear());
var m = s.getMonth() + 1;
Format = Format.replace("MM", m > 9 ? m : "0" + m);
Format = Format.replace("DD", s.getDate() > 9 ? s.getDate() : "0" + s.getDate());
Format = Format.replace("hh", s.getHours() > 9 ? s.getHours() : "0" + s.getHours());
Format = Format.replace("mm", s.getMinutes() > 9 ? s.getMinutes() : "0" + s.getMinutes());
Format = Format.replace("ss", s.getSeconds() > 9 ? s.getSeconds() : "0" + s.getSeconds());
return Format;
}
}



方法调用示例
增删改查



//操作增加模块
if (Request.Form("act") == "addmodel") {
// validateAdmin();
var model = Request.Form("model");
var url = Request.Form("url");
var entity = new JCModels();
entity.ModelName = model;
entity.Url = url;
entity.insert();
Response.Write("成功添加");
Response.End();
}

//更新模块
if (Request.Form("act") == "editmodel") {
//validateAdmin();
var id = Request.Form("id");
var model = Request.Form("model");
var url = Request.Form("url");
var entity = new JCModels();
entity.ID = id;
entity.ModelName = model;
entity.Url = url;
entity.update();
Response.Write("成功更新");
Response.End();
}

//后台管理
if (Request.Form("act") == "showdesktop") {
//validateAdmin();
var t = new JCModels();
if (Request.Form("pageno").Count > 0) {
t.pageno = Request.Form("pageno");
}
var items = t.doQuery();
var tableHTML = "";
if (t.recordscount <= 0) {
tableHTML = "暂时没有自定义模块";
}
else {
var label = ReadFile("/templates/modelsTemplate.txt");

var header = ReadHeader(label);
var template = ReadTemplate(label);
var footer = ReadFooter(label);

tableHTML = header;
for (var i = 0; i < items.length; i++) {
var temp = template.replace(/[$][$]ID/g, items[i].ID)
temp = temp.replace(/[$][$]ID/g, items[i].ID); //URL
temp = temp.replace(/[$][$]ModelName/g, items[i].ModelName); //URL
temp = temp.replace(/[$][$]URL/g, items[i].Url); //URL
tableHTML += temp; //$$Ispublish
}
tableHTML += footer;
}
tableHTML += "<script>var CalledObject = new Object();CalledObject.recordscount =" + t.recordscount + ";CalledObject.keyWord='" + t.keyWord + "';CalledObject.pagesize =" + t.pagesize + ";CalledObject.pageno=" + t.pageno + "</script>"; //回调告知分页事件
Response.Write(tableHTML);
Response.End();
}

if (Request.Form("act") == "edit") {
var t = new JCModels();
t.getentity(Request.Form("id"));
//json返回
var s = "var t = {";
for (var property in t) {
s += "\"" + property + "\":\"" + escape(t[property]) + "\"";
s += ",";
}
s += "\"Coder\":\"zyg\"";
s += "};";
Response.Write(s);
Response.End();
}

//删除
if (Request.Form("act") == "delete") {
var t = new JCModels();
t.ID = Request.Form("id");
t.deleteentity();
Response.End();
}



由于doQuery事件中使用了正序排倒序排再分页,使用参数查询有一定难度.故没有使用参数查询,不知有何解决方案.
...全文
68 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyue86 2009-09-21
  • 打赏
  • 举报
回复
UP
凡夫与俗子 2009-09-21
  • 打赏
  • 举报
回复
顶个。
zyug 2009-09-21
  • 打赏
  • 举报
回复
工具是用vc写的.判断字段类型生成相应的代码.

CString s;
s = _T("\tfunction JC")+tb.tableName+_T("()\n");
s += _T("\t{\n");
//生成字段
//s += _T("\t\t//定义字段\n");
for(int i = 0 ; i < tb.fieldsCount ; i ++ )
{

s += _T("\t\tthis.")+tb.fields[i].fieldName+_T(" = ")+ GetDefaultByType(tb.fields[i].nType)+_T(";\n");
}
//给值函数
//生成查询所需

//s += _T("\t\t//初始化查询及分页数据\n");
s += _T("\t\tthis.keyWord = \"\";\n");
s += _T("\t\tthis.pagesize = 20;\n" );
s += _T("\t\tthis.pageno = 1;\n");
s += _T("\t\tthis.recordscount = 0;\n");
//s += _T("\t\t//方法体定义\n");
//生成函数
//insert函数

s += _T("\t\tthis.insert = function()\n");
s += _T("\t\t{\n");
//构造插入的SQL语句
s += _T("\t\t\tvar sql =\" insert into [") + tb.tableName;
s += _T("](");
for(int i = 0 ; i < tb.fieldsCount ; i ++ )
{
s += _T("[");
s += tb.fields[i].fieldName;
s += _T("]");
if( i < (tb.fieldsCount-1 ))s += _T(",");
}
s += _T(") values(");
for(int i = 0 ; i < tb.fieldsCount ; i ++ )
{

s += "?";
if( i < (tb.fieldsCount-1 ))s += _T(",");
}
s += _T(")\";\n");
//构造Connection
s += _T("\t\t\tvar connection = Server.CreateObject(\"adodb.connection\");\n\t\t\tconnection.ConnectionString = MDBCONNECTIONSTRING;\n\t\t\tconnection.Open();\n");
//构造Command
s += _T("\t\t\tvar command = Server.CreateObject(\"adodb.command\");\n");
//指定Command属性
s += _T("\t\t\tcommand.ActiveConnection = connection;\n\t\t\tcommand.CommandText =sql;\n ");
//构造参数
for(int i = 0 ; i < tb.fieldsCount ; i ++ )
{
CString snType;
snType.Format(_T("%d"),tb.fields[i].nType);
CString snSize;
snSize.Format(_T("%d"),tb.fields[i].nSize);
//将日期默认做字符串处理
if (snType == _T("7"))
{
snType = _T("202");
snSize = _T("20");
}
s += _T("\t\t\tvar par_")+tb.fields[i].fieldName +_T(" = command.CreateParameter(\"")+tb.fields[i].fieldName+_T("\",")+snType+_T(",1,")+snSize+_T(",this.")+tb.fields[i].fieldName+_T(");\n");
s += _T("\t\t\tcommand.Parameters.Append(par_")+tb.fields[i].fieldName+_T(");\n");
}
s += _T("\t\t\tcommand.Execute();\n\t\t\tconnection.Close();\n");
s += _T("\t\t}\n");

//生成update函数
s += _T("\t\tthis.update = function()\n");
s += _T("\t\t{\n");
s += _T("\t\t\tvar sql = \"update [")+ tb.tableName +_T("] set ");;
for(int i = 1 ; i < tb.fieldsCount ; i ++ )
{
s += _T("[");
s += tb.fields[i].fieldName;
s += _T("]=");
s += "?";
if( i < (tb.fieldsCount-1 ))s += _T(",");
}
s += _T(" where [")+ tb.fields[0].fieldName+ _T("] = ? \";\n");
//构造Connection
s += _T("\t\t\tvar connection = Server.CreateObject(\"adodb.connection\");\n\t\t\tconnection.ConnectionString = MDBCONNECTIONSTRING;\n\t\t\tconnection.Open();\n");
//构造Command
s += _T("\t\t\tvar command = Server.CreateObject(\"adodb.command\");\n");
//指定Command属性
s += _T("\t\t\tcommand.ActiveConnection = connection;\n\t\t\tcommand.CommandText =sql;\n ");
//构造参数
for(int i = 1 ; i < tb.fieldsCount ; i ++ )
{
CString snType;
snType.Format(_T("%d"),tb.fields[i].nType);
CString snSize;
snSize.Format(_T("%d"),tb.fields[i].nSize);
if (snType == _T("7"))
{
snType = _T("202");
snSize = _T("20");
}

s += _T("\t\t\tvar par_")+tb.fields[i].fieldName +_T(" = command.CreateParameter(\"")+tb.fields[i].fieldName+_T("\",")+snType+_T(",1,")+snSize+_T(",this.")+tb.fields[i].fieldName+_T(");\n");
s += _T("\t\t\tcommand.Parameters.Append(par_")+tb.fields[i].fieldName+_T(");\n");

}
s += _T("\t\t\tvar par_")+tb.fields[0].fieldName +_T(" = command.CreateParameter(\"")+tb.fields[0].fieldName+_T("\",3,1,4,this.")+tb.fields[0].fieldName+_T(");\n");
s += _T("\t\t\tcommand.Parameters.Append(par_")+tb.fields[0].fieldName+_T(")\n");

s += _T("\t\t\tcommand.Execute();\n\t\t\tconnection.Close();\n");
s += _T("\t\t}\n");

//生成删除语句
s += _T("\t\tthis.deleteentity = function()\n");
s += _T("\t\t{\n");
s += _T("\t\t\tvar sql = \"delete from [")+tb.tableName+_T("] where [")+tb.fields[0].fieldName+_T("] = ?\";\n");
s += _T("\t\t\tvar connection = Server.CreateObject(\"adodb.connection\");\n\t\t\tconnection.ConnectionString = MDBCONNECTIONSTRING;\n\t\t\tconnection.Open();\n");
s += _T("\t\t\tvar command = Server.CreateObject(\"adodb.command\");\n");
s += _T("\t\t\tcommand.ActiveConnection = connection;\n\t\t\tcommand.CommandText =sql;\n ");
int i = 0;
CString snType;
snType.Format(_T("%d"),tb.fields[i].nType);
CString snSize;
snSize.Format(_T("%d"),tb.fields[i].nSize);


s += _T("\t\t\tvar par_")+tb.fields[i].fieldName +_T(" = command.CreateParameter(\"")+tb.fields[i].fieldName+_T("\",")+snType+_T(",1,")+snSize+_T(",this.")+tb.fields[i].fieldName+_T(");\n");
s += _T("\t\t\tcommand.Parameters.Append(par_")+tb.fields[i].fieldName+_T(");\n");

s += _T("\t\t\tcommand.Execute();\n\t\t\tconnection.Close();\n");
s += _T("\t\t}\n");



//



//生成得到语句

s += _T("\t\tthis.getentity = function(entityid)\n");
s += _T("\t\t{\n");
s += _T("\t\t\tvar sql = \"select * from [")+tb.tableName+_T("] where [")+tb.fields[0].fieldName+_T("] = ?\";\n");
s += _T("\t\t\tvar connection = Server.CreateObject(\"adodb.connection\");\n\t\t\tconnection.ConnectionString = MDBCONNECTIONSTRING;\n\t\t\tconnection.Open();\n");
s += _T("\t\t\tvar command = Server.CreateObject(\"adodb.command\");\n");
s += _T("\t\t\tcommand.ActiveConnection = connection;\n\t\t\tcommand.CommandText =sql;\n ");
i = 0;
//CString snType;
snType.Format(_T("%d"),tb.fields[i].nType);
//CString snSize;
snSize.Format(_T("%d"),tb.fields[i].nSize);

s += _T("\t\t\tvar par_")+tb.fields[i].fieldName +_T(" = command.CreateParameter(\"")+tb.fields[i].fieldName+_T("\",")+snType+_T(",1,")+snSize+_T(",entityid);\n");
s += _T("\t\t\tcommand.Parameters.Append(par_")+tb.fields[i].fieldName+_T(");\n");

s += _T("\t\t\tvar rsEntity = command.Execute();\n");
s += _T("\t\t\tif(!rsEntity.Eof){\n");
for(i = 0 ; i < tb.fieldsCount ; i ++ )
{
if(tb.fields[i].nType != 7)
{
s += _T("\t\t\t\tthis.")+tb.fields[i].fieldName+_T(" = new String(rsEntity(\"")+tb.fields[i].fieldName +_T("\")).replace(/(^[\\s]*)|([\\s]*$)/g,\"\");\n");
}
else
{
s+=_T("\t\t\t\tthis.")+tb.fields[i].fieldName+_T(" = formatDate (new Date(rsEntity(\"")+tb.fields[i].fieldName +_T("\")));\n");
}
}
s += _T("\t\t\t}\n");
s += _T("\t\t\tconnection.Close();\n");
s += _T("\t\t}\n");

i = 0;
//生成查询列表语句
s += _T("\t\tthis.doQuery = function(){\n");
s += _T("\t\t\tvar connection = Server.CreateObject(\"adodb.connection\");\n");
s += _T("\t\t\tconnection.Open(MDBCONNECTIONSTRING);\n");
s += _T("\t\t\tvar order = \" order by [");
s += tb.fields[i].fieldName;
//s += _T("\t\t\tcommand.Parameters.Append(par_")+tb.fields[i].fieldName+_T(");\n");

s += _T("] desc \";\n");
s += _T("\t\t\tvar condition = \" where 1=1 \" ;\n");
//s += _T("condition += \" and ( [subject] like '%\"+this.keyWord+\"%' or [contract_job] like '%\"+this.keyWord+\"%' or Exhibition_date like '%\"+this.keyWord+\"%' )\";\n");
//custom condition here

s += _T("\t\t\tvar sql = \"select top \" + this.pagesize + \" * from [")+ tb.tableName +_T("] \";\n\t\t\tsql += condition ;\n");
s += _T("\t\t\tvar rsCount = connection.Execute(\"select count(1) from [")+ tb.tableName +_T("] \"+condition);\n");
s += _T("\t\t\tthis.recordscount = new Number(rsCount(0));\n");
s += _T("\t\t\tif( this.pageno >1 )\n\t\t\t{\n\t\t\tvar notIncludeCount = (this.pageno -1 ) * this.pagesize;\n\t\t\tvar notInclude = \" select top \"+ notIncludeCount + \" [")+ tb.fields[i].fieldName +_T("] from [")+ tb.tableName +_T("] \"+ condition + order;\n");
s += _T("\t\t\tsql += \" and [")+ tb.fields[i].fieldName +_T("] not in ( \" + notInclude + \" ) \" ;\n\t\t\t}\n\t\t\tsql += order;\n");
s += _T("\t\t\t var rsContent = connection.Execute(sql);\n\t\t\tvar arrayResult = new Array();\n");
s += _T("\t\t\tif(rsContent.Eof)\n\t\t\t{\n\t\t\t ;\n\t\t\t}\n");
s += _T("\t\t\twhile(!rsContent.Eof){\n");
s += _T("\t\t\t\tvar newentity = new JC")+tb.tableName+_T("();\n");
for(i = 0 ; i < tb.fieldsCount ; i ++ )
{
if(tb.fields[i].nType != 7)
{
s += _T("\t\t\t\tnewentity.")+tb.fields[i].fieldName+_T(" = new String(rsContent(\"")+tb.fields[i].fieldName +_T("\")).replace(/(^[\\s]*)|([\\s]*$)/g,\"\");\n");
}
else
{
s+=_T("\t\t\t\tnewentity.")+tb.fields[i].fieldName+_T(" = formatDate (new Date(rsContent(\"")+tb.fields[i].fieldName +_T("\")));\n");
}
}
s += _T("\t\t\t\tarrayResult.push(newentity);\n\t\t\trsContent.MoveNext();\n\t\t\t}\n\t\t\tconnection.Close();\n\t\t\treturn arrayResult;\n\t\t}\n");

//日期处理函数
s += _T("\t\tfunction formatDate(s)\n\t\t{\n");
s += _T("\t\t\ts = new Date(s);\n");
s += _T("\t\t\tvar Format = \"YYYY-MM-DD hh:mm:ss\";\n");
s += _T("\t\t\tFormat = Format.replace(\"YYYY\",s.getYear());\n");
s += _T("\t\t\tvar m = s.getMonth() + 1;\n");
s += _T("\t\t\tFormat = Format.replace(\"MM\", m > 9 ? m: \"0\" + m);\n");
s += _T("\t\t\tFormat = Format.replace(\"DD\",s.getDate() > 9 ? s.getDate(): \"0\" + s.getDate());\n");
s += _T("\t\t\tFormat = Format.replace(\"hh\",s.getHours() > 9 ? s.getHours(): \"0\" + s.getHours());\n");
s += _T("\t\t\tFormat = Format.replace(\"mm\",s.getMinutes() > 9 ? s.getMinutes(): \"0\" + s.getMinutes());\n");
s += _T("\t\t\tFormat = Format.replace(\"ss\",s.getSeconds() > 9 ? s.getSeconds(): \"0\" + s.getSeconds());\n");
s += _T("\t\t\treturn Format;\n");
s+= _T("\t\t}\n");

//end of class
s += _T("\t}\n");
//AfxMessageBox(s);

return s;

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧