private void WriteGetListBy(IEnumerable <DatabaseColumn> columns)
        {
            var methodParameters = CodeWriterUtils.GetMethodParametersForColumns(columns, codeWriterSettings);

            WriteGetListByMethodSummary(methodParameters);
            var methodSignature = CodeWriterUtils.GetGetListByMethodSignature(table, columns, codeWriterSettings, methodParameters);

            using (classBuilder.BeginNest($"public {methodSignature}"))
            {
                CodeWriterUtils.WriteEntryLogging(classBuilder, methodSignature);
                var sqlCommandText = ConstructSqlQuery(methodParameters, null, GetAllColumnNames(new List <DatabaseTable> {
                    table
                }));
                classBuilder.AppendLine($"var entities = new List<{table.NetName}>();");
                WriteBeginCreateConnection();
                WriteBeginCreateCommand();
                classBuilder.AppendLine($"command.CommandText = {sqlCommandText};");
                WriteAddParametersToCommand(methodParameters);
                WriteBeginExecuteReader();
                WriteBeginReadReaderMultiple();
                classBuilder.AppendLine($"var entity = ({table.NetName}){_serviceProviderFieldName}.GetService(typeof({table.NetName}));");
                WriteParseEntityFromReader("entity");
                classBuilder.AppendLine("entities.Add(entity);");
                classBuilder.EndNest();
                classBuilder.EndNest();
                classBuilder.EndNest();
                classBuilder.EndNest();
                classBuilder.AppendLine("");
                classBuilder.AppendLine("return entities;");
                CodeWriterUtils.WriteExitLogging(classBuilder, methodSignature);
            }
        }
Example #2
0
        public void WriteWith(DatabaseConstraint foreignKey)
        {
            // TODO: refactor this method to be consistent with approach taken for other overload
            var refTable = foreignKey.ReferencedTable(table.DatabaseSchema);
            var dataType = refTable.NetName;

            if (foreignKey.Columns.Count != foreignKey.ReferencedColumns(table.DatabaseSchema).Count())
            {
                throw new InvalidOperationException("Number of foreign key columns does not match number of columns referenced!");
            }

            classBuilder.BeginNest($"public virtual {CodeWriterUtils.GetWithMethodSignature(table, foreignKey, codeWriterSettings)}");

            var methodCallParameters = new List <string>();

            var propertyName = codeWriterSettings.Namer.ForeignKeyName(table, foreignKey);

            foreach (var fkc in foreignKey.Columns)
            {
                var tc        = table.Columns.Single(_tc => _tc.Name == fkc);
                var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(tc)}";
                if (tc.Nullable && CodeWriterUtils.FindDataType(tc).EndsWith("?")) // KE: need the check for the "?" so that we correctly handle reference types like string
                {
                    using (classBuilder.BeginNest($"if (!{parameter}.HasValue)"))
                    {
                        classBuilder.AppendLine($"{propertyName} = null;");
                        classBuilder.AppendLine("return this;");
                    }

                    classBuilder.AppendLine("");
                    parameter += ".Value";
                }

                methodCallParameters.Add(parameter);
            }

            var s = string.Join(", ", methodCallParameters);
            var referencedColumnNames = foreignKey.ReferencedColumns(table.DatabaseSchema).ToList();

            referencedColumnNames.Sort();
            var referencedColumns             = referencedColumnNames.Select(c => foreignKey.ReferencedTable(table.DatabaseSchema).FindColumn(c));
            var methodParameters              = CodeWriterUtils.GetMethodParametersForColumns(referencedColumns, codeWriterSettings);
            var methodName                    = CodeWriterUtils.GetMethodName(methodParameters, codeWriterSettings, true, CodeWriterUtils.BaseMethodNameGet);
            var fieldNameForFkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(refTable));

            classBuilder.AppendLine($"{propertyName} = _{fieldNameForFkTableRepository}.{methodName}({s});");
            classBuilder.AppendLine("return this;");
            classBuilder.EndNest();
            classBuilder.AppendLine("");
        }
Example #3
0
        private void WriteGetListBys()
        {
            var combinations = CodeWriterUtils.GetGetListByColumnCombinations(table)?.ToList();

            combinations?.ForEach(c => classBuilder.AppendLine($"{CodeWriterUtils.GetGetListByMethodSignature(table, c, codeWriterSettings, CodeWriterUtils.GetMethodParametersForColumns(c, codeWriterSettings))};"));
        }