121 lines
3.8 KiB
C#
121 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
|
|
namespace ServerExplorer
|
|
{
|
|
public class CodeGen
|
|
{
|
|
public static Boolean GenerarCodigo(CodeGenConfig config,DatabaseDesc database)
|
|
{
|
|
// Asegura la existencia de los directorios destinos
|
|
if (!System.IO.Directory.Exists(config.PathDTO)) { Utiles.CrearDirectorio(config.PathDTO); }
|
|
if (!System.IO.Directory.Exists(config.PathDAL)) { Utiles.CrearDirectorio(config.PathDAL); }
|
|
if (!System.IO.Directory.Exists(config.PathExtra)) { Utiles.CrearDirectorio(config.PathExtra); }
|
|
|
|
// Itera por cada tabla para crear sus DTOs
|
|
foreach (TablaDesc t in database.Tablas)
|
|
{
|
|
string nombreObj = t.Esquema + t.Nombre;
|
|
string nombreFichero = config.PathDTO + "/" + nombreObj + "_DTO.cs";
|
|
EscritorCodigo escritor = new EscritorCodigo(nombreFichero);
|
|
|
|
// Cabecera.
|
|
escritor.Escribe("using System;");
|
|
escritor.Escribe("using System.Data;");
|
|
escritor.Escribe("");
|
|
escritor.EscribeInd("namespace namespace");
|
|
|
|
// El objeto de la tabla.
|
|
escritor.EscribeInd("public partial class " + nombreObj);
|
|
foreach(ColumnaDesc c in t.Columnas){
|
|
// El atributo privado para cada columna.
|
|
escritor.Escribe("private Object " + "_" + c.Nombre + ";");
|
|
|
|
// La propiedad publica para cada columna.
|
|
escritor.EscribeInd("public Object " + c.Nombre );
|
|
escritor.EscribeInd("get");
|
|
escritor.Escribe("return _" + c.Nombre + ";");
|
|
escritor.DeInd();
|
|
escritor.EscribeInd("set");
|
|
escritor.Escribe("_" + c.Nombre + " = value;");
|
|
escritor.DeInd();
|
|
escritor.DeInd();
|
|
escritor.Escribe("");
|
|
}
|
|
escritor.DeInd();
|
|
|
|
// El Pie
|
|
escritor.DeInd();
|
|
|
|
// Cerrar ficheros.
|
|
escritor.Cerrar();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private class Indentado
|
|
{
|
|
private int indentado = 0;
|
|
private StringBuilder sb = new StringBuilder();
|
|
|
|
public void Inc()
|
|
{
|
|
indentado++;
|
|
}
|
|
public void Dec()
|
|
{
|
|
indentado--;
|
|
if (indentado < 0)
|
|
{
|
|
indentado = 0;
|
|
}
|
|
}
|
|
public string Str()
|
|
{
|
|
sb.Remove(0, sb.Length);
|
|
for (int i=0; i < indentado; i++)
|
|
{
|
|
sb.Append("\t");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
|
|
private class EscritorCodigo
|
|
{
|
|
StreamWriter escritor;
|
|
Indentado indentado;
|
|
|
|
public EscritorCodigo(string nombreFichero)
|
|
{
|
|
escritor = new StreamWriter(nombreFichero);
|
|
indentado = new Indentado();
|
|
}
|
|
public void Cerrar()
|
|
{
|
|
escritor.Close();
|
|
}
|
|
|
|
public void Escribe(String str)
|
|
{
|
|
escritor.WriteLine(indentado.Str() + str);
|
|
}
|
|
public void EscribeInd(String str)
|
|
{
|
|
escritor.WriteLine(indentado.Str() + str);
|
|
escritor.WriteLine(indentado.Str() + "{");
|
|
indentado.Inc();
|
|
}
|
|
public void DeInd()
|
|
{
|
|
indentado.Dec();
|
|
escritor.WriteLine(indentado.Str() + "}");
|
|
}
|
|
}
|
|
}
|
|
}
|