void CargarDesdeXML(object sender, RoutedEventArgs e) { OpenFileDialog dialogo = new OpenFileDialog(); dialogo.ShowDialog(); if (String.IsNullOrEmpty(dialogo.FileName) == false) { string ruta = dialogo.FileName.ToString(); string DirectorioActual = System.Windows.Forms.Application.StartupPath.ToString(); Tabla t = null; ColumaTabla col = null; //eliminamos las tablas existentes NodoTablas.Items.Clear(); //cargamos el archivo xml Console.WriteLine("Cargando archivo XML"); XmlDocument xDoc = new XmlDocument(); xDoc.Load(Path.Combine(DirectorioActual, ruta)); XmlNodeList nodoTablas = xDoc.GetElementsByTagName("Tablas"); foreach (XmlElement ntabla in nodoTablas[0].ChildNodes) { t = new Tabla(ntabla.Name.ToString()); Console.WriteLine("Nombre tabla: " + ntabla.Name.ToString()); //navegar por las columnas de la tabla foreach (XmlElement nColumna in ntabla.ChildNodes) { Console.WriteLine("\tColumna: " + nColumna.Name.ToString()); col = new ColumaTabla(nColumna.Name, nColumna.Attributes["tipo"].Value.ToString()); if (nColumna.Attributes.Count > 1) { col.isKey = true; } t.Columnas.Add(col); } //agregar a la lista ponerTablaEnTreeView(t); } xDoc = null; GC.Collect(); Console.WriteLine("XML cargado"); } }
private string ponerFormato(ColumaTabla col) { string txt = ""; switch (col.TipoDato) { case "varchar": txt += "'@" + col.Nombre + "'"; break; case "datetime": txt += "#@" + col.Nombre + "#"; break; default: txt += "@" + col.Nombre + ""; break; } return(txt); }
private string generarEntidadesObjeto() { string txt = ""; txt += "\t#region \"ENTIDADES-OBJETO\"\r\n"; foreach (Tabla tb in Tablas) { ColumaTabla col = null; txt += "\tpublic class " + tb.NombreTabla + "\r\n\t{"; //poner propiedades for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; txt += "\r\n\t\tpublic "; switch (col.TipoDato) { case "varchar": txt += "String "; break; case "int": txt += "Int32 "; break; case "datetime": txt += "DateTime "; break; } txt += Capitalizar(col.Nombre) + "{ get; set; }"; } txt += "\r\n"; txt += "\t}\r\n"; //final de clase } txt += "\t#endregion\r\n"; return(txt); }
/// <summary> /// Genera los objetos tabla que enlazaran DB y objetos-entidad /// </summary> /// <returns></returns> private string generarObjetosTabla() { string txt = ""; foreach (Tabla tb in Tablas) { txt = "package " + NAMESPACE + ";\r\n\r\n"; txt += "import java.sql.ResultSet;\r\n" + "import java.util.ArrayList;\r\n\r\n"; txt += "\t///////////////#region \"TABLAS-OBJETO\"\r\n"; string nombreTabla = pluralizar(tb.NombreTabla); ColumaTabla col = null; txt += "\tpublic class " + nombreTabla + "\r\n\t{\r\n"; //SENTENCIA INSERT { txt += "\t\tprivate static String INSERT = \"INSERT INTO " + tb.NombreTabla + " ("; for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; if (col.isKey == false) { txt += col.Nombre; if ((i + 1) < tb.Columnas.Count() == true) { txt += ","; } } } txt += ") VALUES ("; for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; if (col.isKey == false) { txt += ponerFormato(col); if ((i + 1) < tb.Columnas.Count() == true) { txt += ","; } } } txt += ")\";\r\n"; //delete txt += "\t\tprivate static String DELETE = \"DELETE FROM " + tb.NombreTabla + " WHERE "; List <ColumaTabla> idCols = (from u in tb.Columnas where u.isKey == true select u).ToList(); for (int i = 0; i < idCols.Count(); i++) { col = idCols[i]; txt += col.Nombre + " = " + ponerFormato(col); if ((i + 1) < idCols.Count() == true) { txt += " and "; } } txt += "\";\r\n"; } //update (no actualiza los campos id) { txt += "\t\tprivate static String UPDATE = \"UPDATE " + tb.NombreTabla + " SET "; List <ColumaTabla> notIdCols = (from u in tb.Columnas where u.isKey == false select u).ToList(); for (int i = 0; i < notIdCols.Count(); i++) { col = notIdCols[i]; txt += col.Nombre + " = " + ponerFormato(col); if ((i + 1) < notIdCols.Count() == true) { txt += ", "; } } txt += " WHERE "; List <ColumaTabla> idCols = (from u in tb.Columnas where u.isKey == true select u).ToList(); for (int i = 0; i < idCols.Count(); i++) { col = idCols[i]; txt += col.Nombre + " = " + ponerFormato(col); if ((i + 1) < idCols.Count() == true) { txt += ", "; } } txt += "\";\r\n"; } //select txt += "\t\tprivate static String SELECT = \"SELECT * FROM " + tb.NombreTabla + "\";\r\n\r\n"; txt += @" public static int Add(" + tb.NombreTabla + @" _conf) { String sql = mapeoSQL(INSERT, _conf); int resultado = GestorDB.Ejecutar(sql); return resultado; } public static int Delete(" + tb.NombreTabla + @" _conf) { String sql = mapeoSQL(DELETE, _conf); int resultado = GestorDB.Ejecutar(sql); return resultado; } public static int Update(" + tb.NombreTabla + @" _conf) { String sql = mapeoSQL(UPDATE, _conf); int resultado = GestorDB.Ejecutar(sql); return resultado; } private static String mapeoSQL(String _sql, " + tb.NombreTabla + @" _conf) { String s = _sql;" ; string campo = ""; foreach (ColumaTabla col1 in tb.Columnas) { campo = "_conf.get" + Capitalizar(col1.Nombre) + "()"; txt += "\r\n\t\t\t\ts = s.replaceAll(\"@" + col1.Nombre + "\", " + campo + ".toString());"; } txt += @" return s; } " ; txt += @" public static ArrayList<" + tb.NombreTabla + @"> toArrayList(){ ArrayList<" + tb.NombreTabla + @"> lista = new ArrayList<>(); ResultSet rs = GestorDB.Consulta(SELECT); " + tb.NombreTabla + @" g =null; try{ while(rs.next()){ g = new " + tb.NombreTabla + @"(); " ; campo = ""; foreach (ColumaTabla col2 in tb.Columnas) { campo = "g.set" + Capitalizar(col2.Nombre) + "(rs.get"; switch (col2.TipoDato) { case "varchar": campo += "String"; break; case "int": campo += "Int"; break; case "bool": campo += "Boolean"; break; case "float": campo += "Float "; break; } campo += "(\"" + col2.Nombre + "\")"; txt += "\r\n\t\t\t\t\t" + campo + ");"; } txt += @" lista.add(g); } }catch(Exception ex){ ex.printStackTrace(); } return lista; } " ; txt += "\r\n\t}\r\n"; //final de clase escribirFichero("out\\" + nombreTabla + ".java", txt); } //txt +="\t#endregion\r\n"; return(txt); }
private string generarEntidadesObjeto() { string txt = ""; //txt +="\t#region \"ENTIDADES-OBJETO\"\r\n"; foreach (Tabla tb in Tablas) { txt = "package " + NAMESPACE + ";\r\n\r\n"; ColumaTabla col = null; txt += "\tpublic class " + tb.NombreTabla + "\r\n\t{"; //poner propiedades for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; txt += "\r\n\t\tprivate "; switch (col.TipoDato) { case "varchar": txt += "String "; break; case "int": txt += "Integer "; break; case "datetime": txt += "DateTime "; break; case "bool": txt += "Boolean "; break; case "float": txt += "Float "; break; } txt += Capitalizar(col.Nombre) + ";"; } txt += "\r\n"; //poner metodos get y set for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; txt += "\r\n\t\tpublic "; switch (col.TipoDato) { case "varchar": txt += "String "; break; case "int": txt += "Integer "; break; case "datetime": txt += "DateTime "; break; case "bool": txt += "Boolean "; break; case "float": txt += "Float "; break; } txt += "get" + Capitalizar(col.Nombre) + "(){"; txt += "\r\n\t\t\treturn " + Capitalizar(col.Nombre) + ";\r\n\t\t}"; } txt += "\r\n"; for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; txt += "\r\n\t\tpublic void set" + Capitalizar(col.Nombre) + "("; switch (col.TipoDato) { case "varchar": txt += "String "; break; case "int": txt += "Integer "; break; case "datetime": txt += "DateTime "; break; case "bool": txt += "Boolean "; break; case "float": txt += "Float "; break; } txt += " _valor){"; txt += "\r\n\t\t\t " + Capitalizar(col.Nombre) + " = _valor;\r\n\t\t}"; } txt += "\r\n"; txt += "\t}\r\n"; //final de clase escribirFichero("out\\" + tb.NombreTabla + ".java", txt); } txt += "\r\n"; return(txt); }
/// <summary> /// Genera los objetos tabla que enlazaran DB y objetos-entidad /// </summary> /// <returns></returns> private string generarObjetosTabla() { string txt = ""; txt += "\t#region \"TABLAS-OBJETO\"\r\n"; foreach (Tabla tb in Tablas) { string nombreTabla = pluralizar(tb.NombreTabla); ColumaTabla col = null; txt += "\tpublic class " + nombreTabla + "\r\n\t{\r\n"; //SENTENCIA INSERT { txt += "\t\tprivate const String INSERT = \"INSERT INTO " + tb.NombreTabla + " ("; for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; if (col.isKey == false) { txt += col.Nombre; if ((i + 1) < tb.Columnas.Count() == true) { txt += ","; } } } txt += ") VALUES ("; for (int i = 0; i < tb.Columnas.Count(); i++) { col = tb.Columnas[i]; if (col.isKey == false) { txt += ponerFormato(col); if ((i + 1) < tb.Columnas.Count() == true) { txt += ","; } } } txt += ")\";\r\n"; //delete txt += "\t\tprivate const String DELETE = \"DELETE FROM " + tb.NombreTabla + " WHERE "; List <ColumaTabla> idCols = (from u in tb.Columnas where u.isKey == true select u).ToList(); for (int i = 0; i < idCols.Count(); i++) { col = idCols[i]; txt += col.Nombre + " = " + ponerFormato(col); if ((i + 1) < idCols.Count() == true) { txt += " and "; } } txt += "\";\r\n"; } //update (no actualiza los campos id) { txt += "\t\tprivate const String UPDATE = \"UPDATE " + tb.NombreTabla + " SET "; List <ColumaTabla> notIdCols = (from u in tb.Columnas where u.isKey == false select u).ToList(); for (int i = 0; i < notIdCols.Count(); i++) { col = notIdCols[i]; txt += col.Nombre + " = " + ponerFormato(col); if ((i + 1) < notIdCols.Count() == true) { txt += ", "; } } txt += " WHERE "; List <ColumaTabla> idCols = (from u in tb.Columnas where u.isKey == true select u).ToList(); for (int i = 0; i < idCols.Count(); i++) { col = idCols[i]; txt += col.Nombre + " = " + ponerFormato(col); if ((i + 1) < idCols.Count() == true) { txt += ", "; } } txt += "\";\r\n"; } //select txt += "\t\tprivate const String SELECT = \"SELECT * FROM " + tb.NombreTabla + "\";\r\n\r\n"; txt += "\t\tprivate static List<" + tb.NombreTabla + "> _lista = null;\r\n\r\n"; txt += "\t\tpublic static List<" + tb.NombreTabla + "> toList()\r\n\t\t{\r\n"; txt += @" if (_lista == null) { _lista = new List<" + tb.NombreTabla + @">(); } _lista = mapeoObjeto(GestorDB.Consulta(SELECT)); return _lista; " ; txt += "\r\n\t\t}\r\n\r\n"; //final metodo Filas txt += @" public static int Add(" + tb.NombreTabla + @" _conf) { String sql = mapeoSQL(INSERT, _conf); int resultado = GestorDB.Ejecuta(sql); return resultado; } public static int Delete(" + tb.NombreTabla + @" _conf) { String sql = mapeoSQL(DELETE, _conf); int resultado = GestorDB.Ejecuta(sql); return resultado; } public static int Update(" + tb.NombreTabla + @" _conf) { String sql = mapeoSQL(UPDATE, _conf); int resultado = GestorDB.Ejecuta(sql); return resultado; } private static String mapeoSQL(String _sql, " + tb.NombreTabla + @" _conf) { String s = _sql;" ; string campo = ""; foreach (ColumaTabla col1 in tb.Columnas) { campo = "_conf." + Capitalizar(col1.Nombre); if (col1.TipoDato == "datetime") { campo = "(_conf." + Capitalizar(col1.Nombre) + ".Year + \"/\" + _conf." + Capitalizar(col1.Nombre) + ".Month + \"/\" + _conf." + Capitalizar(col1.Nombre) + ".Day)"; } txt += "\r\n\t\t\t\ts = s.Replace(\"@" + col1.Nombre + "\", " + campo + ".ToString());"; } txt += @" return s; } private static List<" + tb.NombreTabla + @"> mapeoObjeto(DataTable dt) { List<" + tb.NombreTabla + @"> resp = new List<" + tb.NombreTabla + @">(); foreach (DataRow fila in dt.Rows) {" ; txt += "\r\n\t\t\t" + tb.NombreTabla + " conf = new " + tb.NombreTabla + "();"; foreach (ColumaTabla col2 in tb.Columnas) { //conf.Id = fila.Field<Int32>("ID"); txt += "\r\n\t\t\t\tconf." + Capitalizar(col2.Nombre) + " = fila.Field<"; switch (col2.TipoDato) { case "varchar": txt += "String"; break; case "int": txt += "Int32"; break; case "datetime": txt += "DateTime"; break; } txt += ">(\"" + col2.Nombre + "\");"; } txt += @" resp.Add(conf); } return resp; } " ; txt += "\r\n\t}\r\n"; //final de clase } txt += "\t#endregion\r\n"; return(txt); }