Exemple #1
0
        private static void WriteExistsUniqueKey(this SourceBuilder sb, Entity entity)
        {
            var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_");

            foreach (var key in entity.UniqueKeys)
            {
                var fields            = key.FieldReferences.Select(fr => fr.ResolvedField);
                var methodNamePostfix = string.Join("_", fields.Select(f => f.Name));
                var parameters        = fields.Select(f =>
                {
                    return(f.Type.ToCSharp(), f.Name.ToCamelCase());
                });
                sb.PublicStaticMethod("global::SqlKata.Query", $"Exists_{entityName}_UK_{methodNamePostfix}", parameters, sb =>
                {
                    var viewName = entity.GetViewName();
                    sb.AL($@"return new global::SqlKata.Query(""{viewName}"")");
                    sb.I(sb =>
                    {
                        foreach (var field in fields)
                        {
                            var fieldName     = field.Name;
                            var parameterName = fieldName.ToCamelCase();
                            sb.AL($@".Where(""{fieldName}"", {parameterName})");
                        }
                        sb.AL(@".SelectRaw(""1"")");
                        sb.AL(@".Limit(1);");
                    });
                });
            }
        }
        private static void WriteCount(this SourceBuilder sb, Entity entity)
        {
            var modelName      = $"global::Allvis.Kaylee.Generated.SqlKata.Entities.{entity.DisplayName.Replace(".", "").Replace("::", ".")}";
            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey().ToList();

            var fields = fullPrimaryKey.Take(fullPrimaryKey.Count - 1).Select(fr =>
            {
                var field = fr.ResolvedField;
                return(field.Type.ToCSharp(), field.Name, field.Name.ToCamelCase());
            }).ToList();

            var stackedGroupable = new Stack <(string Type, string Name, string NameCamel)>(fields);
            var i = stackedGroupable.Count;

            while (i >= 0)
            {
                var stackedPivotable = new Stack <(string Type, string Name, string NameCamel)>(fields.Take(i));
                var j = stackedPivotable.Count;
                while (j >= 0)
                {
                    if (j == 0)
                    {
                        var parameters = stackedGroupable.Select(p => (p.Type, p.NameCamel)).Reverse().PrefixWithQueryFactory();
                        var arguments  = string.Join(", ", stackedGroupable.Reverse().Select(p => p.NameCamel));
                        sb.PublicStaticMethod("global::System.Threading.Tasks.Task<int>", $"Count_{entityName}", parameters, sb =>
                        {
                            sb.AL($@"return _db.ExecuteScalarAsync<int>(global::Allvis.Kaylee.Generated.SqlKata.Queries.Count_{entityName}({arguments}));");
                        });
                    }
                    else if (i > 0)
                    {
                        var parameters  = stackedPivotable.Skip(1).Select(p => (p.Type, p.NameCamel)).Reverse().PrefixWithQueryFactory();
                        var arguments   = string.Join(", ", stackedPivotable.Skip(1).Reverse().Select(p => p.NameCamel));
                        var resultTuple = stackedGroupable.Reverse().Select(p => (p.Type, p.Name)).Concat(new[] { ("int", "Count") }).Join();
Exemple #3
0
        private static void WriteExists(this SourceBuilder sb, Entity entity)
        {
            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey();
            var parameters     = fullPrimaryKey.Select(fr =>
            {
                var field = fr.ResolvedField;
                return(field.Type.ToCSharp(), field.Name.ToCamelCase());
            });

            sb.PublicStaticMethod("global::SqlKata.Query", $"Exists_{entityName}", parameters, sb =>
            {
                var viewName = entity.GetViewName();
                sb.AL($@"return new global::SqlKata.Query(""{viewName}"")");
                sb.I(sb =>
                {
                    foreach (var field in fullPrimaryKey)
                    {
                        var fieldName     = field.FieldName;
                        var parameterName = fieldName.ToCamelCase();
                        sb.AL($@".Where(""{fieldName}"", {parameterName})");
                    }
                    sb.AL(@".SelectRaw(""1"")");
                    sb.AL(@".Limit(1);");
                });
            });
        }
Exemple #4
0
        public void TestPublicStaticMethod_Optional()
        {
            // Arrange
            var sb = new SourceBuilder();

            // Act
            sb.PublicStaticMethod("bool", "IsHappy", new[] { (true, "string", "roommate"), (false, "int", "age") }, sb =>
Exemple #5
0
        private static void WriteGet(this SourceBuilder sb, Entity entity)
        {
            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey().ToList();
            var parameters     = fullPrimaryKey.Select(fr =>
            {
                var field = fr.ResolvedField;
                return(field.Type.ToCSharp(), field.Name.ToCamelCase());
            }).ToList();
            var allFields = fullPrimaryKey.Select(fr => fr.ResolvedField).Concat(entity.Fields).Distinct().ToList();

            var stackedFullPrimaryKey = new Stack <FieldReference>(fullPrimaryKey);
            var stackedParameters     = new Stack <(string Type, string Name)>(parameters);

            var i = stackedParameters.Count;

            while (i >= 0)
            {
                sb.PublicStaticMethod("global::SqlKata.Query", $"Get_{entityName}", stackedParameters.Reverse(), sb =>
                {
                    var viewName = entity.GetViewName();
                    sb.AL($@"return new global::SqlKata.Query(""{viewName}"")");
                    sb.I(sb =>
                    {
                        foreach (var field in stackedFullPrimaryKey.Reverse())
                        {
                            var fieldName     = field.FieldName;
                            var parameterName = fieldName.ToCamelCase();
                            sb.AL($@".Where(""{fieldName}"", {parameterName})");
                        }

                        allFields.ForEach((field, last) =>
                        {
                            var fieldName = field.Name;
                            var semicolon = last ? ";" : "";
                            sb.AL($@".Select(""{fieldName}""){semicolon}");
                        });
                    });
                });
                if (stackedParameters.Count > 0)
                {
                    stackedFullPrimaryKey.Pop();
                    stackedParameters.Pop();
                }
                i--;
            }
        }
        private static void WriteExists(this SourceBuilder sb, Entity entity)
        {
            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey();
            var parameters     = fullPrimaryKey.Select(fr =>
            {
                var field = fr.ResolvedField;
                return(Type: field.Type.ToCSharp(), Name: field.Name.ToCamelCase());
            });

            sb.PublicStaticMethod("async global::System.Threading.Tasks.Task<bool>", $"Exists_{entityName}", parameters.PrefixWithQueryFactory(), sb =>
            {
                var arguments = string.Join(", ", parameters.Select(p => p.Name));
                sb.AL($"var _rows = await _db.GetAsync<int>(global::Allvis.Kaylee.Generated.SqlKata.Queries.Exists_{entityName}({arguments})).ConfigureAwait(false);");
                sb.AL("return _rows.Any();");
            });
        }
Exemple #7
0
        private static void WriteInsert(this SourceBuilder sb, Entity entity)
        {
            bool IsOptional(Field field)
            {
                return(!field.IsPartOfParentKey(entity) && (field.HasDefault() || field.Nullable));
            }

            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey();
            var allFields      = fullPrimaryKey.Select(fr => fr.ResolvedField).Where(f => f.IsInsertablePK(entity)).Concat(entity.Fields.Where(f => f.IsInsertable(entity))).Distinct();
            var parameters     = allFields.Select(f => (IsOptional(f), f.Type.ToCSharp(), f.Name.ToCamelCase()));

            sb.PublicStaticMethod("global::SqlKata.Query", $"Insert_{entityName}", parameters, sb =>
            {
                sb.AL("var _columns = new global::System.Collections.Generic.List<string>();");
                sb.AL("var _values = new global::System.Collections.Generic.List<object?>();");
                foreach (var field in allFields)
                {
                    var fieldName     = field.Name;
                    var parameterName = fieldName.ToCamelCase();
                    var optional      = IsOptional(field);
                    if (optional)
                    {
                        sb.AL($"if ({parameterName} != null)");
                        sb.B(sb =>
                        {
                            sb.AL($@"_columns.Add(""{fieldName}"");");
                            sb.AL($@"_values.Add({parameterName});");
                        });
                    }
                    else
                    {
                        sb.AL($@"_columns.Add(""{fieldName}"");");
                        sb.AL($@"_values.Add({parameterName});");
                    }
                }
                var tableName = entity.GetTableName();
                sb.AL($@"return new global::SqlKata.Query(""{tableName}"")");
                sb.I(sb =>
                {
                    sb.AL(@".AsInsert(_columns, _values);");
                });
            });
        }
        private static void WriteExistsUniqueKey(this SourceBuilder sb, Entity entity)
        {
            var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_");

            foreach (var key in entity.UniqueKeys)
            {
                var fields            = key.FieldReferences.Select(fr => fr.ResolvedField);
                var methodNamePostfix = string.Join("_", fields.Select(f => f.Name));
                var parameters        = fields.Select(f =>
                {
                    return(Type: f.Type.ToCSharp(), Name: f.Name.ToCamelCase());
                });
                sb.PublicStaticMethod("async global::System.Threading.Tasks.Task<bool>", $"Exists_{entityName}_UK_{methodNamePostfix}", parameters.PrefixWithQueryFactory(), sb =>
                {
                    var arguments = string.Join(", ", parameters.Select(p => p.Name));
                    sb.AL($"var _rows = await _db.GetAsync<int>(global::Allvis.Kaylee.Generated.SqlKata.Queries.Exists_{entityName}_UK_{methodNamePostfix}({arguments})).ConfigureAwait(false);");
                    sb.AL("return _rows.Any();");
                });
            }
        }
Exemple #9
0
        private static void WriteGetUniqueKey(this SourceBuilder sb, Entity entity)
        {
            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey().ToList();
            var allFields      = fullPrimaryKey.Select(fr => fr.ResolvedField).Concat(entity.Fields).Distinct().ToList();

            foreach (var key in entity.UniqueKeys)
            {
                var fields            = key.FieldReferences.Select(fr => fr.ResolvedField);
                var methodNamePostfix = string.Join("_", fields.Select(f => f.Name));
                var parameters        = fields.Select(f =>
                {
                    return(f.Type.ToCSharp(), f.Name.ToCamelCase());
                });
                sb.PublicStaticMethod("global::SqlKata.Query", $"Get_{entityName}_UK_{methodNamePostfix}", parameters, sb =>
                {
                    var viewName = entity.GetViewName();
                    sb.AL($@"return new global::SqlKata.Query(""{viewName}"")");
                    sb.I(sb =>
                    {
                        foreach (var field in fields)
                        {
                            var fieldName     = field.Name;
                            var parameterName = fieldName.ToCamelCase();
                            sb.AL($@".Where(""{fieldName}"", {parameterName})");
                        }

                        allFields.ForEach((field, last) =>
                        {
                            var fieldName = field.Name;
                            var semicolon = last ? ";" : "";
                            sb.AL($@".Select(""{fieldName}""){semicolon}");
                        });
                    });
                });
            }
        }
Exemple #10
0
        private static void WriteCount(this SourceBuilder sb, Entity entity)
        {
            var entityName     = entity.DisplayName.Replace(".", "").Replace("::", "_");
            var fullPrimaryKey = entity.GetFullPrimaryKey().ToList();

            var fields = fullPrimaryKey.Take(fullPrimaryKey.Count - 1).Select(fr =>
            {
                var field = fr.ResolvedField;
                return(field.Type.ToCSharp(), field.Name, field.Name.ToCamelCase());
            }).ToList();

            var stackedGroupable = new Stack <(string Type, string Name, string NameCamel)>(fields);
            var i = stackedGroupable.Count;

            while (i >= 0)
            {
                var stackedPivotable = new Stack <(string Type, string Name, string NameCamel)>(fields.Take(i));
                var j = stackedPivotable.Count;
                while (j >= 0)
                {
                    if (j == 0)
                    {
                        var parameters = stackedGroupable.Select(p => (p.Type, p.NameCamel)).Reverse();
                        var arguments  = string.Join(", ", stackedGroupable.Reverse().Select(p => p.NameCamel));
                        sb.PublicStaticMethod("global::SqlKata.Query", $"Count_{entityName}", parameters, sb =>
                        {
                            var viewName = entity.GetViewName();
                            sb.AL($@"return new global::SqlKata.Query(""{viewName}"")");
                            sb.I(sb =>
                            {
                                foreach (var field in stackedGroupable.Reverse())
                                {
                                    var fieldName     = field.Name;
                                    var parameterName = field.NameCamel;
                                    sb.AL($@".Where(""{fieldName}"", {parameterName})");
                                }
                                foreach (var field in stackedPivotable.Reverse())
                                {
                                    var fieldName = field.Name;
                                    sb.AL($@".GroupBy(""{fieldName}"")");
                                }
                                foreach (var field in stackedPivotable.Reverse())
                                {
                                    var fieldName = field.Name;
                                    sb.AL($@".Select(""{fieldName}"")");
                                }
                                sb.AL(".AsCount();");
                            });
                        });
                    }
                    else if (i > 0)
                    {
                        var parameters  = stackedPivotable.Skip(1).Select(p => (p.Type, p.NameCamel)).Reverse();
                        var groupByName = string.Join("_", stackedGroupable.Reverse().Select(p => p.Name));
                        sb.PublicStaticMethod("global::SqlKata.Query", $"Count_{entityName}_GroupBy_{groupByName}", parameters, sb =>
                        {
                            var viewName = entity.GetViewName();
                            sb.AL($@"return new global::SqlKata.Query(""{viewName}"")");
                            sb.I(sb =>
                            {
                                foreach (var field in stackedPivotable.Skip(1).Reverse())
                                {
                                    var fieldName     = field.Name;
                                    var parameterName = field.NameCamel;
                                    sb.AL($@".Where(""{fieldName}"", {parameterName})");
                                }
                                foreach (var field in stackedGroupable.Reverse())
                                {
                                    var fieldName = field.Name;
                                    sb.AL($@".GroupBy(""{fieldName}"")");
                                }
                                foreach (var field in stackedGroupable.Reverse())
                                {
                                    var fieldName = field.Name;
                                    sb.AL($@".Select(""{fieldName}"")");
                                }
                                sb.AL(@".SelectRaw(""COUNT(*) as Count"");");
                            });
                        });
                    }
                    if (stackedPivotable.Count > 0)
                    {
                        stackedPivotable.Pop();
                    }
                    j--;
                }
                if (stackedGroupable.Count > 0)
                {
                    stackedGroupable.Pop();
                }
                i--;
            }
        }