private void ParametrosCamposNoClavePorComas(CSourceWriter sw)
        {
            bool Primero = true;

            foreach (CFieldM f in this.lFieldData)
            {
                sw.WriteComma(ref Primero);
                sw.Write(SUtilBD.Param(f.Name));
            }
        }
        private void ParametrosCamposNoIdPorComas(CSourceWriter sw)
        {
            bool Primero = true;

            foreach (CFieldM f in this.lFieldAll)
            {
                if (!f.IsIdentity)
                {
                    sw.WriteComma(ref Primero);
                    sw.Write(SUtilBD.Param(f.Name));
                }
            }
        }
        private void GeneraDelete(CSourceWriter sw)
        {
            sw.Write("delete from ");
            sw.Write(this.Name);
            sw.Write(" where ");
            bool Primero = true;

            foreach (CFieldM f in this.lFieldPk)
            {
                sw.WriteAnd(ref Primero);
                sw.Write(f.Name);
                sw.Write(" = ");
                sw.Write(SUtilBD.Param(f.Name));
            }
        }
        private void GeneraSelectByPk(CSourceWriter sw)
        {
            sw.Write("select");
            sw.Space();
            this.CamposPorComas(sw);
            sw.Space();
            sw.Write("from");
            sw.Space();
            sw.Write(this.Name);
            bool Primero = true;
            var  sb      = new StringBuilder();

            foreach (CFieldM f in this.lFieldPk)
            {
                SUtilBD.AddCondicion(ref Primero, sb, f.Name + " = @" + SUtilBD.ParamName(f.Name));
            }
            sw.Space();
            sw.Write(sb.ToString());
        }
        private void GeneraUpdate(CSourceWriter sw)
        {
            sw.Write("update ");
            sw.Write(this.Name);
            sw.Write(" set ");
            bool Primero = true;

            foreach (CFieldM f in this.lFieldData)
            {
                sw.WriteComma(ref Primero);
                sw.Write(f.Name);
                sw.Write(" = ");
                sw.Write(SUtilBD.Param(f.Name));
            }
            sw.Write(" where ");
            Primero = true;
            foreach (CFieldM f in this.lFieldPk)
            {
                sw.WriteComma(ref Primero);
                sw.Write(f.Name);
                sw.Write(" = ");
                sw.Write(SUtilBD.Param(f.Name));
            }
        }
Exemple #6
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);
        }