Beispiel #1
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);
        }
Beispiel #2
0
        private void GenerateBo(CDBM db, CSharpProject p, string NamespaceName, CUsing NamespacePocos, CUsing NamespaceDao, TextWriter mensajes)
        {
            string ClassName = "SBo";

            p.AddCSharpFile(ClassName + ".generated", NamespaceName);
            CClass c = p.AddClass(ClassName, isStatic: true, isPartial: true);

            c.lUsing.Add(CSharpStandarUsing.System_Collections_Generic);
            c.lUsing.Add(CSharpStandarUsing.System_Data_SqlClient);
            c.lUsing.Add(NamespacePocos);
            c.lUsing.Add(NamespaceDao);
            c.lUsing.Add(new CUsing("IvanCruz.Util.UtilBD"));
            CMethod m;

            foreach (CTableM t in db.lTable)
            {
                //Metodo getTableAll
                m = c.AddMethod(new CMethod($"get{t.Name}All", new TextualType($"List <C{t.Name}Poco>"), CSharpVisibility.cvPublic, isStatic: true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddSentence(new CTextualSentence($"return S{t.Name}Dao.SelectAll(con);"));
                //Metodo TableInsert
                m = c.AddMethod(new CMethod($"{t.Name}Insert", new TextualType("void"), CSharpVisibility.cvPublic, isStatic: true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddParam("item", new TextualType($"C{t.Name}Poco"));
                m.AddSentence(new CTextualSentence($"S{t.Name}Dao.Insert(con,item);"));
                //Metodo TableUpdate
                m = c.AddMethod(new CMethod($"{t.Name}Update", new TextualType("void"), CSharpVisibility.cvPublic, isStatic: true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddParam("item", new TextualType($"C{t.Name}Poco"));
                m.AddSentence(new CTextualSentence($"S{t.Name}Dao.Update(con,item);"));
                //Metodo TableDelete
                m = c.AddMethod(new CMethod($"{t.Name}Delete", new TextualType("void"), CSharpVisibility.cvPublic, isStatic: true));
                m.AddParam("con", new TextualType("SqlConnection"));
                m.AddParam("item", new TextualType($"C{t.Name}Poco"));
                StringBuilder sb = new StringBuilder();
                sb.Append("No se ha podido eliminar ");
                if (t.Gender == EGender.male)
                {
                    sb.Append("el ");
                }
                else
                {
                    sb.Append("la ");
                }
                sb.Append(t.SingularTitle);
                sb.Append(" porque hay otras tablas que se referencian a ");
                if (t.Gender == EGender.male)
                {
                    sb.Append("él");
                }
                else
                {
                    sb.Append("ella");
                }
                m.AddSentence(new CTextualSentence("try {"));
                m.AddSentence(new CTextualSentence($"\tS{t.Name}Dao.Delete(con,item);"));

                m.AddSentence(new CTextualSentence("} catch (SqlException sex) {"));
                m.AddSentence(new CTextualSentence("\tswitch (sex.Number) {"));
                m.AddSentence(new CTextualSentence("\tcase 547://Error por clave foranea apuntando a registro a eliminar"));
                m.AddSentence(new CTextualSentence($"\t\tthrow new BoException({SUtil.DoubleQuote(sb.ToString())},sex);"));
                m.AddSentence(new CTextualSentence("default:throw;"));
                m.AddSentence(new CTextualSentence("\t}"));
                m.AddSentence(new CTextualSentence("}"));
            }
        }