private void RenderTableMenu(ITableInfoModel selectedTable) { WinConsole.WriteLine("Actions:"); WinConsole.WriteLine(@"\ForModel [\c] [ColumnName] [[NewName] | [\d]] "); WinConsole.WriteLine(@" Adds a ForModelAttribute to a Property or class with \c for class. deletes it with \d"); WinConsole.WriteLine("Example:"); WinConsole.WriteLine(@" \ForModelAttribute \c NewTableName"); WinConsole.WriteLine(" Adding a ForModelAttribute Attribute to the generated class"); WinConsole.WriteLine(@" \ForModelAttribute ID_Column NewName"); WinConsole.WriteLine( " Adding a ForModelAttribute Attribute to the generated Property with the value NewName"); WinConsole.WriteLine(); WinConsole.WriteLine(@"\Exclude [true | false] [ColumnName]"); WinConsole.WriteLine(" Exclude this table from the Process"); WinConsole.WriteLine(@"\InsertIgnore true | false] [ColumnName]"); WinConsole.WriteLine(" Exclude this column from inserts"); WinConsole.WriteLine(@"\Enum [true | false] [ColumnName]"); WinConsole.WriteLine(" Marks this Column as an ENUM field on the Database. Experimental"); WinConsole.WriteLine(@"\Fallack [true | false]]"); WinConsole.WriteLine(" Should create a LoadNotImplimentedDynamic Property for Not Loaded fields"); WinConsole.WriteLine(@"\Createloader [true | false]]"); WinConsole.WriteLine(" Should create a Dataloader that loads the Properties from the IDataRecord"); WinConsole.WriteLine(@"\CreateSelect [true | false]]"); WinConsole.WriteLine(" Should create a Attribute with a Select Statement"); WinConsole.WriteLine(@"\stats"); WinConsole.WriteLine(" Shows all avalible data from this table"); WinConsole.WriteLine(@"\back"); WinConsole.WriteLine(" Go back to Main Menu"); var readLine = Program.AutoConsole.GetNextOption(); if (string.IsNullOrEmpty(readLine)) { RenderMenu(); return; } var parts = readLine.Split(' ').ToArray(); if (parts.Any()) { switch (parts[0].ToLower()) { case @"\formodel": if (parts.Length == 3) { var deleteOrNewName = parts[2]; if (parts[1] == @"\c") { if (deleteOrNewName == @"\d") { selectedTable.NewTableName = string.Empty; } else { selectedTable.NewTableName = parts[2]; } WinConsole.WriteLine("Renamed from {0} to {1}", selectedTable.Info.TableName, selectedTable.NewTableName); } else { var oldName = parts[1]; var columnToRename = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == oldName); if (deleteOrNewName != @"\d") { if (columnToRename != null) { columnToRename.NewColumnName = deleteOrNewName; WinConsole.WriteLine("Renamed from {0} to {1}", oldName, deleteOrNewName); } else { WinConsole.WriteLine("There is no Column that is named like {0}", deleteOrNewName); } } else { if (columnToRename != null) { columnToRename.NewColumnName = string.Empty; WinConsole.WriteLine("Removed the Renaming from {0} to {1}", deleteOrNewName, parts[1]); } else { WinConsole.WriteLine("There is no Column that is named like {0}", deleteOrNewName); } } } } else { WinConsole.WriteLine("Unvalid Input expected was [ColumnName] [NewName] "); WinConsole.WriteLine(); } break; case @"\insertignore": if (parts.Length == 3) { bool result; bool.TryParse(parts[1], out result); var column = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == parts[2]); if (column == null) { WinConsole.WriteLine("Unvalid Input expected was [ColumnName] "); WinConsole.WriteLine(); break; } column.InsertIgnore = result; } else { WinConsole.WriteLine("Unvalid Input expected was true | false "); WinConsole.WriteLine(); } break; case @"\enum": if (parts.Length == 3) { bool result; bool.TryParse(parts[1], out result); var column = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == parts[2]); if (column == null) { WinConsole.WriteLine("Unvalid Input expected was [ColumnName] "); WinConsole.WriteLine(); break; } if (result == false) { column.EnumDeclaration = null; break; } if (column.ForgeinKeyDeclarations == null) { WinConsole.WriteLine("Declare the Enum:"); WinConsole.WriteLine("Format: [Number] [Description]"); WinConsole.WriteLine("Example: '1 Valid'"); WinConsole.WriteLine("Type ok to submit"); WinConsole.WriteLine("Type cancel to revert"); column.EnumDeclaration = new EnumDeclarationModel(); WinConsole.WriteLine("Name of Enum:"); column.EnumDeclaration.Name = Program.AutoConsole.GetNextOption(); while (true) { var inp = Program.AutoConsole.GetNextOption(); if (inp.ToLower() == "ok") { break; } if (inp.ToLower() == "cancel") { column.EnumDeclaration = null; } var option = inp.Split(' '); if (option.Length == 2) { var enumNumber = option[0]; int enumNumberResult; if (int.TryParse(enumNumber, out enumNumberResult)) { column.EnumDeclaration.Values.Add(enumNumberResult, option[1]); WinConsole.WriteLine("Added Enum member {0} = {1}", option[1], enumNumberResult); } else { WinConsole.WriteLine("Invalid Enum number Supplyed"); } } else { WinConsole.WriteLine("Invalid Enum member Supplyed"); } } } else { WinConsole.WriteLine("Enum is ForgeinKey."); WinConsole.WriteLine("Read data from Database to autogenerate Enum"); WinConsole.WriteLine("Reading table: '{0}'", column.ForgeinKeyDeclarations.TableName); var tableContent = MsSqlStructure.GetEnumValuesOfType(column.ForgeinKeyDeclarations.TableName); if (!tableContent.Any()) { WinConsole.WriteLine("The Enum table '{0}' does not contain any data", column.ForgeinKeyDeclarations.TableName); break; } if (tableContent.First().PropertyBag.Count > 2) { WinConsole.WriteLine("The Enum table '{0}' contains more then 2 columns", column.ForgeinKeyDeclarations.TableName); break; } if (!tableContent.Any(s => s.PropertyBag.Any(f => f.Value is int))) { WinConsole.WriteLine("The Enum table '{0}' does not contains exactly one column of type int", column.ForgeinKeyDeclarations.TableName); break; } if (!tableContent.Any(s => s.PropertyBag.Any(f => f.Value is string))) { WinConsole.WriteLine("The Enum table '{0}' does not contains exactly one column of type int", column.ForgeinKeyDeclarations.TableName); break; } column.EnumDeclaration = new EnumDeclarationModel(); column.EnumDeclaration.Name = column.ForgeinKeyDeclarations.TableName + "LookupValues"; foreach (var item in tableContent) { var pk = (int)item.PropertyBag.FirstOrDefault(s => s.Value is int).Value; var value = (string)item.PropertyBag.FirstOrDefault(s => s.Value is string).Value; column.EnumDeclaration.Values.Add(pk, value); WinConsole.WriteLine("Adding Enum member '{0}' = '{1}'", value, pk); } } } else { WinConsole.WriteLine("Unvalid Input expected was true | false "); WinConsole.WriteLine(); } break; case @"\exclude": if (parts.Length == 2) { bool result; bool.TryParse(parts[1], out result); selectedTable.Exclude = result; } else { if (parts.Length == 3) { bool result; bool.TryParse(parts[1], out result); var column = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == parts[2]); if (column == null) { WinConsole.WriteLine("Unvalid Input expected was [ColumnName] "); WinConsole.WriteLine(); break; } column.Exclude = result; } else { WinConsole.WriteLine("Unvalid Input expected was true | false "); WinConsole.WriteLine(); } } break; case @"\fallack": if (parts.Length == 2) { bool result; bool.TryParse(parts[1], out result); selectedTable.CreateFallbackProperty = result; } else { WinConsole.WriteLine("Unvalid Input expected was true | false "); WinConsole.WriteLine(); } break; case @"\createloader": if (parts.Length == 2) { bool result; bool.TryParse(parts[1], out result); selectedTable.CreateDataRecordLoader = result; } else { WinConsole.WriteLine("Unvalid Input expected was true | false "); WinConsole.WriteLine(); } break; case @"\createSelect": if (parts.Length == 2) { bool result; bool.TryParse(parts[1], out result); selectedTable.CreateSelectFactory = result; } else { WinConsole.WriteLine("Unvalid Input expected was true | false "); WinConsole.WriteLine(); } break; case @"\stats": WinConsole.WriteLine("Name = {0}", selectedTable.Info.TableName); WinConsole.WriteLine("Cs class Name = {0}", selectedTable.GetClassName()); WinConsole.WriteLine("Exclude = {0}", selectedTable.Exclude); WinConsole.WriteLine("Create Fallback Property = {0}", selectedTable.CreateFallbackProperty); WinConsole.WriteLine("\t Create Select Factory = {0}", selectedTable.CreateSelectFactory); WinConsole.WriteLine("\t Create Dataloader = {0}", selectedTable.CreateDataRecordLoader); WinConsole.WriteLine("Columns"); foreach (var columnInfo in selectedTable.ColumnInfos) { WinConsole.WriteLine("--------------------------------------------------------"); WinConsole.WriteLine("\t Name = {0}", columnInfo.ColumnInfo.ColumnName); WinConsole.WriteLine("\t Is Primary Key = {0}", columnInfo.PrimaryKey); WinConsole.WriteLine("\t Cs Property Name = {0}", columnInfo.GetPropertyName()); WinConsole.WriteLine("\t Position From Top = {0}", columnInfo.ColumnInfo.PositionFromTop); WinConsole.WriteLine("\t Nullable = {0}", columnInfo.ColumnInfo.Nullable); WinConsole.WriteLine("\t Cs Type = {0}", columnInfo.ColumnInfo.TargetType.Name); WinConsole.WriteLine("\t forgeinKey Type = {0}", columnInfo.ForgeinKeyDeclarations); WinConsole.WriteLine("\t Is Enum Type = {0}", columnInfo.EnumDeclaration != null); } break; case @"\back": RenderMenu(); return; default: break; } } RenderTableMenu(selectedTable); }
public async Task CreateEntrysAsync(string connection, string outputPath, string database) { Status = "Try to connect"; //Data Source=(LocalDb)\ProjectsV12;Integrated Security=True;Database=TestDB; IsEnumeratingDatabase = true; TargetDir = outputPath; var checkDatabase = false; if (connection.StartsWith("file:\\\\")) { MsSqlStructure = new DacpacMsSqlStructure(connection.Replace("file:\\\\", "")); } else { var dbAccessLayer = new DbAccessLayer(DbAccessType.MsSql, connection); MsSqlStructure = new DatabaseMsSqlStructure(dbAccessLayer); try { checkDatabase = dbAccessLayer.CheckDatabase(); } catch (Exception) { IsEnumeratingDatabase = false; Connected = false; checkDatabase = false; } var databaseName = string.IsNullOrEmpty(MsSqlStructure.GetDatabaseName()) ? database : MsSqlStructure.GetDatabaseName(); if (string.IsNullOrEmpty(databaseName)) { IsEnumeratingDatabase = false; Status = "Database not exists. Maybe wrong Connection or no Selected Database?"; Connected = false; return; } } DbConfig.EnableGlobalThreadSafety = true; if (!Connected) { IsEnumeratingDatabase = false; Status = "Database not accessible. Maybe wrong Connection or no Selected Database?"; return; } Status = "Connection OK ... Reading Server Version ..."; //SqlVersion = Manager.RunPrimetivSelect<string>("SELECT SERVERPROPERTY('productversion')").FirstOrDefault(); Status = "Reading Tables"; var counter = 2; var createTables = SimpleWork(() => { var tables = MsSqlStructure.GetTables() .Select( s => new TableInfoModel(s, MsSqlStructure.GetDatabaseName(), MsSqlStructure)) .Select(s => new TableInfoViewModel(s, this)) .ToList(); foreach (var source in tables) { if (Tables.All(f => f.Info.TableName != source.Info.TableName)) { Tables.Add(source); } } }); var createViews = SimpleWork(() => { var views = MsSqlStructure.GetViews() .Select(s => new TableInfoModel(s, MsSqlStructure.GetDatabaseName(), MsSqlStructure)) .Select(s => new TableInfoViewModel(s, this)) .ToList(); foreach (var source in views) { if (Views.All(f => f.Info.TableName != source.Info.TableName)) { Views.Add(source); } } }); await createTables; await createViews; SelectedTable = Tables.FirstOrDefault(); IsEnumeratingDatabase = false; Status = "Done"; }
public void CreateEntrys(string connection, string outputPath, string database) { TargetDir = outputPath; bool checkDatabase = false; if (connection.StartsWith("file:\\\\")) { MsSqlStructure = new DacpacMsSqlStructure(connection.Replace("file:\\\\", "")); checkDatabase = true; } else { var dbAccessLayer = new DbAccessLayer(DbAccessType.MsSql, connection); MsSqlStructure = new DatabaseMsSqlStructure(dbAccessLayer); try { checkDatabase = dbAccessLayer.CheckDatabase(); } catch (Exception) { checkDatabase = false; } var databaseName = string.IsNullOrEmpty(dbAccessLayer.Database.DatabaseName) ? database : dbAccessLayer.Database.DatabaseName; if (string.IsNullOrEmpty(databaseName)) { throw new Exception("Database not exists. Maybe wrong Connection or no Selected Database?"); } } if (!checkDatabase) { throw new Exception("Database not accessible. Maybe wrong Connection or no Selected Database?"); } WinConsole.WriteLine("Connection OK ... Reading Server Version ..."); SqlVersion = MsSqlStructure.GetVersion().ToString(); WinConsole.WriteLine("Server version is {0}", SqlVersion); WinConsole.WriteLine("Reading Tables from {0} ...", MsSqlStructure.GetDatabaseName()); Tables = MsSqlStructure.GetTables() //.AsParallel() .Select(s => new TableInfoModel(s, MsSqlStructure.GetDatabaseName(), MsSqlStructure)) .ToList(); Views = MsSqlStructure.GetViews() //.AsParallel() .Select(s => new TableInfoModel(s, MsSqlStructure.GetDatabaseName(), MsSqlStructure)) .ToList(); StoredProcs = MsSqlStructure.GetStoredProcedures() .Select(s => new StoredPrcInfoModel(s)) .ToList(); WinConsole.WriteLine( "Found {0} Tables, {1} Views, {2} Procedures ... select a Table to see Options or start an Action", Tables.Count(), Views.Count(), StoredProcs.Count()); Enums = new List <Dictionary <int, string> >(); RenderMenu(); }