%
#encoding: UTF-8
=begin
CapicuaGen
CapicuaGen es un software que ayuda a la creación automática de
sistemas empresariales a través de la definición y ensamblado de
diversos generadores de características.
El proyecto fue iniciado por José Luis Bautista Martín, el 6 de enero
de 2016.
Puede modificar y distribuir este software, según le plazca, y usarlo
para cualquier fin ya sea comercial, personal, educativo, o de cualquier
índole, siempre y cuando incluya este mensaje, y se permita acceso al
código fuente.
Este software es código libre, y se licencia bajo LGPL.
Para más información consultar http://www.gnu.org/licenses/lgpl.html
=end
%>using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections.Generic;
<%=get_namespaces_text(:business_interfaces) %>
namespace <%=generation_attributes[:namespace]%>
{
///
/// Capa de acceso a datos de una<%=template_target.class_name%>
///
partial class <%=template_target.class_name%> : AccesoDatos where T : class, <%=get_entity_interface_name(template_target.entity_schema.name)%>, new()
{
#region Sentencias SQL
///
/// Consulta
///
/// Registro a consultar
/// Registro consultado
public override T Consultar(T <%=template_target.entity_schema.name.downcase%>)
{
T resultado = ConsultarExiste(<%=template_target.entity_schema.name.downcase%>);
if (resultado == null)
{
throw new AccesoDatosException("No existe el registro");
}
else
{
return resultado;
}
}
///
/// Lista todos los registros
///
/// Registros
public override IEnumerable Listar()
{
List listaResultado = new List();
StringBuilder query = new StringBuilder();
query.AppendLine("SELECT");
<%
total=template_target.entity_schema.fields.count
indice=0
template_target.entity_schema.fields.each { |field|
indice=indice+1
%>query.AppendLine(" <%=field.name%><%="," if indice!=total%>");
<%}%>query.AppendLine("FROM");
query.AppendLine(" <%=template_target.entity_schema.sql_name.downcase%>");
DbCommand comando = CrearComando(query.ToString());
using (DbConnection conexion = CrearConexionAbierta())
{
comando.Connection = conexion;
DbDataReader reader = comando.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
T resultado = new T();
<%
template_target.entity_schema.fields.each { |field|
%> if (reader["<%=field.name%>"] != DBNull.Value) resultado.<%=field.name%> = (<%=field.net_type%>)reader["<%=field.name%>"];
<%}%>
listaResultado.Add(resultado);
}
}
}
return listaResultado;
}
///
/// Consulta si un registro existe
///
/// Registro a consultar
/// Devuelve el registro o nulo
public override T ConsultarExiste(T <%=template_target.entity_schema.name.downcase%>)
{
StringBuilder query = new StringBuilder();
query.AppendLine("SELECT");
<%
total=template_target.entity_schema.fields.count
indice=0
template_target.entity_schema.fields.each { |field|
indice=indice+1
%>query.AppendLine(" <%=field.name%><%="," if indice!=total%>");
<%}%>query.AppendLine("FROM");
query.AppendLine(" <%=template_target.entity_schema.sql_name.downcase%>");
query.AppendLine("WHERE");
<%
total=template_target.entity_schema.primary_fields.count
indice=0
template_target.entity_schema.primary_fields.each { |field|
indice=indice+1
%>query.AppendLine("<%=field.name%>=@p<%=indice%><%=" AND" if indice!=total%>");
<%}%>
DbCommand comando = CrearComando(query.ToString());
<%
indice=0
template_target.entity_schema.primary_fields.each { |field|
indice=indice+1
%>AgregarParametro(comando, "@p<%=indice%>",<%=field.sql_net_type%>, <%=template_target.entity_schema.name.downcase%>.<%=field.name%>);
<%}%>
using (DbConnection conexion = CrearConexionAbierta())
{
comando.Connection = conexion;
DbDataReader reader = comando.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
T resultado = new T();
<%
template_target.entity_schema.fields.each { |field|
%> if (reader["<%=field.name%>"] != DBNull.Value) resultado.<%=field.name%> = (<%=field.net_type%>)reader["<%=field.name%>"];
<%}%>
return resultado;
}
}
return null;
}
}
///
/// Actualización
///
/// Registro a actualizar
public override void Actualizar(T <%=template_target.entity_schema.name.downcase%>)
{
Consultar(<%=template_target.entity_schema.name.downcase%>);
StringBuilder query = new StringBuilder();
query.AppendLine("UPDATE <%=template_target.entity_schema.sql_name.downcase%>");
query.AppendLine("SET");
<%
total=template_target.entity_schema.non_primary_fields.count
indice=0
template_target.entity_schema.non_primary_fields.each { |field|
indice=indice+1
%>query.AppendLine(" <%=field.name%>=@p<%=indice%><%="," if indice!=total%>");
<%}%>query.AppendLine("WHERE");
<%
total=indice + template_target.entity_schema.primary_fields.count
template_target.entity_schema.primary_fields.each { |field|
indice=indice+1
%>query.AppendLine(" <%=field.name%>=@p<%=indice%><%=" AND" if indice!=total%>");
<%}%>
DbCommand comando = CrearComando(query.ToString());
<%
indice=0
template_target.entity_schema.non_primary_fields.each { |field|
indice=indice+1
%>AgregarParametro(comando, "@p<%=indice%>",<%=field.sql_net_type%>, <%=template_target.entity_schema.name.downcase%>.<%=field.name%>);
<%}
template_target.entity_schema.primary_fields.each { |field|
indice=indice+1
%>AgregarParametro(comando, "@p<%=indice%>",<%=field.sql_net_type%>, <%=template_target.entity_schema.name.downcase%>.<%=field.name%>);
<%}%>
using (DbConnection conexion = CrearConexionAbierta())
{
comando.Connection = conexion;
comando.ExecuteNonQuery();
}
}
///
/// Inserta un registro
///
/// Registro para insertar
public override void Insertar(T <%=template_target.entity_schema.name.downcase%>)
{
T registroExistente = ConsultarExiste(<%=template_target.entity_schema.name.downcase%>);
if (registroExistente != null)
{
throw new AccesoDatosException("El registro ya existe");
}
StringBuilder query = new StringBuilder();
query.AppendLine("INSERT INTO <%=template_target.entity_schema.sql_name.downcase%>");
query.AppendLine("(");
<%
total=template_target.entity_schema.fields.count
indice=0
template_target.entity_schema.fields.each { |field|
indice=indice+1
next if field.identity
%>query.AppendLine(" <%=field.name%><%="," if indice!=total%>");
<%}%>query.AppendLine(")");
query.AppendLine("VALUES");
query.AppendLine("(");
<%
total=template_target.entity_schema.fields.count
indice=0
template_target.entity_schema.fields.each { |field|
indice=indice+1
next if field.identity
%>query.AppendLine(" @p<%=indice%><%="," if indice!=total%>");
<%}%>query.AppendLine(")");
DbCommand comando = CrearComando(query.ToString());
<%
indice=0
template_target.entity_schema.fields.each { |field|
indice=indice+1
next if field.identity
%>AgregarParametro(comando, "@p<%=indice%>",<%=field.sql_net_type%>, <%=template_target.entity_schema.name.downcase%>.<%=field.name%>);
<%}%>
using (DbConnection conexion = CrearConexionAbierta())
{
comando.Connection = conexion;
comando.ExecuteNonQuery();
}
}
///
/// Borrado de registros
///
/// Registro a eliminar
public override void Eliminar(T <%=template_target.entity_schema.name.downcase%>)
{
Consultar(<%=template_target.entity_schema.name.downcase%>);
StringBuilder query = new StringBuilder();
query.AppendLine("DELETE <%=template_target.entity_schema.sql_name.downcase%>");
query.AppendLine("WHERE");
<%
total=template_target.entity_schema.primary_fields.count
indice=0
template_target.entity_schema.primary_fields.each { |field|
indice=indice+1
%>query.AppendLine("<%=field.name%>=@p<%=indice%><%=" AND" if indice!=total%>");
<%}%>
DbCommand comando = CrearComando(query.ToString());
<%
indice=0
template_target.entity_schema.primary_fields.each { |field|
indice=indice+1
%>AgregarParametro(comando, "@p<%=indice%>",<%=field.sql_net_type%>, <%=template_target.entity_schema.name.downcase%>.<%=field.name%>);
<%}%>
using (DbConnection conexion = CrearConexionAbierta())
{
comando.Connection = conexion;
comando.ExecuteNonQuery();
}
}
#endregion
}
}