C#分析数据库结构,使用XSL模板自动生成代码(二)

来源:互联网 发布:方舟子美国间谍知乎 编辑:程序博客网 时间:2024/04/29 15:04
########################## temp_HTML代码.xml #####################################################
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/*">
<html>
<head>
<style>
body{ font-family: "宋体"; font-size: 12px}
table { width:100%; border-collapse:collapse;
border: 1px #CC0066 solid; font-family: "宋体";
font-size: 12px}
.tablehead{background-color:#CCCCFF}
td{ border: 1px #CC0066 solid}
</style>
</head>
<body>
<xsl:for-each select="table">
数据表 <b><xsl:value-of select="translate(@tablename,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /></b> 结构
共 <xsl:value-of select="count(*)" /> 个字段
<br />
<table >
<tr class="tablehead">
<td>字段名</td>
<td>类型</td>
<td>长度</td>
</tr>
<xsl:for-each select="*">
<tr>
<td>
<xsl:value-of select="@fieldname" />
</td>
<td>
<xsl:value-of select="@fieldtype" />
</td>
<td>
<xsl:value-of select="@fieldwidth" />
</td>
</tr>
</xsl:for-each>
</table>
<p /><hr />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
################################ temp_Java_Struct.xml #######################################################################
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<html><head></head><body>
<xsl:for-each select="*/table">
<br />-------------- 文件名 <xsl:value-of select="@tablename" />.java -----------------------------
<pre style=" background-color:gainsboro">

package com.haitai.emr.struct;
import java.sql.*;
import java.io.*;
/** <xsl:value-of select="@cname" />
* @author 代码生成器 */
public class <xsl:value-of select="@tablename" /> implements Serializable
{ // 定义数据库字段变量 ////////////////////////////////////////////////////////////////
<xsl:for-each select="*">
<xsl:variable name="javatype">
<xsl:choose>
<xsl:when test="@isstring='1'">String </xsl:when>
<xsl:when test="boolean('true')" >int </xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="lowfieldname">
<xsl:value-of select="translate(@fixname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:variable>
private <xsl:value-of select="$javatype" /> <xsl:text disable-output-escaping="yes"></xsl:text><xsl:value-of select="$lowfieldname" /> ; // 字段 <xsl:value-of select="@cname" />
</xsl:for-each>
public static final String SELECT =
"Select <xsl:for-each select="*">
<xsl:value-of select="normalize-space(@fieldname)" />
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each> From <xsl:value-of select="@tablename" />";
/** @param conn
* @exception SQLException */
public java.sql.PreparedStatement makeInsSt (java.sql.Connection conn) throws SQLException{
PreparedStatement pst=conn.prepareStatement("insert into <xsl:value-of select="@tablename" />(<xsl:for-each select="*">
<xsl:value-of select="normalize-space(@fieldname)" />
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>)"
+"values(<xsl:for-each select="*">?<xsl:if test="position() != last()">,</xsl:if></xsl:for-each>)");
int index=0;
<xsl:for-each select="*">
pst.setString(++index,this.get<xsl:value-of select="@fieldname" />()); // <xsl:value-of select="@cname" />
</xsl:for-each>
return pst;
}

/** @param conn
* @exception SQLException */
public java.sql.PreparedStatement makeUpdSt (java.sql.Connection conn) throws SQLException{

// TODO : implement
PreparedStatement pst=conn.prepareStatement("update <xsl:value-of select="@tablename" /> set <xsl:for-each select="*"><xsl:value-of select="normalize-space(@fieldname)" /> =? <xsl:if test="position() != last()">,</xsl:if></xsl:for-each>)"
+"where 数据表关键字段名=?");
int index=0;
<xsl:for-each select="*">
pst.setString(++index,this.get<xsl:value-of select="@fieldname" />()); // <xsl:value-of select="@cname" />
</xsl:for-each>

//关键字
pst.setString(++index,this.get数据表关键字段名());//数据表关键字段说明

return pst;
}

public String toString (){

// TODO : implement
return <xsl:for-each select="*">"<xsl:if test="position() != 1">,</xsl:if><xsl:value-of select="normalize-space(@fieldname)" />="+ <xsl:value-of select="@lowfieldname" /><xsl:if test="position() != last()"> + </xsl:if> </xsl:for-each>;
}

// 读取和修改数据的接口
<xsl:for-each select="*">
<xsl:variable name="javatype">
<xsl:choose>
<xsl:when test="@isstring='1'">String</xsl:when>
<xsl:when test="boolean('true')">int</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="lowfieldname">
<xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:variable>
public <xsl:value-of select="$javatype" /> get<xsl:value-of select="@fieldname" />(){
return <xsl:value-of select="normalize-space($lowfieldname)" /> ;
}
//@param <xsl:value-of select="@cname" />
public void set<xsl:value-of select="@fieldname" />(<xsl:value-of select="@javatype" /> value){
<xsl:value-of select="normalize-space($lowfieldname)" /> = value ;
}
</xsl:for-each>
} // 类 <xsl:value-of select="@tablename" /> 定义结束
</pre>
</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>
######################################## temp_VB.xml ############################################################
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="*/table">
<br />-------------- 文件名 <xsl:value-of select="@tablename" />.cls -----------------------------
<pre style=" background-color:gainsboro">
'******************************************************************************
'**
'** 数据表 <xsl:value-of select="@cname" />[ <xsl:value-of select="@tablename" /> ]操作的对象
'**
'** 编制:代码生成器
'** 时间:
'**
'******************************************************************************
'** 定义和数据库字段对应的变量 *************************************************************
private const c_TableName As String = "<xsl:value-of select="@tablename" />" '** 数据表名称
<xsl:for-each select="*">
<xsl:variable name="vbtype">
<xsl:choose>
<xsl:when test="@isstring='1'">String </xsl:when>
<xsl:when test="boolean('true')" >Integer </xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="lowfieldname">
<xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:variable>
private m_<xsl:value-of select="@fixname" /> As <xsl:value-of select="$vbtype" /> '** 字段 <xsl:value-of select="@cname" />
</xsl:for-each>

'** 定义数据库字段属性接口 ***************************************************************
Public Property Get TableName() As String
TableName = c_TableName
End Property
<xsl:for-each select="*">
<xsl:variable name="vbtype">
<xsl:choose>
<xsl:when test="@isstring='1'">String </xsl:when>
<xsl:when test="boolean('true')" >Integer </xsl:when>
</xsl:choose>
</xsl:variable>
'** 数据库字段 <xsl:value-of select="@cname" />
Public Property Get m<xsl:value-of select="@fieldname" />() As <xsl:value-of select="$vbtype" />
m<xsl:value-of select="@fieldname" /> = m_<xsl:value-of select="@fieldname" />
End Property
Public Property Let m<xsl:value-of select="@fieldname" />(Byval Value As <xsl:value-of select="$vbtype" />)
m_<xsl:value-of select="@fieldname" /> = m<xsl:value-of select="@fieldname" />
End Property
</xsl:for-each>
'** 获得查询所有数据使用的SQL语句 **
public Function GetBaseSQL() As String
GetBaseSQL ="Select <xsl:for-each select="*">
<xsl:value-of select="@fieldname" />
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each> From " <xsl:text disable-output-escaping="yes">&</xsl:text> c_TableName
End Function

'** 定义从数据库记录集获得数据的方法 **
Public Function SelectRS(ByVal rs As ADODB.Recordset) As Boolean
On Error GoTo SelectErr
SelectRS = False
<xsl:for-each select="*">
m_<xsl:value-of select="@fixname" /> = rs.Fields(<xsl:value-of select="position()-1" />).Value '** 字段 <xsl:value-of select="@cname" />
</xsl:for-each>
SelectRS = True
Exit Function
SelectErr:
SelectRS = False
End Function

 

</pre>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

####################### temp_表说明文档.xml ##################################
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="*/table">
-------------表 <xsl:value-of select="@tablename" /> 的说明文档 <xsl:value-of select="count(*)" />个字段
<br /><pre style="word-wrap:break-word;background-color:gainsboro">
<xsl:for-each select="*"><xsl:value-of select="@fixname" /><xsl:text disable-output-escaping="yes"> </xsl:text><xsl:value-of select="@fieldtype" />
<xsl:if test="@isstring='1'">(<xsl:value-of select="@fieldwidth" />)</xsl:if> .
<xsl:text disable-output-escaping="yes"></xsl:text>
</xsl:for-each>
</pre>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

####################### temp_创建表的SQL语句.xml ##################################
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="*/table">
-------------创建表 <xsl:value-of select="@tablename" /> 的SQL语句 <xsl:value-of select="count(*)" />个字段
<br /><pre style="word-wrap:break-word;background-color:gainsboro">
CREATE TABLE <xsl:value-of select="@tablename" />(
<xsl:for-each select="*">
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:value-of select="@fixname" />
<xsl:value-of select="@fieldtype" />
<xsl:if test="@isstring='1'">(<xsl:value-of select="@fieldwidth" />)</xsl:if>
<xsl:if test="position() != last()"> ,
</xsl:if>
</xsl:for-each>
)
</pre>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

####################### temp_选择表使用的SQL语句.xml ##################################
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="*/table">
-------------选择表 <xsl:value-of select="@tablename" /> 的SQL语句 <xsl:value-of select="count(*)" />个字段
<br /><pre style="word-wrap:break-word;background-color:gainsboro">
Select <xsl:for-each select="*">
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:value-of select="normalize-space(@fieldname)" />
<xsl:if test="position() != last()"> , </xsl:if>
</xsl:for-each>
From <xsl:value-of select="@tablename" /></pre>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>