Exemplo n.º 1
0
        public static SchemaMapping[] Compile(DataContextModel model)
        {
            var options = new CompilerParameters(References.Select(a => a.Location).ToArray()) {
                GenerateInMemory = true
            };
            try {
                var sourceFile = options.TempFiles.AddExtension("cs");

                using (var writer = new StreamWriter(sourceFile))
                    model.WriteClasses(writer);

                var compiler = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });

                var results = compiler.CompileAssemblyFromFile(options, sourceFile);

                if (results.Errors.Count > 0)
                    throw new InvalidOperationException(results.Errors.Cast<CompilerError>().Join(Environment.NewLine, ce => ce.ErrorText));

                return
                    model.Schemas.Select(s =>
                        (SchemaMapping)
                            results.CompiledAssembly
                                   .GetType(model.Namespace + "." + s.RowClassName)
                                   .GetProperty("SchemaMapping").GetValue(null, null)
                    ).SortDependencies(sm => sm.Schema)
                     .ToArray();
            } finally { options.TempFiles.Delete(); }
        }
Exemplo n.º 2
0
        public static string GenerateSource(this DataContextModel context)
        {
            var writer = new StringWriter(CultureInfo.InvariantCulture);

            context.WriteClasses(writer);
            return(writer.ToString());
        }
Exemplo n.º 3
0
        public SchemaModel(DataContextModel owner)
        {
            Owner = owner;

            Columns = new ColumnList(this);

            ChildSchemas = new ReadOnlyObservableCollection <SchemaModel>(childSchemas);
        }
Exemplo n.º 4
0
        public static void WriteClasses(this DataContextModel model, TextWriter writer)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            var indentedWriter = new IndentedTextWriter(writer);

            indentedWriter.WriteLine("using System;");
            indentedWriter.WriteLine("using System.CodeDom.Compiler;");
            indentedWriter.WriteLine("using System.Diagnostics;");
            indentedWriter.WriteLine("using System.Linq;");
            indentedWriter.WriteLine("using ShomreiTorah.Singularity;");
            indentedWriter.WriteLine("using ShomreiTorah.Singularity.Sql;");

            foreach (var import in model.Imports)
            {
                indentedWriter.WriteLine("using " + import.Namespace + ";");
            }

            indentedWriter.WriteLine();
            indentedWriter.WriteLine("namespace " + model.Namespace + " {");
            indentedWriter.Indent++;

            bool first = true;

            foreach (var schema in model.Schemas.Where(s => !s.IsExternal).OrderBy(s => s.Name))
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    indentedWriter.WriteLine();
                }
                WriteSchema(schema, indentedWriter);
            }

            indentedWriter.Indent--;
            indentedWriter.WriteLine("}");
        }
Exemplo n.º 5
0
        public SchemaModel(DataContextModel owner, XElement element)
            : this(owner) {
            Name = element.Attribute("Name").Value;

            RowClassVisibility  = Utils.ParseVisibility(element.Attribute("RowClassVisibility").Value);
            RowClassName        = element.Attribute("RowClassName").Value;
            RowClassDescription = element.Attribute("RowClassDescription").Value;
            SqlName             = element.Attribute("SqlName").Value;
            SqlSchemaName       = element.Attribute("SqlSchemaName").Value;

            Columns.AddRange(element.Elements("Column").Select(e => new ColumnModel(this, e)));

            var pKey = element.Attribute("PrimaryKeyName");
            if (pKey != null)
            {
                PrimaryKey = Columns.Single(c => c.Name == pKey.Value);
            }
        }
Exemplo n.º 6
0
        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();
        }
Exemplo n.º 7
0
        private void openFile_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!CheckDirty()) return;

            using (var openDialog = new OpenFileDialog {
                FileName = context.Name,
                Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
                Title = "Open DataContext"
            }) {
                if (openDialog.ShowDialog(this) != DialogResult.OK)
                    return;
                CurrentFilePath = openDialog.FileName;
            }
            context = new DataContextModel(CurrentFilePath);
            BindToContext();
        }
Exemplo n.º 8
0
        private void newFile_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!CheckDirty()) return;
            CurrentFilePath = null;
            context = new DataContextModel();

            BindToContext();
        }
Exemplo n.º 9
0
 public SchemaModel(DataContextModel owner, XElement element, bool isExternal)
     : this(owner, element)
 {
     IsExternal = isExternal;
 }
Exemplo n.º 10
0
        public SchemaModel(DataContextModel owner, XElement element)
            : this(owner)
        {
            Name = element.Attribute("Name").Value;

            RowClassVisibility = Utils.ParseVisibility(element.Attribute("RowClassVisibility").Value);
            RowClassName = element.Attribute("RowClassName").Value;
            RowClassDescription = element.Attribute("RowClassDescription").Value;
            SqlName = element.Attribute("SqlName").Value;
            SqlSchemaName = element.Attribute("SqlSchemaName").Value;

            Columns.AddRange(element.Elements("Column").Select(e => new ColumnModel(this, e)));

            var pKey = element.Attribute("PrimaryKeyName");
            if (pKey != null)
                PrimaryKey = Columns.Single(c => c.Name == pKey.Value);
        }
Exemplo n.º 11
0
 public SchemaModel(DataContextModel owner, XElement element, bool isExternal) : this(owner, element)
 {
     IsExternal = isExternal;
 }
Exemplo n.º 12
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;
        }