Esempio n. 1
0
 public TableMergeItem(ITableInfoModel left, ITableInfoModel right)
 {
     Left             = left;
     Right            = right;
     TableMerges      = new ThreadSaveObservableCollection <PropertyMergeItem>();
     ColumnMergeItems = new ThreadSaveObservableCollection <ColumnMergeItem>();
 }
Esempio n. 2
0
		public void Compare(ITableInfoModel left, ITableInfoModel right)
		{
			Result = new TableMergeItem(left, right);
			foreach (var source in TableInfoModel.Propertys.Where(f => !typeof(IEnumerable).IsAssignableFrom(f.Value.PropertyType)))
			{
				var leftValue = source.Value.Getter.Invoke(left);
				var rightValue = source.Value.Getter.Invoke(right);

				if (leftValue != rightValue)
				{
					Result.TableMerges.Add(new PropertyMergeItem(source.Key, leftValue, rightValue, MergeStatus.NotSame));
				}
			}

			foreach (var leftColumn in left.ColumnInfos)
			{
				var rightColumn = right.ColumnInfos
					.FirstOrDefault(s => s.ColumnInfo.ColumnName == leftColumn.ColumnInfo.ColumnName);

				if (rightColumn == null)
				{
					Result.ColumnMergeItems.Add(new ColumnMergeItem(leftColumn, null, MergeStatus.RightMissing));
				}
				else
				{
					var columComparer = new ColumnComparer();
					columComparer.Compare(leftColumn, rightColumn);
					Result.ColumnMergeItems.Add(columComparer.Result);
				}
			}
		}
Esempio n. 3
0
		public TableMergeItem(ITableInfoModel left, ITableInfoModel right)
		{
			Left = left;
			Right = right;
			TableMerges = new ThreadSaveObservableCollection<PropertyMergeItem>();
			ColumnMergeItems = new ThreadSaveObservableCollection<ColumnMergeItem>();
		}
Esempio n. 4
0
        public void Compare(ITableInfoModel left, ITableInfoModel right)
        {
            Result = new TableMergeItem(left, right);
            foreach (var source in TableInfoModel.Propertys.Where(f => !typeof(IEnumerable).IsAssignableFrom(f.Value.PropertyType)))
            {
                var leftValue  = source.Value.Getter.Invoke(left);
                var rightValue = source.Value.Getter.Invoke(right);

                if (leftValue != rightValue)
                {
                    Result.TableMerges.Add(new PropertyMergeItem(source.Key, leftValue, rightValue, MergeStatus.NotSame));
                }
            }

            foreach (var leftColumn in left.ColumnInfos)
            {
                var rightColumn = right.ColumnInfos
                                  .FirstOrDefault(s => s.ColumnInfo.ColumnName == leftColumn.ColumnInfo.ColumnName);

                if (rightColumn == null)
                {
                    Result.ColumnMergeItems.Add(new ColumnMergeItem(leftColumn, null, MergeStatus.RightMissing));
                }
                else
                {
                    var columComparer = new ColumnComparer();
                    columComparer.Compare(leftColumn, rightColumn);
                    Result.ColumnMergeItems.Add(columComparer.Result);
                }
            }
        }
Esempio n. 5
0
 public TableInfoViewModel(ITableInfoModel sourceElement, SqlEntityCreatorViewModel compilerOptions)
 {
     _compilerOptions     = compilerOptions;
     SourceElement        = sourceElement;
     CreatePreviewCommand = new DelegateCommand(CreatePreviewExecute, CanCreatePreviewExecute);
     AddColumnCommand     = new DelegateCommand(AddColumnExecute, CanAddColumnExecute);
     RemoveColumnCommand  = new DelegateCommand(RemoveColumnExecute, CanRemoveColumnExecute);
     ColumnInfoModels     = new ThreadSaveObservableCollection <ColumnInfoViewModel>();
     Refresh();
 }
Esempio n. 6
0
		private void RenderTableMenu(ITableInfoModel selectedTable)
		{
			Console.WriteLine("Actions:");

			Console.WriteLine(@"\ForModel   [\c] [ColumnName] [[NewName] | [\d]]  ");
			Console.WriteLine(@"        Adds a ForModelAttribute to a Property or class with \c for class. deletes it with \d");
			Console.WriteLine("Example:");
			Console.WriteLine(@"        \ForModelAttribute \c NewTableName");
			Console.WriteLine("             Adding a ForModelAttribute Attribute to the generated class");
			Console.WriteLine(@"        \ForModelAttribute ID_Column NewName");
			Console.WriteLine("             Adding a ForModelAttribute Attribute to the generated Property with the value NewName");
			Console.WriteLine();
			Console.WriteLine(@"\Exclude			[true | false] [ColumnName]");
			Console.WriteLine("         Exclude this table from the Process");
			Console.WriteLine(@"\InsertIgnore		true | false] [ColumnName]");
			Console.WriteLine("         Exclude this column from inserts");
			Console.WriteLine(@"\Enum				[true | false] [ColumnName]");
			Console.WriteLine("         Marks this Column as an ENUM field on the Database. Experimental");
			Console.WriteLine(@"\Fallack			[true | false]]");
			Console.WriteLine("         Should create a LoadNotImplimentedDynamic Property for Not Loaded fieds");
			Console.WriteLine(@"\Createloader		[true | false]]");
			Console.WriteLine("         Should create a Dataloader that loads the Propertys from the IDataRecord");
			Console.WriteLine(@"\CreateSelect		[true | false]]");
			Console.WriteLine("         Should create a Attribute with a Select Statement");
			Console.WriteLine(@"\stats");
			Console.WriteLine("         Shows all avalible data from this table");
			Console.WriteLine(@"\back");
			Console.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];
								}
								Console.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;
										Console.WriteLine("Renamed from {0} to {1}", oldName, deleteOrNewName);
									}
									else
									{
										Console.WriteLine("There is no Column that is named like {0}", deleteOrNewName);
									}
								}
								else
								{
									if (columnToRename != null)
									{
										columnToRename.NewColumnName = string.Empty;
										Console.WriteLine("Removed the Renaming from {0} to {1}", deleteOrNewName, parts[1]);
									}
									else
									{
										Console.WriteLine("There is no Column that is named like {0}", deleteOrNewName);
									}
								}
							}
						}
						else
						{

							Console.WriteLine("Unvalid Input expected was [ColumnName] [NewName] ");
							Console.WriteLine();
						}
						break;
					case @"\insertignore":
						if (parts.Length == 3)
						{
							bool result;
							Boolean.TryParse(parts[1], out result);

							var column = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == parts[2]);
							if (column == null)
							{

								Console.WriteLine("Unvalid Input expected was  [ColumnName] ");
								Console.WriteLine();
								break;
							}

							column.InsertIgnore = result;
						}
						else
						{

							Console.WriteLine("Unvalid Input expected was  true | false ");
							Console.WriteLine();
						}
						break;
					case @"\enum":
						if (parts.Length == 3)
						{
							bool result;
							Boolean.TryParse(parts[1], out result);

							var column = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == parts[2]);
							if (column == null)
							{

								Console.WriteLine("Unvalid Input expected was  [ColumnName] ");
								Console.WriteLine();
								break;
							}

							if (result == false)
							{
								column.EnumDeclaration = null;
								break;
							}

							if (column.ForgeinKeyDeclarations == null)
							{
								Console.WriteLine("Declare the Enum:");
								Console.WriteLine("Format: [Number] [Description]");
								Console.WriteLine("Example: '1 Valid'");
								Console.WriteLine("Type ok to submit");
								Console.WriteLine("Type cancel to revert");

								column.EnumDeclaration = new EnumDeclarationModel();

								Console.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]);
											Console.WriteLine("Added Enum member {0} = {1}", option[1], enumNumberResult);
										}
										else
										{
											Console.WriteLine("Invalid Enum number Supplyed");
										}
									}
									else
									{
										Console.WriteLine("Invalid Enum member Supplyed");
									}
								}
							}
							else
							{
								Console.WriteLine("Enum is ForgeinKey.");
								Console.WriteLine("Read data from Database to autogenerate Enum");
								Console.WriteLine("Reading table: '{0}'", column.ForgeinKeyDeclarations.TableName);

								var tableContent = Manager.Select<DynamicTableContentModel>(new object[] { column.ForgeinKeyDeclarations.TableName });

								if (!tableContent.Any())
								{
									Console.WriteLine("The Enum table '{0}' does not contain any data", column.ForgeinKeyDeclarations.TableName);
									break;
								}

								if (tableContent.First().DataHolder.Count > 2)
								{
									Console.WriteLine("The Enum table '{0}' contains more then 2 columns", column.ForgeinKeyDeclarations.TableName);
									break;
								}

								if (!tableContent.Any(s => s.DataHolder.Any(f => f.Value is int)))
								{
									Console.WriteLine("The Enum table '{0}' does not contains exactly one column of type int", column.ForgeinKeyDeclarations.TableName);
									break;
								}

								if (!tableContent.Any(s => s.DataHolder.Any(f => f.Value is string)))
								{
									Console.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.DataHolder.FirstOrDefault(s => s.Value is int).Value;
									var value = (string)item.DataHolder.FirstOrDefault(s => s.Value is string).Value;
									column.EnumDeclaration.Values.Add(pk, value);
									Console.WriteLine("Adding Enum member '{0}' = '{1}'", value, pk);
								}
							}
						}
						else
						{

							Console.WriteLine("Unvalid Input expected was  true | false ");
							Console.WriteLine();
						}
						break;
					case @"\exclude":
						if (parts.Length == 2)
						{
							bool result;
							Boolean.TryParse(parts[1], out result);
							selectedTable.Exclude = result;
						}
						else
						{
							if (parts.Length == 3)
							{
								bool result;
								Boolean.TryParse(parts[1], out result);

								var column = selectedTable.ColumnInfos.FirstOrDefault(s => s.ColumnInfo.ColumnName == parts[2]);
								if (column == null)
								{

									Console.WriteLine("Unvalid Input expected was  [ColumnName] ");
									Console.WriteLine();
									break;
								}

								column.Exclude = result;
							}
							else
							{

								Console.WriteLine("Unvalid Input expected was  true | false ");
								Console.WriteLine();
							}
						}
						break;
					case @"\fallack":
						if (parts.Length == 2)
						{
							bool result;
							Boolean.TryParse(parts[1], out result);
							selectedTable.CreateFallbackProperty = result;
						}
						else
						{

							Console.WriteLine("Unvalid Input expected was  true | false ");
							Console.WriteLine();
						}
						break;

					case @"\createloader":
						if (parts.Length == 2)
						{
							bool result;
							Boolean.TryParse(parts[1], out result);
							selectedTable.CreateDataRecordLoader = result;
						}
						else
						{

							Console.WriteLine("Unvalid Input expected was  true | false ");
							Console.WriteLine();
						}
						break;
					case @"\createSelect":
						if (parts.Length == 2)
						{
							bool result;
							Boolean.TryParse(parts[1], out result);
							selectedTable.CreateSelectFactory = result;
						}
						else
						{

							Console.WriteLine("Unvalid Input expected was  true | false ");
							Console.WriteLine();
						}
						break;

					case @"\stats":
						Console.WriteLine("Name =                       {0}", selectedTable.Info.TableName);
						Console.WriteLine("Cs class Name =              {0}", selectedTable.GetClassName());
						Console.WriteLine("Exclude =                    {0}", selectedTable.Exclude);
						Console.WriteLine("Create Fallback Property =   {0}", selectedTable.CreateFallbackProperty);
						Console.WriteLine("\t Create Select Factory =   {0}", selectedTable.CreateSelectFactory);
						Console.WriteLine("\t Create Dataloader =       {0}", selectedTable.CreateDataRecordLoader);
						Console.WriteLine("Columns");
						foreach (var columnInfo in selectedTable.ColumnInfos)
						{
							Console.WriteLine("--------------------------------------------------------");
							Console.WriteLine("\t Name =                    {0}", columnInfo.ColumnInfo.ColumnName);
							Console.WriteLine("\t Is Primary Key =          {0}", columnInfo.PrimaryKey);
							Console.WriteLine("\t Cs Property Name =        {0}", columnInfo.GetPropertyName());
							Console.WriteLine("\t Position From Top =       {0}", columnInfo.ColumnInfo.PositionFromTop);
							Console.WriteLine("\t Nullable =                {0}", columnInfo.ColumnInfo.Nullable);
							Console.WriteLine("\t Cs Type =                 {0}", columnInfo.ColumnInfo.TargetType.Name);
							Console.WriteLine("\t forgeinKey Type =         {0}", columnInfo.ForgeinKeyDeclarations);
							Console.WriteLine("\t Is Enum Type =		    {0}", columnInfo.EnumDeclaration != null);
						}
						break;

					case @"\back":
						RenderMenu();
						return;
					default:
						break;
				}

			RenderTableMenu(selectedTable);
		}
Esempio n. 7
0
        private static void CompileTableLike(ITableInfoModel tableInfoModel, IMsSqlCreator sourceCreator, string typeName, Stream to = null)
        {
            if (tableInfoModel.Exclude)
            {
                return;
            }

            var targetCsName = tableInfoModel.GetClassName();

            var compiler = new ClassCompiler(sourceCreator.TargetDir, targetCsName);

            compiler.Type          = typeName;
            compiler.CompileHeader = sourceCreator.GenerateCompilerHeader;
            compiler.Namespace     = sourceCreator.Namespace;
            compiler.TableName     = tableInfoModel.Info.TableName;
            if (to != null)
            {
                compiler.WriteAllways = true;
            }
            compiler.GenerateConfigMethod = sourceCreator.GenerateConfigMethod;

            if (tableInfoModel.WrapNullables || sourceCreator.WrapNullables)
            {
                compiler.AddAttribute(new CodeAttributeDeclaration(typeof(WrapDbNullablesAttribute).Name));
            }

            if (tableInfoModel.CreateSelectFactory || sourceCreator.GenerateConstructor)
            {
                compiler.GenerateTypeConstructorBasedOnElements(tableInfoModel.ColumnInfos.Where(s => !s.Exclude));
            }

            foreach (var columInfoModel in tableInfoModel.ColumnInfos)
            {
                if (columInfoModel.Exclude)
                {
                    continue;
                }

                var codeMemberProperty = compiler.AddProperty(columInfoModel);

                if (columInfoModel.PrimaryKey)
                {
                    codeMemberProperty.CustomAttributes.Add(
                        new CodeAttributeDeclaration(typeof(PrimaryKeyAttribute).Name));
                }
                if (columInfoModel.InsertIgnore)
                {
                    codeMemberProperty.CustomAttributes.Add(
                        new CodeAttributeDeclaration(typeof(InsertIgnoreAttribute).Name));
                }
                if (columInfoModel.ForgeinKeyDeclarations != null)
                {
                    var isRefTypeKnown =
                        sourceCreator.Tables.FirstOrDefault(s => s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName);

                    if (isRefTypeKnown == null)
                    {
                        codeMemberProperty.CustomAttributes.Add(
                            new CodeAttributeDeclaration(typeof(ForeignKeyDeclarationAttribute).Name,
                                                         new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TargetColumn)),
                                                         new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TableName))));
                    }
                    else
                    {
                        codeMemberProperty.CustomAttributes.Add(
                            new CodeAttributeDeclaration(typeof(ForeignKeyDeclarationAttribute).Name,
                                                         new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TargetColumn)),
                                                         new CodeAttributeArgument(new CodeTypeOfExpression(isRefTypeKnown.GetClassName()))));
                    }
                }

                if (columInfoModel.ColumnInfo.SqlType == SqlDbType.Timestamp)
                {
                    codeMemberProperty.CustomAttributes.Add(
                        new CodeAttributeDeclaration(typeof(RowVersionAttribute).Name));
                }
            }

            if (tableInfoModel.CreateFallbackProperty)
            {
                compiler.AddFallbackProperty();
            }

            Logger.WriteLine("Compile Class {0}", compiler.Name);
            compiler.Compile(tableInfoModel.ColumnInfos, sourceCreator.SplitByType, to);
        }
Esempio n. 8
0
 public static void CompileTable(ITableInfoModel tableInfoModel, IMsSqlCreator sourceCreator, Stream to = null)
 {
     CompileTableLike(tableInfoModel, sourceCreator, "Table", to);
 }
Esempio n. 9
0
        private static void CompileTableLike(ITableInfoModel tableInfoModel,
                                             IMsSqlCreator sourceCreator,
                                             string typeName,
                                             Stream to = null)
        {
            if (tableInfoModel.Exclude)
            {
                return;
            }

            var targetCsName = tableInfoModel.GetClassName();

            var compiler = new ClassCompiler(sourceCreator.TargetDir, targetCsName);

            compiler.Type          = typeName;
            compiler.CompileHeader = sourceCreator.GenerateCompilerHeader;
            compiler.Namespace     = sourceCreator.Namespace;
            compiler.TableName     = tableInfoModel.Info.TableName;
            if (to != null)
            {
                compiler.WriteAlways = true;
            }

            compiler.GenerateConfigMethod = sourceCreator.GenerateConfigMethod;

            foreach (var columInfoModel in tableInfoModel.ColumnInfos)
            {
                if (columInfoModel.Exclude)
                {
                    continue;
                }

                var codeMemberProperty = compiler.AddProperty(columInfoModel);

                if (columInfoModel.PrimaryKey)
                {
                    codeMemberProperty.Attributes.Add(new AttributeInfo()
                    {
                        Name = nameof(PrimaryKeyAttribute)
                    });
                }

                if (columInfoModel.InsertIgnore)
                {
                    codeMemberProperty.Attributes.Add(new AttributeInfo()
                    {
                        Name = nameof(InsertIgnoreAttribute)
                    });
                }

                if (columInfoModel.ForgeinKeyDeclarations != null)
                {
                    var isRefTypeKnown =
                        sourceCreator.Tables
                        .FirstOrDefault(s =>
                                        s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName);

                    if (isRefTypeKnown == null)
                    {
                        codeMemberProperty.Attributes.Add(new AttributeInfo()
                        {
                            Name = nameof(ForeignKeyDeclarationAttribute),
                            ConstructorSetters =
                            {
                                { "foreignKey",   "\"" + columInfoModel.ForgeinKeyDeclarations.TargetColumn + "\"" },
                                { "foreignTable", "\"" + columInfoModel.ForgeinKeyDeclarations.TableName + "\""    },
                            }
                        });
                    }
                    else
                    {
                        codeMemberProperty.Attributes.Add(new AttributeInfo()
                        {
                            Name = nameof(ForeignKeyDeclarationAttribute),
                            ConstructorSetters =
                            {
                                { "foreignKey",   $"\"{columInfoModel.ForgeinKeyDeclarations.TargetColumn}\"" },
                                { "foreignTable", $"typeof({isRefTypeKnown.GetClassName()})"                  },
                            }
                        });
                    }
                }

                if (columInfoModel.ColumnInfo.SqlType == SqlDbType.Timestamp)
                {
                    codeMemberProperty.Attributes.Add(new AttributeInfo()
                    {
                        Name = nameof(RowVersionAttribute)
                    });
                }

                if (tableInfoModel.CreateFallbackProperty)
                {
                    compiler.AddFallbackProperty();
                }
            }

            foreach (var columInfoModel in tableInfoModel.ColumnInfos)
            {
                if (columInfoModel.Exclude)
                {
                    continue;
                }

                if (columInfoModel.ForgeinKeyDeclarations != null)
                {
                    var isRefTypeKnown =
                        sourceCreator.Tables
                        .FirstOrDefault(s =>
                                        s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName);

                    if (isRefTypeKnown != null)
                    {
                        compiler.Generator.NamespaceImports.Add(typeof(EagarDataRecord).Namespace);
                        compiler.Generator.NamespaceImports.Add(typeof(DbAccessLayerHelper).Namespace);
                        var navPropertyName = isRefTypeKnown.GetClassName();
                        if (!navPropertyName.EndsWith("s"))
                        {
                            navPropertyName = navPropertyName.TrimEnd('s');
                        }

                        var tempName = navPropertyName;
                        var counter  = 1;
                        while (compiler.Generator.Properties.Any(f => f.Name == tempName ||
                                                                 tempName == tableInfoModel.GetClassName()))
                        {
                            tempName = navPropertyName + counter++;
                        }

                        navPropertyName = tempName;
                        var refProp = compiler.AddProperty(navPropertyName, null, new ClassType()
                        {
                            Name = isRefTypeKnown.GetClassName(),
                        });
                        refProp.ForeignKey = new PropertyInfo.ForeignKeyDeclaration(columInfoModel.ForgeinKeyDeclarations.SourceColumn,
                                                                                    columInfoModel.ForgeinKeyDeclarations.TargetColumn);
                        refProp.Attributes.Add(new AttributeInfo()
                        {
                            Name = nameof(ForeignKeyAttribute),
                            ConstructorSetters =
                            {
                                { "foreignKey",   columInfoModel.ForgeinKeyDeclarations.SourceColumn.AsStringOfString() },
                                { "referenceKey", columInfoModel.ForgeinKeyDeclarations.TargetColumn.AsStringOfString() },
                            }
                        });
                    }
                }
            }

            foreach (var infoModel in sourceCreator.Tables)
            {
                foreach (var columInfoModel in infoModel
                         .ColumnInfos
                         .Where(f => f.ForgeinKeyDeclarations?.TableName == tableInfoModel.Info.TableName))
                {
                    compiler.Generator.NamespaceImports.Add(typeof(EagarDataRecord).Namespace);
                    compiler.Generator.NamespaceImports.Add(typeof(DbAccessLayerHelper).Namespace);
                    var navPropertyName = infoModel.GetClassName();
                    if (!navPropertyName.EndsWith("s"))
                    {
                        navPropertyName = navPropertyName.TrimEnd('s');
                    }

                    var tempName = navPropertyName;
                    var counter  = 1;
                    while (compiler.Generator.Properties.Any(f => f.Name == tempName ||
                                                             tempName == tableInfoModel.GetClassName()))
                    {
                        tempName = navPropertyName + counter++;
                    }

                    navPropertyName = tempName;

                    var refProp = compiler.AddProperty(navPropertyName, null, new ClassType()
                    {
                        Name         = $"{nameof(DbCollection<object>)}",
                        IsList       = true,
                        GenericTypes =
                        {
                            new ClassType()
                            {
                                Name = infoModel.GetClassName()
                            }
                        }
                    });
                    refProp.ForeignKey = new PropertyInfo.ForeignKeyDeclaration(columInfoModel.ForgeinKeyDeclarations.SourceColumn,
                                                                                columInfoModel.ForgeinKeyDeclarations.TargetColumn);
                    refProp.ForeignKey.DirectionFromParent = true;
                    refProp.Attributes.Add(new AttributeInfo()
                    {
                        Name = nameof(ForeignKeyAttribute),
                        ConstructorSetters =
                        {
                            { "referenceKey", columInfoModel.ForgeinKeyDeclarations.SourceColumn.AsStringOfString() },
                            { "foreignKey",   columInfoModel.ForgeinKeyDeclarations.TargetColumn.AsStringOfString() },
                        }
                    });
                }
            }

            compiler.GenerateConfigMethod          = true;
            compiler.Generator.GenerateFactory     = tableInfoModel.CreateSelectFactory || sourceCreator.GenerateFactory;
            compiler.Generator.GenerateConstructor = sourceCreator.GenerateConstructor;

            Logger.WriteLine("Compile Class {0}", compiler.Name);
            compiler.Compile(tableInfoModel.ColumnInfos, sourceCreator.SplitByType, sourceCreator.SetNotifyProperties, to);
        }
Esempio n. 10
0
		public static void CompileTable(ITableInfoModel tableInfoModel, IMsSqlCreator sourceCreator, Stream to = null)
		{
			if (tableInfoModel.Exclude)
				return;

			var targetCsName = tableInfoModel.GetClassName();

			var compiler = new ClassCompiler(sourceCreator.TargetDir, targetCsName);
			compiler.CompileHeader = sourceCreator.GenerateCompilerHeader;
			compiler.Namespace = sourceCreator.Namespace;
			compiler.TableName = tableInfoModel.Info.TableName;
			if (to != null)
			{
				compiler.WriteAllways = true;
			}
			compiler.GenerateConfigMethod = sourceCreator.GenerateConfigMethod;

			if (tableInfoModel.CreateSelectFactory || sourceCreator.GenerateConstructor)
			{
				compiler.GenerateTypeConstructorBasedOnElements(tableInfoModel.ColumnInfos.Where(s => !s.Exclude));
			}

			foreach (var columInfoModel in tableInfoModel.ColumnInfos)
			{
				if (columInfoModel.Exclude)
					continue;

				var codeMemberProperty = compiler.AddProperty(columInfoModel);
				if (columInfoModel.PrimaryKey)
				{
					codeMemberProperty.CustomAttributes.Add(
						new CodeAttributeDeclaration(typeof(PrimaryKeyAttribute).Name));
				}
				if (columInfoModel.InsertIgnore)
				{
					codeMemberProperty.CustomAttributes.Add(
						new CodeAttributeDeclaration(typeof(InsertIgnoreAttribute).Name));
				}
				if (columInfoModel.ForgeinKeyDeclarations != null)
				{
					var isRefTypeKnown =
						sourceCreator.Tables.FirstOrDefault(s => s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName);

					if (isRefTypeKnown == null)
					{
						codeMemberProperty.CustomAttributes.Add(
							new CodeAttributeDeclaration(typeof(ForeignKeyDeclarationAttribute).Name,
								new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TargetColumn)),
								new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TableName))));
					}
					else
					{
						codeMemberProperty.CustomAttributes.Add(
							new CodeAttributeDeclaration(typeof(ForeignKeyDeclarationAttribute).Name,
								new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TargetColumn)),
								new CodeAttributeArgument(new CodeTypeOfExpression(isRefTypeKnown.GetClassName()))));
					}
				}

				if (columInfoModel.ColumnInfo.TargetType2 == "Timestamp")
				{
					codeMemberProperty.CustomAttributes.Add(
						new CodeAttributeDeclaration(typeof(RowVersionAttribute).Name));
				}
			}

			if (tableInfoModel.CreateFallbackProperty)
			{
				compiler.AddFallbackProperty();
			}

			Logger.WriteLine("Compile Class {0}", compiler.Name);
			compiler.Compile(tableInfoModel.ColumnInfos, to);
		}
Esempio n. 11
0
        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 =
                                Manager.Select <Any>(new object[] { 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);
        }