示例#1
0
        public async Task ExecuteWithReference()
        {
            var info = new SqlFunctionInfo(new ObjectName("count"), PrimitiveTypes.BigInt());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.VarChar()));

            var function = new SqlAggregateFunctionDelegate(info, accumulate => {
                SqlObject r;
                if (accumulate.IsFirst)
                {
                    r = accumulate.Current;
                }
                else
                {
                    var x = accumulate.Accumulation;
                    var y = accumulate.Current;

                    r = x.Add(y);
                }

                accumulate.SetResult(r);
            });

            var result = await function.ExecuteAsync(context, SqlExpression.Reference(ObjectName.Parse("a")));

            Assert.NotNull(result);
            Assert.NotNull(result.ReturnedValue);
            Assert.True(result.HasReturnedValue);

            Assert.IsType <SqlConstantExpression>(result.ReturnedValue);

            var value = ((SqlConstantExpression)result.ReturnedValue).Value;

            Assert.Equal(SqlObject.Integer(56), value);
        }
示例#2
0
        public static void VisitCast()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(SqlObject.String(new SqlString("223"))),
                                         PrimitiveTypes.BigInt());

            Visit(exp);
        }
示例#3
0
        protected override void BeforeLoop(ExecutionContext context)
        {
            context.Request.Context.DeclareVariable(IndexName, PrimitiveTypes.BigInt());
            context.Request.Context.SetVariable(IndexName, SqlExpression.Constant(Field.BigInt(0)));

            base.BeforeLoop(context);
        }
        public static void RemoveNamed()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            Assert.True(tableInfo.Columns.Remove("b"));
            Assert.Single(tableInfo.Columns);
        }
        public static void ClearColumns()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            tableInfo.Columns.Clear();

            Assert.Empty(tableInfo.Columns);
        }
        public static void MakeReadOnly()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            tableInfo = tableInfo.AsReadOnly();

            Assert.True(tableInfo.Columns.IsReadOnly);
        }
示例#7
0
        public void AlterTable_AddColumn()
        {
            var addColumn = new AddColumnAction(new SqlTableColumn("a", PrimitiveTypes.BigInt()));
            var statement = new AlterTableStatement(ObjectName.Parse("APP.test_table1"), addColumn);

            SerializeAndAssert(statement, (serialized, deserialized) => {
                Assert.IsNotNull(deserialized);
                Assert.IsNotNull(statement.TableName);
                Assert.IsNotNull(statement.Action);
                Assert.IsInstanceOf <AddColumnAction>(statement.Action);
            });
        }
        public static void BigIntWithDefault()
        {
            var statement = new DeclareVariableStatement("c", PrimitiveTypes.BigInt())
            {
                DefaultExpression =
                    SqlExpression.Multiply(SqlExpression.Constant(56), SqlExpression.Reference(new ObjectName("test.a")))
            };

            var sql      = statement.ToString();
            var expected = "c BIGINT := 56 * test.a";

            Assert.AreEqual(expected, sql);
        }
        public static void MutateReadOnlyTableInfo()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            tableInfo = tableInfo.AsReadOnly();

            Assert.Throws <InvalidOperationException>(() => tableInfo.Columns.RemoveAt(0));
            Assert.Throws <InvalidOperationException>(() => tableInfo.Columns.Add(new ColumnInfo("v", PrimitiveTypes.Integer())));
        }
        public static void AliasTableInfo()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            var alias   = new ObjectName("alias1");
            var aliased = tableInfo.As(alias);

            Assert.NotEqual(tableName, aliased.TableName);
            Assert.Equal(aliased.Columns.Count, tableInfo.Columns.Count);
        }
示例#11
0
        public void CreateNewTable()
        {
            var tableInfo = new TableInfo(ObjectName.Parse("sys.table1"));

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));

            manager.CreateTable(tableInfo);

            Assert.NotEmpty(manager.Transaction.State.VisibleTables);
            Assert.Equal(2, manager.Transaction.State.VisibleTables.Count());
            Assert.Single(events);
            Assert.IsType <TableCreatedEvent>(events[0]);
            Assert.Equal(tableInfo.TableName, events[0].TableName);
        }
        public static void NewJoinedTableInfo()
        {
            var tableName1 = new ObjectName("tab1");
            var tableInfo1 = new TableInfo(tableName1);

            tableInfo1.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo1.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            var tableName2 = new ObjectName("tab2");
            var tableInfo2 = new TableInfo(tableName2);

            tableInfo2.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo2.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            var joinedInfo = new JoinedTableInfo(new TableInfo[] { tableInfo1, tableInfo2 });

            Assert.True(joinedInfo.Columns.IsReadOnly);

            Assert.Equal(4, joinedInfo.Columns.Count);

            Assert.Equal(0, joinedInfo.Columns.IndexOf(ObjectName.Parse("tab1.a")));
            Assert.Equal(1, joinedInfo.Columns.IndexOf(ObjectName.Parse("tab1.b")));
            Assert.Equal(2, joinedInfo.Columns.IndexOf(ObjectName.Parse("tab2.a")));
            Assert.Equal(3, joinedInfo.Columns.IndexOf(ObjectName.Parse("tab2.b")));

            // In this case both the tables have 'a' and 'b' columns
            Assert.Equal(0, joinedInfo.Columns.IndexOf("a"));
            Assert.Equal(1, joinedInfo.Columns.IndexOf("b"));

            Assert.Equal(0, joinedInfo.GetTableOffset(0));
            Assert.Equal(0, joinedInfo.GetTableOffset(1));
            Assert.Equal(1, joinedInfo.GetTableOffset(2));
            Assert.Equal(1, joinedInfo.GetTableOffset(3));

            Assert.Equal(ObjectName.Parse("tab1.a"), joinedInfo.Columns.GetColumnName(0));
            Assert.Equal(ObjectName.Parse("tab1.b"), joinedInfo.Columns.GetColumnName(1));
            Assert.Equal(ObjectName.Parse("tab2.a"), joinedInfo.Columns.GetColumnName(2));
            Assert.Equal(ObjectName.Parse("tab2.b"), joinedInfo.Columns.GetColumnName(3));

            var col1 = joinedInfo.Columns[0];
            var col2 = joinedInfo.Columns[1];
            var col3 = joinedInfo.Columns[2];
            var col4 = joinedInfo.Columns[3];

            Assert.Equal("a", col1.ColumnName);
            Assert.Equal("b", col2.ColumnName);
            Assert.Equal("a", col3.ColumnName);
            Assert.Equal("b", col4.ColumnName);
        }
        public static void RemoveColumn()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            var col = new ColumnInfo("c", PrimitiveTypes.TimeStamp());

            tableInfo.Columns.Add(col);

            Assert.True(tableInfo.Columns.Remove(col));
            Assert.False(tableInfo.Columns.Remove(col));
        }
        public TableIndexTests()
        {
            var tableInfo = new TableInfo(ObjectName.Parse("test_table1"));

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.String()));

            var temp = new TemporaryTable(tableInfo);

            temp.AddRow(new [] { SqlObject.BigInt(22), SqlObject.String("o102") });
            temp.AddRow(new [] { SqlObject.BigInt(103), SqlObject.String("t-23") });
            temp.AddRow(new[] { SqlObject.BigInt(104), SqlObject.String("test22") });
            temp.BuildIndex();

            table = temp;
        }
        private void CreateTable(IQuery query)
        {
            var tableName = ObjectName.Parse("APP.persons");

            query.Access().CreateTable(table => table
                                       .Named(tableName)
                                       .WithColumn(column => column
                                                   .Named("id")
                                                   .HavingType(PrimitiveTypes.BigInt())
                                                   .NotNull()
                                                   .WithDefault(SqlExpression.FunctionCall("UNIQUEKEY", new SqlExpression[] {
                SqlExpression.Constant(tableName.FullName)
            })))
                                       .WithColumn("name", PrimitiveTypes.String())
                                       .WithColumn("age", PrimitiveTypes.Integer()));
        }
        public static void NewSimpleTableInfo()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            Assert.Equal(tableName, tableInfo.TableName);
            Assert.Equal(2, tableInfo.Columns.Count);
            Assert.Equal(0, tableInfo.Columns.IndexOf("a"));
            Assert.Equal(1, tableInfo.Columns.IndexOf("b"));
            Assert.Equal(0, tableInfo.Columns.IndexOf(ObjectName.Parse("tab1.a")));
            Assert.Equal(1, tableInfo.Columns.IndexOf(ObjectName.Parse("tab1.b")));
            Assert.Equal(ObjectName.Parse("tab1.a"), tableInfo.Columns.GetColumnName(0));
            Assert.Equal(ObjectName.Parse("tab1.b"), tableInfo.Columns.GetColumnName(1));
        }
示例#17
0
        protected override void OnAfterSetup(string testName)
        {
            var tableName = ObjectName.Parse("APP.test_table");

            AdminQuery.Access().CreateTable(tableBuilder => tableBuilder
                                            .Named(tableName)
                                            .WithColumn("a", PrimitiveTypes.Integer())
                                            .WithColumn("b", PrimitiveTypes.String()));

            var table = AdminQuery.Access().GetMutableTable(tableName);

            for (int i = 0; i < 10; i++)
            {
                var row = table.NewRow();
                row.SetValue(0, i);
                row.SetValue(1, String.Format("ID: {0}", i));
                table.AddRow(row);
            }

            var query = (SqlQueryExpression)SqlExpression.Parse("SELECT * FROM APP.test_table");

            var cursorInfo = new CursorInfo("c1", query);

            cursorInfo.Flags = CursorFlags.Scroll;

            AdminQuery.Access().CreateObject(cursorInfo);
            var c1 = (Cursor)AdminQuery.Access().GetObject(DbObjectType.Cursor, new ObjectName("c1"));

            c1.Open(AdminQuery);

            if (testName.StartsWith("FetchInto"))
            {
                var query2 = (SqlQueryExpression)SqlExpression.Parse("SELECT a FROM APP.test_table");

                var cursorInfo2 = new CursorInfo("c2", query2);
                cursorInfo2.Flags = CursorFlags.Scroll;

                AdminQuery.Access().CreateObject(cursorInfo2);
                var c2 = (Cursor)AdminQuery.Access().GetObject(DbObjectType.Cursor, new ObjectName("c2"));
                c2.Open(AdminQuery);

                AdminQuery.Access().CreateObject(new VariableInfo("var1", PrimitiveTypes.BigInt(), false));
                AdminQuery.Access().CreateObject(new VariableInfo("var2", PrimitiveTypes.String(), false));
            }
        }
示例#18
0
        protected override void BeforeLoop(ExecutionContext context)
        {
            var cursor = context.Request.Context.FindCursor(CursorName);

            if (cursor == null)
            {
                throw new StatementException(String.Format("Cursor '{0}' was not defined in this scope.", CursorName));
            }
            if (cursor.Status != CursorStatus.Open)
            {
                throw new StatementException(String.Format("The cursor '{0}' is in an invalid status ({1}).", CursorName,
                                                           cursor.Status.ToString().ToUpperInvariant()));
            }

            context.Request.Context.DeclareVariable(IndexName, PrimitiveTypes.BigInt());
            context.Request.Context.SetVariable(IndexName, SqlExpression.Constant(Field.BigInt(0)));

            base.BeforeLoop(context);
        }
        public static void GetString()
        {
            var tableName = new ObjectName("tab1");
            var tableInfo = new TableInfo(tableName);

            tableInfo.Columns.Add(new ColumnInfo("a", PrimitiveTypes.BigInt()));
            tableInfo.Columns.Add(new ColumnInfo("b", PrimitiveTypes.VarChar(22)));

            var sb = new StringBuilder();

            sb.AppendLine("tab1 (");
            sb.AppendLine("  a BIGINT,");
            sb.AppendLine("  b VARCHAR(22)");
            sb.Append(")");

            var expected = sb.ToString();
            var sql      = tableInfo.ToSqlString();

            Assert.Equal(expected, sql);
        }