public ColumnModel(SchemaModel owner, XElement element)
            : this(owner)
        {
            name = element.Attribute("Name").Value;

            dataType = Type.GetType(element.Attribute("DataType").Value);

            var def = element.Element("DefaultValue");
            if (def.Element("Null") == null)    //If it's not <Null />
                DefaultValue = def.Value;

            allowNulls = (bool)element.Attribute("AllowNulls");
            description = element.Attribute("Description").Value;
            expression = element.Attribute("Expression").Value.NonEmpty();
            generateSqlMapping = (bool)element.Attribute("GenerateSqlMapping");
            isUnique = (bool)element.Attribute("IsUnique");
            propertyName = element.Attribute("PropertyName").Value;
            propertyVisibility = Utils.ParseVisibility(element.Attribute("PropertyVisibility").Value);
            sqlName = element.Attribute("SqlName").Value.NonEmpty();

            var pSchema = element.Attribute("ForeignSchemaName");
            if (pSchema != null)
                ForeignSchema = Owner.Owner.Schemas.Single(c => c.Name == pSchema.Value);
            //The ForeignSchema resets these properties
            foreignRelationName = element.Attribute("ForeignRelationName").Value.NonEmpty();
            foreignRelationPropertyDescription = element.Attribute("ForeignRelationPropertyDescription").Value.NonEmpty();
            foreignRelationPropertyName = element.Attribute("ForeignRelationPropertyName").Value.NonEmpty();
        }
        public SchemaDetailForm(DataContextModel context, SchemaModel schema)
        {
            InitializeComponent();
            this.schema = schema;
            this.context = context;

            dataTypeEdit.Items.AddRange(standardColumnTypes);
            dataTypeEdit.DropDownRows = standardColumnTypes.Length;

            columnPickerEdit.DataSource = schema.Columns;
            schemaBindingSource.DataSource = schema;
            schemaVGrid.DataSource = new[] { schema };

            UpdateNames();

            UpdateForeignSchemaEdit(true);
            UpdateColumnSqlNameEdit();

            schema.PropertyChanged += schema_PropertyChanged;
            context.Schemas.ListChanged += Schemas_ListChanged;
            columnsView.BestFitColumns();
            columnPickerEdit.View.BestFitColumns();
        }
        public ColumnModel(SchemaModel owner)
        {
            if (owner == null) throw new ArgumentNullException("owner");

            Owner = owner;
        }
Exemple #4
0
 private void addSchema_ItemClick(object sender, ItemClickEventArgs e)
 {
     var newSchema = new SchemaModel(context) { Name = "Table" + (context.Schemas.Count + 1) };
     context.Schemas.Add(newSchema);
     new SchemaDetailForm(context, newSchema) { MdiParent = this }.Show();
 }
Exemple #5
0
        void AddSchemaTree(SchemaModel schema, TreeListNode parentNode)
        {
            var node = tree.AppendNode(new object[] { schema.Name, schema.SqlSchemaName, schema.IsExternal }, parentNode, schema);

            foreach (var child in schema.ChildSchemas) {
                AddSchemaTree(child, node);
            }
        }
        static void WriteSchema(SchemaModel schema, IndentedTextWriter writer)
        {
            writer.WriteLine(@"///<summary>" + schema.RowClassDescription.EscapeXml() + @"</summary>");
            writer.WriteLine(schema.RowClassVisibility.ToString().ToLowerInvariant() + " partial class " + schema.RowClassName + " : Row {");
            writer.Indent++;

            writer.WriteLine(@"///<summary>Creates a new " + schema.RowClassName.EscapeXml() + @" instance.</summary>");
            writer.WriteLine("public " + schema.RowClassName + " () : base(Schema) { Initialize(); }");
            writer.WriteLine("partial void Initialize();");

            writer.WriteLine();

            writer.WriteLine(@"///<summary>Creates a strongly-typed " + schema.Name.EscapeXml() + @" table.</summary>");
            writer.WriteLine("public static TypedTable<" + schema.RowClassName + "> CreateTable() "
                                + "{ return new TypedTable<" + schema.RowClassName + ">(Schema, () => new " + schema.RowClassName + "()); }");

            writer.WriteLine();
            foreach (var column in schema.Columns) {
                writer.WriteLine(@"///<summary>Gets the schema's " + column.Name.EscapeXml() + @" column.</summary>");
                writer.WriteLine("public static " + column.ColumnType.Name + " " + column.ColumnPropertyName + " { get; private set; }");
            }
            writer.WriteLine();

            writer.WriteLine(@"///<summary>Gets the " + schema.Name.EscapeXml() + @" schema instance.</summary>");
            writer.WriteLine("public static new TypedSchema<" + schema.RowClassName + "> Schema { get; private set; }");

            writer.WriteLine(@"///<summary>Gets the SchemaMapping that maps this schema to the SQL Server " + schema.SqlName.EscapeXml() + @" table.</summary>");
            writer.WriteLine("public static SchemaMapping SchemaMapping { get; private set; }");

            writer.WriteLine();
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("static " + schema.RowClassName + "() {");
            writer.Indent++;
            writer.WriteLine("#region Create Schema");
            writer.WriteLine("Schema = new TypedSchema<" + schema.RowClassName + ">(" + schema.Name.Quote() + ");");
            foreach (var column in schema.Columns) {
                writer.WriteLine();
                WriteColumnCreation(column, writer);
            }
            writer.WriteLine("#endregion");
            writer.WriteLine();

            writer.WriteLine("#region Create SchemaMapping");
            writer.WriteLine("SchemaMapping = new SchemaMapping(Schema, false);");
            writer.WriteLine("SchemaMapping.SqlName = " + schema.SqlName.Quote() + ";");
            writer.WriteLine("SchemaMapping.SqlSchemaName = " + schema.SqlSchemaName.Quote() + ";");
            writer.WriteLine();
            foreach (var column in schema.Columns.Where(c => c.GenerateSqlMapping))
                writer.WriteLine("SchemaMapping.Columns.AddMapping(" + column.PropertyName + "Column, " + column.SqlName.Quote() + ");");
            writer.WriteLine("#endregion");
            writer.WriteLine("SchemaMapping.SetPrimaryMapping(SchemaMapping);");

            writer.Indent--;
            writer.WriteLine("}");

            writer.WriteLine();

            writer.WriteLine(@"///<summary>Gets the typed table that contains this row, if any.</summary>");
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("public new TypedTable<" + schema.RowClassName + "> Table { get { return (TypedTable<" + schema.RowClassName + ">)base.Table; } }");

            writer.WriteLine("#region Value Properties");
            foreach (var column in schema.Columns)
                WriteValueProperty(column, writer);
            writer.WriteLine("#endregion");

            if (schema.ChildSchemas.Any()) {
                writer.WriteLine();

                writer.WriteLine("#region ChildRows Properties");
                //Support schemas with two foreign keys
                //that reference the same parent schema
                foreach (var foreignColumn in schema.ChildSchemas.Distinct().SelectMany(fs => fs.Columns.Where(c => c.ForeignSchema == schema))) {
                    //Example:
                    //public IChildRowCollection<Pledge> Pledges { get { return TypedChildRows<Pledge>(Pledge.PersonIdColumn); } }

                    writer.WriteLine(@"///<summary>" + foreignColumn.ForeignRelationPropertyDescription.EscapeXml() + @"</summary>");
                    writer.WriteLine(foreignColumn.Owner.RowClassVisibility.ToString().ToLowerInvariant()
                                   + " IChildRowCollection<" + foreignColumn.Owner.RowClassName + "> " + foreignColumn.ForeignRelationPropertyName
                                   + " { get { return TypedChildRows<" + foreignColumn.Owner.RowClassName + ">(" + foreignColumn.Owner.RowClassName + "." + foreignColumn.PropertyName + "Column); } }");
                }
                writer.WriteLine("#endregion");
            }

            writer.WriteLine();

            writer.WriteLine("#region Partial Methods");
            writer.WriteLine("partial void OnColumnChanged(Column column, object oldValue, object newValue);");

            foreach (var column in schema.Columns.NonCalculated()) {
                writer.WriteLine();
                WriteColumnPartials(column, writer);
            }
            writer.WriteLine("#endregion");

            writer.WriteLine();

            writer.WriteLine("#region Column Callbacks");
            #region ValidateValue
            writer.WriteLine(@"///<summary>Checks whether a value would be valid for a given column in an attached row.</summary>");
            writer.WriteLine(@"///<param name=""column"">The column containing the value.</param>");
            writer.WriteLine(@"///<param name=""newValue"">The value to validate.</param>");
            writer.WriteLine(@"///<returns>An error message, or null if the value is valid.</returns>");
            writer.WriteLine(@"///<remarks>This method is overridden by typed rows to perform custom validation logic.</remarks>");
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("public override string ValidateValue(Column column, object newValue) {");
            writer.Indent++;
            writer.WriteLine("string error = base.ValidateValue(column, newValue);");
            writer.WriteLine("if (!String.IsNullOrEmpty(error)) return error;");
            writer.WriteLine("Action<string> reporter = s => error = s;");
            writer.WriteLine();

            bool first = true;
            foreach (var column in schema.Columns.NonCalculated()) {
                if (first)
                    first = false;
                else
                    writer.Write("} else ");
                writer.WriteLine("if (column == " + column.PropertyName + "Column) {");
                writer.Indent++;
                writer.WriteLine("Validate" + column.PropertyName + "((" + column.ActualType + ")newValue, reporter);");
                writer.WriteLine("if (!String.IsNullOrEmpty(error)) return error;");
                writer.Indent--;
            }
            writer.WriteLine("}");

            writer.WriteLine("return null;");

            writer.Indent--;
            writer.WriteLine("}");
            #endregion
            #region OnValueChanged
            writer.WriteLine(@"///<summary>Processes an explicit change of a column value.</summary>");
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("protected override void OnValueChanged(Column column, object oldValue, object newValue) {");
            writer.Indent++;
            writer.WriteLine("base.OnValueChanged(column, oldValue, newValue);");
            writer.WriteLine("OnColumnChanged(column, oldValue, newValue);");
            first = true;
            foreach (var column in schema.Columns.NonCalculated()) {
                if (first)
                    first = false;
                else
                    writer.Write("else ");
                writer.WriteLine("if (column == " + column.PropertyName + "Column)");
                writer.WriteLine("\tOn" + column.PropertyName + "Changed((" + column.NullableType + ")oldValue, (" + column.NullableType + ")newValue);");
            }
            writer.Indent--;
            writer.WriteLine("}");
            #endregion
            writer.WriteLine("#endregion");

            writer.Indent--;
            writer.WriteLine("}");
        }
Exemple #7
0
 public ColumnList(SchemaModel schema)
 {
     this.schema = schema; AllowNew = true;
 }
Exemple #8
0
 internal void RemoveChild(SchemaModel schema)
 {
     childSchemas.Remove(schema);
 }
Exemple #9
0
 internal void AddChild(SchemaModel schema)
 {
     childSchemas.Add(schema);
 }
Exemple #10
0
        public static IEnumerable<SchemaModel> ReadSchemas(DataContextModel owner, DBConnector database)
        {
            if (database == null) throw new ArgumentNullException("database");

            List<SchemaModel> tables = new List<SchemaModel>();
            List<Action> postColumnActions = new List<Action>();

            Func<string, string, SchemaModel> Table = (schema, name) =>	//First look for a new table from SQL Server, then for an existing one.
                String.IsNullOrEmpty(name) ? null : tables.SingleOrDefault(t => t.SqlSchemaName == schema && t.SqlName == name)
                                                 ?? owner.Schemas.SingleOrDefault(t => t.SqlSchemaName == schema && t.SqlName == name);

            using (var connection = database.OpenConnection()) {
                #region Read Tables
                using (var reader = database.ExecuteReader(TablesSql)) {
                    while (reader.Read()) {
                        var table = new SchemaModel(owner) {
                            Name = (string)reader["TableName"],
                            SqlName = (string)reader["TableName"],
                            SqlSchemaName = reader["SchemaName"] as string
                        };

                        if (Table(table.SqlSchemaName, table.SqlName) != null) continue;	//TODO: Import column

                        string keyName = reader["PrimaryKeyName"] as string;
                        if (!String.IsNullOrEmpty(keyName)) {
                            postColumnActions.Add(
                                () => table.PrimaryKey = table.Columns.Single(c => c.SqlName == keyName)
                            );
                        }

                        tables.Add(table);
                    }
                }
                #endregion

                using (var reader = database.ExecuteReader(ColumnsSql)) {
                    while (reader.Read()) {
                        var table = Table((string)reader["SchemaName"], (string)reader["TableName"]);
                        if (table == null) continue;	//Skip tables without RowVersion columns

                        var name = (string)reader["ColumnName"];
                        if (table.Columns.Any(c => c.SqlName == name))
                            continue;	//Don't add duplicate columns to existing tables.

                        table.Columns.Add(new ColumnModel(table) {
                            Name = name,
                            SqlName = name,
                            DataType = SqlTypes[(string)reader["DataType"]],

                            AllowNulls = 1 == (int)reader["AllowNulls"],
                            IsUnique = 1 == (int)reader["IsUnique"],

                            ForeignSchema = Table(reader["ForeignSchema"] as string, reader["ForeignTable"] as string)
                        });
                    }
                }
            }

            postColumnActions.ForEach(a => a());

            return tables;
        }
Exemple #11
0
        static void WriteSchema(SchemaModel schema, IndentedTextWriter writer)
        {
            writer.WriteLine(@"///<summary>" + schema.RowClassDescription.EscapeXml() + @"</summary>");
            writer.WriteLine(schema.RowClassVisibility.ToString().ToLowerInvariant() + " partial class " + schema.RowClassName + " : Row {");
            writer.Indent++;

            writer.WriteLine(@"///<summary>Creates a new " + schema.RowClassName.EscapeXml() + @" instance.</summary>");
            writer.WriteLine("public " + schema.RowClassName + " () : base(Schema) { Initialize(); }");
            writer.WriteLine("partial void Initialize();");

            writer.WriteLine();

            writer.WriteLine(@"///<summary>Creates a strongly-typed " + schema.Name.EscapeXml() + @" table.</summary>");
            writer.WriteLine("public static TypedTable<" + schema.RowClassName + "> CreateTable() "
                             + "{ return new TypedTable<" + schema.RowClassName + ">(Schema, () => new " + schema.RowClassName + "()); }");

            writer.WriteLine();
            foreach (var column in schema.Columns)
            {
                writer.WriteLine(@"///<summary>Gets the schema's " + column.Name.EscapeXml() + @" column.</summary>");
                writer.WriteLine("public static " + column.ColumnType.Name + " " + column.ColumnPropertyName + " { get; private set; }");
            }
            writer.WriteLine();


            writer.WriteLine(@"///<summary>Gets the " + schema.Name.EscapeXml() + @" schema instance.</summary>");
            writer.WriteLine("public static new TypedSchema<" + schema.RowClassName + "> Schema { get; private set; }");

            writer.WriteLine(@"///<summary>Gets the SchemaMapping that maps this schema to the SQL Server " + schema.SqlName.EscapeXml() + @" table.</summary>");
            writer.WriteLine("public static SchemaMapping SchemaMapping { get; private set; }");

            writer.WriteLine();
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("static " + schema.RowClassName + "() {");
            writer.Indent++;
            writer.WriteLine("#region Create Schema");
            writer.WriteLine("Schema = new TypedSchema<" + schema.RowClassName + ">(" + schema.Name.Quote() + ");");
            foreach (var column in schema.Columns)
            {
                writer.WriteLine();
                WriteColumnCreation(column, writer);
            }
            writer.WriteLine("#endregion");
            writer.WriteLine();

            writer.WriteLine("#region Create SchemaMapping");
            writer.WriteLine("SchemaMapping = new SchemaMapping(Schema, false);");
            writer.WriteLine("SchemaMapping.SqlName = " + schema.SqlName.Quote() + ";");
            writer.WriteLine("SchemaMapping.SqlSchemaName = " + schema.SqlSchemaName.Quote() + ";");
            writer.WriteLine();
            foreach (var column in schema.Columns.Where(c => c.GenerateSqlMapping))
            {
                writer.WriteLine("SchemaMapping.Columns.AddMapping(" + column.PropertyName + "Column, " + column.SqlName.Quote() + ");");
            }
            writer.WriteLine("#endregion");
            writer.WriteLine("SchemaMapping.SetPrimaryMapping(SchemaMapping);");

            writer.Indent--;
            writer.WriteLine("}");

            writer.WriteLine();

            writer.WriteLine(@"///<summary>Gets the typed table that contains this row, if any.</summary>");
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("public new TypedTable<" + schema.RowClassName + "> Table { get { return (TypedTable<" + schema.RowClassName + ">)base.Table; } }");

            writer.WriteLine("#region Value Properties");
            foreach (var column in schema.Columns)
            {
                WriteValueProperty(column, writer);
            }
            writer.WriteLine("#endregion");

            if (schema.ChildSchemas.Any())
            {
                writer.WriteLine();

                writer.WriteLine("#region ChildRows Properties");
                //Support schemas with two foreign keys
                //that reference the same parent schema
                foreach (var foreignColumn in schema.ChildSchemas.Distinct().SelectMany(fs => fs.Columns.Where(c => c.ForeignSchema == schema)))
                {
                    //Example:
                    //public IChildRowCollection<Pledge> Pledges { get { return TypedChildRows<Pledge>(Pledge.PersonIdColumn); } }

                    writer.WriteLine(@"///<summary>" + foreignColumn.ForeignRelationPropertyDescription.EscapeXml() + @"</summary>");
                    writer.WriteLine(foreignColumn.Owner.RowClassVisibility.ToString().ToLowerInvariant()
                                     + " IChildRowCollection<" + foreignColumn.Owner.RowClassName + "> " + foreignColumn.ForeignRelationPropertyName
                                     + " { get { return TypedChildRows<" + foreignColumn.Owner.RowClassName + ">(" + foreignColumn.Owner.RowClassName + "." + foreignColumn.PropertyName + "Column); } }");
                }
                writer.WriteLine("#endregion");
            }

            writer.WriteLine();

            writer.WriteLine("#region Partial Methods");
            writer.WriteLine("partial void OnColumnChanged(Column column, object oldValue, object newValue);");


            foreach (var column in schema.Columns.NonCalculated())
            {
                writer.WriteLine();
                WriteColumnPartials(column, writer);
            }
            writer.WriteLine("#endregion");

            writer.WriteLine();

            writer.WriteLine("#region Column Callbacks");
            #region ValidateValue
            writer.WriteLine(@"///<summary>Checks whether a value would be valid for a given column in an attached row.</summary>");
            writer.WriteLine(@"///<param name=""column"">The column containing the value.</param>");
            writer.WriteLine(@"///<param name=""newValue"">The value to validate.</param>");
            writer.WriteLine(@"///<returns>An error message, or null if the value is valid.</returns>");
            writer.WriteLine(@"///<remarks>This method is overridden by typed rows to perform custom validation logic.</remarks>");
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("public override string ValidateValue(Column column, object newValue) {");
            writer.Indent++;
            writer.WriteLine("string error = base.ValidateValue(column, newValue);");
            writer.WriteLine("if (!String.IsNullOrEmpty(error)) return error;");
            writer.WriteLine("Action<string> reporter = s => error = s;");
            writer.WriteLine();

            bool first = true;
            foreach (var column in schema.Columns.NonCalculated())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write("} else ");
                }
                writer.WriteLine("if (column == " + column.PropertyName + "Column) {");
                writer.Indent++;
                writer.WriteLine("Validate" + column.PropertyName + "((" + column.ActualType + ")newValue, reporter);");
                writer.WriteLine("if (!String.IsNullOrEmpty(error)) return error;");
                writer.Indent--;
            }
            writer.WriteLine("}");

            writer.WriteLine("return null;");

            writer.Indent--;
            writer.WriteLine("}");
            #endregion
            #region OnValueChanged
            writer.WriteLine(@"///<summary>Processes an explicit change of a column value.</summary>");
            writer.WriteGeneratedCodeAttribute();
            writer.WriteLine("protected override void OnValueChanged(Column column, object oldValue, object newValue) {");
            writer.Indent++;
            writer.WriteLine("base.OnValueChanged(column, oldValue, newValue);");
            writer.WriteLine("OnColumnChanged(column, oldValue, newValue);");
            first = true;
            foreach (var column in schema.Columns.NonCalculated())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write("else ");
                }
                writer.WriteLine("if (column == " + column.PropertyName + "Column)");
                writer.WriteLine("\tOn" + column.PropertyName + "Changed((" + column.NullableType + ")oldValue, (" + column.NullableType + ")newValue);");
            }
            writer.Indent--;
            writer.WriteLine("}");
            #endregion
            writer.WriteLine("#endregion");

            writer.Indent--;
            writer.WriteLine("}");
        }