Esempio n. 1
0
        public DocumentStorage(ISerializer serializer, DocumentMapping mapping, bool useCharBufferPooling)
        {
            _serializer           = serializer;
            _mapping              = mapping;
            _useCharBufferPooling = useCharBufferPooling;
            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 ?";
            }
        }
Esempio n. 2
0
        public ComputedIndex(DocumentMapping mapping, MemberInfo[][] members)
        {
            _members = members;

            var mebersLocator = members
                                .Select(m =>
            {
                var sql = mapping.FieldFor(m).SqlLocator.Replace("d.", "");
                switch (Casing)
                {
                case Casings.Upper:
                    return($" upper({sql})");

                case Casings.Lower:
                    return($" lower({sql})");

                default:
                    return($" ({sql})");
                }
            })
                                .Join(",");

            _locator = $" {mebersLocator}";

            _table = mapping.Table;
        }
Esempio n. 3
0
 public FullTextIndex(DocumentMapping mapping, string regConfig = null, string dataConfig = null, string indexName = null)
 {
     _table = mapping.Table;
     RegConfig = regConfig;
     DataConfig = dataConfig;
     IndexName = indexName;
 }
Esempio n. 4
0
        public NgramIndex(DocumentMapping mapping, string dataConfig = null, string indexName = null)
        {
            _table     = mapping.TableName;
            DataConfig = dataConfig;
            _indexName = indexName;

            Method = IndexMethod.gin;
        }
Esempio n. 5
0
        public ComputedIndex(DocumentMapping mapping, MemberInfo[] members)
        {
            _members = members;
            var field = mapping.FieldFor(members);

            _locator = field.SqlLocator.Replace("d.", "");
            _table   = mapping.Table;
        }
Esempio n. 6
0
        public SystemFunction(string schema, string functionName, string args)
            : base(new DbObjectName(schema, functionName))
        {
            _args     = args;
            _function = new DbObjectName(schema, functionName);
            _dropSql  = $"drop function if exists {schema}.{functionName}({args}) cascade;";

            Name = functionName;
        }
Esempio n. 7
0
        public FunctionBody DefinitionForFunction(DbObjectName 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 = _tenant.CreateConnection())
            {
                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();
                }
            }
        }
Esempio n. 8
0
        public DocumentStorage(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";

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

            if (mapping.TenancyStyle == TenancyStyle.Conjoined)
            {
                _loaderSql    += $" and {TenantWhereFragment.Filter}";
                _loadArraySql += $" and {TenantWhereFragment.Filter}";
            }


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

            _sprocWriter = buildSprocWriter(mapping);


            _upsertName = mapping.UpsertFunction;

            if (mapping.DeleteStyle == DeleteStyle.Remove)
            {
                DeleteByIdSql    = $"delete from {_mapping.Table.QualifiedName} as d where id = ?";
                DeleteByWhereSql = $"delete from {_mapping.Table.QualifiedName} as d where ?";
            }
            else
            {
                DeleteByIdSql    = $"update {_mapping.Table.QualifiedName} as d set {DocumentMapping.DeletedColumn} = True, {DocumentMapping.DeletedAtColumn} = now() where id = ?";
                DeleteByWhereSql = $"update {_mapping.Table.QualifiedName} as d set {DocumentMapping.DeletedColumn} = True, {DocumentMapping.DeletedAtColumn} = now() where ?";
            }

            if (mapping.VersionMember is FieldInfo)
            {
                _setVersion = LambdaBuilder.SetField <T, Guid>(mapping.VersionMember.As <FieldInfo>());
            }

            if (mapping.VersionMember is PropertyInfo)
            {
                _setVersion = LambdaBuilder.SetProperty <T, Guid>(mapping.VersionMember.As <PropertyInfo>());
            }
        }
Esempio n. 9
0
        public IEnumerable <ActualIndex> AllIndexes()
        {
            var sql = @"
SELECT
  U.usename                AS user_name,
  ns.nspname               AS schema_name,
  pg_catalog.textin(pg_catalog.regclassout(idx.indrelid :: REGCLASS)) AS table_name,
  i.relname                AS index_name,
  pg_get_indexdef(i.oid) as ddl,
  idx.indisunique          AS is_unique,
  idx.indisprimary         AS is_primary,
  am.amname                AS index_type,
  idx.indkey,
       ARRAY(
           SELECT pg_get_indexdef(idx.indexrelid, k + 1, TRUE)
           FROM
             generate_subscripts(idx.indkey, 1) AS k
           ORDER BY k
       ) AS index_keys,
  (idx.indexprs IS NOT NULL) OR (idx.indkey::int[] @> array[0]) AS is_functional,
  idx.indpred IS NOT NULL AS is_partial
FROM pg_index AS idx
  JOIN pg_class AS i
    ON i.oid = idx.indexrelid
  JOIN pg_am AS am
    ON i.relam = am.oid
  JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
  JOIN pg_user AS U ON i.relowner = U.usesysid
WHERE
ns.nspname = ANY(?) AND
NOT nspname LIKE 'pg%' AND i.relname like 'mt_%'; -- Excluding system table
";

            Func <DbDataReader, ActualIndex> transform =
                r => new ActualIndex(DbObjectName.Parse(r.GetString(2)), r.GetString(3), r.GetString(4));

            var schemaNames = _features.AllSchemaNames();

            return(_tenant.Fetch(sql, transform, (object)schemaNames));
        }
Esempio n. 10
0
 public string ToComputedIndex(DbObjectName tableName)
 {
     return($"CREATE INDEX {tableName.Name}_{MemberName.ToTableAlias()} ON {tableName.QualifiedName} (({SqlLocator}));");
 }
Esempio n. 11
0
 public ActualForeignKey(DbObjectName table, string name, string ddl)
 {
     Table = table;
     Name  = name;
     DDL   = ddl;
 }
Esempio n. 12
0
        public static void Drop(this IDDLRunner runner, object subject, DbObjectName table)
        {
            var sql = $"drop table if exists {table.QualifiedName} cascade;";

            runner.Apply(subject, sql);
        }
Esempio n. 13
0
 protected bool Equals(DbObjectName other)
 {
     return(GetType() == other.GetType() && string.Equals(QualifiedName, other.QualifiedName, StringComparison.OrdinalIgnoreCase));
 }
Esempio n. 14
0
 public FullTextIndex(DocumentMapping parent, string config)
 {
     _table     = parent.Table;
     _config    = config;
     _indexName = $"{_table.Name}_idx_fts";
 }
Esempio n. 15
0
        public bool TableExists(DbObjectName table)
        {
            var schemaTables = SchemaTables();

            return(schemaTables.Contains(table));
        }
Esempio n. 16
0
        public static void RemoveColumn(this IDDLRunner runner, object subject, DbObjectName table, string columnName)
        {
            var sql = $"alter table if exists {table.QualifiedName} drop column if exists {columnName};";

            runner.Apply(subject, sql);
        }
Esempio n. 17
0
 public IEnumerable <ActualIndex> IndexesFor(DbObjectName table)
 {
     return(AllIndexes().Where(x => x.Table.Equals(table)).ToArray());
 }
Esempio n. 18
0
 static ActualIndex Transform(DbDataReader r) => new ActualIndex(DbObjectName.Parse(r.GetString(2)), r.GetString(3), r.GetString(4));