Example #1
0
		internal void Generate()
		{
			IParserLogic2 parserLogic2 = new ParserLogic2();

			_classDataList = new List<ClassData>();
			_database.Tables
				.Where(t => t.Include)
				.ToList()
				.ForEach(table =>
		   {
			   //	Set data for the very [class].
			   var classData = new ClassData
			   {
				   Name = table.Name,
				   IsPartial = _pocoSettings.MakePartial,
				   Properties = new List<PropertyData>(),
                   Methods = new List<MethodData>()
			   };

			   //	#	Create the properties.
			   table.Columns
			   .ForEach(column =>
			   {
				   classData.Properties.Add(
					   new PropertyData
					   {
						   Name = column.Name,
						   Scope = Common.VisibilityScope.Public,
						   SystemType = parserLogic2.ConvertDatabaseTypeToDotnetType(column.DatabaseTypeName),
						   Comment = column.IsInPrimaryKey ?
								new CommentData("This property is part of the primary key.")
								: null
					   });
			   });

			   //	#	Create the constructors.
			   if (_pocoSettings.CreateDefaultConstructor)
			   {
				   classData.Methods.Add(new MethodData
				   {
					   IsConstructor = true,
					   Name = classData.Name,
					   Comment = new CommentData("Default constructor needed for instance for de/serialising.")
				   });
			   }

			   if (_pocoSettings.CreateAllPropertiesConstructor)
			   {
				   classData.Methods.Add( new MethodData
				   {
					   IsConstructor = true,
					   Name = classData.Name,
					   Comment = new CommentData("This constructor takes all properties as parameters."),
					   Parameters = table.Columns.Select(p =>
					   new ParameterData
					   {
						   Name = p.Name,
						   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString( p.DatabaseTypeName)
					   }).ToList()
				   });
               }

			   if (_pocoSettings.CreateAllPropertiesSansPrimaryKeyConstructor)
			   {
				   classData.Methods.Add(new MethodData
				   {
					   IsConstructor = true,
					   Name = classData.Name,
					   Comment = new CommentData("This constructor takes all properties but primary keys as parameters."),
					   Parameters = table.Columns
						.Where(p=>false == p.IsInPrimaryKey)
						.Select(p =>
						   new ParameterData
						   {
							   Name = p.Name,
							   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString( p.DatabaseTypeName)
						   }).ToList()
				   });
			   }

			   if(_pocoSettings.CreateCopyConstructor)
			   {
				   classData.Methods.Add(new MethodData
				   {
					   IsConstructor = true,
					   Name = classData.Name,
					   Comment = new CommentData("This is the copy constructor."),
					   Parameters = new List<ParameterData>
					   {
						   new ParameterData
						   {
							   Name = classData.Name,
							   SystemTypeString = classData.Name
						   }
					   },
					   Body = new BodyData
					   {
						   Lines = table.Columns
							.Select(p => string.Format("this.{0} = {1}.{2};",
							p.Name, Common.Safe(Common.ToCamelCase(table.Name)), p.Name))
							.ToList()
					   }
				   });
			   }

			   //	#	Create the methods.
			   if (_pocoSettings.CreateMethodEquals && Regex.IsMatch(classData.Name, _pocoSettings.CreateMethodEqualsRegex))
			   {
				   classData.Methods.Add(
					   CreateEqualsMethod(table, classData, "o"));

				   classData.Methods.Add(
					   CreateHashMethod(table, classData));
			   }

			   _classDataList.Add(classData);
		   });

			_log.Add("Included classes are:");
			_log.Add(_classDataList.ToInfo());
		}
Example #2
0
		internal void Generate()
		{
			_surfaceDataList = new List<ClassData>();

			var xmlRdbSchemaPathfile = Path.Combine(
				//@"C:\DATA\PROJEKT\St4mpede\St4mpede\St4mpede",
				_coreSettings.RootFolder,
				//	\RdbSchema",
				_rdbSchemaSettings.ProjectPath,
				//St4mpede.RdbSchema.xml";
				_rdbSchemaSettings.DatabaseXmlFile);

			IParserLogic2 parserLogic2 = new ParserLogic2();

			_log.Add("Reading xml {0}.", xmlRdbSchemaPathfile);

			_database = parserLogic2.GetResult(xmlRdbSchemaPathfile);

			_database.Tables.ForEach(table =>
			{
			//	Set data for the very [class].
			var surfaceClass = new ClassData
			{
				Name = $"{table.Name}Surface",
				Comment = new CommentData($"This is the Surface for the {table.Name} table."),
				IsPartial = false,
				Properties = new List<PropertyData>(),
				Methods = new List<MethodData>()
			};
			_surfaceDataList.Add(surfaceClass);

			//	#	Create the properties.
			//	Well... we don't have any properties.

			//	#	Create the constructor.
			var constructorMethod = new MethodData
			{
				IsConstructor = true,
				Name = surfaceClass.Name
			};
			surfaceClass.Methods.Add(constructorMethod);

				//	#	Create the methods.
				//	##	Create the Add method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "Add",
					Comment = new CommentData( new[] {
						"This method is used for adding a new record in the database.",
						"<para>St4mpede guid:{BC89740A-CBA5-4EDE-BFF4-9B0D8BA4058F}</para>"}),
					Scope = Common.VisibilityScope.Private,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters = new[] {
							new ParameterData
							{
								Name = "context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							}
						}
						.Concat(
								table.NonPrimaryKeyColumns
									.Select(p =>
										new ParameterData
									   {
										   Name = p.Name,
										   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString(p.DatabaseTypeName)
									   }))
						.ToList(),
					Body = new BodyData(new[]
					{
						$"return Add( ",
						"\tcontext, ",
						$"	new TheDAL.Poco.{table.Name}(", //	TODO:OF:Fetch namespace from Poco project.
						//	TODO:OF:We should set default value and not 0 to the primary keys.
						"\t\t" + string.Join(", ", table.PrimaryKeyColumns.Select(c=>"0")) + ", ",
						"\t\t" + string.Join(", ", table.NonPrimaryKeyColumns.Select(c=>c.Name.ToCamelCase()).ToList()),
						"));"
					})
				});

		//	##	Create the Add method.
		surfaceClass.Methods.Add(new MethodData
				{
					Name = "Add",
					Comment = new CommentData(new[] {
						"This method is used for adding a new record in the database.",
						"<para>St4mpede guid:{759FBA46-A462-4E4A-BA2B-9B5AFDA572DE}</para>"}),
					Scope = Common.VisibilityScope.Private,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters =
							new[] { new ParameterData
							{
								Name="context", 
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }
						.Concat(
							new[] {	new ParameterData
							   {
								   Name = table.Name,	//	TODO:OF:Make camelcase.
								   SystemTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
								}
							})
						.ToList(),
					Body = new BodyData(new[]
					{
						$"const string Sql = @" + "\"",
						$"	Insert Into {table.Name}",
						$"	(",
						$"		{string.Join(", ",table.Columns.Where(c=>false==c.IsInPrimaryKey).Select(c=>c.Name))}",
						$"	)",
						$"	Values",
						$"	(",
						//	TODO:OF:Make lowercase.
						$"		{string.Join(", ",table.Columns.Where(c=>false==c.IsInPrimaryKey).Select(c=>"@" + c.Name))}",
						$"	)",
						$"	Select * From {table.Name} Where {table.Columns.Single(c=>c.IsInPrimaryKey).Name} = Scope_Identity()" + "\";",
						$"var ret = context.Connection.Query<TheDAL.Poco.{table.Name}>(", //	TODO:OF:Fetch namespace from Poco project.
						"	Sql, ",
						"	new {",
						$"	{string.Join(", ", table.Columns.Where(c=>c.IsInPrimaryKey).Select(c=> "\t" + c.Name.ToCamelCase() + " = " + table.Name.ToCamelCase() + "." + c.Name  ))}",
						"	},",
						"	context.Transaction,", 
						"	false, null, null);", 
						"return ret.Single();",
					})
				});

				//	##	Create the Delete method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "Delete",
					Comment = new CommentData(new[] {
						"This method is used for deleting a record.",
						"<para>St4mpede guid:{F74246AE-0295-4094-AA7F-1D118C11229D}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"void",
					Parameters =
							new[] { new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }
						.Concat(
							new [] { new ParameterData
							   {
								   Name = table.Name,
								   SystemTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
							   }
					}).ToList(),
					Body = new BodyData(new[]
					{
						"DeleteById( context, " +
						string.Join(", ", table.PrimaryKeyColumns.Select(c=>
						$"{table.Name.ToCamelCase()}.{c.Name}")) + ");"
					})
				});

				//	##	Create the DeleteById method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "DeleteById",
					Comment = new CommentData(new[] {
						"This method is used for deleting a record by its primary key.",
						"<para>St4mpede guid:{F0137E62-1D45-4B92-A48E-05954850FFE8}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"void",
					Parameters =
							new[] { new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }
						.Concat(
							table.PrimaryKeyColumns
								.Select(p =>
								   new ParameterData
								   {
									   Name = p.Name,
									   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString(p.DatabaseTypeName)
								   }))
						.ToList(),
					Body = new BodyData(new[]
						{
							$"const string Sql = @" + "\"",
							$"	Delete From {table.Name}",
							"\t Where " + string.Join(" And ", table.PrimaryKeyColumns.Select(c=>$"{c.Name} = @{c.Name.ToCamelCase()}")),
							"\";",
							$"context.Connection.Execute(",
							"	Sql, ",
							"	new {",
							$"	{string.Join(", ", table.Columns.Where(c=>c.IsInPrimaryKey).Select(c=> "\t" +$"{c.Name.ToCamelCase()} = {c.Name.ToCamelCase()}"  ))}",
							"	},",
							"	context.Transaction,",
							"	null, null);",
						})
				});

				//	##	Create the GetAll method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "GetAll",
					Comment = new CommentData(new[] {
						"This method returns every record for this table/surface.",
						"Do not use it on too big tables.",
						"<para>St4mpede guid:{4A910A99-9A50-4977-B2A2-404240CDDC73}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"IList<TheDAL.Poco.{table.Name}>", //	TODO:OF:Fetch namespace from Poco project.
					Parameters =
						new[] { new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }.ToList(),
					Body = new BodyData(new[]
						{
							$"const string Sql = @" + "\"",
							$"	Select * From {table.Name}",
							"\";",
							$"var res = context.Connection.Query<TheDAL.Poco.{table.Name}>(",	//	TODO:OF:Fetch namespace from settings.
							"	Sql, ",
							"	null, ",
							"	context.Transaction,",
							"	false, null, null);",
							"return res.ToList();"
					})
				});

				//	##	Create the GetById method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "GetById",
					Comment = new CommentData(new[] {
						"This method is used for getting a record but its Id/primary key.",
						"If nothing is found an exception is thrown. If you want to get null back use LoadById <see cref=\"LoadById\"/> instead.",
						"<para>St4mpede guid:{71CF185E-0DD1-4FAE-9721-920B5C3C12D9}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters =
						new[] { new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }
						.Concat(
							table.PrimaryKeyColumns
								.Select(p =>
								   new ParameterData
								   {
									   Name = p.Name,
									   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString(p.DatabaseTypeName)
								   }))
						.ToList(),
					Body = new BodyData(new[]
						{
							$"const string Sql = @" + "\"",
							$"	Select * From {table.Name}",
							"\t Where " + string.Join(" And ", table.PrimaryKeyColumns.Select(c => $"{c.Name} = @{c.Name.ToCamelCase()}")),
							"\";",
							$"var res = context.Connection.Query<TheDAL.Poco.{table.Name}>(",	//	TODO:OF:Fetch namespace from settings.
							"	Sql, ",
							"	new {",
							$"		{string.Join(", ", table.Columns.Where(c => c.IsInPrimaryKey).Select(c =>$"{c.Name.ToCamelCase()} = {c.Name.ToCamelCase()}"))}",
							"	},",
							"	context.Transaction,",
							"	false, null, null);",
							"return res.Single();"
						})
				});

				//	##	Create the LoadById method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "LoadById",
					Comment = new CommentData(new[] {
						"This method is used for getting a record but its Id/primary key.",
						"If nothing is found an null is returned. If you want to throw an exception use GetById <see cref=\"GetById\"> instead.",
						"<para>St4mpede guid:{BC171F29-81F2-41ED-AC5C-AD6884EC9718}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters =
						new[] { new ParameterData
						{
							Name="context",
							SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
						} }
						.Concat(
							table.Columns
								.Where(c => c.IsInPrimaryKey)
								.Select(p =>
								   new ParameterData
								   {
									   Name = p.Name,
									   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString(p.DatabaseTypeName)
								   }))
						.ToList(),
					Body = new BodyData(new[]
						{
							$"const string Sql = @" + "\"",
							$"	Select * From {table.Name}",
							"\t Where " + string.Join(" And ", table.PrimaryKeyColumns.Select(c => $"{c.Name} = @{c.Name.ToCamelCase()}")),
							"\";",
							$"var res = context.Connection.Query<TheDAL.Poco.{table.Name}>(",	//	TODO:OF:Fetch namespace from settings.
							"	Sql, ",
							"	new {",
							$"		{string.Join(", ", table.Columns.Where(c => c.IsInPrimaryKey).Select(c =>$"{c.Name.ToCamelCase()} = {c.Name.ToCamelCase()}"))}",
							"	},",
							"	context.Transaction,",
							"	false, null, null);",
							"return res.SingleOrDefault();"
						})
				});

				//	##	Create the Update method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "Update",
					Comment = new CommentData(new [] {
						"This method is used for updating an existing record in the database.",
						"<para>St4mpede guid:{5A4CE926-447C-4F3F-ADFC-8CA9229C60BF}</para>"
						}),
					Scope = Common.VisibilityScope.Private,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters =
							new[] { new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }
						.Concat(
							table.Columns
								.Select(p =>
								   new ParameterData
								   {
									   Name = p.Name,
									   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString(p.DatabaseTypeName)
								   }))
						.ToList(),
					Body = new BodyData(new[]
						{
							$"return Update( ",
							"\tcontext, ",
							$"\tnew TheDAL.Poco.{table.Name}(",	//	TODO:OF:Fetch namespace from settings.
							"\t\t" + string.Join(", ",table.Columns.Select( c=> $"{c.Name.ToCamelCase()}")),
							"));"
						})
				});

				//	##	Create the Update method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "Update",
					Comment = new CommentData(new []{
						"This method is used for updating an existing record in the database.",
						"<para>St4mpede guid:{B2B1B845-5F93-4A5C-9F90-FBA570228542}</para>"
						}),
					Scope = Common.VisibilityScope.Private,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters = new[] {
							new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							},
							new ParameterData
							{
								Name = table.Name,	//	TODO:OF:Make camelcase.
								SystemTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
							} }
						.ToList(),
					Body = new BodyData(new[]
						{
						$"const string Sql = @" + "\"",
						$"	Update {table.Name}",
						$"	Set",
						"\t\t" + string.Join(", ",table.NonPrimaryKeyColumns.Select(c=>
							$"{c.Name} = @{c.Name.ToCamelCase()}"
						)),
						$"\tWhere",
						"\t\t" + string.Join(" And ",table.PrimaryKeyColumns.Select(c=>
							$"{c.Name} = @{c.Name}"))
						,
						$"\t Select * From {table.Name} Where ", 
						$"\t\t" + string.Join(" And ", table.PrimaryKeyColumns.Select(c=>$"{c.Name} = ${c.Name.ToCamelCase()}")),
						"\t\";",
						$"var ret = context.Connection.Query<TheDAL.Poco.{table.Name}>(", //	TODO:OF:Fetch namespace from Poco project.
						"	Sql, ",
						"	new {",
						$"	{string.Join(", ", table.Columns.Select(c=> "\t" + c.Name.ToCamelCase() + " = " + table.Name.ToCamelCase() + "." + c.Name  ))}",
						"	},",
						"	context.Transaction,",
						"	false, null, null);",
						"return ret.Single();",
					})
				});

				//	##	Create the Upsert method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "Upsert",
					Comment = new CommentData(new[] {
						"This method is used for creating a new or  updating an existing record in the database.",
						"If the primary key is 0 (zero) we know it is a new record and try to add it. Otherwise we try to update the record.",
						"<para>St4mpede guid:{A821E709-7333-4ABA-9F38-E85617C906FE}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters =
						new[] { new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							} }
						.Concat(
						table.Columns
							.Select(c =>
							   new ParameterData
							   {
								   Name = c.Name,
								   SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString(c.DatabaseTypeName)
							   }))
						.ToList(),
					Body = new BodyData(new[]
						{
						$"return Upsert(",
						"\tcontext,",
						$"\tnew TheDAL.Poco.{table.Name}(", //	TODO:OF:Fetch namespace from settings.
						"\t\t" + string.Join( ", ", table.Columns.Select(c=>$"{c.Name.ToCamelCase()}")),
						"));"
					})
				});

				//	##	Create the Upsert method.
				surfaceClass.Methods.Add(new MethodData
				{
					Name = "Upsert",
					Comment = new CommentData(new[] {
						"This method is used for creating a new or  updating an existing record in the database.",
						"If the primary key is default value (typically null or zero) we know it is a new record and try to add it. Otherwise we try to update the record.",
						"<para>St4mpede guid:{97D67E96-7C3E-4D8B-8984-104896646077}</para>"
						}),
					Scope = Common.VisibilityScope.Public,
					ReturnTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
					Parameters = new[] {
							new ParameterData
							{
								Name="context",
								SystemTypeString = "TheDAL.CommitScope"	//	TODO:OF:Fetch namespace from settings.
							},
							new ParameterData
							{
								Name = table.Name,	//	TODO:OF:Make camelcase.
								SystemTypeString = $"TheDAL.Poco.{table.Name}", //	TODO:OF:Fetch namespace from Poco project.
							} }
						.ToList(),
					Body = new BodyData(new[]
						{
						"if(" + string.Join(" &&", table.PrimaryKeyColumns.Select(c=> $"{table.Name.ToCamelCase()}.{c.Name} == default({parserLogic2.ConvertDatabaseTypeToDotnetTypeString(c.DatabaseTypeName)})") ) + "){",
						$"\treturn Add(context, {table.Name.ToCamelCase()});", 
						"}else{",
						$"\treturn Update(context, {table.Name.ToCamelCase()});", 
						"}"
					})
				});
			});

			_log.Add("Included surfaces are:");
			_log.Add(_surfaceDataList.ToInfo());
		}