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

C#分析数据库结构,使用XSL模板自动生成代码
看过一些自动生成"笨"代码的工具,小弟也自己做了一个,使用C#分析数据库结构,并使用XSL来 生成关于数据库表结构的代码,只需修改配置文件dbxmlcfg.xml中OLEDB数据库连接字符串就可 更新数据库连接属性,XSL高手还可以自己来编制代码生成模块, 模板文件为temp_模板名.xsl。 下图为用户界面

####################### index.htm   ##################################
 1<html>
 2<head>
 3<title>分析数据库结构,自动生成代码</title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5</head>
 6<frameset cols="237,767" rows="*">
 7<frame src="dbxml.aspx"/>
 8<frame name="code" src="about:blank"/>
 9</frameset>
10</html>

########################### dbxml.aspx 文件内容,该文件没有C#代码文件 #############

  1<script language="C#" runat="server">
  2        System.Xml.XmlDocument myCfgXML = new System.Xml.XmlDocument();
  3        // 获得系统配置字符串
  4        string GetAppConfig(string strKey)
  5        {
  6            System.Xml.XmlElement cfgElement =  myCfgXML.SelectSingleNode ("//setting[@key='" + strKey + "']" )
  7                as System.Xml.XmlElement ;
  8            if( cfgElement == null )
  9                return "";
 10            else
 11                return cfgElement.InnerText ;
 12        }
 13        
 14        // 判断字符串是否是空白字符串
 15        bool isBlankString(string strText )
 16        {
 17            if(strText != null)
 18            {
 19                int iCount;
 20                for(iCount=0;iCount<strText.Length ;iCount++)
 21                {
 22                    if(System.Char.IsWhiteSpace ( strText[iCount])==false)
 23                        return false;
 24                }
 25            }
 26            return true;
 27        }
 28        void Page_Load(Object sender, EventArgs e)
 29        {
 30            // 加载系统配置文件
 31            myCfgXML.Load(this.MapPath(".") + "\\dbxmlcfg.xml");
 32            string strType    = this.Request["type"];
 33            string strXSL    = "main.xml";
 34            
 35            if(strType == null)
 36                strType = "querytable";
 37            System.Xml.XmlDocument myDoc = new System.Xml.XmlDocument();
 38            myDoc.LoadXml("<dbxml />");
 39            string strConnection = GetAppConfig("conndbxml");
 40            System.Text.Encoding myEncode = System.Text.Encoding.GetEncoding(936);
 41                                
 42            if(isBlankString(strConnection)==false)
 43            {
 44                using(System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection(strConnection))
 45                {
 46                    myConn.Open();
 47                    if(myConn.State == System.Data.ConnectionState.Open )
 48                    {
 49                        string strSQL = GetAppConfig(strType + "_" + myConn.Provider);
 50                        if(isBlankString(strSQL)==false)
 51                        {
 52                            using(System.Data.OleDb.OleDbCommand myCmd = myConn.CreateCommand())
 53                            {
 54                                string strTableName = null;
 55                                if(strType.Equals("queryfield"))
 56                                {
 57                                    // 修正SQL语句
 58                                    string strTableList = this.Request.Form["tablelist"];
 59                                    string []strTables = strTableList.Split(",".ToCharArray());
 60                                    strXSL = System.Web.HttpUtility.UrlPathEncode(this.Request.Form["template"] ) + ".xml";
 61                                    strTableList = null;
 62                                    for(int iCount = 0 ; iCount < strTables.Length ; iCount ++ )
 63                                    {
 64                                        if(isBlankString(strTables[iCount])==false)
 65                                        {
 66                                            if(strTableList == null)
 67                                                strTableList = "'" + strTables[iCount] + "'";
 68                                            else
 69                                                strTableList = strTableList + ",'" + strTables[iCount] + "'";
 70                                        }
 71                                    }
 72                                    strSQL = strSQL.Replace("#tablelist", strTableList);
 73                                    myCmd.CommandText                    = strSQL ;
 74                                    string strLastTableName                = null;
 75                                    string strFieldName                    = null;
 76                                    System.Xml.XmlElement TableElement    = null;
 77                                    System.Data.OleDb.OleDbDataReader myReader = myCmd.ExecuteReader();
 78                                    while(myReader.Read())
 79                                    {
 80                                        strTableName = myReader[0].ToString().ToUpper();
 81                                        if(strTableName.Equals(strLastTableName)==false)
 82                                        {
 83                                            // 填充表说明元素
 84                                            strLastTableName    = strTableName ;
 85                                            TableElement        = myDoc.CreateElement("table");
 86                                            TableElement.SetAttribute("tablename", strTableName);
 87                                            myDoc.DocumentElement.AppendChild(TableElement);
 88                                        }
 89                                        // 填充字段说明元素
 90                                        System.Xml.XmlElement FieldElement = myDoc.CreateElement("field");
 91                                            FieldElement.SetAttribute("fieldname",    myReader[1].ToString());
 92                                            FieldElement.SetAttribute("fieldtype",    myReader[2].ToString());
 93                                            FieldElement.SetAttribute("fieldwidth",    myReader[3].ToString());
 94                                            FieldElement.SetAttribute("isstring",  (myReader[2].ToString().ToUpper().IndexOf("CHAR")>=0?"1":"0"));
 95                                            strFieldName = myReader[1].ToString();
 96                                            int iLen = myEncode.GetByteCount(strFieldName);
 97                                            if(iLen < 20)
 98                                                FieldElement.SetAttribute("fixname", strFieldName + new string(' ', 20 - iLen));
 99                                        TableElement.AppendChild(FieldElement);
100                                    }
101                                    myReader.Close();
102                                }
103                                else
104                                {
105                                    // 填充模板列表
106                                    string [] strFileNames = System.IO.Directory.GetFiles(this.Server.MapPath("."),"temp_*.xml");
107                                    for(int iCount = 0 ; iCount < strFileNames.Length ; iCount ++ )
108                                    {
109                                        System.Xml.XmlElement tempXML = myDoc.CreateElement("template");
110                                        tempXML.SetAttribute("key",System.IO.Path.GetFileNameWithoutExtension(strFileNames[iCount]));
111                                        myDoc.DocumentElement.AppendChild(tempXML);
112                                    }
113                                    // 填充表名列表
114                                    myCmd.CommandText = strSQL ;
115                                    System.Data.OleDb.OleDbDataReader myReader = myCmd.ExecuteReader();
116                                    System.Xml.XmlElement TableElement = null;
117                                    while(myReader.Read())
118                                    {
119                                        TableElement = myDoc.CreateElement("table");
120                                        myDoc.DocumentElement.AppendChild(TableElement);
121                                        strTableName = myReader[0].ToString();
122                                        TableElement.SetAttribute("name", strTableName );
123                                        TableElement.SetAttribute("count", myReader[1].ToString());
124                                        int iLen = myEncode.GetByteCount(strTableName);
125                                        if(iLen < 20 )
126                                            TableElement.SetAttribute("fixname",strTableName + new string(' ', 20 - iLen));
127                                    }
128                                    myReader.Close();
129                                }
130                            }
131                        }
132                    }
133                    myConn.Close();
134                }
135            }
136            // 输出文档
137            this.Response.ContentType = "text/xml";
138            this.Response.ContentEncoding = myEncode ;
139            this.Response.Write("<?xml version=\"1.0\" encoding=\"GB2312\" ?>");
140            this.Response.Write("<?xml-stylesheet type=\"text/xsl\" href=\"" + strXSL  + "\"?>");
141            this.Response.Write(myDoc.DocumentElement.OuterXml);
142        }
143    </script>

文件 dbxmlcfg.xml内容


 1<application>
 2<settings>
 3<setting key="conn">Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=CPR;Initial Catalog=HTIOA;Data Source=192.168.0.124</setting>
 4<setting key="conndbxml">Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=IssueVision;Data Source=(local)</setting>
 5<!-- 
 6            
 7                定义查询表结构使用的SQL语句,  
 8                
 9                queryfield_驱动程序名称 定义了查询指定表的字段定义的SQL语句,该语句带有一个参数 
10                
11                querytable_驱动程序名称 定义了查询所有表名及其字段个数的SQL语句,该语句没有参数 
12                
13                目前定义了 oracle和ms sql server 的SQL语句 
14                
15             -->
16<setting key="queryfield_OraOLEDB.Oracle.1">Select TName  ,CName  ,coltype  ,width    From  Col  where tname in (#tablelist) Order by TName,CName</setting>
17<setting key="querytable_OraOLEDB.Oracle.1">Select TName   ,count(*)   From Col group by tname Order by TName </setting>
18<setting key="queryfield_SQLOLEDB.1"><![CDATA[select  sysobjects.name  ,syscolumns.name   ,systypes.name  ,syscolumns.length  from syscolumns,sysobjects,systypes where syscolumns.id=sysobjects.id and syscolumns.xtype=systypes.xtype and sysobjects.type='U' and systypes.name <>'_default_' and systypes.name<>'sysname' and sysobjects.name in (#tablelist) order by sysobjects.name,syscolumns.name]]></setting>
19<setting key="querytable_SQLOLEDB.1"><![CDATA[select  sysobjects.name ,count(*) from syscolumns,sysobjects,systypes where syscolumns.id=sysobjects.id and syscolumns.xtype=systypes.xtype and sysobjects.type='U' and systypes.name <>'_default_' and systypes.name<>'sysname' group by sysobjects.name order by sysobjects.name]]></setting>
20</settings>
21</application>

####################### main.xml ##################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:output indent="yes" method="xml"></xsl:output>
 3<xsl:template match="/*">
 4<html>
 5<head>
 6<title></title>
 7<style>
 8                    select2{ font-family: "宋体"; font-size: 12px}
 9                    body{  font-family: "宋体"; font-size: 12px} 
10                    table{ width:100%; border-collapse:collapse;
11                        border: 1px #CC0066 solid; font-family: "宋体"; 
12                        font-size: 12px}
13                    .tablehead{background-color:#CCCCFF}
14                    td{ border: 1px #CC0066 solid}            </style>
15</head>
16<body leftmargin="1" rightmargin="1" topmargin="1">
17<form action="dbxml.aspx?type=queryfield" method="POST" name="frm" target="code">
18                    模板<select name="template">
19<xsl:for-each select="template">
20<option>
21<xsl:attribute name="value">
22<xsl:value-of select="@key"></xsl:value-of>
23</xsl:attribute>
24<xsl:value-of select="@key"></xsl:value-of>
25</option>
26</xsl:for-each>
27</select>
28<input type="submit" value="提交"/>
29<table>
30<tr class="tablehead">
31<td nowrap="1">选择</td>
32<td nowrap="1">表名</td>
33<td nowrap="1">字段个数</td>
34</tr>
35<xsl:for-each select="table">
36<tr>
37<td nowrap="1">
38<input name="tablelist" style="width:13;height:13" type="checkbox"/>
39<xsl:attribute name="value">
40<xsl:value-of select="@name"></xsl:value-of>
41</xsl:attribute>
42</td>
43<td>
44<xsl:value-of select="@name"></xsl:value-of>
45</td>
46<td>
47<xsl:value-of select="@count"></xsl:value-of>
48</td>
49</tr>
50</xsl:for-each>
51</table>
52</form>
53</body>
54</html>
55</xsl:template>
56</xsl:stylesheet>

############################### temp_CSharp.xml 内容 ############################################

  1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2<xsl:output indent="yes" method="xml"></xsl:output>
  3<xsl:template match="/">
  4<html>
  5<head></head>
  6<body>
  7<xsl:for-each select="*/table">
  8<br/>-------------- 文件名 <xsl:value-of select="@tablename"></xsl:value-of>.cs -----------------------------
  9                <pre style=" background-color:gainsboro">
 10    //-----------------------------------------------------------------------------
 11    <xsl:text disable-output-escaping="yes">
 12        /// <summary></summary></xsl:text>
 13        /// 数据库表 <xsl:value-of select="@tablename"></xsl:value-of> 操作对象
 14        /// 编制: 代码生成器
 15        /// 时间:
 16    <xsl:text disable-output-escaping="yes">    /// </xsl:text>
 17        public class Struct<xsl:value-of select="@tablename"></xsl:value-of>  : CommonStruct
 18        {    private const string c_TableName ="<xsl:value-of select="@tablename"></xsl:value-of>";
 19            // 定义数据库字段变量 ////////////////////////////////////////////////////////////////
 20            <xsl:for-each select="*">
 21                <xsl:variable name="csharptype">
 22                    <xsl:choose>
 23                        <xsl:when test="@isstring='1'">string </xsl:when>
 24                        <xsl:when test="boolean('true')">int    </xsl:when>
 25                    </xsl:choose>
 26                </xsl:variable>
 27                <xsl:variable name="lowfieldname">
 28                    <xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>
 29                </xsl:variable>
 30            private  <xsl:value-of select="$csharptype"></xsl:value-of> m_<xsl:value-of select="@fixname"></xsl:value-of> ; // 字段 <xsl:value-of select="@cname"></xsl:value-of> 
 31            </xsl:for-each>
 32            
 33            // 定义属性 ///////////////////////////////////////////////////////////
 34            <xsl:for-each select="*">
 35            <xsl:variable name="csharptype">
 36                    <xsl:choose>
 37                        <xsl:when test="@isstring='1'">string </xsl:when>
 38                        <xsl:when test="boolean('true')">int    </xsl:when>
 39                    </xsl:choose>
 40                </xsl:variable>
 41            /// <xsl:text disable-output-escaping="yes">&lt;summary&gt;</xsl:text>
 42            /// 设置/返回数据库字段属性 <xsl:value-of select="@cname"></xsl:value-of>
 43             /// <xsl:text disable-output-escaping="yes">&lt;/summary&gt;</xsl:text>
 44            /// <xsl:text disable-output-escaping="yes">&lt;returns&gt;</xsl:text><xsl:value-of select="@cname"></xsl:value-of><xsl:text disable-output-escaping="yes"> &lt;/returns&gt;</xsl:text>
 45            public <xsl:value-of select="$csharptype"></xsl:value-of> m<xsl:value-of select="@fieldname"></xsl:value-of>
 46            {
 47                get{ return m_<xsl:value-of select="@fieldname"></xsl:value-of>  ;}
 48                set{ m_<xsl:value-of select="@fieldname"></xsl:value-of> = value ;}
 49            }
 50            </xsl:for-each>
 51            
 52            
 53            new public  static string getTableName()
 54            {return c_TableName ;}
 55            new public static string getSelectSQL()
 56            {
 57                return  "Select <xsl:for-each select="*">
 58                                <xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>
 59                                <xsl:if test="position() != last()">,</xsl:if>
 60                            </xsl:for-each> From " + c_TableName ;
 61            }
 62            new public static string getTypeName()
 63            {
 64                return "<xsl:value-of select="translate(@tablename,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>";
 65            }
 66            new public  static string SearchKey(string strKey)
 67            {    
 68                return getSelectSQL() + " Where SEQ =" + strKey  ;
 69            }
 70            public override bool SetCommandParams( System.Data.OleDb.OleDbCommand myCmd,bool SetValues )
 71            {
 72                if(myCmd!= null)
 73                {
 74                    myCmd.Parameters.Clear ();
 75                    <xsl:for-each select="*">
 76                        myCmd.Parameters.Add("<xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>",System.Data.OleDb.OleDbType.<xsl:if test="@isstring='1'">VarWChar</xsl:if><xsl:if test="@isstring='0'">Integer</xsl:if>);</xsl:for-each>
 77                    if(SetValues)
 78                    {
 79                        <xsl:for-each select="*">
 80                        myCmd.Parameters[<xsl:value-of select="position()-1"></xsl:value-of>].Value = m_<xsl:value-of select="@fieldname"></xsl:value-of> ; // 字段 <xsl:value-of select="@cname"></xsl:value-of> 
 81                        </xsl:for-each>
 82                    }
 83                    return true;
 84                }
 85                return false;
 86            }
 87            public override bool SetInsertCommand( System.Data.OleDb.OleDbCommand myCmd)
 88            {
 89                if(myCmd != null)
 90                {
 91                    myCmd.CommandText ="Insert Into " + c_TableName 
 92                                + " ( <xsl:for-each select="*">[<xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>]<xsl:if test="position() != last()">,</xsl:if>
 93                            </xsl:for-each>) Values (<xsl:for-each select="*">?<xsl:if test="position() != last()">,</xsl:if>
 94                            </xsl:for-each>)";
 95                    return this.SetCommandParams(myCmd,true);
 96                }
 97                return false;
 98            }
 99            public override bool SetUpdateCommand(System.Data.OleDb.OleDbCommand myCmd)
100            {
101                if(myCmd != null)
102                {
103                    myCmd.CommandText ="Update " + c_TableName    
104                                + " Set <xsl:for-each select="*">[<xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>]=? <xsl:if test="position() != last()">,</xsl:if></xsl:for-each> Where SEQ=" + m_SEQ ;
105                    return this.SetCommandParams(myCmd,true);
106                }
107                return false;
108            }
109            public override bool SelectRS(System.Data.OleDb.OleDbDataReader myReader)
110            {
111                try
112                {
113                    if(myReader != null)
114                    {
115                        if (myReader.FieldCount==5)
116                        {
117                            <xsl:for-each select="*">
118                            m_<xsl:value-of select="@fixname"></xsl:value-of> = Convert.To<xsl:if test="@isstring='1'">String</xsl:if><xsl:if test="@isstring='0'">Int32</xsl:if>(myReader[<xsl:value-of select="position()-1"></xsl:value-of>]);</xsl:for-each>
119                            return true;
120                        }
121                    }
122                }
123                catch 
124                {}
125                return false;
126            }
127            public override bool ToXML(System.Xml.XmlElement myElement)
128            {
129                if(myElement != null)
130                {
131                    <xsl:for-each select="*">
132                    myElement.SetAttribute("<xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>",m_<xsl:value-of select="@fieldname"></xsl:value-of><xsl:if test="@isstring='0'">.ToString()</xsl:if>);</xsl:for-each>
133                    return true;
134                }
135                return false;
136            }
137            public override bool FromXML(System.Xml.XmlElement myElement)
138            {
139                try
140                {
141                    if(myElement != null)
142                    {
143                        <xsl:for-each select="*">
144                        m_<xsl:value-of select="@fixname"></xsl:value-of> = <xsl:if test="@isstring='0'">Convert.ToInt32(</xsl:if>myElement.GetAttribute("<xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>")<xsl:if test="@isstring='0'">)</xsl:if>;</xsl:for-each>
145                        return true;
146                    }
147                }
148                catch
149                {}
150                return false;
151            }
152        }// 数据库操作类 Struct<xsl:value-of select="@tablename"></xsl:value-of> 定义结束
153    </pre>
154</xsl:for-each>
155</body>
156</html>
157</xsl:template>
158</xsl:stylesheet>

########################## temp_HTML代码.xml #####################################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:output indent="yes" method="xml"></xsl:output>
 3<xsl:template match="/*">
 4<html>
 5<head>
 6<style>
 7                body{    font-family: "宋体"; font-size: 12px} 
 8                table { width:100%; border-collapse:collapse;
 9                        border: 1px #CC0066 solid; font-family: "宋体"; 
10                        font-size: 12px}
11                .tablehead{background-color:#CCCCFF}
12                td{ border: 1px #CC0066 solid}
13                </style>
14</head>
15<body>
16<xsl:for-each select="table">
17            数据表 <b><xsl:value-of select="translate(@tablename,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"></xsl:value-of></b> 结构
18            共 <xsl:value-of select="count(*)"></xsl:value-of> 个字段
19            <br/>
20<table>
21<tr class="tablehead">
22<td>字段名</td>
23<td>类型</td>
24<td>长度</td>
25</tr>
26<xsl:for-each select="*">
27<tr>
28<td>
29<xsl:value-of select="@fieldname"></xsl:value-of>
30</td>
31<td>
32<xsl:value-of select="@fieldtype"></xsl:value-of>
33</td>
34<td>
35<xsl:value-of select="@fieldwidth"></xsl:value-of>
36</td>
37</tr>
38</xsl:for-each>
39</table>
40<p></p><hr/>
41</xsl:for-each>
42</body>
43</html>
44</xsl:template>
45</xsl:stylesheet>

################################ temp_Java_Struct.xml #######################################################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:output indent="yes" method="xml"></xsl:output>
 3<xsl:template match="/">
 4<html><head></head><body>
 5<xsl:for-each select="*/table">
 6<br/>-------------- 文件名 <xsl:value-of select="@tablename"></xsl:value-of>.java -----------------------------
 7                <pre style=" background-color:gainsboro">
 8    
 9    package com.haitai.emr.struct;
10    import java.sql.*;
11    import java.io.*;
12    /**   <xsl:value-of select="@cname"></xsl:value-of>
13     *    @author 代码生成器 */
14    public class <xsl:value-of select="@tablename"></xsl:value-of> implements Serializable
15    {        // 定义数据库字段变量 ////////////////////////////////////////////////////////////////
16            <xsl:for-each select="*">
17            <xsl:variable name="javatype">
18                        <xsl:choose>
19                            <xsl:when test="@isstring='1'">String  </xsl:when>
20                            <xsl:when test="boolean('true')">int     </xsl:when>
21                        </xsl:choose>
22                    </xsl:variable>
23                    <xsl:variable name="lowfieldname">
24                        <xsl:value-of select="translate(@fixname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>
25                    </xsl:variable>
26        private <xsl:value-of select="$javatype"></xsl:value-of> <xsl:text disable-output-escaping="yes"></xsl:text><xsl:value-of select="$lowfieldname"></xsl:value-of> ; // 字段 <xsl:value-of select="@cname"></xsl:value-of>  
27            </xsl:for-each>
28                    public static final String SELECT = 
29                           "Select <xsl:for-each select="*">
30                    <xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>
31                    <xsl:if test="position() != last()">,</xsl:if>
32                </xsl:for-each> From <xsl:value-of select="@tablename"></xsl:value-of>";
33        /** @param conn 
34         * @exception SQLException */
35       public java.sql.PreparedStatement makeInsSt (java.sql.Connection conn) throws SQLException{
36            PreparedStatement pst=conn.prepareStatement("insert into <xsl:value-of select="@tablename"></xsl:value-of>(<xsl:for-each select="*">
37                    <xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>
38                    <xsl:if test="position() != last()">,</xsl:if>
39                </xsl:for-each>)"
40                 +"values(<xsl:for-each select="*">?<xsl:if test="position() != last()">,</xsl:if></xsl:for-each>)");
41          int index=0;
42          <xsl:for-each select="*">
43          pst.setString(++index,this.get<xsl:value-of select="@fieldname"></xsl:value-of>()); // <xsl:value-of select="@cname"></xsl:value-of>
44          </xsl:for-each>
45          return pst;
46       }
47       
48        /** @param conn 
49         * @exception SQLException */
50       public java.sql.PreparedStatement makeUpdSt (java.sql.Connection conn) throws SQLException{
51          
52             // TODO : implement
53             PreparedStatement pst=conn.prepareStatement("update <xsl:value-of select="@tablename"></xsl:value-of> set <xsl:for-each select="*"><xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of> =? <xsl:if test="position() != last()">,</xsl:if></xsl:for-each>)"
54             +"where 数据表关键字段名=?");
55             int index=0;
56           <xsl:for-each select="*">
57          pst.setString(++index,this.get<xsl:value-of select="@fieldname"></xsl:value-of>()); // <xsl:value-of select="@cname"></xsl:value-of>
58          </xsl:for-each>
59     
60             //关键字
61             pst.setString(++index,this.get数据表关键字段名());//数据表关键字段说明
62       
63             return pst;
64          }
65       
66       public String toString (){
67       
68          // TODO : implement
69          return <xsl:for-each select="*">"<xsl:if test="position() != 1">,</xsl:if><xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>="+ <xsl:value-of select="@lowfieldname"></xsl:value-of><xsl:if test="position() != last()"> + </xsl:if> </xsl:for-each>;
70       }
71       
72       // 读取和修改数据的接口
73       <xsl:for-each select="*">
74            <xsl:variable name="javatype">
75                        <xsl:choose>
76                            <xsl:when test="@isstring='1'">String</xsl:when>
77                            <xsl:when test="boolean('true')">int</xsl:when>
78                        </xsl:choose>
79                    </xsl:variable>
80                    <xsl:variable name="lowfieldname">
81                        <xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>
82                    </xsl:variable>
83           public <xsl:value-of select="$javatype"></xsl:value-of> get<xsl:value-of select="@fieldname"></xsl:value-of>(){
84               return <xsl:value-of select="normalize-space($lowfieldname)"></xsl:value-of> ;
85           }
86           //@param <xsl:value-of select="@cname"></xsl:value-of>
87           public void  set<xsl:value-of select="@fieldname"></xsl:value-of>(<xsl:value-of select="@javatype"></xsl:value-of> value){
88               <xsl:value-of select="normalize-space($lowfieldname)"></xsl:value-of> = value ;
89           }
90       </xsl:for-each>
91    } // 类 <xsl:value-of select="@tablename"></xsl:value-of> 定义结束
92    </pre>
93</xsl:for-each>
94</body>
95</html>
96</xsl:template>
97</xsl:stylesheet>

######################################## temp_VB.xml ############################################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:output indent="yes" method="xml"></xsl:output>
 3<xsl:template match="/">
 4<html>
 5<head></head>
 6<body>
 7<xsl:for-each select="*/table">
 8<br/>-------------- 文件名 <xsl:value-of select="@tablename"></xsl:value-of>.cls -----------------------------
 9                <pre style=" background-color:gainsboro">
10    '******************************************************************************
11    '**
12    '**          数据表 <xsl:value-of select="@cname"></xsl:value-of>[ <xsl:value-of select="@tablename"></xsl:value-of> ]操作的对象
13    '**
14    '** 编制:代码生成器 
15    '** 时间:
16    '**
17    '******************************************************************************
18    '** 定义和数据库字段对应的变量 *************************************************************
19        private const c_TableName As String  = "<xsl:value-of select="@tablename"></xsl:value-of>"        '** 数据表名称
20        <xsl:for-each select="*">
21        <xsl:variable name="vbtype">
22                    <xsl:choose>
23                        <xsl:when test="@isstring='1'">String  </xsl:when>
24                        <xsl:when test="boolean('true')">Integer </xsl:when>
25                    </xsl:choose>
26                </xsl:variable>
27                <xsl:variable name="lowfieldname">
28                    <xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"></xsl:value-of>
29                </xsl:variable>
30        private m_<xsl:value-of select="@fixname"></xsl:value-of> As <xsl:value-of select="$vbtype"></xsl:value-of>  '** 字段 <xsl:value-of select="@cname"></xsl:value-of>    
31        </xsl:for-each>
32        
33    '** 定义数据库字段属性接口 ***************************************************************
34            Public Property Get TableName() As String
35                TableName = c_TableName
36            End Property
37        <xsl:for-each select="*">
38        <xsl:variable name="vbtype">
39                    <xsl:choose>
40                        <xsl:when test="@isstring='1'">String  </xsl:when>
41                        <xsl:when test="boolean('true')">Integer </xsl:when>
42                    </xsl:choose>
43                </xsl:variable>
44            '** 数据库字段 <xsl:value-of select="@cname"></xsl:value-of>
45            Public Property Get m<xsl:value-of select="@fieldname"></xsl:value-of>() As <xsl:value-of select="$vbtype"></xsl:value-of>
46                m<xsl:value-of select="@fieldname"></xsl:value-of> =  m_<xsl:value-of select="@fieldname"></xsl:value-of>
47            End Property
48            Public Property Let m<xsl:value-of select="@fieldname"></xsl:value-of>(Byval Value As <xsl:value-of select="$vbtype"></xsl:value-of>)
49                m_<xsl:value-of select="@fieldname"></xsl:value-of> =  m<xsl:value-of select="@fieldname"></xsl:value-of>
50            End Property
51        </xsl:for-each>
52    '** 获得查询所有数据使用的SQL语句 **
53            public Function GetBaseSQL() As String 
54                GetBaseSQL ="Select <xsl:for-each select="*">
55                                <xsl:value-of select="@fieldname"></xsl:value-of>
56                                <xsl:if test="position() != last()">,</xsl:if>
57                            </xsl:for-each> From " <xsl:text disable-output-escaping="yes">&amp;</xsl:text> c_TableName
58            End Function
59            
60    '** 定义从数据库记录集获得数据的方法 **
61        Public Function SelectRS(ByVal rs As ADODB.Recordset) As Boolean
62            On Error GoTo SelectErr
63            SelectRS = False
64            <xsl:for-each select="*">
65            m_<xsl:value-of select="@fixname"></xsl:value-of> = rs.Fields(<xsl:value-of select="position()-1"></xsl:value-of>).Value  '** 字段 <xsl:value-of select="@cname"></xsl:value-of>
66            </xsl:for-each>
67            SelectRS = True
68            Exit Function
69    SelectErr:
70            SelectRS = False
71        End Function
72        
73        
74        
75    </pre>
76</xsl:for-each>
77</body>
78</html>
79</xsl:template>
80</xsl:stylesheet>

####################### temp_表说明文档.xml ##################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:template match="/">
 3<html>
 4<head></head>
 5<body>
 6<xsl:for-each select="*/table">
 7    -------------表 <xsl:value-of select="@tablename"></xsl:value-of> 的说明文档 <xsl:value-of select="count(*)"></xsl:value-of>个字段
 8    <br/><pre style="word-wrap:break-word;background-color:gainsboro">
 9    <xsl:for-each select="*"><xsl:value-of select="@fixname"></xsl:value-of><xsl:text disable-output-escaping="yes">      </xsl:text><xsl:value-of select="@fieldtype"></xsl:value-of>  
10    <xsl:if test="@isstring='1'">(<xsl:value-of select="@fieldwidth"></xsl:value-of>)</xsl:if>                .
11    <xsl:text disable-output-escaping="yes"></xsl:text>
12    </xsl:for-each>
13                        </pre>
14</xsl:for-each>
15</body>
16</html>
17</xsl:template>
18</xsl:stylesheet>

####################### temp_创建表的SQL语句.xml ##################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:template match="/">
 3<html>
 4<head></head>
 5<body>
 6<xsl:for-each select="*/table">
 7    -------------创建表 <xsl:value-of select="@tablename"></xsl:value-of> 的SQL语句 <xsl:value-of select="count(*)"></xsl:value-of>个字段
 8    <br/><pre style="word-wrap:break-word;background-color:gainsboro">
 9    CREATE TABLE <xsl:value-of select="@tablename"></xsl:value-of>( 
10        <xsl:for-each select="*">
11    <xsl:text disable-output-escaping="yes"></xsl:text>
12                                <xsl:value-of select="@fixname"></xsl:value-of>
13                                <xsl:value-of select="@fieldtype"></xsl:value-of>
14                                <xsl:if test="@isstring='1'">(<xsl:value-of select="@fieldwidth"></xsl:value-of>)</xsl:if>
15                                <xsl:if test="position() != last()"> ,
16            </xsl:if>
17                            </xsl:for-each>
18    )
19                            </pre>
20</xsl:for-each>
21</body>
22</html>
23</xsl:template>
24</xsl:stylesheet>

####################### temp_选择表使用的SQL语句.xml ##################################

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:template match="/">
 3<html>
 4<head></head>
 5<body>
 6<xsl:for-each select="*/table">
 7    -------------选择表 <xsl:value-of select="@tablename"></xsl:value-of> 的SQL语句 <xsl:value-of select="count(*)"></xsl:value-of>个字段
 8    <br/><pre style="word-wrap:break-word;background-color:gainsboro">
 9    Select <xsl:for-each select="*">
10                                <xsl:text disable-output-escaping="yes"></xsl:text>
11                                <xsl:value-of select="normalize-space(@fieldname)"></xsl:value-of>
12                                <xsl:if test="position() != last()"> , </xsl:if>
13                            </xsl:for-each> 
14    From <xsl:value-of select="@tablename"></xsl:value-of></pre>
15</xsl:for-each>
16</body>
17</html>
18</xsl:template>
19</xsl:stylesheet>

`

Published At
Categories with Web编程
Tagged with
comments powered by Disqus