|
<%@ Page Language="C#" %>
<%@ Import namespace="SubSonic.Utilities"%>
<%@ Import Namespace="SubSonic" %>
<%
//The data we need
string providerName = "#PROVIDER#";
string tableName = "#TABLE#";
TableSchema.Table tbl = DataService.GetSchema(tableName, providerName, TableType.Table);
DataProvider provider = DataService.Providers[providerName];
ICodeLanguage lang = new CSharpCodeLanguage();
//The main vars we need
TableSchema.TableColumnCollection cols = tbl.Columns;
TableSchema.TableColumn[] keys = cols.GetPrimaryKeys();
%>
//Generated on <%=DateTime.Now.ToString() %> by <%=Environment.UserName %>
namespace <%=provider.GeneratedNamespace %>
{
/// <summary>
/// Controller class for <%=tbl.Name %>
/// </summary>
[System.ComponentModel.DataObject]
public partial class <%=tbl.ClassName %>Controller
{
// Preload our schema..
<%=tbl.ClassName%> thisSchemaLoad = new <%=tbl.ClassName%>();
private string userName = string.Empty;
protected string UserName
{
get
{
if (userName.Length == 0)
{
if (System.Web.HttpContext.Current != null)
{
userName=System.Web.HttpContext.Current.User.Identity.Name;
}
else
{
userName=System.Threading.Thread.CurrentPrincipal.Identity.Name;
}
}
return userName;
}
}
[DataObjectMethod(DataObjectMethodType.Select, true)]
public <%=tbl.ClassName%>Collection FetchAll()
{
<%=tbl.ClassName%>Collection coll = new <%=tbl.ClassName%>Collection();
Query qry = new Query(<%=tbl.ClassName%>.Schema);
coll.LoadAndCloseReader(qry.ExecuteReader());
return coll;
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public <%=tbl.ClassName%>Collection FetchByID(object <%=tbl.PrimaryKey.PropertyName%>)
{
<%=tbl.ClassName%>Collection coll = new <%=tbl.ClassName%>Collection().Where("<%=tbl.PrimaryKey.ColumnName %>", <%=tbl.PrimaryKey.PropertyName%>).Load();
return coll;
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public <%=tbl.ClassName%>Collection FetchByQuery(Query qry)
{
<%=tbl.ClassName%>Collection coll = new <%=tbl.ClassName%>Collection();
coll.LoadAndCloseReader(qry.ExecuteReader());
return coll;
}
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public bool Delete(object <%=tbl.PrimaryKey.PropertyName%>)
{
return (<%=tbl.ClassName%>.Delete(<%=tbl.PrimaryKey.PropertyName%>) == 1);
}
[DataObjectMethod(DataObjectMethodType.Delete, false)]
public bool Destroy(object <%=tbl.PrimaryKey.PropertyName%>)
{
return (<%=tbl.ClassName%>.Destroy(<%=tbl.PrimaryKey.PropertyName%>) == 1);
}
<%
string deleteArgs = string.Empty;
string whereArgs = string.Empty;
string deleteDelim = "";
string whereDelim = "";
bool useNullables = tbl.Provider.GenerateNullableProperties;
foreach (TableSchema.TableColumn key in keys)
{
string propName = key.PropertyName;
bool useNullType = useNullables ? key.IsNullable : false;
string varType = Utility.GetVariableType(key.DataType, useNullType, lang);
deleteArgs += deleteDelim + varType + " " + propName;
deleteDelim = ",";
whereArgs += whereDelim + "(\"" + propName + "\", " + propName + ")";
whereDelim = ".AND";
}
// add this delete method if the table has multiple keys
if (keys.Length > 0)
{
%>
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public bool Delete(<%=deleteArgs%>)
{
Query qry = new Query(<%=tbl.ClassName%>.Schema);
qry.QueryType = QueryType.Delete;
qry.AddWhere<%=whereArgs%>;
qry.Execute();
return (true);
}
<%
}
string insertArgs = string.Empty;
string updateArgs = string.Empty;
string seperator = ",";
foreach (TableSchema.TableColumn col in cols)
{
string propName = col.PropertyName;
bool useNullType = useNullables ? col.IsNullable : false;
string varType = Utility.GetVariableType(col.DataType, useNullType, lang);
updateArgs += varType + " " + propName + seperator;
if (!col.AutoIncrement)
{
insertArgs += varType + " " + propName + seperator;
}
}
if (insertArgs.Length > 0)
insertArgs = insertArgs.Remove(insertArgs.Length - seperator.Length, seperator.Length);
if (updateArgs.Length > 0)
updateArgs = updateArgs.Remove(updateArgs.Length - seperator.Length, seperator.Length);
%>
/// <summary>
/// Inserts a record, can be used with the Object Data Source
/// </summary>
[DataObjectMethod(DataObjectMethodType.Insert, true)]
public void Insert(<%=insertArgs%>)
{
<%=tbl.ClassName %> item = new <%= tbl.ClassName %>();
<%
foreach (TableSchema.TableColumn col in cols) {
if (!col.AutoIncrement) {
%>
item.<%=col.PropertyName%> = <%=col.PropertyName%>;
<%
}
}
%>
item.Save(UserName);
}
/// <summary>
/// Updates a record, can be used with the Object Data Source
/// </summary>
[DataObjectMethod(DataObjectMethodType.Update, true)]
public void Update(<%=updateArgs%>)
{
<%=tbl.ClassName%> item = new <%=tbl.ClassName %>();
<%
foreach (TableSchema.TableColumn col in cols)
{
%>
item.<%=col.PropertyName%> = <%=col.PropertyName%>;
<%
}
%>
item.MarkOld();
item.Save(UserName);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.