C#分析數據庫結構,使用XSL模板自動生成代碼(二)

########################## 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>

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章