void WriteInsertMethod() { var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var insertInfo = new InsertInfoCreator().Create(TableInfo); var sqlParameters = insertInfo.SqlParameters; file.AppendLine($"public static SqlInfo Insert({typeContractName} contract)"); file.OpenBracket(); file.AppendLine("const string sql = @\""); file.AppendAll(insertInfo.Sql); file.AppendLine(); if (TableInfo.HasIdentityColumn) { file.AppendLine("SELECT CAST(SCOPE_IDENTITY() AS INT)"); } file.AppendLine("\";"); file.AppendLine(); file.AppendLine("var sqlInfo = new SqlInfo { CommandText = sql };"); if (sqlParameters.Any()) { file.AppendLine(); foreach (var columnInfo in sqlParameters) { file.AppendLine($"sqlInfo.AddInParameter(\"@{columnInfo.ColumnName}\", SqlDbType.{columnInfo.SqlDbType}, {ParameterHelper.GetValueForSqlInfoParameter(columnInfo)});"); } } file.AppendLine(); file.AppendLine("return sqlInfo;"); file.CloseBracket(); }
void WriteInsertMethod() { var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var methodName = "Create" + CamelCasedTableName; var callerMemberPath = $"{FullClassName}.{methodName}"; var insertInfo = new InsertInfoCreator { ExcludedColumnNames = Config.ExcludedColumnNamesWhenInsertOperation }.Create(TableInfo); file.AppendLine("/// <summary>"); file.AppendLine($"///{Padding.ForComment} Inserts new record into table."); foreach (var sequenceInfo in TableInfo.SequenceList) { file.AppendLine($"///{Padding.ForComment} <para>Automatically initialize '{sequenceInfo.TargetColumnName.ToContractName()}' property by using '{sequenceInfo.Name}' sequence.</para>"); } file.AppendLine("/// </summary>"); file.AppendLine($"public void {methodName}({typeContractName} {contractParameterName})"); file.OpenBracket(); file.AppendLine($"const string CallerMemberPath = \"{callerMemberPath}\";"); file.AppendLine(); file.AppendLine("if (contract == null)"); file.OpenBracket(); file.AppendLine("throw new ArgumentNullException(nameof(contract));"); file.CloseBracket(); foreach (var sequenceInfo in TableInfo.SequenceList) { file.AppendLine(); file.OpenBracket(); file.AppendLine($"// Init sequence for {sequenceInfo.TargetColumnName.ToContractName()}"); file.AppendLine(); file.AppendLine($"const string sqlNextSequence = @\"SELECT NEXT VALUE FOR {sequenceInfo.Name}\";"); file.AppendLine(); file.AppendLine($"const string callerMemberPath = \"{callerMemberPath} -> sqlNextSequence -> {sequenceInfo.Name}\";"); file.AppendLine(); file.AppendLine("var responseSequence = unitOfWork.ExecuteScalar<object>(callerMemberPath, new SqlInfo {CommandText = sqlNextSequence});"); file.AppendLine(); var columnInfo = TableInfo.Columns.First(x => x.ColumnName == sequenceInfo.TargetColumnName); if (columnInfo.DotNetType == DotNetTypeName.DotNetInt32Nullable) { file.AppendLine($"if({contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} == null)"); file.OpenBracket(); file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt32(responseSequence);"); file.CloseBracket(); } else if (columnInfo.DotNetType == DotNetTypeName.DotNetInt32) { file.AppendLine($"if({contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} == 0)"); file.OpenBracket(); file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt32(responseSequence);"); file.CloseBracket(); } else if (columnInfo.DotNetType == DotNetTypeName.DotNetStringName) { file.AppendLine($"if({contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} == null)"); file.OpenBracket(); file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToString(responseSequence);"); file.CloseBracket(); } else if (columnInfo.DotNetType == DotNetTypeName.DotNetInt64Nullable) { file.AppendLine($"if({contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} == null)"); file.OpenBracket(); file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt64(responseSequence);"); file.CloseBracket(); } else { file.AppendLine($"if({contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} == 0)"); file.OpenBracket(); file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt64(responseSequence);"); file.CloseBracket(); } file.CloseBracket(); } if (insertInfo.SqlParameters.Any()) { BoaRepositoryFileExporter.WriteDefaultValues(file, Config.DefaultValuesForInsertMethod, insertInfo.SqlParameters); } file.AppendLine(); file.AppendLine($"var sqlInfo = {sharedRepositoryClassAccessPath}.Insert({contractParameterName});"); file.AppendLine(); if (TableInfo.HasIdentityColumn) { file.AppendLine("var identity = unitOfWork.ExecuteScalar<int>(CallerMemberPath, sqlInfo);"); file.AppendLine(); file.AppendLine($"{contractParameterName}.{TableInfo.IdentityColumn.ColumnName.ToContractName()} = identity;"); } else { file.AppendLine("unitOfWork.ExecuteNonQuery(CallerMemberPath, sqlInfo);"); } file.CloseBracket(); }
void WriteBulkInsertMethod() { var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var methodName = "Create" + CamelCasedTableName; var callerMemberPath = $"{FullClassName}.{methodName}"; var insertInfo = new InsertInfoCreator { ExcludedColumnNames = Config.ExcludedColumnNamesWhenInsertOperation }.Create(TableInfo); file.AppendLine("/// <summary>"); file.AppendLine($"///{Padding.ForComment} Inserts new record into table."); foreach (var sequenceInfo in TableInfo.SequenceList) { file.AppendLine($"///{Padding.ForComment} <para>Automatically initialize '{sequenceInfo.TargetColumnName.ToContractName()}' property by using '{sequenceInfo.Name}' sequence.</para>"); } file.AppendLine("/// </summary>"); file.AppendLine($"public void {methodName}(IList<{typeContractName}> contracts)"); file.OpenBracket(); file.AppendLine($"const string CallerMemberPath = \"{callerMemberPath}\";"); file.AppendLine(); file.AppendLine("if (contracts == null)"); file.OpenBracket(); file.AppendLine("throw new ArgumentNullException(nameof(contracts));"); file.CloseBracket(); foreach (var sequenceInfo in TableInfo.SequenceList) { file.AppendLine(); file.OpenBracket(); file.AppendLine($"// Init sequence for {sequenceInfo.TargetColumnName.ToContractName()}"); file.AppendLine($"const string callerMemberPath = \"{callerMemberPath} -> sqlNextSequence -> {sequenceInfo.Name}\";"); file.AppendLine(); file.AppendLine($"var sequenceList = unitOfWork.GetSequenceList(callerMemberPath, \"{sequenceInfo.Name}\", contracts.Count);"); file.AppendLine(); file.AppendLine("for(var i = 0; i < contracts.Count; i++)"); file.OpenBracket(); var columnInfo = TableInfo.Columns.First(x => x.ColumnName == sequenceInfo.TargetColumnName); if (columnInfo.DotNetType == DotNetTypeName.DotNetInt32 || columnInfo.DotNetType == DotNetTypeName.DotNetInt32Nullable) { file.AppendLine($"contracts[i].{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt32(sequenceList[i]);"); } else if (columnInfo.DotNetType == DotNetTypeName.DotNetStringName) { file.AppendLine($"contracts[i].{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToString(sequenceList[i]);"); } else { file.AppendLine($"contracts[i].{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt64(sequenceList[i]);"); } file.CloseBracket(); file.CloseBracket(); } if (insertInfo.SqlParameters.Any()) { file.AppendLine(); file.AppendLine("foreach( var contract in contracts)"); file.OpenBracket(); BoaRepositoryFileExporter.WriteDefaultValues(file, Config.DefaultValuesForInsertMethod, insertInfo.SqlParameters); file.CloseBracket(); } file.AppendLine(); file.AppendLine($"var dt = new DataTable(\"{TableInfo.SchemaName}.{TableInfo.TableName}\");"); foreach (var c in TableInfo.Columns) { var dataColumnName = $"\"{c.ColumnName}\""; if (c.SqlReaderMethod == SqlReaderMethods.GetBooleanNullableValueFromChar) { file.AppendLine("dt.Columns.Add(new DataColumn(" + dataColumnName + ", typeof(string)) { AllowDBNull = true});"); } else if (c.SqlReaderMethod == SqlReaderMethods.GetBooleanValueFromChar) { file.AppendLine("dt.Columns.Add(new DataColumn(" + dataColumnName + ", typeof(string)));"); } else if (c.IsNullable) { file.AppendLine("dt.Columns.Add(new DataColumn(" + dataColumnName + ", typeof(" + c.DotNetType.RemoveFromEnd("?") + ")) { AllowDBNull = true});"); } else { file.AppendLine($"dt.Columns.Add({dataColumnName}, typeof({c.DotNetType}));"); } } file.AppendLine(); file.AppendLine("foreach (var contract in contracts)"); file.OpenBracket(); file.AppendLine("var dataRow = dt.NewRow();"); file.AppendLine(); foreach (var c in TableInfo.Columns) { var propertyName = c.ColumnName.ToContractName(); var dataColumnName = $"\"{c.ColumnName}\""; if (c.SqlReaderMethod == SqlReaderMethods.GetBooleanValueFromChar) { file.AppendLine($"dataRow[{dataColumnName}] = contract.{propertyName} ? \"1\" : \"0\";"); } else if (c.SqlReaderMethod == SqlReaderMethods.GetBooleanNullableValueFromChar) { file.AppendLine($"if (contract.{propertyName} != null)"); file.OpenBracket(); file.AppendLine($"dataRow[{dataColumnName}] = contract.{propertyName}.Value ? \"1\" : \"0\";"); file.CloseBracket(); } else if (c.IsNullable) { file.AppendLine($"if (contract.{propertyName} != null)"); file.OpenBracket(); file.AppendLine($"dataRow[{dataColumnName}] = contract.{propertyName};"); file.CloseBracket(); } else { file.AppendLine($"dataRow[{dataColumnName}] = contract.{propertyName};"); } } file.AppendLine("dt.Rows.Add(dataRow);"); file.CloseBracket(); file.AppendLine(); file.AppendLine("unitOfWork.ExecuteBulkInsert(CallerMemberPath, dt);"); file.CloseBracket(); // make override file.AppendLine(); file.AppendLine($"public void CreateBulk{CamelCasedTableName}(IList<{typeContractName}> contracts)"); file.OpenBracket(); file.AppendLine($"{methodName}(contracts);"); file.CloseBracket(); }
void WriteInsertMethod() { var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var callerMemberPath = $"{RepositoryNamespace}.{ClassName}.Insert"; var insertInfo = new InsertInfoCreator { ExcludedColumnNames = Config.ExcludedColumnNamesWhenInsertOperation }.Create(TableInfo); file.AppendLine("/// <summary>"); file.AppendLine($"///{Padding.ForComment} Inserts new record into table."); foreach (var sequenceInfo in TableInfo.SequenceList) { file.AppendLine($"///{Padding.ForComment} <para>Automatically initialize '{sequenceInfo.TargetColumnName.ToContractName()}' property by using '{sequenceInfo.Name}' sequence.</para>"); } file.AppendLine("/// </summary>"); file.AppendLine($"public GenericResponse<int> Insert({typeContractName} {contractParameterName})"); file.OpenBracket(); file.AppendLine($"const string CallerMemberPath = \"{callerMemberPath}\";"); file.AppendLine(); file.AppendLine("if (contract == null)"); file.AppendLine("{"); file.AppendLine(" return this.ContractCannotBeNull(CallerMemberPath);"); file.AppendLine("}"); foreach (var sequenceInfo in TableInfo.SequenceList) { file.AppendLine(); file.OpenBracket(); file.AppendLine($"// Init sequence for {sequenceInfo.TargetColumnName.ToContractName()}"); file.AppendLine(); file.AppendLine($"const string sqlNextSequence = @\"SELECT NEXT VALUE FOR {sequenceInfo.Name}\";"); file.AppendLine(); file.AppendLine($"const string callerMemberPath = \"{callerMemberPath} -> sqlNextSequence -> {sequenceInfo.Name}\";"); file.AppendLine(); file.AppendLine("var responseSequence = this.ExecuteScalar<object>(callerMemberPath, new SqlInfo {CommandText = sqlNextSequence});"); file.AppendLine("if (!responseSequence.Success)"); file.AppendLine("{"); file.AppendLine(" return this.SequenceFetchError(responseSequence, callerMemberPath);"); file.AppendLine("}"); file.AppendLine(); var columnInfo = TableInfo.Columns.First(x => x.ColumnName == sequenceInfo.TargetColumnName); if (columnInfo.DotNetType == DotNetTypeName.DotNetInt32 || columnInfo.DotNetType == DotNetTypeName.DotNetInt32Nullable) { file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt32(responseSequence.Value);"); } else if (columnInfo.DotNetType == DotNetTypeName.DotNetStringName) { file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToString(responseSequence.Value);"); } else { file.AppendLine($"{contractParameterName}.{sequenceInfo.TargetColumnName.ToContractName()} = Convert.ToInt64(responseSequence.Value);"); } file.CloseBracket(); } if (insertInfo.SqlParameters.Any()) { WriteDefaultValues(file, Config.DefaultValuesForInsertMethod, insertInfo.SqlParameters); } file.AppendLine(); file.AppendLine($"var sqlInfo = {sharedRepositoryClassAccessPath}.Insert({contractParameterName});"); file.AppendLine(); if (TableInfo.HasIdentityColumn) { file.AppendLine("var response = this.ExecuteScalar<int>(CallerMemberPath, sqlInfo);"); file.AppendLine(); file.AppendLine($"{contractParameterName}.{TableInfo.IdentityColumn.ColumnName.ToContractName()} = response.Value;"); file.AppendLine(); file.AppendLine("return response;"); } else { file.AppendLine("return this.ExecuteNonQuery(CallerMemberPath, sqlInfo);"); } file.CloseBracket(); }