コード例 #1
0
ファイル: Table.cs プロジェクト: crossql/crossql
        public override string ToString()
        {
            var ddl = "";

            if (_updateTable)
            {
                foreach (var column in Columns)
                {
                    ddl += Environment.NewLine + string.Format(_dialect.UpdateTable, Name, column);
                }

                // Sqlite can't added constraints after the table has already been created.
                if (_dialect.GetType().Name != "SqliteDialiect")
                {
                    foreach (var constraint in Constraints)
                    {
                        ddl += Environment.NewLine + string.Format(_dialect.UpdateTable, Name, constraint);
                    }
                }
            }
            else
            {
                var columnsAndConstraints = string.Join(",", Columns);
                if (Constraints.Count > 0)
                {
                    columnsAndConstraints += "," + Environment.NewLine + string.Join(",", Constraints);
                }
                ddl += Environment.NewLine + string.Format(_dialect.CreateTable, Name, columnsAndConstraints);
            }

            return(ddl);
        }
コード例 #2
0
            public void Ignores_tables_in_other_schemas(IDialect dialect)
            {
                using (var database = BlankDatabaseFactory.MakeDatabase(dialect))
                {
                    // Arrange
                    database.Insert(new SchemaOther {
                        Name = "Other"
                    });
                    string name;
                    switch (dialect)
                    {
                    case SqlServer2012Dialect _:
                        name = "Other.SchemaOther";
                        break;

                    case PostgreSqlDialect _:
                        name = "other.schemaother";
                        break;

                    default:
                        throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name);
                    }

                    // Act
                    DataWiper.ClearAllData(database, new HashSet <string> {
                        name
                    });

                    // Assert
                    database.Count <SchemaOther>().Should().Be(1);
                }
            }
コード例 #3
0
            public void Ignores_specified_tables(IDialect dialect)
            {
                using (var database = BlankDatabaseFactory.MakeDatabase(dialect))
                {
                    // Arrange
                    database.Insert(new Dog {
                        Name = "Some Name 1", Age = 10
                    });

                    string tableName;
                    switch (dialect)
                    {
                    case SqlServer2012Dialect _:
                        tableName = "dbo.Dogs";
                        break;

                    case PostgreSqlDialect _:
                        tableName = "public.dog";
                        break;

                    default:
                        throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name);
                    }

                    // Act
                    DataWiper.ClearAllData(database, new HashSet <string> {
                        tableName
                    });

                    // Assert
                    database.Count <Dog>().Should().Be(1);
                }
            }
コード例 #4
0
        public static IDatabase MakeDatabase(IDialect dialect)
        {
            CleanUp();

            switch (dialect)
            {
            case PostgreSqlDialect _:
                return(OpenBlankDatabase(PostgresPool, cs => new NpgsqlConnection(cs), PeregrineConfig.Postgres));

            case SqlServer2012Dialect _:
                return(OpenBlankDatabase(SqlServer2012Pool, cs => new SqlConnection(cs), PeregrineConfig.SqlServer2012));

            default:
                throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name);
            }

            IDatabase OpenBlankDatabase(
                ObjectPool <string> pool,
                Func <string, IDbConnection> makeConnection,
                PeregrineConfig config)
            {
                var pooledConnectionString = pool.Acquire();

                try
                {
                    IDbConnection dbConnection = null;
                    try
                    {
                        dbConnection = makeConnection(pooledConnectionString.Item);
                        dbConnection.Open();

                        IDbConnection pooledConnection = new PooledConnection <IDbConnection>(pooledConnectionString, dbConnection);
                        var           database         = new DefaultDatabase(pooledConnection, config);

                        DataWiper.ClearAllData(database);
                        return(database);
                    }
                    catch
                    {
                        dbConnection?.Dispose();
                        throw;
                    }
                }
                catch
                {
                    pooledConnectionString?.Dispose();
                    throw;
                }
            }
        }
コード例 #5
0
            public void Errors_when_an_ignored_table_references_an_unignored_one(IDialect dialect)
            {
                using (var database = BlankDatabaseFactory.MakeDatabase(dialect))
                {
                    // Arrange
                    var dogId = database.Insert <int>(new Dog {
                        Name = "Some Name 1", Age = 10
                    });

                    database.Insert(new SimpleForeignKey {
                        Name = "Some Name 1", DogId = dogId
                    });

                    string tableName;
                    switch (dialect)
                    {
                    case SqlServer2012Dialect _:
                        tableName = "dbo.SimpleForeignKeys";
                        break;

                    case PostgreSqlDialect _:
                        tableName = "public.simple_foreign_key";
                        break;

                    default:
                        throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name);
                    }

                    // Act
                    Action act = () => DataWiper.ClearAllData(database, new HashSet <string> {
                        tableName
                    });

                    // Assert
                    act.Should().Throw <Exception>();

                    // Cleanup
                    database.DeleteAll <SimpleForeignKey>();
                }
            }