初识CodeSmith分享两个模板
CodeSmith模板下载
在网上down下个C#能生成Oracle Package和Data Access methods
的模板,由于工作需要改成了VB的
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
C#
<%@ CodeTemplate Language="C#" Inherits="CodeSmith.BaseTemplates.SqlCodeTemplate" TargetLanguage="T-SQL" Debug=False Description="Generates SELECT/INSERT/UPDATE/DELETE stored procedure based on a database table schema." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
<%@ Property Name="IncludeInsert" Type="System.Boolean" Default="True" Category="Options" Description="If true insert statements will be generated." %>
<%@ Property Name="IncludeUpdate" Type="System.Boolean" Default="True" Category="Options" Description="If true update statements will be generated." %>
<%@ Property Name="IncludeDelete" Type="System.Boolean" Default="True" Category="Options" Description="If true delete statements will be generated." %>
<%@ Property Name="IncludeGet" Type="System.Boolean" Default="True" Category="Options" Description="If true get statements will be generated." %>
<%@ Property Name="IncludeGetList" Type="System.Boolean" Default="True" Category="Options" Description="If true getlist statements will be generated." %>
<%@ Property Name="IsolationLevel" Type="TransactionIsolationLevelEnum" Default="ReadCommitted" Category="Options" Description="Isolation level to use in generated procedures." %>
<%@ Property Name="InsertPrefix" Type="System.String" Default="Insert" Category="Style" Description="Prefix to use for all generated INSERT stored procedures." %>
<%@ Property Name="UpdatePrefix" Type="System.String" Default="Update" Category="Style" Description="Prefix to use for all generated UPDATE stored procedures." %>
<%@ Property Name="DeletePrefix" Type="System.String" Default="Delete" Category="Style" Description="Prefix to use for all generated DELETE stored procedures." %>
<%@ Property Name="SelectPrefix" Type="System.String" Default="Get" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Property Name="SelectAllPrefix" Type="System.String" Default="List" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<% if (SourceTable.PrimaryKey == null) throw new ApplicationException("SourceTable does not contain a primary key."); %>
/*Oracle Packages, Package Bodies and Sequences****************************************************************/
CREATE OR REPLACE PACKAGE <%=GetPackageName()%> AS
TYPE T_CURSOR IS REF CURSOR;
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Declaration---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
);
<% } %>
<% if (IncludeGetList) { %>
--Get All <%=SourceTable.Name%>s Declaration---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR);
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Declaration---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
);
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Declaration---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
);
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Declaration---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
);
<%}%>
END <%=GetPackageName()%>;
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
--create sequence if we need it-----
CREATE SEQUENCE <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
NOCACHE;
<%}%>
CREATE OR REPLACE PACKAGE BODY <%=GetPackageName()%> AS
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Body---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= SelectPrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeGetList) { %>
--Get <%=SourceTable.Name%> List Body---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%> ;
END <%= SelectAllPrefix %>All<%=SourceTable.Name%>s;
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Body---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
IS
BEGIN
UPDATE <%=SourceTable.Name%>
SET
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> = p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> <% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= UpdatePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Body---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
)
IS
BEGIN
DELETE FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= DeletePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Body---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
)
AS
BEGIN
INSERT INTO <%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
VALUES (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.NEXTVAL,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<%}%>
);
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
SELECT <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.CURRVAL INTO p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> FROM DUAL;
<%}%>
END <%= InsertPrefix %><%=SourceTable.Name%>;
<%}%>
END <%=GetPackageName()%>;
/* C# data access methods - uses to OracleClient driver ****************************************************************/
<% if (IncludeGet) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> record from db using a oracle
/// package
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> object. Will be null if record not found.
/// </summary>
public <%= SourceTable.Name %> Get<%= SourceTable.Name %>ById(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
{
<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %> = null;
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectPrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>;
<% } %>
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output;
try
{
// open the connection and create the DataReader
this._sqlCon.Open();
OracleDataReader dr = sqlCmd.ExecuteReader();
// output the results and close the connection. Should be <=1 results.
while(dr.Read())
{
p<%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>);
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpDataType(column)%>(dr["<%=GetPropertyName(column)%>"]);
<% }%>
}
this._sqlCon.Close();
}catch (OracleException pEx){
this._sqlCon.Close();
//do something here to catch exception
}
return p<%= GetClassName(SourceTable) %>;
}
<% } %>
<% if (IncludeGetList) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> collection object of all
/// <%= GetClassName(SourceTable) %> records in the db
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> collection object.
/// </summary>
public <%= GetCollectionClassName(SourceTable) %> GetAll<%= GetClassName(SourceTable) %>s()
{
<%= GetCollectionClassName(SourceTable) %> p<%= GetCollectionClassName(SourceTable) %> =new <%= GetCollectionClassName(SourceTable) %>();
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectAllPrefix %>All<%=SourceTable.Name%>s";
sqlCmd.CommandType = CommandType.StoredProcedure;
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output;
try
{
// open the connection and create the DataReader
this._sqlCon.Open();
OracleDataReader dr = sqlCmd.ExecuteReader();
// output the results and add to collection and close the connection.
while(dr.Read())
{
<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>Convert.To<%=GetCSharpVariableTypeForCasting(SourceTable.PrimaryKey.MemberColumns[i])%>(dr["<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>"])<% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>);
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpVariableTypeForCasting(column)%>(dr["<%=GetPropertyName(column)%>"]);
<% }%>
p<%= GetCollectionClassName(SourceTable) %>.Add(p<%=SourceTable.Name%>);
}
this._sqlCon.Close();
}catch (OracleException pEx){
this._sqlCon.Close();
//do something here to catch exception
}
return p<%= GetCollectionClassName(SourceTable) %>;
}
<% } %>
<% if (IncludeUpdate) { %>
/// <summary>
///Description :updates a <%= SourceTable.Name %> record in the db
///Pre-condition : none
///Post-condition: none
///Return : true - if record was update .
// false - if record was not updated;
/// </summary>
public bool <%= UpdatePrefix %><%= SourceTable.Name %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>)
{
bool pboolResult = false;
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= UpdatePrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>;
<% } %>
try
{
// open the connection
this._sqlCon.Open();
//execute the update command
sqlCmd.ExecuteNonQuery();
//close the connection
this._sqlCon.Close();
//if we got here we can assume that the update was succesful
pboolResult = true;
}
catch (OracleException pEx)
{
this._sqlCon.Close();
throw;
//do something here to catch exception
}
return pboolResult;
}
<% } %>
<% if (IncludeInsert) { %>
/// <summary>
///Description : create a new <%= SourceTable.Name %> record in the db
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
/// DB stored procedure will return PK used when inserting this record
<% } %>
///Pre-condition : none
///Post-condition: none
///Return : <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>PK id for this <%= SourceTable.Name %><% } else {%> None <% } %>.
/// </summary>
public <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>int<% }else{%>void<%}%> <%= InsertPrefix %><%= GetClassName(SourceTable) %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>)
{
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>int intNew<%= GetClassName(SourceTable) %>Id = -1;<%}%>
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= InsertPrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>;
<% } %>
<% } else{ //we need to retrieve an id %>
sqlCmd.Parameters.Add("p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],false)%>).Direction =ParameterDirection.Output;
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>;
<%}%>
<% } %>
try
{
// open the connection and run the insert command
this._sqlCon.Open();
sqlCmd.ExecuteNonQuery();
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
//retrieve return output from insert procedure
intNew<%= GetClassName(SourceTable) %>Id = Convert.ToInt32(sqlCmd.Parameters["p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>"].Value);
<% } %>
this._sqlCon.Close();
}
catch (OracleException pEx)
{
this._sqlCon.Close();
throw;
//do something here to catch exception
}
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>return intNewTESTUSERId;<% } %>
}
<% } %>
<% if (IncludeDelete) { %>
/// <summary>
///Description : Deletes a <%= SourceTable.Name %> record from the db
///Pre-condition : record has to exist in db
///Post-condition: none
///Return : true - if record was deleted successfully.
/// false - if record was not deleted;
/// </summary>
public bool Delete<%= SourceTable.Name %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
{
bool pboolResult = false;
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= DeletePrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>;
<% } %>
try
{
// open the connection and create the DataReader
this._sqlCon.Open();
sqlCmd.ExecuteNonQuery();
this._sqlCon.Close();
pboolResult = true;
}
catch (OracleException pEx)
{
this._sqlCon.Close();
throw;
//do something here to catch exception
}
return pboolResult;
}
<% } %>
<script runat="template">
public enum TransactionIsolationLevelEnum
{
ReadCommitted,
ReadUncommitted,
RepeatableRead,
Serializable
}
public string GetListProcedureName()
{
return SelectPrefix + SelectAllPrefix + SourceTable.Name+"s";
}
public string GetPackageName()
{
return SourceTable.Name+"Package";
}
public string GetSetTransactionIsolationLevelStatement()
{
switch (IsolationLevel)
{
case TransactionIsolationLevelEnum.ReadCommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
case TransactionIsolationLevelEnum.ReadUncommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
}
case TransactionIsolationLevelEnum.RepeatableRead:
{
return "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
}
case TransactionIsolationLevelEnum.Serializable:
{
return "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE";
}
}
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
public string GetTableOwner()
{
return GetTableOwner(true);
}
public string GetTableOwner(bool includeDot)
{
/*if (SourceTable.Owner.Length > 0)
{
return "[" + SourceTable.Owner + "].";
}
else
{
}*/
return "";
}
// methods to generate c# data access methods --------------------
public string GetEnums(){
/*string strResult = "";
string strArgName = "";
string strTabWidth = " ";
int intEnumCount = 0;
foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) {
intEnumCount++;
strArgName = GetMemberVariableName(column);
if (intEnumCount==1){
strResult += strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}else{
strResult += strTabWidth + strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}
} */
return "";//strResult.Substring(0,strResult.Length - 2);
}
public string GetReaderAssignmentStatement(ColumnSchema column, int index)
{
string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";
statement += GetMemberVariableName(column) + " = ";
if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";
statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";
return statement;
}
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = "_" + GetCamelCaseName(propertyName);
return memberVariableName;
}
public string GetClassName(TableSchema table)
{
if (table.Name.EndsWith("s"))
{
return table.Name.Substring(0, table.Name.Length - 1);
}
else
{
return table.Name;
}
}
public string GetCollectionClassName(TableSchema table)
{
return GetClassName(table)+"Collection";
}
public string GetPrimaryKeyType(TableSchema table)
{
if (table.PrimaryKey != null)
{
if (table.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
}
else
{
throw new ApplicationException("This template will not work on primary keys with more than one member column.");
}
}
else
{
throw new ApplicationException("This template will only work on tables with a primary key.");
}
}
public string GetCSharpVariableTypeForCasting(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "String";
case DbType.AnsiStringFixedLength: return "String";
case DbType.Binary: return "Byte";
case DbType.Boolean: return "Boolean";
case DbType.Byte: return "Byte";
case DbType.Currency: return "Decimal";
case DbType.Date: return "DateTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: return "Decimal";
case DbType.Double: return "Double";
case DbType.Int16: return "Int32";
case DbType.Int32: return "Int32";
case DbType.Int64: return "Int64";
case DbType.SByte: return "Byte";
case DbType.Single: return "Double";
case DbType.String: return "String";
case DbType.StringFixedLength: return "String";
case DbType.UInt16: return "UInt32";
case DbType.UInt32: return "UInt32";
case DbType.UInt64: return "UInt64";
case DbType.VarNumeric: return "Decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
public string GetCSharpDataType(ColumnSchema column){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Decimal"; break;
case "binary": strResult= "Byte"; break;
case "char": strResult= "String"; break;
case "date": strResult= "DateTime"; break;
case "long": strResult= "Int64"; break;
case "blob": strResult= "Byte"; break;
case "nvarchar2": strResult= "String"; break;
case "raw": strResult= "Byte"; break;
case "rowid": strResult= "String"; break;
case "clob": strResult= "Byte"; break;
case "nclob": strResult= "Byte"; break;
case "bfile": strResult= "Byte"; break;
case "float": strResult= "Single"; break;
case "varchar2": strResult= "String"; break;
}
return strResult;
}
public string GetOracleDataType(ColumnSchema column, bool boolForPackage){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Number"; break;
case "binary": strResult= "Binary"; break;
case "char": strResult= "Char"; break;
case "date":
if (boolForPackage){
strResult= "Date";
}else{
strResult= "DateTime";
}
break;
case "long": strResult= "Long"; break;
case "blob": strResult= "Blob"; break;
case "nvarchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "NVarChar";
}
break;
case "raw": strResult= "Raw"; break;
case "rowid": strResult= "RowId"; break;
case "clob": strResult= "Clob"; break;
case "nclob": strResult= "NClob"; break;
case "bfile": strResult= "BFile"; break;
case "float": strResult= "Float"; break;
case "varchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "VarChar";
}
break;
}
//put string into uppercase if its to be used in a package
if (boolForPackage){
strResult = strResult.ToUpper();
}
return strResult;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
VB.NET
<%@ CodeTemplate Language="C#" Inherits="CodeSmith.BaseTemplates.SqlCodeTemplate" TargetLanguage="T-SQL" Debug=False Description="Generates SELECT/INSERT/UPDATE/DELETE stored procedure based on a database table schema." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
<%@ Property Name="IncludeInsert" Type="System.Boolean" Default="True" Category="Options" Description="If true insert statements will be generated." %>
<%@ Property Name="IncludeUpdate" Type="System.Boolean" Default="True" Category="Options" Description="If true update statements will be generated." %>
<%@ Property Name="IncludeDelete" Type="System.Boolean" Default="True" Category="Options" Description="If true delete statements will be generated." %>
<%@ Property Name="IncludeGet" Type="System.Boolean" Default="True" Category="Options" Description="If true get statements will be generated." %>
<%@ Property Name="IncludeGetList" Type="System.Boolean" Default="True" Category="Options" Description="If true getlist statements will be generated." %>
<%@ Property Name="IsolationLevel" Type="TransactionIsolationLevelEnum" Default="ReadCommitted" Category="Options" Description="Isolation level to use in generated procedures." %>
<%@ Property Name="InsertPrefix" Type="System.String" Default="Insert" Category="Style" Description="Prefix to use for all generated INSERT stored procedures." %>
<%@ Property Name="UpdatePrefix" Type="System.String" Default="Update" Category="Style" Description="Prefix to use for all generated UPDATE stored procedures." %>
<%@ Property Name="DeletePrefix" Type="System.String" Default="Delete" Category="Style" Description="Prefix to use for all generated DELETE stored procedures." %>
<%@ Property Name="SelectPrefix" Type="System.String" Default="Get" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Property Name="SelectAllPrefix" Type="System.String" Default="List" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<% if (SourceTable.PrimaryKey == null) throw new ApplicationException("SourceTable does not contain a primary key."); %>
/*Oracle Packages, Package Bodies and Sequences****************************************************************/
CREATE OR REPLACE PACKAGE <%=GetPackageName()%> AS
TYPE T_CURSOR IS REF CURSOR;
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Declaration---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
);
<% } %>
<% if (IncludeGetList) { %>
--Get All <%=SourceTable.Name%>s Declaration---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR);
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Declaration---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
);
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Declaration---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
);
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Declaration---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
);
<%}%>
END <%=GetPackageName()%>;
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
--create sequence if we need it-----
CREATE SEQUENCE <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
NOCACHE;
<%}%>
CREATE OR REPLACE PACKAGE BODY <%=GetPackageName()%> AS
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Body---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= SelectPrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeGetList) { %>
--Get <%=SourceTable.Name%> List Body---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%> ;
END <%= SelectAllPrefix %>All<%=SourceTable.Name%>s;
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Body---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
IS
BEGIN
UPDATE <%=SourceTable.Name%>
SET
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> = p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> <% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= UpdatePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Body---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
)
IS
BEGIN
DELETE FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= DeletePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Body---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
)
AS
BEGIN
INSERT INTO <%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
VALUES (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.NEXTVAL,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<%}%>
);
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
SELECT <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.CURRVAL INTO p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> FROM DUAL;
<%}%>
END <%= InsertPrefix %><%=SourceTable.Name%>;
<%}%>
END <%=GetPackageName()%>;
/* C# data access methods - uses to OracleClient driver ****************************************************************/
<% if (IncludeGet) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> record from db using a oracle
/// package
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> object. Will be null if record not found.
/// </summary>
public Function Get<%= SourceTable.Name %>ById(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>) As <%= SourceTable.Name %>
Dim p<%= GetClassName(SourceTable) %> As <%= GetClassName(SourceTable) %> = Nothing
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectPrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>
<% } %>
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output
try
{
// open the connection and create the DataReader
Me._sqlCon.Open()
OracleDataReader dr = sqlCmd.ExecuteReader()
// output the results and close the connection. Should be <=1 results.
while dr.Read()
p<%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpDataType(column)%>(dr("<%=GetPropertyName(column)%>"))
<% }%>
End While
Me._sqlCon.Close()
catch (OracleException pEx){
Me._sqlCon.Close()
//do something here to catch exception
End Try
return p<%= GetClassName(SourceTable) %>
End Function
<% } %>
<% if (IncludeGetList) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> collection object of all
/// <%= GetClassName(SourceTable) %> records in the db
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> collection object.
/// </summary>
Public <%= GetCollectionClassName(SourceTable) %> GetAll<%= GetClassName(SourceTable) %>s() As <%= GetClassName(SourceTable) %>
Dim p<%= GetCollectionClassName(SourceTable) %> As <%= GetCollectionClassName(SourceTable) %> =Nothing
//create the command for the stored procedure
Dim sqlCmd As OracleCommand = New OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectAllPrefix %>All<%=SourceTable.Name%>s"
sqlCmd.CommandType = CommandType.StoredProcedure
//add the parameters for the stored procedure including the REF CURSOR
//to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output
Try
//open the connection and create the DataReader
Me._sqlCon.Open()
Dim dr As OracleDataReader = sqlCmd.ExecuteReader()
//output the results and add to collection and close the connection.
While dr.Read()
Dim p<%= GetClassName(SourceTable) %> As <%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>Convert.To<%=GetCSharpVariableTypeForCasting(SourceTable.PrimaryKey.MemberColumns[i])%>(dr("<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>"))<% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpVariableTypeForCasting(column)%>(dr("<%=GetPropertyName(column)%>"))
<% }%>
p<%= GetCollectionClassName(SourceTable) %>.Add(p<%=SourceTable.Name%>)
End While
Me._sqlCon.Close()
Catch pEx As OracleException
Me._sqlCon.Close()
//do something here to catch exception
End Try
Return p<%= GetCollectionClassName(SourceTable) %>
End Function
<% } %>
<% if (IncludeUpdate) { %>
/// <summary>
///Description :updates a <%= SourceTable.Name %> record in the db
///Pre-condition : none
///Post-condition: none
///Return : true - if record was update .
// false - if record was not updated;
/// </summary>
Public Function <%= UpdatePrefix %><%= SourceTable.Name %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>) As Boolean
Dim pboolResult As Boolean = false
//create the command for the stored procedure
Dim sqlCmd As OracleCommand = New OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= UpdatePrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>
<% } %>
try
// open the connection
Me._sqlCon.Open()
//execute the update command
sqlCmd.ExecuteNonQuery()
//close the connection
Me._sqlCon.Close()
//if we got here we can assume that the update was succesful
pboolResult = true
catch (OracleException pEx)
Me._sqlCon.Close();
//do something here to catch exception
End Try
return pboolResult
End Function
<% } %>
<% if (IncludeInsert) { %>
/// <summary>
///Description : create a new <%= SourceTable.Name %> record in the db
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
/// DB stored procedure will return PK used when inserting this record
<% } %>
///Pre-condition : none
///Post-condition: none
///Return : <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>PK id for this <%= SourceTable.Name %><% } else {%> None <% } %>.
/// </summary>
public <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>Function <% }else{%>Sub <%}%><%= InsertPrefix %><%= GetClassName(SourceTable) %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>) <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%> As Integer<% }%>
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>Dim intNew As Integer<%= GetClassName(SourceTable) %>Id = -1;<%}%>
// create the command for the stored procedure
Dim sqlCmd As OracleCommand = new OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= InsertPrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>
<% } %>
<% } else{ //we need to retrieve an id %>
sqlCmd.Parameters.Add("p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],false)%>).Direction =ParameterDirection.Output
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>
<%}%>
<% } %>
try
// open the connection and run the insert command
this._sqlCon.Open();
sqlCmd.ExecuteNonQuery();
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
//retrieve return output from insert procedure
intNew<%= GetClassName(SourceTable) %>Id = Convert.ToInt32(sqlCmd.Parameters("p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>").Value)
<% } %>
Me._sqlCon.Close()
catch (OracleException pEx)
Me._sqlCon.Close()
//do something here to catch exception
End Try
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>return intNewTESTUSERId;<% } %>
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>End Function<% }else{%>End Sub<%}%>
<% } %>
<% if (IncludeDelete) { %>
/// <summary>
///Description : Deletes a <%= SourceTable.Name %> record from the db
///Pre-condition : record has to exist in db
///Post-condition: none
///Return : true - if record was deleted successfully.
/// false - if record was not deleted;
/// </summary>
Public Function Delete<%= SourceTable.Name %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>) As Boolean
Dim pboolResult As Boolean = false
// create the command for the stored procedure
Dim sqlCmd As OracleCommand = new OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= DeletePrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>
<% } %>
try
// open the connection and create the DataReader
Me._sqlCon.Open()
sqlCmd.ExecuteNonQuery()
Me._sqlCon.Close()
pboolResult = true
catch (OracleException pEx)
Me._sqlCon.Close();
//do something here to catch exception
End Try
End Function
<% } %>
<script runat="template">
public enum TransactionIsolationLevelEnum
{
ReadCommitted,
ReadUncommitted,
RepeatableRead,
Serializable
}
public string GetListProcedureName()
{
return SelectPrefix + SelectAllPrefix + SourceTable.Name+"s";
}
public string GetPackageName()
{
return SourceTable.Name+"Package";
}
public string GetSetTransactionIsolationLevelStatement()
{
switch (IsolationLevel)
{
case TransactionIsolationLevelEnum.ReadCommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
case TransactionIsolationLevelEnum.ReadUncommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
}
case TransactionIsolationLevelEnum.RepeatableRead:
{
return "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
}
case TransactionIsolationLevelEnum.Serializable:
{
return "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE";
}
}
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
public string GetTableOwner()
{
return GetTableOwner(true);
}
public string GetTableOwner(bool includeDot)
{
/*if (SourceTable.Owner.Length > 0)
{
return "[" + SourceTable.Owner + "].";
}
else
{
}*/
return "";
}
// methods to generate c# data access methods --------------------
public string GetEnums(){
/*string strResult = "";
string strArgName = "";
string strTabWidth = " ";
int intEnumCount = 0;
foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) {
intEnumCount++;
strArgName = GetMemberVariableName(column);
if (intEnumCount==1){
strResult += strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}else{
strResult += strTabWidth + strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}
} */
return "";//strResult.Substring(0,strResult.Length - 2);
}
public string GetReaderAssignmentStatement(ColumnSchema column, int index)
{
string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";
statement += GetMemberVariableName(column) + " = ";
if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";
statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";
return statement;
}
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = "_" + GetCamelCaseName(propertyName);
return memberVariableName;
}
public string GetClassName(TableSchema table)
{
if (table.Name.EndsWith("s"))
{
return table.Name.Substring(0, table.Name.Length - 1);
}
else
{
return table.Name;
}
}
public string GetCollectionClassName(TableSchema table)
{
return GetClassName(table)+"Collection";
}
public string GetPrimaryKeyType(TableSchema table)
{
if (table.PrimaryKey != null)
{
if (table.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
}
else
{
throw new ApplicationException("This template will not work on primary keys with more than one member column.");
}
}
else
{
throw new ApplicationException("This template will only work on tables with a primary key.");
}
}
public string GetCSharpVariableTypeForCasting(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "String";
case DbType.AnsiStringFixedLength: return "String";
case DbType.Binary: return "Byte";
case DbType.Boolean: return "Boolean";
case DbType.Byte: return "Byte";
case DbType.Currency: return "Decimal";
case DbType.Date: return "DateTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: return "Decimal";
case DbType.Double: return "Double";
case DbType.Int16: return "Int32";
case DbType.Int32: return "Int32";
case DbType.Int64: return "Int64";
case DbType.SByte: return "Byte";
case DbType.Single: return "Double";
case DbType.String: return "String";
case DbType.StringFixedLength: return "String";
case DbType.UInt16: return "UInt32";
case DbType.UInt32: return "UInt32";
case DbType.UInt64: return "UInt64";
case DbType.VarNumeric: return "Decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
public string GetCSharpDataType(ColumnSchema column){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Decimal"; break;
case "binary": strResult= "Byte"; break;
case "char": strResult= "String"; break;
case "date": strResult= "DateTime"; break;
case "long": strResult= "Int64"; break;
case "blob": strResult= "Byte"; break;
case "nvarchar2": strResult= "String"; break;
case "raw": strResult= "Byte"; break;
case "rowid": strResult= "String"; break;
case "clob": strResult= "Byte"; break;
case "nclob": strResult= "Byte"; break;
case "bfile": strResult= "Byte"; break;
case "float": strResult= "Single"; break;
case "varchar2": strResult= "String"; break;
}
return strResult;
}
public string GetOracleDataType(ColumnSchema column, bool boolForPackage){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Number"; break;
case "binary": strResult= "Binary"; break;
case "char": strResult= "Char"; break;
case "date":
if (boolForPackage){
strResult= "Date";
}else{
strResult= "DateTime";
}
break;
case "long": strResult= "Long"; break;
case "blob": strResult= "Blob"; break;
case "nvarchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "NVarChar";
}
break;
case "raw": strResult= "Raw"; break;
case "rowid": strResult= "RowId"; break;
case "clob": strResult= "Clob"; break;
case "nclob": strResult= "NClob"; break;
case "bfile": strResult= "BFile"; break;
case "float": strResult= "Float"; break;
case "varchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "VarChar";
}
break;
}
//put string into uppercase if its to be used in a package
if (boolForPackage){
strResult = strResult.ToUpper();
}
return strResult;
}
</script>
CodeSmith 模板(C#版)
CodeSmith 模板(VB.NET版)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ CodeTemplate Language="C#" Inherits="CodeSmith.BaseTemplates.SqlCodeTemplate" TargetLanguage="T-SQL" Debug=False Description="Generates SELECT/INSERT/UPDATE/DELETE stored procedure based on a database table schema." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
<%@ Property Name="IncludeInsert" Type="System.Boolean" Default="True" Category="Options" Description="If true insert statements will be generated." %>
<%@ Property Name="IncludeUpdate" Type="System.Boolean" Default="True" Category="Options" Description="If true update statements will be generated." %>
<%@ Property Name="IncludeDelete" Type="System.Boolean" Default="True" Category="Options" Description="If true delete statements will be generated." %>
<%@ Property Name="IncludeGet" Type="System.Boolean" Default="True" Category="Options" Description="If true get statements will be generated." %>
<%@ Property Name="IncludeGetList" Type="System.Boolean" Default="True" Category="Options" Description="If true getlist statements will be generated." %>
<%@ Property Name="IsolationLevel" Type="TransactionIsolationLevelEnum" Default="ReadCommitted" Category="Options" Description="Isolation level to use in generated procedures." %>
<%@ Property Name="InsertPrefix" Type="System.String" Default="Insert" Category="Style" Description="Prefix to use for all generated INSERT stored procedures." %>
<%@ Property Name="UpdatePrefix" Type="System.String" Default="Update" Category="Style" Description="Prefix to use for all generated UPDATE stored procedures." %>
<%@ Property Name="DeletePrefix" Type="System.String" Default="Delete" Category="Style" Description="Prefix to use for all generated DELETE stored procedures." %>
<%@ Property Name="SelectPrefix" Type="System.String" Default="Get" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Property Name="SelectAllPrefix" Type="System.String" Default="List" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<% if (SourceTable.PrimaryKey == null) throw new ApplicationException("SourceTable does not contain a primary key."); %>
/*Oracle Packages, Package Bodies and Sequences****************************************************************/
CREATE OR REPLACE PACKAGE <%=GetPackageName()%> AS
TYPE T_CURSOR IS REF CURSOR;
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Declaration---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
);
<% } %>
<% if (IncludeGetList) { %>
--Get All <%=SourceTable.Name%>s Declaration---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR);
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Declaration---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
);
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Declaration---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
);
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Declaration---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
);
<%}%>
END <%=GetPackageName()%>;
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
--create sequence if we need it-----
CREATE SEQUENCE <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
NOCACHE;
<%}%>
CREATE OR REPLACE PACKAGE BODY <%=GetPackageName()%> AS
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Body---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= SelectPrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeGetList) { %>
--Get <%=SourceTable.Name%> List Body---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%> ;
END <%= SelectAllPrefix %>All<%=SourceTable.Name%>s;
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Body---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
IS
BEGIN
UPDATE <%=SourceTable.Name%>
SET
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> = p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> <% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= UpdatePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Body---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
)
IS
BEGIN
DELETE FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= DeletePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Body---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
)
AS
BEGIN
INSERT INTO <%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
VALUES (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.NEXTVAL,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<%}%>
);
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
SELECT <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.CURRVAL INTO p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> FROM DUAL;
<%}%>
END <%= InsertPrefix %><%=SourceTable.Name%>;
<%}%>
END <%=GetPackageName()%>;
/* C# data access methods - uses to OracleClient driver ****************************************************************/
<% if (IncludeGet) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> record from db using a oracle
/// package
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> object. Will be null if record not found.
/// </summary>
public <%= SourceTable.Name %> Get<%= SourceTable.Name %>ById(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
{
<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %> = null;
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectPrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>;
<% } %>
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output;
try
{
// open the connection and create the DataReader
this._sqlCon.Open();
OracleDataReader dr = sqlCmd.ExecuteReader();
// output the results and close the connection. Should be <=1 results.
while(dr.Read())
{
p<%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>);
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpDataType(column)%>(dr["<%=GetPropertyName(column)%>"]);
<% }%>
}
this._sqlCon.Close();
}catch (OracleException pEx){
this._sqlCon.Close();
//do something here to catch exception
}
return p<%= GetClassName(SourceTable) %>;
}
<% } %>
<% if (IncludeGetList) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> collection object of all
/// <%= GetClassName(SourceTable) %> records in the db
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> collection object.
/// </summary>
public <%= GetCollectionClassName(SourceTable) %> GetAll<%= GetClassName(SourceTable) %>s()
{
<%= GetCollectionClassName(SourceTable) %> p<%= GetCollectionClassName(SourceTable) %> =new <%= GetCollectionClassName(SourceTable) %>();
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectAllPrefix %>All<%=SourceTable.Name%>s";
sqlCmd.CommandType = CommandType.StoredProcedure;
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output;
try
{
// open the connection and create the DataReader
this._sqlCon.Open();
OracleDataReader dr = sqlCmd.ExecuteReader();
// output the results and add to collection and close the connection.
while(dr.Read())
{
<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>Convert.To<%=GetCSharpVariableTypeForCasting(SourceTable.PrimaryKey.MemberColumns[i])%>(dr["<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>"])<% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>);
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpVariableTypeForCasting(column)%>(dr["<%=GetPropertyName(column)%>"]);
<% }%>
p<%= GetCollectionClassName(SourceTable) %>.Add(p<%=SourceTable.Name%>);
}
this._sqlCon.Close();
}catch (OracleException pEx){
this._sqlCon.Close();
//do something here to catch exception
}
return p<%= GetCollectionClassName(SourceTable) %>;
}
<% } %>
<% if (IncludeUpdate) { %>
/// <summary>
///Description :updates a <%= SourceTable.Name %> record in the db
///Pre-condition : none
///Post-condition: none
///Return : true - if record was update .
// false - if record was not updated;
/// </summary>
public bool <%= UpdatePrefix %><%= SourceTable.Name %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>)
{
bool pboolResult = false;
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= UpdatePrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>;
<% } %>
try
{
// open the connection
this._sqlCon.Open();
//execute the update command
sqlCmd.ExecuteNonQuery();
//close the connection
this._sqlCon.Close();
//if we got here we can assume that the update was succesful
pboolResult = true;
}
catch (OracleException pEx)
{
this._sqlCon.Close();
throw;
//do something here to catch exception
}
return pboolResult;
}
<% } %>
<% if (IncludeInsert) { %>
/// <summary>
///Description : create a new <%= SourceTable.Name %> record in the db
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
/// DB stored procedure will return PK used when inserting this record
<% } %>
///Pre-condition : none
///Post-condition: none
///Return : <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>PK id for this <%= SourceTable.Name %><% } else {%> None <% } %>.
/// </summary>
public <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>int<% }else{%>void<%}%> <%= InsertPrefix %><%= GetClassName(SourceTable) %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>)
{
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>int intNew<%= GetClassName(SourceTable) %>Id = -1;<%}%>
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= InsertPrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>;
<% } %>
<% } else{ //we need to retrieve an id %>
sqlCmd.Parameters.Add("p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],false)%>).Direction =ParameterDirection.Output;
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>;
<%}%>
<% } %>
try
{
// open the connection and run the insert command
this._sqlCon.Open();
sqlCmd.ExecuteNonQuery();
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
//retrieve return output from insert procedure
intNew<%= GetClassName(SourceTable) %>Id = Convert.ToInt32(sqlCmd.Parameters["p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>"].Value);
<% } %>
this._sqlCon.Close();
}
catch (OracleException pEx)
{
this._sqlCon.Close();
throw;
//do something here to catch exception
}
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>return intNewTESTUSERId;<% } %>
}
<% } %>
<% if (IncludeDelete) { %>
/// <summary>
///Description : Deletes a <%= SourceTable.Name %> record from the db
///Pre-condition : record has to exist in db
///Post-condition: none
///Return : true - if record was deleted successfully.
/// false - if record was not deleted;
/// </summary>
public bool Delete<%= SourceTable.Name %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
{
bool pboolResult = false;
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand();
sqlCmd.Connection = this._sqlCon; //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= DeletePrefix %><%= SourceTable.Name %>";
sqlCmd.CommandType = CommandType.StoredProcedure;
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>;
<% } %>
try
{
// open the connection and create the DataReader
this._sqlCon.Open();
sqlCmd.ExecuteNonQuery();
this._sqlCon.Close();
pboolResult = true;
}
catch (OracleException pEx)
{
this._sqlCon.Close();
throw;
//do something here to catch exception
}
return pboolResult;
}
<% } %>
<script runat="template">
public enum TransactionIsolationLevelEnum
{
ReadCommitted,
ReadUncommitted,
RepeatableRead,
Serializable
}
public string GetListProcedureName()
{
return SelectPrefix + SelectAllPrefix + SourceTable.Name+"s";
}
public string GetPackageName()
{
return SourceTable.Name+"Package";
}
public string GetSetTransactionIsolationLevelStatement()
{
switch (IsolationLevel)
{
case TransactionIsolationLevelEnum.ReadCommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
case TransactionIsolationLevelEnum.ReadUncommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
}
case TransactionIsolationLevelEnum.RepeatableRead:
{
return "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
}
case TransactionIsolationLevelEnum.Serializable:
{
return "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE";
}
}
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
public string GetTableOwner()
{
return GetTableOwner(true);
}
public string GetTableOwner(bool includeDot)
{
/*if (SourceTable.Owner.Length > 0)
{
return "[" + SourceTable.Owner + "].";
}
else
{
}*/
return "";
}
// methods to generate c# data access methods --------------------
public string GetEnums(){
/*string strResult = "";
string strArgName = "";
string strTabWidth = " ";
int intEnumCount = 0;
foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) {
intEnumCount++;
strArgName = GetMemberVariableName(column);
if (intEnumCount==1){
strResult += strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}else{
strResult += strTabWidth + strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}
} */
return "";//strResult.Substring(0,strResult.Length - 2);
}
public string GetReaderAssignmentStatement(ColumnSchema column, int index)
{
string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";
statement += GetMemberVariableName(column) + " = ";
if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";
statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";
return statement;
}
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = "_" + GetCamelCaseName(propertyName);
return memberVariableName;
}
public string GetClassName(TableSchema table)
{
if (table.Name.EndsWith("s"))
{
return table.Name.Substring(0, table.Name.Length - 1);
}
else
{
return table.Name;
}
}
public string GetCollectionClassName(TableSchema table)
{
return GetClassName(table)+"Collection";
}
public string GetPrimaryKeyType(TableSchema table)
{
if (table.PrimaryKey != null)
{
if (table.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
}
else
{
throw new ApplicationException("This template will not work on primary keys with more than one member column.");
}
}
else
{
throw new ApplicationException("This template will only work on tables with a primary key.");
}
}
public string GetCSharpVariableTypeForCasting(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "String";
case DbType.AnsiStringFixedLength: return "String";
case DbType.Binary: return "Byte";
case DbType.Boolean: return "Boolean";
case DbType.Byte: return "Byte";
case DbType.Currency: return "Decimal";
case DbType.Date: return "DateTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: return "Decimal";
case DbType.Double: return "Double";
case DbType.Int16: return "Int32";
case DbType.Int32: return "Int32";
case DbType.Int64: return "Int64";
case DbType.SByte: return "Byte";
case DbType.Single: return "Double";
case DbType.String: return "String";
case DbType.StringFixedLength: return "String";
case DbType.UInt16: return "UInt32";
case DbType.UInt32: return "UInt32";
case DbType.UInt64: return "UInt64";
case DbType.VarNumeric: return "Decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
public string GetCSharpDataType(ColumnSchema column){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Decimal"; break;
case "binary": strResult= "Byte"; break;
case "char": strResult= "String"; break;
case "date": strResult= "DateTime"; break;
case "long": strResult= "Int64"; break;
case "blob": strResult= "Byte"; break;
case "nvarchar2": strResult= "String"; break;
case "raw": strResult= "Byte"; break;
case "rowid": strResult= "String"; break;
case "clob": strResult= "Byte"; break;
case "nclob": strResult= "Byte"; break;
case "bfile": strResult= "Byte"; break;
case "float": strResult= "Single"; break;
case "varchar2": strResult= "String"; break;
}
return strResult;
}
public string GetOracleDataType(ColumnSchema column, bool boolForPackage){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Number"; break;
case "binary": strResult= "Binary"; break;
case "char": strResult= "Char"; break;
case "date":
if (boolForPackage){
strResult= "Date";
}else{
strResult= "DateTime";
}
break;
case "long": strResult= "Long"; break;
case "blob": strResult= "Blob"; break;
case "nvarchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "NVarChar";
}
break;
case "raw": strResult= "Raw"; break;
case "rowid": strResult= "RowId"; break;
case "clob": strResult= "Clob"; break;
case "nclob": strResult= "NClob"; break;
case "bfile": strResult= "BFile"; break;
case "float": strResult= "Float"; break;
case "varchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "VarChar";
}
break;
}
//put string into uppercase if its to be used in a package
if (boolForPackage){
strResult = strResult.ToUpper();
}
return strResult;
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ CodeTemplate Language="C#" Inherits="CodeSmith.BaseTemplates.SqlCodeTemplate" TargetLanguage="T-SQL" Debug=False Description="Generates SELECT/INSERT/UPDATE/DELETE stored procedure based on a database table schema." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
<%@ Property Name="IncludeInsert" Type="System.Boolean" Default="True" Category="Options" Description="If true insert statements will be generated." %>
<%@ Property Name="IncludeUpdate" Type="System.Boolean" Default="True" Category="Options" Description="If true update statements will be generated." %>
<%@ Property Name="IncludeDelete" Type="System.Boolean" Default="True" Category="Options" Description="If true delete statements will be generated." %>
<%@ Property Name="IncludeGet" Type="System.Boolean" Default="True" Category="Options" Description="If true get statements will be generated." %>
<%@ Property Name="IncludeGetList" Type="System.Boolean" Default="True" Category="Options" Description="If true getlist statements will be generated." %>
<%@ Property Name="IsolationLevel" Type="TransactionIsolationLevelEnum" Default="ReadCommitted" Category="Options" Description="Isolation level to use in generated procedures." %>
<%@ Property Name="InsertPrefix" Type="System.String" Default="Insert" Category="Style" Description="Prefix to use for all generated INSERT stored procedures." %>
<%@ Property Name="UpdatePrefix" Type="System.String" Default="Update" Category="Style" Description="Prefix to use for all generated UPDATE stored procedures." %>
<%@ Property Name="DeletePrefix" Type="System.String" Default="Delete" Category="Style" Description="Prefix to use for all generated DELETE stored procedures." %>
<%@ Property Name="SelectPrefix" Type="System.String" Default="Get" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Property Name="SelectAllPrefix" Type="System.String" Default="List" Category="Style" Description="Prefix to use for all generated SELECT stored procedures." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<% if (SourceTable.PrimaryKey == null) throw new ApplicationException("SourceTable does not contain a primary key."); %>
/*Oracle Packages, Package Bodies and Sequences****************************************************************/
CREATE OR REPLACE PACKAGE <%=GetPackageName()%> AS
TYPE T_CURSOR IS REF CURSOR;
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Declaration---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
);
<% } %>
<% if (IncludeGetList) { %>
--Get All <%=SourceTable.Name%>s Declaration---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR);
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Declaration---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
);
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Declaration---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
);
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Declaration---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
);
<%}%>
END <%=GetPackageName()%>;
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
--create sequence if we need it-----
CREATE SEQUENCE <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
NOCACHE;
<%}%>
CREATE OR REPLACE PACKAGE BODY <%=GetPackageName()%> AS
<% if (IncludeGet) { %>
--Get <%=SourceTable.Name%> Body---
PROCEDURE <%= SelectPrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %>,
<% } %>
cur_<%=SourceTable.Name%> OUT T_CURSOR
)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= SelectPrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeGetList) { %>
--Get <%=SourceTable.Name%> List Body---
PROCEDURE <%= SelectAllPrefix %>All<%=SourceTable.Name%>s (cur_<%=SourceTable.Name%> OUT T_CURSOR)
IS
BEGIN
OPEN cur_<%=SourceTable.Name%> FOR
SELECT * FROM <%=SourceTable.Name%> ;
END <%= SelectAllPrefix %>All<%=SourceTable.Name%>s;
<% } %>
<% if (IncludeUpdate) { %>
--Update <%=SourceTable.Name%> Body---
PROCEDURE <%= UpdatePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
IS
BEGIN
UPDATE <%=SourceTable.Name%>
SET
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> = p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> <% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= UpdatePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeDelete) { %>
--Delete <%=SourceTable.Name%> Body---
PROCEDURE <%= DeletePrefix %><%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%>
<% } %>
)
IS
BEGIN
DELETE FROM <%=SourceTable.Name%>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%> = p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i])%><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %> AND <% }%>
<% } %>;
END <%= DeletePrefix %><%=SourceTable.Name%>;
<% } %>
<% if (IncludeInsert) { %>
--Insert <%=SourceTable.Name%> Body---
PROCEDURE <%= InsertPrefix %><%=SourceTable.Name%> (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%> IN <%=GetOracleDataType(SourceTable.Columns[i],true)%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> OUT <%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],true)%>,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%> IN <%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],true)%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<% } %>
)
AS
BEGIN
INSERT INTO <%=SourceTable.Name%> (
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
)
VALUES (
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.Columns[i])%><% if (i < (SourceTable.Columns.Count - 1) ) { %>,<% }%>
<%}%>
<%}else{%>
<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.NEXTVAL,
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
p_<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])%><% if (i < (SourceTable.NonPrimaryKeyColumns.Count - 1) ) { %>,<% }%>
<%}%>
<%}%>
);
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
SELECT <%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>_SEQ.CURRVAL INTO p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%> FROM DUAL;
<%}%>
END <%= InsertPrefix %><%=SourceTable.Name%>;
<%}%>
END <%=GetPackageName()%>;
/* C# data access methods - uses to OracleClient driver ****************************************************************/
<% if (IncludeGet) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> record from db using a oracle
/// package
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> object. Will be null if record not found.
/// </summary>
public Function Get<%= SourceTable.Name %>ById(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>) As <%= SourceTable.Name %>
Dim p<%= GetClassName(SourceTable) %> As <%= GetClassName(SourceTable) %> = Nothing
// create the command for the stored procedure
OracleCommand sqlCmd = new OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectPrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>
<% } %>
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output
try
{
// open the connection and create the DataReader
Me._sqlCon.Open()
OracleDataReader dr = sqlCmd.ExecuteReader()
// output the results and close the connection. Should be <=1 results.
while dr.Read()
p<%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpDataType(column)%>(dr("<%=GetPropertyName(column)%>"))
<% }%>
End While
Me._sqlCon.Close()
catch (OracleException pEx){
Me._sqlCon.Close()
//do something here to catch exception
End Try
return p<%= GetClassName(SourceTable) %>
End Function
<% } %>
<% if (IncludeGetList) { %>
/// <summary>
///Description :gets a <%= GetClassName(SourceTable) %> collection object of all
/// <%= GetClassName(SourceTable) %> records in the db
///Pre-condition : none
///Post-condition: none
///Return : <%= GetClassName(SourceTable) %> collection object.
/// </summary>
Public <%= GetCollectionClassName(SourceTable) %> GetAll<%= GetClassName(SourceTable) %>s() As <%= GetClassName(SourceTable) %>
Dim p<%= GetCollectionClassName(SourceTable) %> As <%= GetCollectionClassName(SourceTable) %> =Nothing
//create the command for the stored procedure
Dim sqlCmd As OracleCommand = New OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= SelectAllPrefix %>All<%=SourceTable.Name%>s"
sqlCmd.CommandType = CommandType.StoredProcedure
//add the parameters for the stored procedure including the REF CURSOR
//to retrieve the result set
sqlCmd.Parameters.Add("cur_<%= GetClassName(SourceTable) %>", OracleType.Cursor).Direction =
ParameterDirection.Output
Try
//open the connection and create the DataReader
Me._sqlCon.Open()
Dim dr As OracleDataReader = sqlCmd.ExecuteReader()
//output the results and add to collection and close the connection.
While dr.Read()
Dim p<%= GetClassName(SourceTable) %> As <%= GetClassName(SourceTable) %> = new <%= GetClassName(SourceTable) %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>Convert.To<%=GetCSharpVariableTypeForCasting(SourceTable.PrimaryKey.MemberColumns[i])%>(dr("<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>"))<% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>)
<% foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) { %>
p<%= GetClassName(SourceTable) %>.<%=GetPropertyName(column)%> = Convert.To<%=GetCSharpVariableTypeForCasting(column)%>(dr("<%=GetPropertyName(column)%>"))
<% }%>
p<%= GetCollectionClassName(SourceTable) %>.Add(p<%=SourceTable.Name%>)
End While
Me._sqlCon.Close()
Catch pEx As OracleException
Me._sqlCon.Close()
//do something here to catch exception
End Try
Return p<%= GetCollectionClassName(SourceTable) %>
End Function
<% } %>
<% if (IncludeUpdate) { %>
/// <summary>
///Description :updates a <%= SourceTable.Name %> record in the db
///Pre-condition : none
///Post-condition: none
///Return : true - if record was update .
// false - if record was not updated;
/// </summary>
Public Function <%= UpdatePrefix %><%= SourceTable.Name %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>) As Boolean
Dim pboolResult As Boolean = false
//create the command for the stored procedure
Dim sqlCmd As OracleCommand = New OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= UpdatePrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>
<% } %>
try
// open the connection
Me._sqlCon.Open()
//execute the update command
sqlCmd.ExecuteNonQuery()
//close the connection
Me._sqlCon.Close()
//if we got here we can assume that the update was succesful
pboolResult = true
catch (OracleException pEx)
Me._sqlCon.Close();
//do something here to catch exception
End Try
return pboolResult
End Function
<% } %>
<% if (IncludeInsert) { %>
/// <summary>
///Description : create a new <%= SourceTable.Name %> record in the db
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
/// DB stored procedure will return PK used when inserting this record
<% } %>
///Pre-condition : none
///Post-condition: none
///Return : <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>PK id for this <%= SourceTable.Name %><% } else {%> None <% } %>.
/// </summary>
public <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>Function <% }else{%>Sub <%}%><%= InsertPrefix %><%= GetClassName(SourceTable) %>(<%= GetClassName(SourceTable) %> p<%= GetClassName(SourceTable) %>) <% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%> As Integer<% }%>
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>Dim intNew As Integer<%= GetClassName(SourceTable) %>Id = -1;<%}%>
// create the command for the stored procedure
Dim sqlCmd As OracleCommand = new OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= InsertPrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% if (SourceTable.PrimaryKey.MemberColumns.Count >1){%>
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(column) %>",OracleType.<%=GetOracleDataType(column,false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(column) %>
<% } %>
<% } else{ //we need to retrieve an id %>
sqlCmd.Parameters.Add("p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[0],false)%>).Direction =ParameterDirection.Output
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.NonPrimaryKeyColumns[i],false)%>).Value =p<%= GetClassName(SourceTable)%>.<%=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>
<%}%>
<% } %>
try
// open the connection and run the insert command
this._sqlCon.Open();
sqlCmd.ExecuteNonQuery();
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>
//retrieve return output from insert procedure
intNew<%= GetClassName(SourceTable) %>Id = Convert.ToInt32(sqlCmd.Parameters("p_<%=GetPropertyName(SourceTable.PrimaryKey.MemberColumns[0])%>").Value)
<% } %>
Me._sqlCon.Close()
catch (OracleException pEx)
Me._sqlCon.Close()
//do something here to catch exception
End Try
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>return intNewTESTUSERId;<% } %>
<% if (SourceTable.PrimaryKey.MemberColumns.Count ==1){%>End Function<% }else{%>End Sub<%}%>
<% } %>
<% if (IncludeDelete) { %>
/// <summary>
///Description : Deletes a <%= SourceTable.Name %> record from the db
///Pre-condition : record has to exist in db
///Post-condition: none
///Return : true - if record was deleted successfully.
/// false - if record was not deleted;
/// </summary>
Public Function Delete<%= SourceTable.Name %>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i])%> <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < (SourceTable.PrimaryKey.MemberColumns.Count - 1) ) { %>,<% }%><% } %>) As Boolean
Dim pboolResult As Boolean = false
// create the command for the stored procedure
Dim sqlCmd As OracleCommand = new OracleCommand()
sqlCmd.Connection = Me._sqlCon //this should change to whatever ur conn variable is;
//oracle package body name
sqlCmd.CommandText = "<%= GetPackageName() %>.<%= DeletePrefix %><%= SourceTable.Name %>"
sqlCmd.CommandType = CommandType.StoredProcedure
// build stored procedure arguements.
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
sqlCmd.Parameters.Add("p_<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>",OracleType.<%=GetOracleDataType(SourceTable.PrimaryKey.MemberColumns[i],false)%>).Value =<%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>
<% } %>
try
// open the connection and create the DataReader
Me._sqlCon.Open()
sqlCmd.ExecuteNonQuery()
Me._sqlCon.Close()
pboolResult = true
catch (OracleException pEx)
Me._sqlCon.Close();
//do something here to catch exception
End Try
End Function
<% } %>
<script runat="template">
public enum TransactionIsolationLevelEnum
{
ReadCommitted,
ReadUncommitted,
RepeatableRead,
Serializable
}
public string GetListProcedureName()
{
return SelectPrefix + SelectAllPrefix + SourceTable.Name+"s";
}
public string GetPackageName()
{
return SourceTable.Name+"Package";
}
public string GetSetTransactionIsolationLevelStatement()
{
switch (IsolationLevel)
{
case TransactionIsolationLevelEnum.ReadCommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
case TransactionIsolationLevelEnum.ReadUncommitted:
{
return "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
}
case TransactionIsolationLevelEnum.RepeatableRead:
{
return "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
}
case TransactionIsolationLevelEnum.Serializable:
{
return "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE";
}
}
return "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
}
public string GetTableOwner()
{
return GetTableOwner(true);
}
public string GetTableOwner(bool includeDot)
{
/*if (SourceTable.Owner.Length > 0)
{
return "[" + SourceTable.Owner + "].";
}
else
{
}*/
return "";
}
// methods to generate c# data access methods --------------------
public string GetEnums(){
/*string strResult = "";
string strArgName = "";
string strTabWidth = " ";
int intEnumCount = 0;
foreach (ColumnSchema column in SourceTable.NonPrimaryKeyColumns) {
intEnumCount++;
strArgName = GetMemberVariableName(column);
if (intEnumCount==1){
strResult += strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}else{
strResult += strTabWidth + strArgName.Substring(1,strArgName.Length - 1) + ",\n";
}
} */
return "";//strResult.Substring(0,strResult.Length - 2);
}
public string GetReaderAssignmentStatement(ColumnSchema column, int index)
{
string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";
statement += GetMemberVariableName(column) + " = ";
if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";
statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";
return statement;
}
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = "_" + GetCamelCaseName(propertyName);
return memberVariableName;
}
public string GetClassName(TableSchema table)
{
if (table.Name.EndsWith("s"))
{
return table.Name.Substring(0, table.Name.Length - 1);
}
else
{
return table.Name;
}
}
public string GetCollectionClassName(TableSchema table)
{
return GetClassName(table)+"Collection";
}
public string GetPrimaryKeyType(TableSchema table)
{
if (table.PrimaryKey != null)
{
if (table.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
}
else
{
throw new ApplicationException("This template will not work on primary keys with more than one member column.");
}
}
else
{
throw new ApplicationException("This template will only work on tables with a primary key.");
}
}
public string GetCSharpVariableTypeForCasting(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "String";
case DbType.AnsiStringFixedLength: return "String";
case DbType.Binary: return "Byte";
case DbType.Boolean: return "Boolean";
case DbType.Byte: return "Byte";
case DbType.Currency: return "Decimal";
case DbType.Date: return "DateTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: return "Decimal";
case DbType.Double: return "Double";
case DbType.Int16: return "Int32";
case DbType.Int32: return "Int32";
case DbType.Int64: return "Int64";
case DbType.SByte: return "Byte";
case DbType.Single: return "Double";
case DbType.String: return "String";
case DbType.StringFixedLength: return "String";
case DbType.UInt16: return "UInt32";
case DbType.UInt32: return "UInt32";
case DbType.UInt64: return "UInt64";
case DbType.VarNumeric: return "Decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
public string GetCSharpDataType(ColumnSchema column){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Decimal"; break;
case "binary": strResult= "Byte"; break;
case "char": strResult= "String"; break;
case "date": strResult= "DateTime"; break;
case "long": strResult= "Int64"; break;
case "blob": strResult= "Byte"; break;
case "nvarchar2": strResult= "String"; break;
case "raw": strResult= "Byte"; break;
case "rowid": strResult= "String"; break;
case "clob": strResult= "Byte"; break;
case "nclob": strResult= "Byte"; break;
case "bfile": strResult= "Byte"; break;
case "float": strResult= "Single"; break;
case "varchar2": strResult= "String"; break;
}
return strResult;
}
public string GetOracleDataType(ColumnSchema column, bool boolForPackage){
string strResult = "__UNKNOWN__" + column.NativeType;
//note: not all oracle data types are supported yet
switch (column.NativeType.Trim().ToLower())
{
case "number": strResult= "Number"; break;
case "binary": strResult= "Binary"; break;
case "char": strResult= "Char"; break;
case "date":
if (boolForPackage){
strResult= "Date";
}else{
strResult= "DateTime";
}
break;
case "long": strResult= "Long"; break;
case "blob": strResult= "Blob"; break;
case "nvarchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "NVarChar";
}
break;
case "raw": strResult= "Raw"; break;
case "rowid": strResult= "RowId"; break;
case "clob": strResult= "Clob"; break;
case "nclob": strResult= "NClob"; break;
case "bfile": strResult= "BFile"; break;
case "float": strResult= "Float"; break;
case "varchar2":
if (boolForPackage){
strResult= "VarChar2";
}else{
strResult= "VarChar";
}
break;
}
//put string into uppercase if its to be used in a package
if (boolForPackage){
strResult = strResult.ToUpper();
}
return strResult;
}
</script>
CodeSmith 模板(C#版)
CodeSmith 模板(VB.NET版)
![]() | 本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 |