你软件中有多少代码是自动生成? 也许你会说,自动生成的代码就是重复代码,是程序本身有问题,但是数据库开发,有些代码不是重复代码,但又需要自动生成。需要自动生成的原因是因为数据库本身包含了大量的信息(也许你又会说了,class->database才是正道),不是重复代码的原因是因为每个对象的信息又都是不一样的。
很多自动代码生成的软件都是通过简单模板来实现的,通过一些自定义的TAG来实现一定的控制,我早期的ceg就是这样的,它伴随着我几个O/R Mapping框架的应用。后来,我的sinolib中的映射XML文件同样需要自动生成,可是ceg的简单模板已经无法再支持映射文件的灵活性了。
于是CodeDG就不得不出来了,它使用了script control来完成脚本解析,实现了代码生成的真正灵活,并且以统一的接口向脚本提供不同平台的数据库结构对象的信息。
以下是sinolib的XML映射的例子:
<?xml version=”1.0″ encoding = “GB2312″ ?>
<sino-mapping>
<class name = “AppRole”>
<property name = “uid” extname = “唯一识别码” type=”string” key=”primary”/>
<property name = “code” extname = “简码” type=”string”/>
<property name = “name” extname = “名称” type=”string” notnull=”true”/>
<property name = “remark” extname = “备注” type=”string”/>
<property name = “isdelete” extname = “删除标志” type=”boolean” notnull=”true”/>
</class>
</sino-mapping>
这是一CodeDG内置的一个示例脚本:
This is a example script template.
You can write script as same as what’s in ASP.
<%
‘test function
function max(a,b)
max = iif(a>b,a,b)
end function
max_value = max(1,2)
‘msgbox max_value
response.write “Table:“ & table.name & vbcrlf
response.write “Comment:“ & table.description & vbcrlf
response.write “Column count:“ & table.columns.count & vbcrlf
response.write “========Traverse all column’s property=========“ & vbcrlf
for each cm in Table.columns.items
%>column:<%
response.write vbtab & cm.name
response.write vbcrlf
%> Primary Key:<%
response.write vbtab & cm.pk
response.write vbcrlf
%> Auto Identity:<%
response.write vbtab & cm.autoid
response.write vbcrlf
%> DataType:<%
response.write vbtab & cm.datatype
response.write vbcrlf
%> Nullable:<%
response.write vbtab & cm.nullable
response.write vbcrlf
%> Length:<%
response.write vbtab & cm.length
response.write vbcrlf
%> Precision:<%
response.write vbtab & cm.Precision
response.write vbcrlf
%> Scale:<%
response.write vbtab & cm.NumScale
response.write vbcrlf
%> Description:<%
response.write vbtab & cm.Description
response.write vbcrlf
next
%>
generate by cpdedg <%= Now %><%
filepath = app.path & “\test.txt”
set fso = createobject(“Scripting.FileSystemObject“)
Set ts = fso.CreateTextFile(filepath, True, True)
ts.Write CEG_RESULT
ts.Close
if iif (msgbox (“file has saved to “ & filepath & vbcrlf _
& “do you want to delete this file?“,vbQuestion +vbYesNo )=vbYes ,true,false) then
fso.DeleteFile filepath ,True
end if
‘ you must use ’start with all tables’ to fill all tables of schema
b=true
for each tbl in schema.Tables.Items
if tbl.columns.count = 0 then
b= false
end if
next
if b then
for each tbl in schema.Tables.Items
response.write “table name:“ & vbtab & tbl.name & vbcrlf
response.write “column count:“ & vbtab & tbl.columns.count & vbcrlf
next
end if
%>
然后这是一个生成结果:
This is a example script template.
You can write script as same as what’s in ASP.
Table:Area
Comment:
Column count:4
========Traverse all column‘s property=========
column: Code
Primary Key: True
Auto Identity: False
DataType: varchar
Nullable: False
Length: 10
Precision:
Scale:
Description: 编号
column: Name
Primary Key: False
Auto Identity: False
DataType: varchar
Nullable: False
Length: 50
Precision:
Scale:
Description: 名称
column: Remark
Primary Key: False
Auto Identity: False
DataType: varchar
Nullable: True
Length: 255 Precision: Scale: Description: 说明column: rowguid Primary Key: False
Auto Identity: False
DataType: uniqueidentifier
Nullable: False
Length:
Precision:
Scale:
Description:









评论人