Beispiel #1
0
 public void GeneratePocos(CDBM db, CSharpProject p, CUsing NamespaceName, TextWriter mensajes)
 {
     foreach (CTableM t in db.lTable)
     {
         p.AddCSharpFile(PocoName(t.Name), NamespaceName);
         CClass c = p.AddClass(PocoName(t.Name));
         c.lUsing.Add(CSharpStandarUsing.System);
         CConstructor consVacio = new CConstructor(c, CSharpVisibility.cvPublic);
         CConstructor cons      = new CConstructor(c, CSharpVisibility.cvPublic);
         //Se añaden al final por estética de la clase generada
         foreach (CFieldM f in t.lFieldAll)
         {
             CType aux = CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable);
             if (aux.Equals(CSharpPrimitiveType.cDateTime))
             {
                 c.lUsing.Add(CSharpStandarUsing.System);
             }
             c.AddField(new CField(f.Name, aux, CSharpVisibility.cvPublic));
             cons.AddParam(SUtil.LowerFirst(f.Name), CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable));
             cons.AddSentence(new CTextualSentence($"this.{f.Name} = {SUtil.LowerFirst(f.Name)};"));
         }
         c.AddConstructor(consVacio);
         c.AddConstructor(cons);
     }
     mensajes.WriteLine("Pocos generados en: " + p.RootDirectory.ActualDirectory.FullName);
 }
Beispiel #2
0
        private void GenerateDaos(CDBM db, CSharpProject p, string NamespaceName, CUsing usingModel, TextWriter mensajes)
        {
            foreach (CTableM t in db.lTable)
            {
                string ClassName = "S" + t.Name + "Dao";
                p.AddCSharpFile(ClassName + ".generated", NamespaceName);

                CClass c = p.AddClass(ClassName, isStatic: true);
                c.lUsing.Add(CSharpStandarUsing.System);
                c.IsPartial = true;
                CMethod m;

                //CreateFromReader
                m = c.AddMethod(new CMethod("CreateFromReader", new TextualType(PocoName(t.Name)), CSharpVisibility.cvPublic, isStatic: true));
                m.AddParam("dr", new TextualType("SqlDataReader"));
                byte i = 0;
                m.AddSentence(new CTextualSentence($"{PocoName(t.Name)} result = new {PocoName(t.Name)}();"));
                foreach (CFieldM f in t.lFieldAll)
                {
                    string textoDataReaderGet = SqlServerGetDataReaderGet(f.Td, i);
                    if (!f.IsNullable)
                    {
                        m.AddSentence(new CTextualSentence($"result.{f.Name} = {textoDataReaderGet};"));
                    }
                    else
                    {
                        m.AddSentence(new CTextualSentence($"if (dr.IsDBNull({i})) {{"));
                        m.AddSentence(new CTextualSentence($"\tresult.{f.Name} = null;"));
                        m.AddSentence(new CTextualSentence("}else{"));
                        m.AddSentence(new CTextualSentence($"\tresult.{f.Name} = {textoDataReaderGet};"));
                        m.AddSentence(new CTextualSentence("}"));
                    }
                    i++;
                }
                m.AddSentence(new CTextualSentence("return result;"));

                //SelectAll
                m = c.AddMethod(new CMethod("SelectAll", new TextualType("List<" + PocoName(t.Name) + ">"), CSharpVisibility.cvPublic, isStatic: true));
                m.AddParam("con", new TextualType("SqlConnection"));
                c.lUsing.Add(CSharpStandarUsing.System_Data_SqlClient);

                m.AddSentence(new CTextualSentence($"List<{PocoName(t.Name)}> result = new List<{PocoName(t.Name)}>();"));
                c.lUsing.Add(CSharpStandarUsing.System_Collections_Generic);
                c.lUsing.Add(usingModel);
                m.AddSentence(new CTextualSentence("using (SqlCommand com = con.CreateCommand()) {"));
                m.AddSentence(new CTextualSentence($"\tcom.CommandText = {SUtil.DoubleQuote(t.GeneraSelectAll())};"));
                m.AddSentence(new CTextualSentence("\tusing (SqlDataReader dr = com.ExecuteReader()){"));
                m.AddSentence(new CTextualSentence("\t\twhile(dr.Read()){"));
                m.AddSentence(new CTextualSentence($"\t\t\tresult.Add({ClassName}.CreateFromReader(dr));"));
                m.AddSentence(new CTextualSentence("\t\t}"));
                m.AddSentence(new CTextualSentence("\t}"));
                m.AddSentence(new CTextualSentence("}"));
                m.AddSentence(new CTextualSentence("return result;"));

                //SelectByPk
                if (t.lFieldData.Count > 0)                   //Si hay algo que seleccionar. Si no hay campos de datos no tendría sentido seleccionar por clave primaria.
                {
                    m = c.AddMethod(new CMethod("SelectByPk", new TextualType(PocoName(t.Name)), CSharpVisibility.cvPublic, isStatic: true));
                    m.AddParam("con", new TextualType("SqlConnection"));
                    foreach (CFieldM f in t.lFieldPk)
                    {
                        m.AddParam(f.Name, CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable));
                    }
                    m.AddSentence(new CTextualSentence(PocoName(t.Name) + " result = null;"));
                    c.lUsing.Add(usingModel);
                    m.AddSentence(new CTextualSentence("using (SqlCommand com = con.CreateCommand()) {"));
                    m.AddSentence(new CTextualSentence($"\tcom.CommandText = {SUtil.DoubleQuote(t.GeneraSelectByPk())};"));
                    foreach (CFieldM f in t.lFieldPk)
                    {
                        m.AddSentence(new CTextualSentence("\tcom.Parameters.AddWithValue(" + SUtil.DoubleQuote(SUtilBD.ParamName(f.Name)) + ", " + f.Name + ");"));
                    }
                    m.AddSentence(new CTextualSentence("\tusing (SqlDataReader dr = com.ExecuteReader()){"));
                    m.AddSentence(new CTextualSentence("\t\tif(dr.Read()){"));
                    m.AddSentence(new CTextualSentence($"\t\t\tresult = {ClassName}.CreateFromReader(dr);"));
                    m.AddSentence(new CTextualSentence("\t\t}"));
                    m.AddSentence(new CTextualSentence("\t}"));
                    m.AddSentence(new CTextualSentence("}"));
                    m.AddSentence(new CTextualSentence("return result;"));
                }
                //Insert
                m = c.AddMethod(new CMethod("Insert", CSharpPrimitiveType.cVoid, CSharpVisibility.cvPublic, true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddParam("item", new TextualType(PocoName(t.Name)));
                m.AddSentence(new CTextualSentence("using(SqlCommand com = con.CreateCommand()){"));
                m.AddSentence(new CTextualSentence($"\tcom.CommandText = {SUtil.DoubleQuote(t.GeneraInsert())};"));
                foreach (CFieldM f in t.lFieldAll)
                {
                    if (!f.IsIdentity)
                    {
                        if (!f.IsNullable)
                        {
                            m.AddSentence(new CTextualSentence($"\tcom.Parameters.AddWithValue({ SUtil.DoubleQuote(f.ParameterName)},item.{f.Name});"));
                        }
                        else
                        {
                            m.AddSentence(new CTextualSentence($"if (item.{f.Name} == null) {{"));
                            m.AddSentence(new CTextualSentence($"\tcom.Parameters.AddWithValue({ SUtil.DoubleQuote(f.ParameterName)},DBNull.Value);"));
                            m.AddSentence(new CTextualSentence("}else{"));
                            m.AddSentence(new CTextualSentence($"\tcom.Parameters.AddWithValue({ SUtil.DoubleQuote(f.ParameterName)},item.{f.Name});"));
                            m.AddSentence(new CTextualSentence("}"));
                        }
                    }
                }
                m.AddSentence(new CTextualSentence("\tcom.ExecuteNonQuery();"));
                foreach (CFieldM f in t.lFieldAll)
                {
                    if (f.IsIdentity)
                    {
                        string ConvertTo;
                        switch (f.Td)
                        {
                        case SqlDbType.BigInt:
                            ConvertTo = "(long)";
                            break;

                        case SqlDbType.Int:
                            ConvertTo = "(int)";
                            break;

                        case SqlDbType.SmallInt:
                            ConvertTo = "(short)";
                            break;

                        case SqlDbType.TinyInt:
                            ConvertTo = "(byte)";
                            break;

                        default:
                            throw new Exception("Tipo de dato no considerado en Identity");
                        }
                        m.AddSentence(new CTextualSentence($"\titem.{f.Name} = {ConvertTo}SUtilBD.getIdentity(con);"));
                        c.lUsing.Add(new CUsing("IvanCruz.Util"));
                    }
                }

                m.AddSentence(new CTextualSentence("}"));
                //Update
                m = c.AddMethod(new CMethod("Update", CSharpPrimitiveType.cVoid, CSharpVisibility.cvPublic, true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddParam("item", new TextualType(PocoName(t.Name)));
                m.AddSentence(new CTextualSentence("using(SqlCommand com = con.CreateCommand()){"));
                m.AddSentence(new CTextualSentence($"\tcom.CommandText = {SUtil.DoubleQuote(t.GeneraUpdate())};"));
                foreach (CFieldM f in t.lFieldData)
                {
                    AddParameterInUpdate(m, f);
                }
                foreach (CFieldM f in t.lFieldPk)
                {
                    AddParameterInUpdate(m, f);
                }
                m.AddSentence(new CTextualSentence("\tcom.ExecuteNonQuery();"));
                m.AddSentence(new CTextualSentence("}"));
                //Delete
                m = c.AddMethod(new CMethod("Delete", CSharpPrimitiveType.cVoid, CSharpVisibility.cvPublic, true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddParam("item", new TextualType(PocoName(t.Name)));
                m.AddSentence(new CTextualSentence("using(SqlCommand com = con.CreateCommand()){"));
                m.AddSentence(new CTextualSentence($"\tcom.CommandText = {SUtil.DoubleQuote(t.GeneraDelete())};"));
                foreach (CFieldM f in t.lFieldPk)
                {
                    m.AddSentence(new CTextualSentence($"\tcom.Parameters.AddWithValue({ SUtil.DoubleQuote(f.ParameterName)},item.{f.Name});"));
                }
                m.AddSentence(new CTextualSentence("\tcom.ExecuteNonQuery();"));
                m.AddSentence(new CTextualSentence("}"));
            }
            mensajes.WriteLine("Pocos generados en: " + p.RootDirectory.ActualDirectory.FullName);
        }