Ejemplo n.º 1
0
        static List <SqlTable> ParseMetadata(DatabaseContext context)
        {
            var parser   = new SqlSchemaParser();
            var metadata = new List <SqlTable>();

            foreach (var fromQuery in context.FromQueries)
            {
                var stmt    = parser.ParseCreateTable(fromQuery);
                var columns = stmt.Columns;

                var foreignKeys = new List <SqlForeignKey>();
                foreach (var foreignKey in fromQuery.ForeignKeys)
                {
                    var foreignKeyStmt = parser.ParseAddForeignKey(fromQuery, foreignKey);
                    foreignKeys.Add(foreignKeyStmt.ForeignKey);
                }

                var indices = new List <SqlIndex>();
                foreach (var index in fromQuery.Indices)
                {
                    var indexStmt = parser.ParseAddIndex(fromQuery, index);
                    indices.Add(indexStmt.Index);
                }

                metadata.Add(new SqlTable()
                {
                    TableName   = fromQuery.TableName,
                    Columns     = columns,
                    ForeignKeys = foreignKeys,
                    Indices     = indices,
                });
            }

            return(metadata);
        }
Ejemplo n.º 2
0
        public void TestCreateDropForeignKey(Type runnerType)
        {
            var runner = (IQueryRunner)Provider.GetRequiredService(runnerType);

            ResetDb(runner);

            var sqlRunner = (SqlQueryRunner)runner;
            var parser    = new SqlSchemaParser();

            var foreignKey = new Schema.ForeignKey()
            {
                ReferenceTableType = typeof(Product),
                Name    = "FK_TEST",
                Columns = new List <string>()
                {
                    nameof(Unit.ProductId)
                },
                ReferenceColumns = new List <string>()
                {
                    nameof(Product.ProductId)
                }
            };

            // TODO: add default fk in test context, and switch the order here
            var addForeignKey = parser.ParseAddForeignKey(DB.Units, foreignKey);

            sqlRunner.ExecuteNonQuery(new List <SqlStatement>()
            {
                addForeignKey
            }, new List <KeyValuePair <string, object> >());

            var dropForeignKey = parser.ParseDropForeignKey(DB.Units, foreignKey);

            sqlRunner.ExecuteNonQuery(new List <SqlStatement>()
            {
                dropForeignKey
            }, new List <KeyValuePair <string, object> >());
        }