Exemplo n.º 1
0
        public bool Matches(ActualIndex index)
        {
            if (!index.Name.EqualsIgnoreCase(IndexName))
            {
                return(false);
            }

            var actual = index.DDL;

            if (Method == IndexMethod.btree)
            {
                actual = actual.Replace("USING btree", "");
            }

            _columns.Each(col =>
            {
                actual = actual.Replace($"({col})", $"(\"{col}\")");
            });

            if (!actual.Contains(_parent.Table.QualifiedName))
            {
                actual = actual.Replace("ON " + _parent.Table.Name, "ON " + _parent.Table.QualifiedName);
            }

            actual = actual.Replace("  ", " ") + ";";

            return(ToDDL().EqualsIgnoreCase(actual));
        }
Exemplo n.º 2
0
        // TODO -- Really need to add some QueryHandlers for all this stuff to eliminate the duplication
        public SchemaObjects FindSchemaObjects(DocumentMapping mapping)
        {
            using (var connection = new ManagedConnection(_factory))
            {
                return(connection.Execute(cmd =>
                {
                    cmd.CommandText = SchemaObjectsSQL;
                    cmd.AddParameter("schema", mapping.Table.Schema);
                    cmd.AddParameter("table_name", mapping.Table.Name);
                    cmd.AddParameter("function", mapping.UpsertFunction.Name);
                    cmd.AddParameter("qualified_name", mapping.Table.OwnerName);

                    var reader = cmd.ExecuteReader();

                    var columns = new List <TableColumn>();
                    while (reader.Read())
                    {
                        var column = new TableColumn(reader.GetString(0), reader.GetString(1));
                        columns.Add(column);
                    }

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

                    reader.NextResult();
                    var upsertDefinition = reader.Read() ? reader.GetString(0) : null;

                    var indices = new List <ActualIndex>();
                    reader.NextResult();
                    while (reader.Read())
                    {
                        var index = new ActualIndex(mapping.Table, reader.GetString(3),
                                                    reader.GetString(4));

                        indices.Add(index);
                    }

                    var table = columns.Any() ? new TableDefinition(mapping.Table, pks.FirstOrDefault(), columns) : null;

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

                    FunctionBody functionBody = upsertDefinition.IsEmpty() ? null : new FunctionBody(mapping.UpsertFunction, drops.ToArray(), upsertDefinition);
                    return new SchemaObjects(mapping.DocumentType, table, indices.ToArray(), functionBody);
                }));
            }
        }
Exemplo n.º 3
0
        public SchemaObjects(Type documentType, TableDefinition table, ActualIndex[] actualIndices, FunctionBody function)
        {
            DocumentType = documentType;
            Table = table;
            Function = function;

            actualIndices.Each(x => ActualIndices.Add(x.Name, x));



        }
Exemplo n.º 4
0
        public bool Matches(ActualIndex index)
        {
            if (!index.Name.EqualsIgnoreCase(IndexName))
            {
                return(false);
            }

            var actual = index.DDL;

            if (Method == IndexMethod.btree)
            {
                actual = actual.Replace("USING btree", "");
            }

            var columnsGroupPattern = "(?<columns>.*(?:(?:[\\w.]+)\\s?(?:[\\w_]+).*))";
            var columnsMatchPattern = $"\\({columnsGroupPattern}\\)";

            if (Expression.IsNotEmpty())
            {
                var escapedExpression = Regex.Escape(Expression);

                columnsMatchPattern = $"\\({escapedExpression.Replace("\\?", columnsGroupPattern)}\\)";
            }

            var match = Regex.Match(actual, columnsMatchPattern);

            if (match.Success)
            {
                var columns = match.Groups["columns"].Value;
                _columns.Each(col =>
                {
                    columns = Regex.Replace(columns, $"({col})\\s?([\\w_]+)?", "\"$1\"$2");
                });

                var replacement = Expression.IsEmpty() ?
                                  $"({columns.Trim()})" :
                                  $"({Expression.Replace("?", columns.Trim())})";

                actual = Regex.Replace(actual, columnsMatchPattern, replacement);
            }

            if (!actual.Contains(_parent.Table.QualifiedName))
            {
                actual = actual.Replace("ON " + _parent.Table.Name, "ON " + _parent.Table.QualifiedName);
            }

            actual = actual.Replace("  ", " ") + ";";

            // if column name being a PostgreSQL keyword, column is already wrapped with double quotes
            // above regex and replace logic will result in additional double quotes, remove the same
            actual = actual.Replace("\"\"", "\"");

            return(ToDDL().EqualsIgnoreCase(actual));
        }
Exemplo n.º 5
0
        public SchemaObjects(Type documentType, TableDefinition table, ActualIndex[] actualIndices, string upsertFunction, List<string> drops)
        {
            DocumentType = documentType;
            Table = table;

            actualIndices.Each(x => ActualIndices.Add(x.Name, x));

            UpsertFunction = upsertFunction?.CanonicizeSql();

            FunctionDropStatements = drops;
        }
Exemplo n.º 6
0
        public bool Matches(ActualIndex index)
        {
            if (!index.Name.EqualsIgnoreCase(IndexName))
            {
                return(false);
            }

            var actual = index.DDL;

            if (Method == IndexMethod.btree)
            {
                actual = actual.Replace("USING btree", "");
            }

            var columnsGroupPattern = "(?<columns>.*(?:(?:[\\w.]+)\\s?(?:[\\w_]+).*))";
            var columnsMatchPattern = $"\\({columnsGroupPattern}\\)";

            if (Expression.IsNotEmpty())
            {
                var escapedExpression = Regex.Escape(Expression);

                columnsMatchPattern = $"\\({escapedExpression.Replace("\\?", columnsGroupPattern)}\\)";
            }

            var match = Regex.Match(actual, columnsMatchPattern);

            if (match.Success)
            {
                var columns = match.Groups["columns"].Value;
                _columns.Each(col =>
                {
                    columns = Regex.Replace(columns, $"({col})\\s?([\\w_]+)?", "\"$1\"$2");
                });

                var replacement = Expression.IsEmpty() ?
                                  $"({columns.Trim()})" :
                                  $"({Expression.Replace("?", columns.Trim())})";

                actual = Regex.Replace(actual, columnsMatchPattern, replacement);
            }

            if (!actual.Contains(_parent.Table.QualifiedName))
            {
                actual = actual.Replace("ON " + _parent.Table.Name, "ON " + _parent.Table.QualifiedName);
            }

            actual = actual.Replace("  ", " ") + ";";

            return(ToDDL().EqualsIgnoreCase(actual));
        }
Exemplo n.º 7
0
        public bool Matches(ActualIndex index)
        {
            if (!index.Name.EqualsIgnoreCase(IndexName))
            {
                return(false);
            }

            var actual = index.DDL;

            if (Method == IndexMethod.btree)
            {
                actual = actual.Replace("USING btree", "");
            }

            string columnsMatchPattern = "\\((?<columns>.*(?:(?:[\\w.]+)\\s?(?:[\\w_]+).*))\\)";
            var    match   = Regex.Match(actual, columnsMatchPattern);
            var    replace = string.Empty;

            if (match.Success)
            {
                var columns = match.Groups["columns"].Value;
                _columns.Each(col =>
                {
                    columns = Regex.Replace(columns, $"({col})\\s?([\\w_]+)?", "\"$1\" $2");
                });

                actual = Regex.Replace(actual, columnsMatchPattern, $"({columns.Trim()})");
            }

            if (!actual.Contains(_parent.Table.QualifiedName))
            {
                actual = actual.Replace("ON " + _parent.Table.Name, "ON " + _parent.Table.QualifiedName);
            }

            actual = actual.Replace("  ", " ") + ";";

            return(ToDDL().EqualsIgnoreCase(actual));
        }
Exemplo n.º 8
0
        public bool Matches(ActualIndex index)
        {
            if (!index.Name.EqualsIgnoreCase(IndexName)) return false;

            var actual = index.DDL;
            if (Method == IndexMethod.btree)
            {
                actual = actual.Replace("USING btree", "");
            }

            _columns.Each(col =>
            {
                actual = actual.Replace($"({col})", $"(\"{col}\")");
            });

            if (!actual.Contains(_parent.Table.QualifiedName))
            {
                actual = actual.Replace("ON " + _parent.Table.Name, "ON " + _parent.Table.QualifiedName);
            }

            actual = actual.Replace("  ", " ") + ";";

            return ToDDL().EqualsIgnoreCase(actual);
        }
Exemplo n.º 9
0
 public bool Matches(ActualIndex index)
 {
     return index != null;
 }
Exemplo n.º 10
0
 protected bool Equals(ActualIndex other)
 {
     return(string.Equals(Name, other.Name) && string.Equals(DDL, other.DDL));
 }
Exemplo n.º 11
0
        // TODO -- Really need to add some QueryHandlers for all this stuff to eliminate the duplication
        public SchemaObjects FindSchemaObjects(DocumentMapping mapping)
        {
            using (var connection = new ManagedConnection(_factory))
            {
                return connection.Execute(cmd =>
                {
                    cmd.CommandText = SchemaObjectsSQL;
                    cmd.AddParameter("schema", mapping.Table.Schema);
                    cmd.AddParameter("table_name", mapping.Table.Name);
                    cmd.AddParameter("function", mapping.UpsertFunction.Name);
                    cmd.AddParameter("qualified_name", mapping.Table.OwnerName);

                    var reader = cmd.ExecuteReader();

                    var columns = new List<TableColumn>();
                    while (reader.Read())
                    {
                        var column = new TableColumn(reader.GetString(0), reader.GetString(1));
                        columns.Add(column);
                    }

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

                    reader.NextResult();
                    var upsertDefinition = reader.Read() ? reader.GetString(0) : null;

                    var indices = new List<ActualIndex>();
                    reader.NextResult();
                    while (reader.Read())
                    {
                        var index = new ActualIndex(mapping.Table, reader.GetString(3),
                            reader.GetString(4));

                        indices.Add(index);
                    }

                    var table = columns.Any() ? new TableDefinition(mapping.Table, pks.FirstOrDefault(), columns) : null;

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

                    FunctionBody functionBody = upsertDefinition.IsEmpty() ? null : new FunctionBody(mapping.UpsertFunction, drops.ToArray(), upsertDefinition);
                    return new SchemaObjects(mapping.DocumentType, table, indices.ToArray(), functionBody);
                });
            }
        }
Exemplo n.º 12
0
 public bool Matches(ActualIndex index)
 {
     return(index != null);
 }