void WriteUpdateByPrimaryKeyMethod() { var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var updateInfo = UpdateByPrimaryKeyInfoCreator.Create(TableInfo); var sqlParameters = updateInfo.SqlParameters; file.AppendLine($"public static SqlInfo Update({typeContractName} contract)"); file.OpenBracket(); file.AppendLine("const string sql = @\""); file.AppendAll(updateInfo.Sql); file.AppendLine(); 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 WriteUpdateByKeyMethod() { if (!TableInfo.IsSupportSelectByKey) { return; } var methodName = "Modify" + CamelCasedTableName; var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var callerMemberPath = $"{FullClassName}.{methodName}"; var updateInfo = UpdateByPrimaryKeyInfoCreator.Create(TableInfo); file.AppendLine(); file.AppendLine("/// <summary>"); file.AppendLine($"///{Padding.ForComment} Updates only one record by given primary keys."); 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({contractParameterName}));"); file.CloseBracket(); if (updateInfo.SqlParameters.Any()) { BoaRepositoryFileExporter.WriteDefaultValues(file, Config.DefaultValuesForUpdateByKeyMethod, updateInfo.SqlParameters); } var whereParameters = string.Join(", ", updateInfo.WhereParameters.Select(column => $"@{column.ColumnName}: {{contract.{column.ColumnName.ToContractName()}}}")); file.AppendLine(); file.AppendLine($"var sqlInfo = {sharedRepositoryClassAccessPath}.Update({contractParameterName});"); file.AppendLine(); file.AppendLine("var effectedRowCount = unitOfWork.ExecuteNonQuery(CallerMemberPath, sqlInfo);"); file.AppendLine("if (effectedRowCount != 1)"); file.OpenBracket(); file.AppendLine($"throw new InvalidDataException($\"{{effectedRowCount}} row effected. Effected row count should be 1 when update table {TableInfo.SchemaName}.{TableInfo.TableName}. WhereParameters: {whereParameters} \");"); file.CloseBracket(); file.CloseBracket(); }
void WriteUpdateByKeyMethod() { if (!TableInfo.IsSupportSelectByKey) { return; } var typeContractName = TableEntityClassNameForMethodParametersInRepositoryFiles; var callerMemberPath = $"{RepositoryNamespace}.{ClassName}.Update"; var updateInfo = UpdateByPrimaryKeyInfoCreator.Create(TableInfo); file.AppendLine(); file.AppendLine("/// <summary>"); file.AppendLine($"///{Padding.ForComment} Updates only one record by given primary keys."); file.AppendLine("/// </summary>"); file.AppendLine($"public GenericResponse<int> Update({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("}"); if (updateInfo.SqlParameters.Any()) { WriteDefaultValues(file, Config.DefaultValuesForUpdateByKeyMethod, updateInfo.SqlParameters); } file.AppendLine(); file.AppendLine($"var sqlInfo = {sharedRepositoryClassAccessPath}.Update({contractParameterName});"); file.AppendLine(); file.AppendLine("return this.ExecuteNonQuery(CallerMemberPath, sqlInfo);"); file.CloseBracket(); }