Ejemplo n.º 1
0
        private void WriteTable(GenerateContext context, Table item)
        {
            var cmd = context.Command;

            var tbl = item;

            if (string.IsNullOrEmpty(tbl.Schema))
            {
                WriteLine("    [TableName(\"{0}\")]", context.EscapeSqlIdentifier(tbl.Name).Replace("\"", "\\\""));
            }
            else
            {
                WriteLine("    [TableName(\"{0}.{1}\")]", context.EscapeSqlIdentifier(tbl.Schema).Replace("\"", "\\\""), context.EscapeSqlIdentifier(tbl.Name).Replace("\"", "\\\""));
            }

            if (tbl.PK != null && tbl.PK.IsAutoIncrement)
            {
                if (tbl.SequenceName == null)
                {
                    WriteLine("    [PrimaryKey(\"{0}\")]", tbl.PK.Name);
                }
                else
                {
                    WriteLine("    [PrimaryKey(\"{0}\", sequenceName=\"{1}\")]", tbl.PK.Name, tbl.SequenceName);
                }
            }

            if (tbl.PK != null && !tbl.PK.IsAutoIncrement)
            {
                WriteLine("    [PrimaryKey(\"{0}\", AutoIncrement=false)]", tbl.PK.Name);
            }

            if (cmd.ExplicitColumns)
            {
                WriteLine("    [ExplicitColumns]");
            }

            WriteLine("    public partial class {0}", tbl.ClassName);
            if (cmd.TrackModifiedColumns)
            {
                //WriteLine("        : Record<{0}>", tbl.ClassName);
                WriteLine("        : CoreDomainModel");
            }
            WriteLine("    {");

            foreach (var col in tbl.Columns.OrderBy(x => x.Name).Where(x => !x.Ignore))
            {
                var columnParts = new List <string>();

                if (!string.IsNullOrEmpty(context.Command.IgnoreColumns) && context.Command.IgnoreColumns.Contains(col.Name))
                {
                    continue;
                }

                if (col.Name != col.PropertyName)
                {
                    columnParts.Add(string.Format("Name = \"{0}\"", col.Name));
                }

                if (col.ForceToUtc)
                {
                    columnParts.Add("ForceToUtc = true");
                }

                if (!string.IsNullOrWhiteSpace(col.InsertTemplate))
                {
                    columnParts.Add(string.Format("InsertTemplate = \"{0}\"", col.InsertTemplate));
                }

                if (!string.IsNullOrWhiteSpace(col.UpdateTemplate))
                {
                    columnParts.Add(string.Format("UpdateTemplate = \"{0}\"", col.UpdateTemplate));
                }

                if (columnParts.Any())
                {
                    WriteLine("        [Column({0})]", string.Join(", ", columnParts));
                }
                else
                {
                    if (cmd.ExplicitColumns)
                    {
                        WriteLine("        [Column]");
                    }
                }

                if (cmd.TrackModifiedColumns)
                {
                    WriteLine("        public {0}{1} {2}", col.PropertyType, Helpers.CheckNullable(col), col.PropertyName);
                    WriteLine("        {");
                    WriteLine("            get {{ return _{0}; }}", col.PropertyName);
                    WriteLine("            set");
                    WriteLine("            {");
                    WriteLine("                _{0} = value;", col.PropertyName);;
                    WriteLine("                MarkColumnModified(\"{0}\");", col.PropertyName);;
                    WriteLine("            }");
                    WriteLine("        }");

                    WriteLine("");

                    WriteLine("        private {0}{1} _{2};", col.PropertyType, Helpers.CheckNullable(col), col.PropertyName);
                }
                else
                {
                    WriteLine("        public {0}{1} {2} {{ get; set; }}", col.PropertyType, Helpers.CheckNullable(col), col.PropertyName);
                }

                WriteLine("");
            }

            WriteLine("    }");
            WriteLine("");
        }
Ejemplo n.º 2
0
        private void WriteTable(GenerateContext context, Table item)
        {
            var cmd = context.Command;

            var tbl = item;

            if (string.IsNullOrEmpty(tbl.Schema))
            {
                WriteLine("    [TableName(\"{0}\")]", context.EscapeSqlIdentifier(tbl.Name).Replace("\"", "\\\""));
            }
            else
            {
                WriteLine("    [TableName(\"{0}.{1}\")]", context.EscapeSqlIdentifier(tbl.Schema).Replace("\"", "\\\""), context.EscapeSqlIdentifier(tbl.Name).Replace("\"", "\\\""));
            }

            var PKs = tbl.PrimaryKeys;

            if (PKs.Count() == 1)
            {
                if (PKs.First().IsAutoIncrement)
                {
                    if (tbl.SequenceName == null)
                    {
                        WriteLine("    [PrimaryKey(\"{0}\")]", PKs.First().Name);
                    }
                    else
                    {
                        WriteLine("    [PrimaryKey(\"{0}\", sequenceName=\"{1}\")]", PKs.First().Name, tbl.SequenceName);
                    }
                }
                else
                {
                    //not IsAutoIncrement
                    WriteLine("    [PrimaryKey(\"{0}\", AutoIncrement=false)]", PKs.First().Name);
                }
            }
            else if (PKs.Count() > 1)
            {
                WriteLine("    //Table has multiple primary keys which PetaPoco doesn't support: {0}", String.Join(", ", PKs.Select(pk => pk.Name).ToArray()));
                WriteLine("    //[PrimaryKey(\"{0}\")]", String.Join(",", PKs.Select(pk => pk.Name).ToArray()));
            }

            if (cmd.ExplicitColumns)
            {
                WriteLine("    [ExplicitColumns]");
            }

            WriteLine("    public partial class {0}", tbl.ClassName);
            if (cmd.TrackModifiedColumns)
            {
                WriteLine("        : Record<{0}>", tbl.ClassName);
            }
            WriteLine("    {");

            foreach (var col in tbl.Columns.OrderBy(x => x.Name).Where(x => !x.Ignore))
            {
                var columnParts = new List <string>();
                if (col.Name != col.PropertyName)
                {
                    columnParts.Add(string.Format("Name = \"{0}\"", col.Name));
                }

                if (col.ForceToUtc)
                {
                    columnParts.Add("ForceToUtc = true");
                }

                if (!string.IsNullOrWhiteSpace(col.InsertTemplate))
                {
                    columnParts.Add(string.Format("InsertTemplate = \"{0}\"", col.InsertTemplate));
                }

                if (!string.IsNullOrWhiteSpace(col.UpdateTemplate))
                {
                    columnParts.Add(string.Format("UpdateTemplate = \"{0}\"", col.UpdateTemplate));
                }

                if (columnParts.Any())
                {
                    WriteLine("        [Column({0})]", string.Join(", ", columnParts));
                }
                else
                {
                    if (cmd.ExplicitColumns)
                    {
                        WriteLine("        [Column]");
                    }
                }

                if (cmd.TrackModifiedColumns)
                {
                    WriteLine("        public {0}{1} {2}", col.PropertyType, Helpers.CheckNullable(col), col.PropertyName);
                    WriteLine("        {");
                    WriteLine("            get {{ return _{0}; }}", col.PropertyName);
                    WriteLine("            set");
                    WriteLine("            {");
                    WriteLine("                _{0} = value;", col.PropertyName);;
                    WriteLine("                MarkColumnModified(\"{0}\");", col.PropertyName);;
                    WriteLine("            }");
                    WriteLine("        }");

                    WriteLine("");

                    WriteLine("        private {0}{1} _{2};", col.PropertyType, Helpers.CheckNullable(col), col.PropertyName);
                }
                else
                {
                    WriteLine("        public {0}{1} {2} {{ get; set; }}", col.PropertyType, Helpers.CheckNullable(col), col.PropertyName);
                }

                WriteLine("");
            }

            WriteLine("    }");
            WriteLine("");
        }