Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (65 sloc) 2.51 KB
<#@ include file="OrmLite.Core.ttinclude" #>
<#
// Settings
ConnectionStringName = ""; // Uses last connection string in config if not specified
var SPNamespace = "StoredProcedures";
ClassPrefix = "";
ClassSuffix = "";
// Read schema
var sps = LoadSPs();
// Generate output
if (sps.Count>0)
{
#>
<#
if (string.IsNullOrEmpty(SPNamespace)) SPNamespace=ConnectionStringName;
if (string.IsNullOrEmpty(SPNamespace)) Namespace="StoredProcedures";
#>
using System;
using System.Data.Common;
using System.Data;
using System.Collections.Generic;
using ServiceStack.OrmLite;
namespace <#=SPNamespace#>
{
/// <summary>
/// Wraps the Stored Procedure Statement with the DbCommand. We need
/// access to the DbCommand to retrieve output variables. This class is needed
/// because OrmLiteSPStatement class hides the DbCommand
/// </summary>
public class OrmLiteStoredProcedureWrapper
{
public OrmLiteSPStatement Statement { get; set; }
public DbCommand DbCommand { get; set; }
}
public static class StoredProcedures
{
private static DbParameter CreateNewParameter(DbCommand dbCommand, string paramName, object paramValue, ParameterDirection paramDirection, DbType paramType)
{
var param = dbCommand.CreateParameter();
param.Direction = paramDirection;
param.DbType = paramType;
param.ParameterName = paramName;
param.Value = paramValue;
return param;
}
<#foreach(var sp in sps){#>
public static OrmLiteStoredProcedureWrapper <#=sp.Schema + "_" + sp.CleanName#>(this IDbConnection dbConnection<#foreach(var param in sp.Parameters){#>, <#=param.NullableSysType#> <#=Inflector.MakeInitialLowerCase(param.Name)#> = null<#}#>)
{
var dbCommand = (DbCommand)dbConnection.CreateCommand();
dbCommand.CommandText = "<#=sp.SchemaQualifiedName#>";
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.Transaction = OrmLiteConfig.TSTransaction!= null ? (DbTransaction)OrmLiteConfig.TSTransaction : null;
<#if (sp.Parameters.Count > 0) { foreach(var param in sp.Parameters){#>
dbCommand.Parameters.Add(CreateNewParameter(dbCommand,"<#=param.Name#>", <#=Inflector.MakeInitialLowerCase(param.Name)#>, ParameterDirection.<#=param.Direction#>, <#=param.DbType#>));
<#}#> <#}#>
return new OrmLiteStoredProcedureWrapper
{
Statement = new OrmLiteSPStatement(dbCommand),
DbCommand = dbCommand
};
}
<#}#>
}
}
<# } #>