Ejemplo n.º 1
0
        public SystemFunction(StoreOptions options, string functionName)
        {
            _function = new FunctionName(options.DatabaseSchemaName, functionName);
            _dropSql  = $"drop function if exists {options.DatabaseSchemaName}.mt_immutable_timestamp cascade";

            Name = functionName;
        }
Ejemplo n.º 2
0
        public UpsertFunction(DocumentMapping mapping)
        {
            if (mapping == null) throw new ArgumentNullException(nameof(mapping));

            _functionName = mapping.UpsertFunction;
            _tableName = mapping.Table;
            _primaryKeyConstraintName = "pk_" + mapping.Table.Name;

            var idType = mapping.IdMember.GetMemberType();
            var pgIdType = TypeMappings.GetPgType(idType);

            Arguments.Add(new UpsertArgument
            {
                Arg = "docId",
                PostgresType = pgIdType,
                Column = "id",
                Members = new[] {mapping.IdMember}
            });
            Arguments.Add(new UpsertArgument
            {
                Arg = "doc",
                PostgresType = "JSONB",
                DbType = NpgsqlDbType.Jsonb,
                Column = "data",
                BulkInsertPattern = "writer.Write(serializer.ToJson(x), NpgsqlDbType.Jsonb);",
                BatchUpdatePattern = "*"
            });
        }
Ejemplo n.º 3
0
        public SystemFunction(StoreOptions options, string functionName)
        {
            _function = new FunctionName(options.DatabaseSchemaName, functionName);
            _dropSql = $"drop function if exists {options.DatabaseSchemaName}.mt_immutable_timestamp cascade";

            Name = functionName;
        }
Ejemplo n.º 4
0
        public Resolver(ISerializer serializer, DocumentMapping mapping)
        {
            _serializer = serializer;
            _mapping    = mapping;
            IdType      = TypeMappings.ToDbType(mapping.IdMember.GetMemberType());


            _loaderSql =
                $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = :id";
            _deleteSql    = $"delete from {_mapping.Table.QualifiedName} where id = :id";
            _loadArraySql =
                $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = ANY(:ids)";


            _identity = LambdaBuilder.Getter <T, object>(mapping.IdMember);

            _sprocWriter = buildSprocWriter(mapping);


            _upsertName = mapping.UpsertFunction;

            if (mapping.DeleteStyle == DeleteStyle.Remove)
            {
                DeleteByIdSql    = $"delete from {_mapping.Table.QualifiedName} where id = ?";
                DeleteByWhereSql = $"delete from {_mapping.Table.QualifiedName} as d where ?";
            }
            else
            {
                DeleteByIdSql    = $"update {_mapping.Table.QualifiedName} set {DocumentMapping.DeletedColumn} = True, {DocumentMapping.DeletedAtColumn} = now() where id = ?";
                DeleteByWhereSql = $"update {_mapping.Table.QualifiedName} as d set {DocumentMapping.DeletedColumn} = True, {DocumentMapping.DeletedAtColumn} = now() where ?";
            }
        }
Ejemplo n.º 5
0
        public UpsertFunction(DocumentMapping mapping)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            _functionName             = mapping.UpsertFunction;
            _tableName                = mapping.Table;
            _primaryKeyConstraintName = "pk_" + mapping.Table.Name;

            var idType   = mapping.IdMember.GetMemberType();
            var pgIdType = TypeMappings.GetPgType(idType);

            Arguments.Add(new UpsertArgument
            {
                Arg          = "docId",
                PostgresType = pgIdType,
                Column       = "id",
                Members      = new[] { mapping.IdMember }
            });

            Arguments.Add(new DocJsonBodyArgument());

            Arguments.AddRange(mapping.DuplicatedFields.Select(x => x.UpsertArgument));

            Arguments.Add(new VersionArgument());

            Arguments.Add(new DotNetTypeArgument());

            if (mapping.IsHierarchy())
            {
                Arguments.Add(new DocTypeArgument());
            }
        }
Ejemplo n.º 6
0
        public SprocCall(BatchCommand parent, FunctionName function)
        {
            if (parent == null) throw new ArgumentNullException(nameof(parent));
            if (function == null) throw new ArgumentNullException(nameof(function));

            _parent = parent;
            _function = function;
        }
Ejemplo n.º 7
0
        public TransformFunction(StoreOptions options, string name, string body)
        {
            _options = options;
            Name = name;
            Body = body;

            Function = new FunctionName(_options.DatabaseSchemaName, $"{Prefix}{Name.ToLower().Replace(".", "_")}");
        }
Ejemplo n.º 8
0
        public SystemFunction(StoreOptions options, string functionName, string args)
        {
            _args = args;
            _function = new FunctionName(options.DatabaseSchemaName, functionName);
            _dropSql = $"drop function if exists {options.DatabaseSchemaName}.{functionName}({args}) cascade";

            Name = functionName;
        }
Ejemplo n.º 9
0
        public SystemFunction(StoreOptions options, string functionName, string args)
        {
            _args     = args;
            _function = new FunctionName(options.DatabaseSchemaName, functionName);
            _dropSql  = $"drop function if exists {options.DatabaseSchemaName}.{functionName}({args}) cascade";

            Name = functionName;
        }
Ejemplo n.º 10
0
        public SprocCall Sproc(FunctionName function, ICallback callback = null)
        {
            if (function == null) throw new ArgumentNullException(nameof(function));

            var call = new SprocCall(this, function);
            AddCall(call, callback);

            return call;
        }
        public void should_stick_the_patch_doc_function_in_the_right_schema()
        {
            StoreOptions(_ => _.DatabaseSchemaName = "other");

            theStore.Schema.ApplyAllConfiguredChangesToDatabase();

            var expected = new FunctionName("other", "mt_transform_patch_doc");
            theStore.Schema.DbObjects.SchemaFunctionNames().Contains(expected).ShouldBeTrue();
        }
Ejemplo n.º 12
0
        public string DefinitionForFunction(FunctionName function)
        {
            var sql = @"
SELECT pg_get_functiondef(pg_proc.oid) 
FROM pg_proc JOIN pg_namespace as ns ON pg_proc.pronamespace = ns.oid WHERE ns.nspname = ? and proname = ?;
";

            return(_factory.Fetch(sql, r => r.GetString(0), function.Schema, function.Name).FirstOrDefault());
        }
Ejemplo n.º 13
0
        public string DefinitionForFunction(FunctionName function)
        {
            var sql = @"
            SELECT pg_get_functiondef(pg_proc.oid)
            FROM pg_proc JOIN pg_namespace as ns ON pg_proc.pronamespace = ns.oid WHERE ns.nspname = ? and proname = ?;
            ";

            return _factory.Fetch(sql, r => r.GetString(0), function.Schema, function.Name).FirstOrDefault();
        }
Ejemplo n.º 14
0
        public FunctionBody DefinitionForFunction(FunctionName function)
        {
            var sql = @"
SELECT pg_get_functiondef(pg_proc.oid) 
FROM pg_proc JOIN pg_namespace as ns ON pg_proc.pronamespace = ns.oid WHERE ns.nspname = :schema and proname = :function;
SELECT format('DROP FUNCTION %s.%s(%s);'
             ,n.nspname
             ,p.proname
             ,pg_get_function_identity_arguments(p.oid))
FROM   pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace 
WHERE  p.proname = :function
AND    n.nspname = :schema;
";

            using (var conn = _factory.Create())
            {
                conn.Open();


                try
                {
                    var cmd = conn.CreateCommand().Sql(sql)
                              .With("schema", function.Schema)
                              .With("function", function.Name);

                    using (var reader = cmd.ExecuteReader())
                    {
                        if (!reader.Read())
                        {
                            return(null);
                        }

                        var definition = reader.GetString(0);

                        reader.NextResult();

                        var drops = new List <string>();
                        while (reader.Read())
                        {
                            drops.Add(reader.GetString(0));
                        }

                        return(new FunctionBody(function, drops.ToArray(), definition));
                    }
                }
                finally
                {
                    conn.Close();
                }
            }
        }
Ejemplo n.º 15
0
        public Resolver(ISerializer serializer, DocumentMapping mapping)
        {
            _serializer = serializer;
            _mapping    = mapping;
            IdType      = TypeMappings.ToDbType(mapping.IdMember.GetMemberType());


            _loaderSql =
                $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = :id";
            _deleteSql    = $"delete from {_mapping.Table.QualifiedName} where id = :id";
            _loadArraySql =
                $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = ANY(:ids)";


            _identity = LambdaBuilder.Getter <T, object>(mapping.IdMember);

            _sprocWriter = buildSprocWriter(mapping);


            _upsertName = mapping.UpsertFunction;
        }
Ejemplo n.º 16
0
        public UpsertFunction(DocumentMapping mapping)
        {
            if (mapping == null) throw new ArgumentNullException(nameof(mapping));

            _functionName = mapping.UpsertFunction;
            _tableName = mapping.Table;
            _primaryKeyConstraintName = "pk_" + mapping.Table.Name;
                
            var idType = mapping.IdMember.GetMemberType();
            var pgIdType = TypeMappings.GetPgType(idType);

            Arguments.Add(new UpsertArgument
            {
                Arg = "docId",
                PostgresType = pgIdType,
                Column = "id",
                Members = new[] {mapping.IdMember}
            });

            Arguments.Add(new DocJsonBodyArgument());

            Arguments.AddRange(mapping.DuplicatedFields.Select(x => x.UpsertArgument));

            Arguments.Add(new VersionArgument());

            Arguments.Add(new DotNetTypeArgument());

            if (mapping.IsHierarchy())
            {
                Arguments.Add(new DocTypeArgument());
            }

            if (mapping.UseOptimisticConcurrency)
            {
                Arguments.Add(new CurrentVersionArgument());
            }

        }
Ejemplo n.º 17
0
        public EventStreamAppender(EventGraph graph)
        {
            _graph = graph;

            AppendEventFunction = new FunctionName(_graph.DatabaseSchemaName, "mt_append_event");
        }
Ejemplo n.º 18
0
 public FunctionBody(FunctionName function, string[] dropStatements, string body)
 {
     Function = function;
     DropStatements = dropStatements;
     Body = body;
 }
Ejemplo n.º 19
0
 public EventProgressWrite(EventGraph events, string key, long number)
 {
     _sproc = new FunctionName(events.DatabaseSchemaName, "mt_mark_event_progression");
     _key = key;
     _number = number;
 }
Ejemplo n.º 20
0
        public SprocCall Sproc(FunctionName function, ICallback callback = null)
        {
            if (function == null) throw new ArgumentNullException(nameof(function));

            return Current().Sproc(function, callback);
        }
Ejemplo n.º 21
0
        public BatchCommand.SprocCall Sproc(FunctionName function)
        {
            if (function == null) throw new ArgumentNullException(nameof(function));

            return Current().Sproc(function);
        }
Ejemplo n.º 22
0
        public FunctionBody DefinitionForFunction(FunctionName function)
        {
            var sql = @"
SELECT pg_get_functiondef(pg_proc.oid) 
FROM pg_proc JOIN pg_namespace as ns ON pg_proc.pronamespace = ns.oid WHERE ns.nspname = :schema and proname = :function;
SELECT format('DROP FUNCTION %s.%s(%s);'
             ,n.nspname
             ,p.proname
             ,pg_get_function_identity_arguments(p.oid))
FROM   pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace 
WHERE  p.proname = :function
AND    n.nspname = :schema;
";

            using (var conn = _factory.Create())
            {
                conn.Open();


                try
                {
                    var cmd = conn.CreateCommand().Sql(sql)
                        .With("schema", function.Schema)
                        .With("function", function.Name);

                    using (var reader = cmd.ExecuteReader())
                    {
                        if (!reader.Read()) return null;
   
                        var definition = reader.GetString(0);

                        reader.NextResult();

                        var drops = new List<string>();
                        while (reader.Read())
                        {
                            drops.Add(reader.GetString(0));
                        }

                        return new FunctionBody(function, drops.ToArray(), definition);
                    }
                }
                finally
                {
                    conn.Close();
                }
            }


        }
Ejemplo n.º 23
0
 public FunctionBody(FunctionName function, string[] dropStatements, string body)
 {
     Function       = function;
     DropStatements = dropStatements;
     Body           = body;
 }
Ejemplo n.º 24
0
        public SprocCall Sproc(FunctionName function)
        {
            if (function == null) throw new ArgumentNullException(nameof(function));

            var call = new SprocCall(this, function);
            _calls.Add(call);

            return call;
        }