Esempio n. 1
0
        private void WriteUpdate()
        {
            var methodParameters = CodeWriterUtils.GetUpdateMethodParameters(table, codeWriterSettings, false, false);

            methodParameters = CodeWriterUtils.AddEntityParameter(methodParameters, table, "An entity with updated values.");
            classBuilder.AppendLine($"{CodeWriterUtils.GetUpdateMethodSignature(table, codeWriterSettings, methodParameters)};");
        }
Esempio n. 2
0
        private void WriteUpdateUniqueByCustomer()
        {
            var methodParameters = CodeWriterUtils.GetUpdateMethodParameters(table, codeWriterSettings, true, true);

            if (methodParameters == null || !methodParameters.Any())
            {
                return;
            }

            methodParameters = CodeWriterUtils.AddEntityParameter(methodParameters, table, "An entity with updated values.");
            classBuilder.AppendLine($"{CodeWriterUtils.GetUpdateMethodSignature(table, codeWriterSettings, methodParameters)};");
        }
        private void WriteUpdateCommon(IEnumerable <Parameter> methodParameters, string fromClause, string whereClause, string columnsToReturn)
        {
            var entityParameterSummary     = "An entity with updated values.";
            var methodParametersWithEntity = CodeWriterUtils.AddEntityParameter(methodParameters, table, entityParameterSummary);

            WriteUpdateMethodSummary(methodParametersWithEntity);
            var methodSignature = CodeWriterUtils.GetUpdateMethodSignature(table, codeWriterSettings, methodParametersWithEntity);

            using (classBuilder.BeginNest($"public {methodSignature}"))
            {
                CodeWriterUtils.WriteEntryLogging(classBuilder, methodSignature);
                WriteGetPropertyColumnPairs();

                classBuilder.AppendLine("var setClause = string.Join(\", \", propertyColumnPairs.Select(pcp => $\"{pcp.Value} = @{pcp.Key.Name}\"));");
                var thisTableAlias = codeWriterSettings.Namer.NameToAcronym(table.Name);
                if (!string.IsNullOrEmpty(fromClause))
                {
                    classBuilder.AppendLine($"var sqlCommandText = $\"UPDATE ONLY \\\"{table.Name}\\\" AS {thisTableAlias} SET {{setClause}} FROM {fromClause} WHERE {whereClause} RETURNING {columnsToReturn};\";");
                }
                else
                {
                    classBuilder.AppendLine($"var sqlCommandText = $\"UPDATE ONLY \\\"{table.Name}\\\" AS {thisTableAlias} SET {{setClause}} WHERE {whereClause} RETURNING {columnsToReturn};\";");
                }

                var entityVariableName = "updatedEntity";
                classBuilder.AppendLine($"{table.NetName} {entityVariableName} = null;");
                WriteBeginCreateConnection();
                WriteBeginCreateCommand();
                classBuilder.AppendLine($"command.CommandText = sqlCommandText;");
                WriteAddParametersToCommand(methodParameters);
                WriteAddParametersToCommand();
                classBuilder.BeginNest("try");
                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();
                WriteCatchPostgresExceptions();
                classBuilder.EndNest();
                classBuilder.EndNest();
                WriteReturnEntityIfNotNull(entityVariableName);
                CodeWriterUtils.WriteExitLogging(classBuilder, methodSignature);
            }

            classBuilder.AppendLine("");
        }