Exemplo n.º 1
0
        public static DbTable[] GetForeignKeyTables(IEnumerable <DbColumn> columns, DbSchema schema)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns");
            }
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            var foreignKeyTables = new List <DbTable>();

            foreach (var column in columns)
            {
                var foreignKey = column.DbForeignKey;
                if (foreignKey != null)
                {
                    var foreignKeyTable = FindTableByForeignKey(schema, foreignKey);
                    var collectionType  = ClrTypeHelper.GetCollectionType(DbTableConverter.ToClrClass(foreignKeyTable, schema.Tables).Properties);
                    if (collectionType == null)
                    {
                        var name   = foreignKeyTable.Name;
                        var exists = false;
                        foreach (var t in foreignKeyTables)
                        {
                            if (t.Name == name)
                            {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists)
                        {
                            foreignKeyTables.Add(foreignKeyTable);
                        }
                    }
                }
            }

            return(foreignKeyTables.ToArray());
        }
Exemplo n.º 2
0
        public void BuilderApp(DbSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (_rootDirectory.Exists)
            {
                _rootDirectory.Delete(true);
            }
            _rootDirectory.Create();

            var dbScript = DbSchemaParser.GenerateScript(schema);

            File.WriteAllText(Path.Combine(_rootDirectory.FullName, @"script.sql"), dbScript);

            // Objects
            var objectsPath = Path.Combine(_rootDirectory.FullName, @"Objects");

            Directory.CreateDirectory(objectsPath);
            // Adapters
            var adaptersPath = Path.Combine(_rootDirectory.FullName, @"Adapters");

            Directory.CreateDirectory(adaptersPath);
            // Helpers
            var helpersPath = Path.Combine(_rootDirectory.FullName, @"Helpers");

            Directory.CreateDirectory(helpersPath);
            // Data
            var dataPath = Path.Combine(_rootDirectory.FullName, @"Data");

            Directory.CreateDirectory(dataPath);

            var buffer = new StringBuilder();

            var tables = schema.Tables;

            foreach (var table in tables)
            {
                var clrClass = DbTableConverter.ToClrClass(table, tables);

                buffer.Clear();
                buffer.AppendLine(GetObjectUsings(clrClass));
                buffer.AppendLine(string.Format(@"namespace {0}.{1}", schema.Name, Path.GetFileName(objectsPath)));
                buffer.AppendLine(@"{");
                buffer.AppendLine(ObjectGenerator.GenerateCode(clrClass, table.IsReadOnly));
                buffer.AppendLine(@"}");
                File.WriteAllText(Path.Combine(objectsPath, clrClass.Name) + @".cs", buffer.ToString());

                buffer.Clear();
                buffer.AppendLine(GetAdapterUsings(schema.Name));
                buffer.AppendLine(string.Format(@"namespace {0}.{1}", schema.Name, Path.GetFileName(adaptersPath)));
                buffer.AppendLine(@"{");
                buffer.AppendLine(AdapterGenerator.GenerateCode(clrClass, table, schema));
                buffer.AppendLine(@"}");
                File.WriteAllText(Path.Combine(adaptersPath, table.Name + @"Adapter") + @".cs", buffer.ToString());

                if (table.IsReadOnly)
                {
                    buffer.Clear();
                    buffer.AppendLine(GetHelperUsings(schema.Name));
                    buffer.AppendLine(string.Format(@"namespace {0}.{1}", schema.Name, Path.GetFileName(helpersPath)));
                    buffer.AppendLine(@"{");
                    buffer.AppendLine(HelperGenerator.GenerateCode(table));
                    buffer.AppendLine(@"}");

                    File.WriteAllText(Path.Combine(helpersPath, clrClass.Name + @"Helper") + @".cs", buffer.ToString());
                }
            }

            buffer.Clear();
            buffer.AppendLine(GetAppUsings(schema.Name));
            buffer.AppendLine(string.Format(@"namespace {0}", schema.Name));
            buffer.AppendLine(@"{");
            buffer.AppendLine(AppGenerator.GenerateCode(schema));
            buffer.AppendLine(@"}");
            File.WriteAllText(Path.Combine(_rootDirectory.FullName, string.Format(@"{0}App.cs", schema.Name)), buffer.ToString());

            buffer.Clear();
            buffer.AppendLine(GetSystemUsings());
            buffer.AppendLine(string.Format(@"namespace {0}", schema.Name));
            buffer.AppendLine(@"{");
            buffer.AppendLine(AppGenerator.GenerateEventArgsClass());
            buffer.AppendLine(@"}");
            File.WriteAllText(Path.Combine(_rootDirectory.FullName, @"HelperLoadedEventArgs.cs"), buffer.ToString());

            buffer.Clear();
            buffer.AppendLine(GetDataUsings());
            buffer.AppendLine(string.Format(@"namespace {0}.{1}", schema.Name, Path.GetFileName(dataPath)));
            buffer.AppendLine(@"{");
            buffer.AppendLine(QueryHelperGenerator.GetCode());
            buffer.AppendLine(@"}");
            File.WriteAllText(Path.Combine(dataPath, @"QueryHelper.cs"), buffer.ToString());
        }