private void WriteDeletePhysicalCommon(IEnumerable <Parameter> methodParameters, string usingClause, string whereClause) { var partialMethodName = CodeWriterUtils.ConvertParametersToMethodNameByPart(methodParameters, codeWriterSettings); classBuilder.BeginNest($"private int {CodeWriterUtils.BaseMethodNameDelete}PhysicalBy{partialMethodName}({CodeWriterUtils.PrintParametersForSignature(methodParameters)})"); var thisTableAlias = codeWriterSettings.Namer.NameToAcronym(table.Name); var sqlCommandText = $"\"DELETE FROM ONLY \\\"{table.Name}\\\" AS {thisTableAlias}"; if (!string.IsNullOrEmpty(usingClause)) { sqlCommandText = $"{sqlCommandText} USING {usingClause}"; } sqlCommandText = $"{sqlCommandText} WHERE {whereClause};\""; WriteBeginCreateConnection(); WriteBeginCreateCommand(); classBuilder.AppendLine($"command.CommandText = {sqlCommandText};"); WriteAddParametersToCommand(methodParameters); classBuilder.BeginNest("try"); classBuilder.AppendLine($"return command.ExecuteNonQuery();"); classBuilder.EndNest(); classBuilder.BeginNest("catch (Exception e)"); classBuilder.BeginNest($"if (e is Npgsql.PostgresException pge && pge.SqlState == \"23503\")"); classBuilder.AppendLine("throw new EntityHasDependenciesException();"); classBuilder.EndNest(); classBuilder.AppendLine(""); classBuilder.AppendLine("throw;"); classBuilder.EndNest(); classBuilder.EndNest(); classBuilder.EndNest(); // TODO: KE - consider throwing here if multiple rows were modified! It should never be the case except for bad data even though the schema allows it classBuilder.EndNest(); }
private void WriteDeleteLogicalCommon(IEnumerable <Parameter> methodParameters, string fromClause, string whereClause, string columnsToReturn) { var partialMethodName = CodeWriterUtils.ConvertParametersToMethodNameByPart(methodParameters, codeWriterSettings); using (classBuilder.BeginNest($"private {table.NetName} {CodeWriterUtils.BaseMethodNameDelete}LogicalBy{partialMethodName}({CodeWriterUtils.PrintParametersForSignature(methodParameters)})")) { var logicalDeleteColumn = table.Columns.Single(c => logicalDeleteColumns.Contains(c.Name)); var setClause = $"\\\"{logicalDeleteColumn.Name}\\\" = NOW()"; var thisTableAlias = codeWriterSettings.Namer.NameToAcronym(table.Name); var sqlCommandText = $"\"UPDATE ONLY \\\"{table.Name}\\\" AS {thisTableAlias} SET {setClause}"; if (!string.IsNullOrEmpty(fromClause)) { sqlCommandText = $"{sqlCommandText} FROM {fromClause}"; } sqlCommandText = $"{sqlCommandText} WHERE {whereClause} RETURNING {columnsToReturn};\";"; var entityVariableName = "deletedEntity"; classBuilder.AppendLine($"{table.NetName} {entityVariableName} = null;"); WriteBeginCreateConnection(); WriteBeginCreateCommand(); classBuilder.AppendLine($"command.CommandText = {sqlCommandText};"); WriteAddParametersToCommand(methodParameters); WriteBeginExecuteReader(); WriteBeginReadReaderSingle(); // TODO: KE - consider throwing here if multiple rows were modified! It should never be the case except for bad data even though the schema allows it classBuilder.AppendLine($"{entityVariableName} = ({table.NetName}){_serviceProviderFieldName}.GetService(typeof({table.NetName}));"); WriteParseEntityFromReader(entityVariableName); classBuilder.EndNest(); classBuilder.EndNest(); classBuilder.EndNest(); classBuilder.EndNest(); WriteReturnEntityIfNotNull(entityVariableName); } }