public Method CreateCompareAllColumnMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "CompareAllColumn"); md.Modifier.Polymophism = MethodPolymophism.Override; md.Parameters.Add(new MethodParameter("Record", "record")); md.ReturnTypeName = new TypeName("Boolean"); md.Body.Add(SourceCodeLanguage.CSharp, "if (record == null) throw new ArgumentNullException(\"record\");"); md.Body.Add(SourceCodeLanguage.CSharp, "var r = record;"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < t.Columns.Count; i++) { var column = t.Columns[i]; sb.Clear(); if (i == 0) { sb.Append("return "); } if (i < t.Columns.Count - 1) { sb.AppendFormat("Object.Equals(this.{0}, r.{0}) && ", column.Name); } else { sb.AppendFormat("Object.Equals(this.{0}, r.{0});", column.Name); } md.Body.Add(SourceCodeLanguage.CSharp, sb.ToString()); } return md; }
private Method CreateCreateDataTableMethod() { Method md = new Method(MethodAccessModifier.Public, "CreateDataTable"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("DataTable"); md.Body.AddRange(CreateDataTableMethodBody()); return md; }
private Method CreateGetValuesMethod() { Method md = new Method(MethodAccessModifier.Public, "GetValues"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("Object[]"); md.Body.AddRange(this.CreateGetValuesMethodBody()); return md; }
public void MethodBasicFeature() { var md = new Method(MethodAccessModifier.Public, "GetNumber"); md.Body.Add(SourceCodeLanguage.CSharp, "return 123;"); Assert.AreEqual("public void GetNumber()" + SourceCodeGenerator.NewLine + "{" + SourceCodeGenerator.NewLine + SourceCodeGenerator.Default.Indent + "return 123;" + SourceCodeGenerator.NewLine + "}" + SourceCodeGenerator.NewLine , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, md)); }
public void MethodPartialFeature() { var md = new Method(MethodAccessModifier.Partial, "GetNumber"); //These will be ignored md.Parameters.Add(new MethodParameter("Int32", "number")); md.GenericParameters.Add("T"); md.ReturnTypeName = new TypeName("void"); md.Body.Add(SourceCodeLanguage.CSharp, "return 123;"); Assert.AreEqual("partial void GetNumber<T>(Int32 number);" + SourceCodeGenerator.NewLine , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, md)); }
public void MethodParameters() { var md = new Method(MethodAccessModifier.Public, "GetDisplayName"); md.Parameters.Add(new MethodParameter("String", "name")); md.Parameters.Add(new MethodParameter("Int32", "age")); md.Body.Add(SourceCodeLanguage.CSharp, "return name + \":\" + age.ToString();"); Assert.AreEqual("public void GetDisplayName(String name, Int32 age)" + SourceCodeGenerator.NewLine + "{" + SourceCodeGenerator.NewLine + SourceCodeGenerator.Default.Indent + "return name + \":\" + age.ToString();" + SourceCodeGenerator.NewLine + "}" + SourceCodeGenerator.NewLine , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, md)); }
public void ClassBasicFeature() { var c = new Class(AccessModifier.Public, "Person"); var f = new Field("Int32", "_Age"); c.Fields.Add(f); var p = new Property("Int32", "Age"); p.Get.Body.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return _Age;")); p.Set.Body.Add(new CodeBlock(SourceCodeLanguage.CSharp, "_Age = value;")); c.Properties.Add(p); var md = new Method(MethodAccessModifier.Public, "GetNumber"); md.Body.Add(SourceCodeLanguage.CSharp, "return 123;"); c.Methods.Add(md); var sw = new StringWriter(); var cs = new CSharpSourceCodeGenerator(sw); cs.Write(c); Assert.AreEqual("public class Person" + SourceCodeGenerator.NewLine + "{" + SourceCodeGenerator.NewLine + " private Int32 _Age;" + SourceCodeGenerator.NewLine + SourceCodeGenerator.NewLine + " public Int32 Age" + SourceCodeGenerator.NewLine + " {" + SourceCodeGenerator.NewLine + " get" + SourceCodeGenerator.NewLine + " {" + SourceCodeGenerator.NewLine + " return _Age;" + SourceCodeGenerator.NewLine + " }" + SourceCodeGenerator.NewLine + " set" + SourceCodeGenerator.NewLine + " {" + SourceCodeGenerator.NewLine + " _Age = value;" + SourceCodeGenerator.NewLine + " }" + SourceCodeGenerator.NewLine + " }" + SourceCodeGenerator.NewLine + SourceCodeGenerator.NewLine + " public void GetNumber()" + SourceCodeGenerator.NewLine + " {" + SourceCodeGenerator.NewLine + " return 123;"+ SourceCodeGenerator.NewLine + " }" + SourceCodeGenerator.NewLine + "}" + SourceCodeGenerator.NewLine , sw.ToString()); }
public abstract void Write(Method method);
public Method CreateUpdateStoredProcedureMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "CreateUpdateStoredProcedure"); md.ReturnTypeName = new TypeName(String.Format("{0}Update", t.Name)); md.Parameters.Add(new MethodParameter("Record", "record")); md.Body.AddRange(CreateUpdateStoredProcedureMethodBody()); return md; }
public Method CreateSetOutputParameterValueMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Protected, "SetOutputParameterValue"); md.Modifier.Polymophism = MethodPolymophism.Override; md.Parameters.Add(new MethodParameter("Record", "record")); md.Parameters.Add(new MethodParameter("StoredProcedure", "storedProcedure")); md.Body.AddRange(CreateSetOutputParameterValueMethodBody()); return md; }
public Method CreateSelectByPrimaryKeyOrNullMethod1() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "SelectByPrimaryKeyOrNull"); md.ReturnTypeName = new TypeName("Record"); md.Parameters.Add(new MethodParameter("Database", "database")); md.Body.Add(SourceCodeLanguage.CSharp, "var sp = new {0}SelectByPrimaryKey();", t.Name); foreach (var column in t.GetPrimaryKeyColumns()) { var pName = this.ToCamelCase(column.Name); md.Parameters.Add(new MethodParameter(column.GetClassName(), pName)); md.Body.Add(SourceCodeLanguage.CSharp, "sp.PK_{0} = {1};", column.Name, pName); } md.Body.Add(SourceCodeLanguage.CSharp, "var rs = sp.GetFirstResultSet(database);", t.Name); md.Body.Add(SourceCodeLanguage.CSharp, "if (rs == null) return null;"); md.Body.Add(SourceCodeLanguage.CSharp, "var r = new Record(rs);"); md.Body.Add(SourceCodeLanguage.CSharp, "r.SetOldRecordProperty();"); md.Body.Add(SourceCodeLanguage.CSharp, "return r;"); return md; }
public Method CreateSelectAllStoredProcedureMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "CreateSelectAllStoredProcedure"); md.ReturnTypeName = new TypeName(String.Format("{0}SelectAll", t.Name)); md.Body.Add(SourceCodeLanguage.CSharp, String.Format("return new {0}SelectAll();", t.Name)); return md; }
public Method CreateDeleteByValueMethod1() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "Delete"); md.ReturnTypeName = new TypeName("Int32"); md.Parameters.Add(new MethodParameter("Database", "database")); md.Body.Add(SourceCodeLanguage.CSharp, "var sp = new {0}Delete();", t.Name); md.Body.Add(SourceCodeLanguage.CSharp, "((IDatabaseContext)sp).TransactionKey = this.TransactionKey;"); foreach (var column in t.GetPrimaryKeyOrTimestampColumns()) { var pName = this.ToCamelCase(column.Name); md.Parameters.Add(new MethodParameter(column.GetClassName(), pName)); md.Body.Add(SourceCodeLanguage.CSharp, "sp.PK_{0} = {1};", column.Name, pName); } md.Body.Add(SourceCodeLanguage.CSharp, "return sp.ExecuteNonQuery(database);"); return md; }
public Method CreateResultSetToStringMethod() { Method md = new Method(MethodAccessModifier.Public, "ToString"); md.ReturnTypeName = new TypeName("String"); md.Modifier.Polymophism = MethodPolymophism.Override; md.Body.AddRange(CreateResultSetToStringMethodBody()); return md; }
public override void Write(Method method) { throw new NotImplementedException(); }
public Method CreateCreateRecordMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "CreateRecord"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("Record"); md.Body.Add(SourceCodeLanguage.CSharp, "return new Record();"); return md; }
public Method CreateCreateStoredProcedureWithResultSetMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "CreateStoredProcedureWithResultSet"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("StoredProcedureWithResultSet"); md.Parameters.Add(new MethodParameter("TableStoredProcedureTypeWithResultSet", "type")); md.Parameters.Add(new MethodParameter("Record", "record")); md.Body.AddRange(CreateCreateStoredProcedureWithResultSetMethodBody()); return md; }
public void ClassLineBreakFeature() { var c = new Class(AccessModifier.Public, "Person"); var md = new Method(MethodAccessModifier.Public, "GetNumber"); md.Body.Add(SourceCodeLanguage.CSharp, "return 123;"); c.Methods.Add(md); Assert.AreEqual("public class Person" + SourceCodeGenerator.NewLine + "{" + SourceCodeGenerator.NewLine + " public void GetNumber()" + SourceCodeGenerator.NewLine + " {" + SourceCodeGenerator.NewLine + " return 123;" + SourceCodeGenerator.NewLine + " }" + SourceCodeGenerator.NewLine + "}" + SourceCodeGenerator.NewLine , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, c)); }
public Method CreateGetDatabaseKeyMethod(String databaseKey) { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "GetDatabaseKey"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("String"); md.Body.Add(SourceCodeLanguage.CSharp, String.Format("return \"{0}\";", databaseKey)); return md; }
public Method CreateGetColumnCountMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "GetColumnCount"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("Int32"); md.Body.Add(SourceCodeLanguage.CSharp, "return {0};", t.Columns.Count); return md; }
public Method CreateSelectByPrimaryKeyOrNullMethod() { var t = this.Table; StringBuilder sb = new StringBuilder(); Boolean isFirst = true; Method md = new Method(MethodAccessModifier.Public, "SelectByPrimaryKeyOrNull"); md.ReturnTypeName = new TypeName("Record"); foreach (var column in t.GetPrimaryKeyColumns()) { var pName = this.ToCamelCase(column.Name); md.Parameters.Add(new MethodParameter(column.GetClassName(), pName)); if (isFirst == false) { sb.Append(", "); } sb.Append(pName); isFirst = false; } md.Body.Add(SourceCodeLanguage.CSharp, "return this.SelectByPrimaryKeyOrNull(this.GetDatabase(), {0});", sb.ToString()); return md; }
public Method CreateGetValueMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "GetValue"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("Object"); md.Parameters.Add(new MethodParameter("Int32", "index")); var cb = new CodeBlock(SourceCodeLanguage.CSharp, "switch (index)"); cb.CurlyBracket = true; Int32 index = 0; foreach (var item in t.Columns) { cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "case {0}: return this.{1};", index, item.Name)); index += 1; } md.Body.Add(cb); md.Body.Add(SourceCodeLanguage.CSharp, "throw new ArgumentOutOfRangeException();"); return md; }
public Method CreateSetDataRowMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "SetDataRow"); md.Modifier.AccessModifier = MethodAccessModifier.Protected; md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("DataRow"); md.Parameters.Add(new MethodParameter("DataRow", "dataRow")); md.Parameters.Add(new MethodParameter("Record", "record")); md.Parameters.Add(new MethodParameter("SaveMode", "saveMode")); md.Body.AddRange(CreateSetDataRowMethodBody()); return md; }
public Method CreateSetPropertyMethod1() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "SetProperty"); md.Parameters.Add(new MethodParameter("IRecord", "record")); md.ReturnTypeName = new TypeName("void"); md.Body.Add(SourceCodeLanguage.CSharp, "if (record == null) throw new ArgumentNullException(\"record\");"); md.Body.Add(SourceCodeLanguage.CSharp, "var r = record;"); foreach (var column in t.Columns) { md.Body.Add(SourceCodeLanguage.CSharp, "this.{0} = r.{0};", column.Name); } return md; }
public Method CreateSetRecordPropertyMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Protected, "SetRecordProperty"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("void"); md.Parameters.Add(new MethodParameter("StoredProcedureResultSet", "resultSet")); md.Parameters.Add(new MethodParameter("Record", "record")); md.Body.Add(SourceCodeLanguage.CSharp, "record.SetProperty(resultSet as IRecord);"); return md; }
public Method CreateSetValueMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "SetValue"); md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("Boolean"); md.Parameters.Add(new MethodParameter("Int32", "index")); md.Parameters.Add(new MethodParameter("Object", "value")); var cb = new CodeBlock(SourceCodeLanguage.CSharp, "switch (index)"); cb.CurlyBracket = true; Int32 index = 0; CodeBlock cbIf = null; CodeBlock cbElse = null; Boolean useCast = false; foreach (var item in t.Columns) { var tp = item.GetClassNameType(); useCast = item.ConvertType == SqlParameterConvertType.Default && (tp == ClassNameType.Object || tp == ClassNameType.String || tp == ClassNameType.ByteArray || tp == ClassNameType.MySqlGeometry || tp == ClassNameType.Geography || tp == ClassNameType.Geometry || tp == ClassNameType.HierarchyId); cb.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "case {0}:", index)); cbIf = new CodeBlock(SourceCodeLanguage.CSharp, "if (value == null)"); cbIf.IndentLevel = 1; cbIf.CurlyBracket = true; if (item.AllowNull == true) { cbIf.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "this.{0} = null;", item.Name)); cbIf.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return true;")); } else { cbIf.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return false;")); } cb.CodeBlocks.Add(cbIf); cbElse = new CodeBlock(SourceCodeLanguage.CSharp, "else"); cbElse.IndentLevel = 1; cbElse.CurlyBracket = true; if (useCast == true) { if (tp.IsStructure() == true) { cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp , "var newValue = value as {0}?;", tp.ToClassNameString())); } else { cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp , "var newValue = value as {0};", item.GetClassName())); } } else { if (item.ConvertType == SqlParameterConvertType.Default) { cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp , "var newValue = TableRecord.TypeConverter.To{0}(value);", tp)); } else if (item.ConvertType == SqlParameterConvertType.Enum) { cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp , "var newValue = TableRecord.TypeConverter.ToEnum<{0}>(value);", item.EnumName)); } } cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "if (newValue == null) return false;")); if (tp.IsStructure() == true || item.ConvertType == SqlParameterConvertType.Enum) { cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "this.{0} = newValue.Value;", item.Name)); } else { cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "this.{0} = newValue;", item.Name)); } cbElse.CodeBlocks.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return true;")); cb.CodeBlocks.Add(cbElse); index += 1; } md.Body.Add(cb); md.Body.Add(SourceCodeLanguage.CSharp , String.Format("throw new ArgumentOutOfRangeException(\"index\", index, \"index must be 0-{0}\");" , t.Columns.Count - 1)); return md; }
public Method CreateCreateDataTableMethod() { var t = this.Table; Method md = new Method(MethodAccessModifier.Public, "CreateDataTable"); md.Modifier.AccessModifier = MethodAccessModifier.Protected; md.Modifier.Polymophism = MethodPolymophism.Override; md.ReturnTypeName = new TypeName("DataTable"); md.Body.Add(SourceCodeLanguage.CSharp, "var dt = new DataTable(Name);"); foreach (var column in t.GetColumns(true, true)) { md.Body.Add(SourceCodeLanguage.CSharp, "dt.Columns.Add(\"@PK_{0}\", typeof({1}));", column.Name, column.GetClassNameText()); } foreach (var column in t.Columns) { md.Body.Add(SourceCodeLanguage.CSharp, "dt.Columns.Add(\"@{0}\", typeof({1}));", column.Name, column.GetClassNameText()); } md.Body.Add(SourceCodeLanguage.CSharp, "return dt;"); return md; }
public static String Write(SourceCodeLanguage language, Method method) { var sc = Create(language); sc.Write(method); return sc.TextWriter.ToString(); }