<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Debug="true" CompilerVersion="v3.5" Description="" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="False" Category="Required" %>
<%@ Property Name="NameSpace" Optional="False" Type="System.String" Default="Beyondbit.App.Entity" Category="Style" Description="Object Namespace." %>
//----------------------------------------------------------------
// Copyright (C) 2013
//
// All rights reserved.
//
// DAO<%=SourceTable.Name%>.cs
//
//
// <%= DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") %>
//
//
//
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using <%=NameSpace%>.Entity;
<%SchemaExplorer.ColumnSchema identityColumn = GetIdentityKeyColumn(SourceTable);%>
<%IList<SchemaExplorer.ColumnSchema> nonIdentityColumns = GetNonIdentityKeyColumns(SourceTable);%>
<%IList<SchemaExplorer.ColumnSchema> primaryKeys = GetPrimaryKeyColumns(SourceTable);%>
<%--for(int j = 0; j < SourceTable.Columns[0].ExtendedProperties.Count; ++ j) {--%>
<%--=SourceTable.Columns[0].ExtendedProperties[j].Name%>:<%=SourceTable.Columns[0].ExtendedProperties[j].Value--%>
<%--}--%>
namespace <%=NameSpace%> {
public class DAO<%=SourceTable.Name%> : DAOLocalBase {
public void Insert(<%=SourceTable.Name%> entity)
{
string commandText = "<%=GenerateInsertSQLText(SourceTable)%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
<%=GenerateInsertSQLCommandParameterText(SourceTable)%>
int id = 0;
try
{
object o = database.ExecuteScalar(cmd);
if (o != null) {
id = Convert.ToInt32(o);
<%if (GetIdentityKeyColumnName(SourceTable) != "") {%>
entity.<%=GetIdentityKeyColumnName(SourceTable)%> = id;
<%} else {%>
<%}%>
}
}
catch
{
}
return id;
}
public <%=SourceTable.Name%> GetEntity(<%
if (identityColumn != null) {
%><%=GetSystemType(identityColumn)%> <%=identityColumn.Name%><%
} else {
for (int i_pk = 0; i_pk < primaryKeys.Count; ++i_pk) {
%><%=GetSystemType(primaryKeys[i_pk])%> <%=primaryKeys[i_pk].Name%><%
if (i_pk != primaryKeys.Count - 1) {
%>,<%
}
}
}
%>) {
<% if (identityColumn != null) {%>
string commandText = @"select * from <%=SourceTable.Name%> where <%=identityColumn.Name%>=@<%=identityColumn.Name%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
database.AddInParameter(cmd, "@<%=identityColumn.Name%>", <%=GetDbType(identityColumn)%>, entity.<%=identityColumn.Name%>);
<% } else {%>
string commandText = @"select * from <%=SourceTable.Name%> where <%
for (int i = 0; i < primaryKeys.Count; ++i) {
%><%=primaryKeys[i].Name%>=@<%=primaryKeys[i].Name%><%
if (i != primaryKeys.Count - 1) {
%>,<%
}
}%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
<%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {%>
database.AddInParameter(cmd, "@<%=primaryKeys[i].Name%>", <%=GetDbType(primaryKeys[i])%>, <%=primaryKeys[i].Name%>);
<% }
}
}
%>
DataSet result = null;
<%=SourceTable.Name%> entity = new <%=SourceTable.Name%>();
try
{
result = database.ExecuteDataSet(cmd);
if (result != null && result.Tables.Count > 0 && result.Tables[0] != null && result.Tables[0].Rows.Count > 0) {
DataRow dr = result.Tables[0].Rows[0];<%
for (int i = 0; i < SourceTable.Columns.Count; ++i) {%>
entity.<%=SourceTable.Columns[i].Name%> = <%=GetConvertToString(SourceTable.Columns[i], "dr[\"" + SourceTable.Columns[i].Name + "\"]")%>;<%
}%>
}
}
catch
{
}
return entity;
}
public void Insert(IList<<%=SourceTable.Name%>> entities) {
foreach (<%=SourceTable.Name%> entity in entities)
Insert(entity);
}
public bool Delete(<%=SourceTable.Name%> entity) {
<%if (identityColumn != null) {%>
string commandText = @"delete from <%=SourceTable.Name%> where <%=identityColumn.Name%>=@<%=identityColumn.Name%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
database.AddInParameter(cmd, "@<%=identityColumn.Name%>", <%=GetDbType(identityColumn)%>, entity.<%=identityColumn.Name%>);
<% } else {%>
string commandText = @"delete from <%=SourceTable.Name%> where <%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {%><%=primaryKeys[i].Name%>=@<%=primaryKeys[i].Name%><%if (i != primaryKeys.Count - 1) {%> And <%}
}
}
%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
<%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {%>
database.AddInParameter(cmd, "@<%=primaryKeys[i].Name%>", <%=GetDbType(primaryKeys[i])%>, entity.<%=primaryKeys[i].Name%>);
<% }
}
}%>
int rowsAffectedCount = 0;
try
{
object o = database.ExecuteNonQuery(cmd);
if (o != null) {
rowsAffectedCount = Convert.ToInt32(o);
}
}
catch
{
}
return rowsAffectedCount > 0;
}
public bool Update(<%=SourceTable.Name%> entity)
{
<%if (identityColumn != null) {%>
string commandText = @"update <%=SourceTable.Name%> set <%
for (int i = 0; i < nonIdentityColumns.Count; ++i) {
%><%=nonIdentityColumns[i].Name%>=@<%=nonIdentityColumns[i].Name%><%
if (i != nonIdentityColumns.Count - 1) {%>,<%
}
}%> where <%=GetIdentityKeyColumnName(SourceTable)%>=@<%=GetIdentityKeyColumnName(SourceTable)%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
database.AddInParameter(cmd, "@<%=identityColumn.Name%>", <%=GetDbType(identityColumn)%>, entity.<%=identityColumn.Name%>);
<%
if (nonIdentityColumns != null && nonIdentityColumns.Count > 0) {
for (int kk = 0; kk < nonIdentityColumns.Count; ++kk) {%>
database.AddInParameter(cmd, "@<%=nonIdentityColumns[kk].Name%>", <%=GetDbType(nonIdentityColumns[kk])%>, entity.<%=nonIdentityColumns[kk].Name%>);
<%
}
}
%>
<%} else {%>
<%IList<SchemaExplorer.ColumnSchema> nonPrimaryKeys = GetNonPrimaryKeyColumns(SourceTable);%>
string commandText = @"update <%=SourceTable.Name%> set <%for (int i = 0; i < nonPrimaryKeys.Count; ++i) {%><%=nonPrimaryKeys[i].Name%>=@<%=nonPrimaryKeys[i].Name%><%
if (i != nonPrimaryKeys.Count - 1) {%>,<%}
}%> where <%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {%><%=primaryKeys[i].Name%>=@<%=primaryKeys[i].Name%><%if (i != primaryKeys.Count - 1) {%> And <%}
}
}
%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
<%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {%>
database.AddInParameter(cmd, "@<%=primaryKeys[i].Name%>", <%=GetDbType(primaryKeys[i])%>, entity.<%=primaryKeys[i].Name%>);
<% }
}
%>
<%for (int i = 0; i < SourceTable.Columns.Count; ++i) {
if (!IsColumnPrimaryKey(SourceTable.Columns[i])){%>
database.AddInParameter(cmd, "@<%=SourceTable.Columns[i].Name%>", <%=GetDbType(SourceTable.Columns[i])%>, entity.<%=SourceTable.Columns[i].Name%>);
<%
}
}%>
<%}%>
int rowsAffectedCount = 0;
try
{
object o = database.ExecuteNonQuery(cmd);
if (o != null) {
rowsAffectedCount = Convert.ToInt32(o);
}
}
catch
{
}
return rowsAffectedCount > 0;
}
public void Update(IList<<%=SourceTable.Name%>> entities) {
foreach (<%=SourceTable.Name%> entity in entities)
Update(entity);
}
public bool Exists(<%=SourceTable.Name%> entity)
{
<%if (identityColumn != null) {%>
string commandText = @"select count(*) from <%=SourceTable.Name%> where <%=GetIdentityKeyColumnName(SourceTable)%>=@<%=GetIdentityKeyColumnName(SourceTable)%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
database.AddInParameter(cmd, "@<%=identityColumn.Name%>", <%=GetDbType(identityColumn)%>, entity.<%=identityColumn.Name%>);
<%} else {%>
string commandText = @"select count(*) from <%=SourceTable.Name%> where <%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {%><%=primaryKeys[i].Name%>=@<%=primaryKeys[i].Name%><%if (i != primaryKeys.Count - 1) {%> And <%}
}
}
%>";
SqlCommand cmd = new SqlCommand(commandText, connection);
<%
if (primaryKeys != null && primaryKeys.Count > 0) {
for (int i = 0; i < primaryKeys.Count; ++i) {
%>
database.AddInParameter(cmd, "@<%=primaryKeys[i].Name%>", <%=GetDbType(primaryKeys[i])%>, entity.<%=primaryKeys[i].Name%>);
<%}
}
%>
<%}%>
bool exists = false;
try
{
object o = database.ExecuteScalar(cmd);
if (o != null) {
exists = Convert.ToBoolean(o);
}
}
catch
{
}
return exists;
}
}
}
<script runat="template">
public string GenerateInsertSQLText(SchemaExplorer.TableSchema table) {
string result = "INSERT INTO " + table.Name + " values(";
foreach (SchemaExplorer.ColumnSchema colmn in table.Columns) {
if (IsColumnIdentity(colmn))
continue;
result += "@" + colmn.Name + ",";
}
return result.TrimEnd(",".ToCharArray()) + ")";
}
public string GenerateInsertSQLCommandParameterText(SchemaExplorer.TableSchema table) {
string result = "";
for(int i = 0; i < table.Columns.Count; ++i) {
if (IsColumnIdentity(table.Columns[i])) {
} else
result += " database.AddInParameter(cmd, \"@"+table.Columns[i].Name+"\", "+GetDbType(table.Columns[i])+", entity."+table.Columns[i].Name+");\r\n";
}
return result;
}
public string GenerateColumnParameterString(SchemaExplorer.ColumnSchema clmn) {
string result = "SqlParameter " + clmn.Name + " = new SqlParameter()";
return result;
}
public bool IsColumnIdentity(SchemaExplorer.ColumnSchema clmn) {
/*
CS_IsRowGuidCol:False
CS_IsIdentity:True
CS_IsComputed:False
CS_IsDeterministic:True
CS_IdentitySeed:1
CS_IdentityIncrement:1
CS_Default:
CS_ComputedDefinition:
CS_Collation:
CS_ObjectID:1
CS_SystemType:int
CS_UserType:int
MS_Description:????
CS_Description:????
*/
return clmn.ExtendedProperties["CS_IsIdentity"].Value.ToString().ToLower() == "true";
return true;
}
public bool IsColumnPrimaryKey(SchemaExplorer.ColumnSchema clmn) {
return clmn.IsPrimaryKeyMember;
}
public string GetDbType(SchemaExplorer.ColumnSchema clmn) {
return "System.Data.DbType." + clmn.DataType.ToString();
}
public string GetSystemType(SchemaExplorer.ColumnSchema clmn) {
string type = clmn.DataType.ToString();
switch (type) {
case "AnsiString":
type = "String";
break;
case "Int32":
type = "int";
break;
default:
break;
}
return type;
}
public string GetConvertToString(SchemaExplorer.ColumnSchema clmn, string drString) {
string type = clmn.DataType.ToString();
switch (type) {
case "AnsiString":
case "String":
type = "Convert.ToString(" + drString + ")";
break;
case "Int32":
type = "Convert.ToInt32(" + drString + ")";
break;
case "Int64":
type = "Convert.ToInt64(" + drString + ")";
break;
case "Int16":
type = "Convert.ToInt16(" + drString + ")";
break;
case "DateTime":
type = "Convert.ToDateTime(" + drString + ")";
break;
case "Double":
type = "Convert.ToDouble(" + drString + ")";
break;
case "Binary":
type = "(byte[])" + drString;
break;
case "Boolean":
type = "Convert.ToBoolean(" + drString + ")";
break;
case "Decimal":
type = "Convert.ToDecimal(" + drString + ")";
break;
case "Single":
type = "Convert.ToDouble(" + drString + ")";
break;
case "Currency":
type = "Convert.ToDecimal(" + drString + ")";
break;
case "Xml":
type = "Convert.ToString(" + drString + ")";
break;
case "AnsiStringFixedLength":
type = "Convert.ToString(" + drString + ")";
break;
case "Guid":
type = "Convert.ToString(" + drString + ")";
break;
case "Byte":
type = "Convert.ToByte(" + drString + ")";
break;
case "float":
type = "Convert.ToDouble(" + drString + ")";
break;
default:
break;
}
return type;
}
public string GetIdentityKeyColumnName(SchemaExplorer.TableSchema tbl) {
string result = "";
for (int i = 0; i < tbl.Columns.Count; ++i) {
if (IsColumnIdentity(tbl.Columns[i])) {
result = tbl.Columns[i].Name;
break;
}
}
return result;
}
public SchemaExplorer.ColumnSchema GetIdentityKeyColumn(SchemaExplorer.TableSchema tbl) {
SchemaExplorer.ColumnSchema result = null;
for (int i = 0; i < tbl.Columns.Count; ++i) {
if (IsColumnIdentity(tbl.Columns[i])) {
result = tbl.Columns[i];
break;
}
}
return result;
}
public IList<SchemaExplorer.ColumnSchema> GetNonIdentityKeyColumns(SchemaExplorer.TableSchema tbl) {
IList<SchemaExplorer.ColumnSchema> result = new List<SchemaExplorer.ColumnSchema>();
for (int i = 0; i < tbl.Columns.Count; ++i) {
if (!IsColumnIdentity(tbl.Columns[i])) {
result.Add(tbl.Columns[i]);
}
}
return result;
}
public IList<SchemaExplorer.ColumnSchema> GetPrimaryKeyColumns(SchemaExplorer.TableSchema tbl) {
IList<SchemaExplorer.ColumnSchema> result = new List<SchemaExplorer.ColumnSchema>();
for (int i = 0; i < tbl.Columns.Count; ++i) {
if (tbl.Columns[i].IsPrimaryKeyMember) {
result.Add(tbl.Columns[i]);
}
}
return result;
}
public IList<SchemaExplorer.ColumnSchema> GetNonPrimaryKeyColumns(SchemaExplorer.TableSchema tbl) {
IList<SchemaExplorer.ColumnSchema> result = new List<SchemaExplorer.ColumnSchema>();
for (int i = 0; i < tbl.Columns.Count; ++i) {
if (!tbl.Columns[i].IsPrimaryKeyMember) {
result.Add(tbl.Columns[i]);
}
}
return result;
}
//public string Get
</script>