コード例 #1
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (!context.Access().TableExists(tableName))
            {
                throw new ObjectNotFoundException(tableName);
            }

            var queryExpression = new SqlQueryExpression(new[] { SelectColumn.Glob("*") });

            queryExpression.FromClause.AddTable(tableName.FullName);
            queryExpression.WhereExpression = WherExpression;

            var queryFrom = QueryExpressionFrom.Create(context, queryExpression);
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, queryExpression));

            var columns = new List <SqlAssignExpression>();

            foreach (var assignment in Assignments)
            {
                var columnName = ObjectName.Parse(assignment.ColumnName);

                var refName    = queryFrom.ResolveReference(columnName);
                var expression = assignment.Expression.Prepare(queryFrom.ExpressionPreparer);

                var assign = SqlExpression.Assign(SqlExpression.Reference(refName), expression);
                columns.Add(assign);
            }

            return(new Prepared(tableName, queryPlan, columns.ToArray(), Limit));
        }
コード例 #2
0
        public void Assignment()
        {
            var assign = SqlExpression.Assign(SqlExpression.Reference(new ObjectName("a")), SqlExpression.Constant(22));

            SerializeAndAssert(assign, (serialized, deserialized) => {
                Assert.AreEqual(SqlExpressionType.Assign, deserialized.ExpressionType);
                Assert.IsInstanceOf <SqlReferenceExpression>(deserialized.ReferenceExpression);
                Assert.IsInstanceOf <SqlConstantExpression>(deserialized.ValueExpression);
            });
        }
コード例 #3
0
        public void InsertRegular()
        {
            var tableName   = ObjectName.Parse("APP.people");
            var assignments = new SqlAssignExpression[] {
                SqlExpression.Assign(SqlExpression.Reference(new ObjectName(tableName, "id")),
                                     SqlExpression.Constant(1)),
                SqlExpression.Assign(SqlExpression.Reference(new ObjectName(tableName, "first_name")),
                                     SqlExpression.Constant("Antonello")),
                SqlExpression.Assign(SqlExpression.Reference(new ObjectName(tableName, "last_name")),
                                     SqlExpression.Constant("Provenzano"))
            };

            Query.InsertIntoTable(tableName, assignments);
        }
コード例 #4
0
        IStatement IPreparableStatement.Prepare(IRequest context)
        {
            var values = Values.ToArray();

            int firstLen = -1;

            for (int n = 0; n < values.Length; ++n)
            {
                var expList = (IList)values[n];
                if (firstLen == -1 || firstLen == expList.Count)
                {
                    firstLen = expList.Count;
                }
                else
                {
                    throw new InvalidOperationException("The insert data list varies in size.");
                }
            }

            var tableName = context.Query.ResolveTableName(TableName);

            var table = context.Query.GetTable(tableName);

            if (table == null)
            {
                throw new ObjectNotFoundException(TableName);
            }

            if (Values.Any(x => x.OfType <SqlQueryExpression>().Any()))
            {
                throw new InvalidOperationException("Cannot set a value from a query.");
            }

            var tableQueryInfo = context.Query.GetTableQueryInfo(tableName, null);
            var fromTable      = new FromTableDirectSource(context.Query.IgnoreIdentifiersCase(), tableQueryInfo, "INSERT_TABLE", tableName, tableName);

            var columns = new string[0];

            if (ColumnNames != null)
            {
                columns = ColumnNames.ToArray();
            }

            if (columns.Length == 0)
            {
                columns = new string[table.TableInfo.ColumnCount];
                for (int i = 0; i < columns.Length; i++)
                {
                    columns[i] = table.TableInfo[i].ColumnName;
                }
            }

            var colIndices  = new int[columns.Length];
            var colResolved = new ObjectName[columns.Length];

            for (int i = 0; i < columns.Length; ++i)
            {
                var inVar = new ObjectName(columns[i]);
                var col   = ResolveColumn(fromTable, inVar);
                int index = table.FindColumn(col);
                if (index == -1)
                {
                    throw new InvalidOperationException(String.Format("Cannot find column '{0}' in table '{1}'.", col, tableName));
                }

                colIndices[i]  = index;
                colResolved[i] = col;
            }


            var columnInfos = new List <ColumnInfo>();

            foreach (var name in columns)
            {
                var columnName = new ObjectName(tableName, name);
                var colIndex   = table.FindColumn(columnName);
                if (colIndex < 0)
                {
                    throw new InvalidOperationException(String.Format("Cannot find column '{0}' in table '{1}'", columnName, table.FullName));
                }

                columnInfos.Add(table.TableInfo[colIndex]);
            }

            var assignments = new List <SqlAssignExpression[]>();

            foreach (var valueSet in values)
            {
                var valueAssign = new SqlAssignExpression[valueSet.Length];

                for (int i = 0; i < valueSet.Length; i++)
                {
                    var columnInfo = columnInfos[i];

                    var value = valueSet[i];
                    if (value != null)
                    {
                        // TODO: Deference columns with a preparer
                    }

                    if (value != null)
                    {
                        var expReturnType = value.ReturnType(context, null);
                        if (!columnInfo.ColumnType.IsComparable(expReturnType))
                        {
                            var sb = new StringBuilder();
                            sb.AppendFormat("Unable to convert type {0} of {1} into type {2} of column {3}", expReturnType, value, columnInfo.ColumnType, columnInfo.FullColumnName.FullName);
                            var ioe = new InvalidOperationException(sb.ToString());
                            throw ioe;
                        }
                    }

                    valueAssign[i] = SqlExpression.Assign(SqlExpression.Reference(columnInfo.FullColumnName), value);
                }

                assignments.Add(valueAssign);
            }

            return(new Prepared(tableName, assignments));
        }