private IDictionary <string, SlaveToken> GetSlaves(IStatementBase statement, IDataReader reader)
                {
                    IEnumerable <ParameterToken> GetParameters(string path)
                    {
                        if (string.IsNullOrEmpty(path))
                        {
                            yield break;
                        }

                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var name = reader.GetName(i);

                            if (name.StartsWith("$" + path + ":"))
                            {
                                yield return(new ParameterToken(name.Substring(path.Length + 2), i));
                            }
                        }
                    }

                    if (statement.HasSlaves)
                    {
                        var tokens = new Dictionary <string, SlaveToken>(statement.Slaves.Count);

                        foreach (var slave in statement.Slaves)
                        {
                            if (slave is SelectStatementBase selection && !string.IsNullOrEmpty(selection.Alias))
                            {
                                var schema = _context.Schema.Find(selection.Alias);

                                if (schema != null)
                                {
                                    tokens.Add(selection.Alias, new SlaveToken(schema, GetParameters(selection.Alias)));
                                }
                            }
                        }

                        if (tokens.Count > 0)
                        {
                            return(tokens);
                        }
                    }

                    return(null);
                }
Example #2
0
        protected override void VisitStatement(ExpressionVisitorContext context, IStatementBase statement)
        {
            switch (statement)
            {
            case TableDefinition table:
                MySqlTableDefinitionVisitor.Instance.Visit(context, table);
                break;

            case SelectStatement select:
                MySqlSelectStatementVisitor.Instance.Visit(context, select);
                break;

            case DeleteStatement delete:
                MySqlDeleteStatementVisitor.Instance.Visit(context, delete);
                break;

            case InsertStatement insert:
                MySqlInsertStatementVisitor.Instance.Visit(context, insert);
                break;

            case UpdateStatement update:
                MySqlUpdateStatementVisitor.Instance.Visit(context, update);
                break;

            case UpsertStatement upsert:
                MySqlUpsertStatementVisitor.Instance.Visit(context, upsert);
                break;

            case AggregateStatement aggregate:
                MySqlAggregateStatementVisitor.Instance.Visit(context, aggregate);
                break;

            case ExistStatement exist:
                MySqlExistStatementVisitor.Instance.Visit(context, exist);
                break;

            case ExecutionStatement execution:
                MySqlExecutionStatementVisitor.Instance.Visit(context, execution);
                break;

            default:
                throw new DataException($"Not supported '{statement}' statement.");
            }
        }
Example #3
0
        protected override IExpression VisitStatement(IStatementBase statement)
        {
            switch (statement)
            {
            case TableDefinition table:
                MsSqlTableDefinitionVisitor.Instance.Visit(this, table);
                break;

            case SelectStatement select:
                MsSqlSelectStatementVisitor.Instance.Visit(this, select);
                break;

            case DeleteStatement delete:
                MsSqlDeleteStatementVisitor.Instance.Visit(this, delete);
                break;

            case InsertStatement insert:
                MsSqlInsertStatementVisitor.Instance.Visit(this, insert);
                break;

            case UpdateStatement update:
                MsSqlUpdateStatementVisitor.Instance.Visit(this, update);
                break;

            case UpsertStatement upsert:
                MsSqlUpsertStatementVisitor.Instance.Visit(this, upsert);
                break;

            case AggregateStatement aggregate:
                MsSqlAggregateStatementVisitor.Instance.Visit(this, aggregate);
                break;

            case ExistStatement exist:
                MsSqlExistStatementVisitor.Instance.Visit(this, exist);
                break;

            case ExecutionStatement execution:
                MsSqlExecutionStatementVisitor.Instance.Visit(this, execution);
                break;
            }

            return(statement);
        }
Example #4
0
        //public override IExpressionDialect Dialect => DummyDialect.Instance;
        #endregion

        #region 重写方法
        protected override IExpression VisitStatement(IStatementBase statement)
        {
            switch (statement)
            {
            case TableDefinition table:
                DummyTableDefinitionVisitor.Instance.Visit(this, table);
                break;

            case CountStatement count:
                DummyCountStatementVisitor.Instance.Visit(this, count);
                break;

            case ExistStatement exist:
                DummyExistStatementVisitor.Instance.Visit(this, exist);
                break;

            case ExecutionStatement execution:
                DummyExecutionStatementVisitor.Instance.Visit(this, execution);
                break;

            case SelectStatement select:
                DummySelectStatementVisitor.Instance.Visit(this, select);
                break;

            case DeleteStatement delete:
                DummyDeleteStatementVisitor.Instance.Visit(this, delete);
                break;

            case InsertStatement insert:
                DummyInsertStatementVisitor.Instance.Visit(this, insert);
                break;

            case UpdateStatement update:
                DummyUpdateStatementVisitor.Instance.Visit(this, update);
                break;

            case UpsertStatement upsert:
                DummyUpsertStatementVisitor.Instance.Visit(this, upsert);
                break;
            }

            return(statement);
        }
Example #5
0
            public void Execute(IDataAccessContext context, IStatementBase statement)
            {
                switch (statement)
                {
                case SelectStatement select:
                    _select.Execute(context, select);
                    break;

                case DeleteStatement delete:
                    _delete.Execute(context, delete);
                    break;

                case InsertStatement insert:
                    _insert.Execute(context, insert);
                    break;

                case UpdateStatement update:
                    _update.Execute(context, update);
                    break;

                case UpsertStatement upsert:
                    _upsert.Execute(context, upsert);
                    break;

                case CountStatement count:
                    _count.Execute(context, count);
                    break;

                case ExistStatement exist:
                    _exist.Execute(context, exist);
                    break;

                case ExecutionStatement execution:
                    _execution.Execute(context, execution);
                    break;

                default:
                    context.Session.Build(statement).ExecuteNonQuery();
                    break;
                }
            }
        private void BuildUpsertion(DataUpdateContext context, object data, IStatementBase master, SchemaMember schema)
        {
            var entityType = schema.Parent == null ? context.EntityType : schema.Parent.Token.MemberType;

            //构建 Upsert 操作上下文
            var upsert = new DataUpsertContext(
                context.DataAccess,
                schema.Token.Property.Entity.Name,
                true,
                data,
                new Schema(schema.Token.Property.Entity, entityType, schema),
                context.HasStates ? context.States : null);

            //构建 Upsert 语句
            var statements = context.Source.Driver.Builder.Build(upsert);

            //将新建的语句加入到主语句的从属集中
            foreach (var statement in statements)
            {
                master.Slaves.Add(statement);
            }
        }
Example #7
0
 protected abstract void VisitStatement(ExpressionVisitorContext context, IStatementBase statement);
 protected virtual IExpression VisitStatement(IStatementBase statement)
 {
     return(statement);
 }
Example #9
0
 protected abstract IExpression GetValue(TContext context, IStatementBase statement, object value);
Example #10
0
 protected abstract IExpression GetField(TContext context, IStatementBase statement, string name);
Example #11
0
        private IExpression GenerateAggregate(TContext context, IStatementBase statement, Operand.AggregateOperand aggregate)
        {
            var                 entity = this.GetEntity(context);
            SelectStatement     selection;
            IDataEntityProperty property;
            FieldIdentifier     field;
            var                 parts = aggregate.Member.Split('.', StringSplitOptions.RemoveEmptyEntries);

            switch (parts.Length)
            {
            case 1:
                if (!entity.Properties.TryGet(parts[0], out property))
                {
                    throw new DataException($"The specified '{parts[0]}' field does not exist in the '{entity.Name}' entity.");
                }

                selection = new SelectStatement(entity);
                field     = selection.Table.CreateField(property);
                selection.Select.Members.Add(new AggregateExpression(aggregate.Function, aggregate.Distinct, field));

                break;

            case 2:
                if (!entity.Properties.TryGet(parts[0], out property))
                {
                    throw new DataException($"The specified '{parts[0]}' field does not exist in the '{entity.Name}' entity.");
                }

                if (property.IsSimplex)
                {
                    throw new DataException($"The specified '{parts[0]}' is a simple property and cannot be navigated.");
                }

                var complex = (IDataEntityComplexProperty)property;

                selection = new SelectStatement(complex.Foreign);
                field     = selection.Table.CreateField(complex.Foreign.Properties.Get(parts[1]));
                selection.Select.Members.Add(new AggregateExpression(aggregate.Function, aggregate.Distinct, field));

                var conditions = ConditionExpression.And();

                foreach (var link in complex.Links)
                {
                    ISource src = statement.Table;

                    foreach (var anchor in link.GetAnchors())
                    {
                        if (anchor.IsComplex)
                        {
                            src = selection.Join(context.Aliaser, selection, (IDataEntityComplexProperty)anchor);
                        }
                        else
                        {
                            conditions.Add(Expression.Equal(
                                               selection.Table.CreateField(link.ForeignKey),
                                               src.CreateField(anchor)));
                        }
                    }
                }

                selection.Where = conditions;
                break;

            default:
                throw new DataException($"Invalid aggregate member ‘{aggregate.Member}’ because its navigation level is too deep.");
            }

            if (aggregate.Filter != null)
            {
                if (selection.Where == null)
                {
                    selection.Where = selection.Where(aggregate.Filter.Flatten(), context.Aliaser);
                }
                else
                {
                    selection.Where = ConditionExpression.And(selection.Where, selection.Where(aggregate.Filter.Flatten(), context.Aliaser));
                }
            }

            selection.Table.Alias = null;
            return(selection);
        }
Example #12
0
 public SubqueryStatement(IStatementBase host, Metadata.IDataEntity entity) : base(new TableIdentifier(entity))
 {
     this.Host = host ?? throw new ArgumentNullException(nameof(host));
 }
Example #13
0
 public SubqueryStatement(IStatementBase host, TableIdentifier table) : base(table)
 {
     this.Host = host ?? throw new ArgumentNullException(nameof(host));
 }