Example #1
0
        public TableDefinition(TableName table, TableColumn primaryKey)
        {
            if (table == null) throw new ArgumentNullException(nameof(table));
            if (primaryKey == null) throw new ArgumentNullException(nameof(primaryKey));

            Table = table;
            PrimaryKey = primaryKey;
        }
Example #2
0
        public TableDefinition(string name, TableColumn primaryKey)
        {
            Name = name;
            PrimaryKey = primaryKey;
            PrimaryKey.Directive = "CONSTRAINT pk_{0} PRIMARY KEY".ToFormat(name);

            Columns.Add(primaryKey);
        }
Example #3
0
        public TableDefinition(string qualifiedName, string name, TableColumn primaryKey)
        {
            if (string.IsNullOrEmpty(qualifiedName)) throw new ArgumentOutOfRangeException(nameof(qualifiedName));
            if (primaryKey == null) throw new ArgumentNullException(nameof(primaryKey));

            QualifiedName = qualifiedName.ToLower();
            Name = name;
            PrimaryKey = primaryKey;
        }
Example #4
0
 public void ReplaceOrAddColumn(string name, string type, string directive = null)
 {
     var column = new TableColumn(name, type) { Directive = directive };
     var columnIndex = Columns.ToList().FindIndex(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
     if (columnIndex >= 0)
     {
         Columns[columnIndex] = column;
     }
     else Columns.Add(column);
 }
Example #5
0
        public TableDefinition(string name, string pkName, IEnumerable<TableColumn> columns)
        {
            Name = name;
            Columns.AddRange(columns);

            PrimaryKey = columns.FirstOrDefault(x => x.Name == pkName);
            if (PrimaryKey != null)
            {
                PrimaryKey.Directive = "CONSTRAINT pk_{0} PRIMARY KEY".ToFormat(name);
            }
        }
Example #6
0
        public TableDefinition(TableName table, string pkName, IEnumerable<TableColumn> columns)
        {
            if (table == null) throw new ArgumentNullException(nameof(table));
            if (string.IsNullOrEmpty(pkName)) throw new ArgumentOutOfRangeException(nameof(pkName));

            Table = table;
            Columns.AddRange(columns);

            var primaryKey = Column(pkName);
            if (primaryKey == null) throw new InvalidOperationException($"Primary key {pkName} not found in columns.");
            PrimaryKey = primaryKey;
        }
Example #7
0
        public void not_matching_with_extra_columns()
        {
            var users = DocumentMapping.For<User>();
            var actual = users.ToTable(null);
            var tableColumn = new TableColumn("new", "varchar");
            actual.Columns.Add(tableColumn);

            var expected = users.ToTable(null);

            var diff = new TableDiff(expected, actual);

            diff.Matches.ShouldBeFalse();
            diff.Extras.Single().ShouldBe(tableColumn);
        }
Example #8
0
        public void not_matching_with_missing_columns()
        {
            var users = DocumentSchemaObjects.For<User>();
            var actual = users.StorageTable();

            var expected = users.StorageTable();
            var tableColumn = new TableColumn("new", "varchar");
            expected.Columns.Add(tableColumn);


            var diff = new TableDiff(expected, actual);
            diff.Matches.ShouldBeFalse();

            diff.Missing.Single().ShouldBe(tableColumn);
            diff.Extras.Any().ShouldBeFalse();
            diff.Different.Any().ShouldBeFalse();
        }
Example #9
0
 public TableDefinition(string name, TableColumn primaryKey)
 {
     Name = name;
     PrimaryKey = primaryKey;
 }
Example #10
0
 protected bool Equals(TableColumn other)
 {
     return string.Equals(Name, other.Name) && string.Equals(TypeMappings.ConvertSynonyms(Type), TypeMappings.ConvertSynonyms(other.Type));
 }
Example #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);
                });
            }
        }
Example #12
0
 protected bool Equals(TableColumn other)
 {
     return(string.Equals(Name, other.Name) && string.Equals(TypeMappings.ConvertSynonyms(Type), TypeMappings.ConvertSynonyms(other.Type)));
 }