예제 #1
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            if (_query.ResultOperators.OfType <LastResultOperator>().Any())
            {
                throw new InvalidOperationException("Marten does not support the Last() or LastOrDefault() operations. Use a combination of ordering and First/FirstOrDefault() instead");
            }

            var select = _mapping.SelectFields("d");
            var sql    = $"select {select} from {_mapping.TableName} as d";

            var where = buildWhereClause();
            var orderBy = toOrderClause();

            sql = appendLateralJoin(sql);
            if (@where != null)
            {
                sql += " where " + @where.ToSql(command);
            }

            if (orderBy.IsNotEmpty())
            {
                sql += orderBy;
            }

            sql = appendLimit(sql);
            sql = appendOffset(sql);

            command.AppendQuery(sql);
        }
예제 #2
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql = _sql;

            if (!sql.Contains("select", StringComparison.OrdinalIgnoreCase))
            {
                var mapping   = _schema.MappingFor(typeof(T)).ToQueryableDocument();
                var tableName = mapping.Table.QualifiedName;
                sql = "select data from {0} {1}".ToFormat(tableName, sql);
            }

            var firstParameter = _parameters.FirstOrDefault();

            if (_parameters.Length == 1 && firstParameter != null && firstParameter.IsAnonymousType())
            {
                command.AddParameters(firstParameter);
            }
            else
            {
                _parameters.Each(x =>
                {
                    var param = command.AddParameter(x);
                    sql       = sql.UseParameter(param);
                });
            }

            command.AppendQuery(sql);
        }
예제 #3
0
        public override void ConfigureCommand(NpgsqlCommand command)
        {
            var sql = Selector.ToSelectClause(null);

            sql += $" where 1 = 1";

            if (_version > 0)
            {
                var versionParam = command.AddParameter(_version);
                sql += " and version <= :" + versionParam.ParameterName;
            }

            if (_before.HasValue)
            {
                var beforeParam = command.AddParameter(_before.Value);
                sql += " and timestamp <= :" + beforeParam.ParameterName;
            }

            if (_after.HasValue)
            {
                var afterParam = command.AddParameter(_after.Value);
                sql += " and timestamp >= :" + afterParam.ParameterName;
            }

            sql += " order by version";

            command.AppendQuery(sql);
        }
예제 #4
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var parameter = command.AddParameter(_id);
            var sql =
                $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = :{parameter.ParameterName}";

            command.AppendQuery(sql);
        }
예제 #5
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql   = ToSelectClause(null);
            var param = command.AddParameter(_streamId);

            sql += " where id = :" + param.ParameterName;

            command.AppendQuery(sql);
        }
예제 #6
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var mapping = _schema.MappingFor(_query).ToQueryableDocument();
            var sql     = "select (count(*) > 0) as result from " + mapping.Table.QualifiedName + " as d";

            var where = _schema.BuildWhereFragment(mapping, _query);
            sql       = sql.AppendWhere(@where, command);

            command.AppendQuery(sql);
        }
예제 #7
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql = "select " +
                      string.Join(", ", _fields.OrderBy(kv => kv.Value).Select(kv => kv.Key)) +
                      $" from {_mapping.Table.QualifiedName} where id = :id";

            command.AppendQuery(sql);

            command.AddParameter("id", _id);
        }
예제 #8
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql =
                $"select {DocumentMapping.VersionColumn}, {DocumentMapping.LastModifiedColumn} from {_mapping.Table.QualifiedName} where id = :id";


            command.AppendQuery(sql);

            command.AddParameter("id", _id);
        }
예제 #9
0
파일: LinqQuery.cs 프로젝트: kennyup/marten
        public void ConfigureAggregate(NpgsqlCommand command, string @operator)
        {
            var locator = _mapping.JsonLocator(Model.SelectClause.Selector);
            var field   = @operator.ToFormat(locator);

            var sql = $"select {field} from {_mapping.Table.QualifiedName} as d";

            sql = AppendWhere(command, sql);

            command.AppendQuery(sql);
        }
예제 #10
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql = _template.CommandText;

            for (var i = 0; i < _template.Parameters.Count; i++)
            {
                var param = _setters[i].AddParameter(_model, command);
                sql = sql.Replace(":" + _template.Parameters[i].ParameterName, ":" + param.ParameterName);
            }

            command.AppendQuery(sql);
        }
예제 #11
0
        public void ConfigureForCount(NpgsqlCommand command)
        {
            var sql = "select count(*) as number from " + _mapping.TableName + " as d";

            var where = buildWhereClause();

            if (@where != null)
            {
                sql += " where " + @where.ToSql(command);
            }

            command.AppendQuery(sql);
        }
예제 #12
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var mapping = _schema.MappingFor(_query.SourceType()).ToQueryableDocument();
            var locator = mapping.JsonLocator(_query.SelectClause.Selector);
            var field   = _operator.ToFormat(locator);

            var sql = $"select {field} from {mapping.Table.QualifiedName} as d";

            var @where = _schema.BuildWhereFragment(mapping, _query);

            sql = sql.AppendWhere(@where, command);

            command.AppendQuery(sql);
        }
예제 #13
0
파일: LinqQuery.cs 프로젝트: kennyup/marten
        public void ConfigureAny(NpgsqlCommand command)
        {
            var select = "select (count(*) > 0) as result";

            if (_subQuery != null)
            {
                select = $"select (sum(jsonb_array_length({_subQuery.SqlLocator})) > 0) as result";
            }

            var sql = $"{select} from {_mapping.Table.QualifiedName} as d";

            sql = new LinqQuery <bool>(_schema, Model, new IIncludeJoin[0], null).AppendWhere(command, sql);

            command.AppendQuery(sql);
        }
예제 #14
0
        public void ConfigureForAny(NpgsqlCommand command)
        {
            var sql = "select (count(*) > 0) as result from " + _mapping.TableName + " as d";

            var where = buildWhereClause();

            sql = appendLateralJoin(sql);

            if (@where != null)
            {
                sql += " where " + @where.ToSql(command);
            }

            command.AppendQuery(sql);
        }
예제 #15
0
        public void ConfigureCommand <T>(NpgsqlCommand cmd, string sql, object[] parameters)
        {
            var mapping = _schema.MappingFor(typeof(T));

            if (!sql.Contains("select", StringComparison.OrdinalIgnoreCase))
            {
                var tableName = mapping.TableName;
                sql = "select data from {0} {1}".ToFormat(tableName, sql);
            }

            parameters.Each(x =>
            {
                var param = cmd.AddParameter(x);
                sql       = sql.UseParameter(param);
            });

            cmd.AppendQuery(sql);
        }
예제 #16
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql    = _selector.ToSelectClause(_mapping);
            var @where = _schema.BuildWhereFragment(_mapping, _query);

            sql = sql.AppendWhere(@where, command);

            var orderBy = _query.ToOrderClause(_mapping);

            if (orderBy.IsNotEmpty())
            {
                sql += orderBy;
            }

            sql += $" LIMIT {_rowLimit}";
            sql  = _query.AppendOffset(sql);

            command.AppendQuery(sql);
        }
예제 #17
0
        public void ConfigureCommand(NpgsqlCommand command)
        {
            var sql = _sql;

            if (!sql.Contains("select", StringComparison.OrdinalIgnoreCase))
            {
                var mapping   = _schema.MappingFor(typeof(T)).ToQueryableDocument();
                var tableName = mapping.Table.QualifiedName;
                sql = "select data from {0} {1}".ToFormat(tableName, sql);
            }

            _parameters.Each(x =>
            {
                var param = command.AddParameter(x);
                sql       = sql.UseParameter(param);
            });

            command.AppendQuery(sql);
        }
예제 #18
0
파일: LinqQuery.cs 프로젝트: kennyup/marten
        public void ConfigureCommand(NpgsqlCommand command, int limit)
        {
            var    isComplexSubQuery = _subQuery != null && _subQuery.IsComplex(_joins);
            string sql = "";

            if (isComplexSubQuery)
            {
                if (_subQuery.HasSelectTransform())
                {
                    sql = $"select {_subQuery.RawChildElementField()} from {_mapping.Table.QualifiedName} as d";
                }
                else
                {
                    sql = _innerSelector.ToSelectClause(_mapping);
                }
            }
            else
            {
                sql = Selector.ToSelectClause(_mapping);
            }

            sql = AppendWhere(command, sql);

            var orderBy = determineOrderClause();

            if (orderBy.IsNotEmpty())
            {
                sql += orderBy;
            }

            if (isComplexSubQuery)
            {
                sql = _subQuery.ConfigureCommand(_joins, Selector, command, sql, limit);
            }
            else
            {
                sql = Model.ApplySkip(command, sql);
                sql = Model.ApplyTake(command, limit, sql);
            }

            command.AppendQuery(sql);
        }
예제 #19
0
        private Task <T> load <T>(object id) where T : class
        {
            if (_identityMap.Has <T>(id))
            {
                return(Task.FromResult(_identityMap.Retrieve <T>(id)));
            }

            var source = new TaskCompletionSource <T>();

            var mapping   = _schema.MappingFor(typeof(T));
            var parameter = _command.AddParameter(id);

            _command.AppendQuery(
                $"select {mapping.SelectFields("d")} from {mapping.TableName} as d where id = :{parameter.ParameterName}");

            var handler = new SingleResultReader <T>(source, _schema.StorageFor(typeof(T)), _identityMap);

            addHandler(handler);

            return(source.Task);
        }
예제 #20
0
파일: LinqQuery.cs 프로젝트: kennyup/marten
        public void ConfigureCount(NpgsqlCommand command)
        {
            var select = "select count(*) as number";

            if (_subQuery != null)
            {
                if (Model.HasOperator <DistinctResultOperator>())
                {
                    throw new NotSupportedException("Marten does not yet support SelectMany() with both a Distinct() and Count() operator");
                }

                // TODO -- this will need to be smarter
                select = $"select sum(jsonb_array_length({_subQuery.SqlLocator})) as number";
            }

            var sql = $"{select} from {_mapping.Table.QualifiedName} as d";

            sql = AppendWhere(command, sql);

            command.AppendQuery(sql);
        }
예제 #21
0
        public override void ConfigureCommand(NpgsqlCommand command)
        {
            var sql = _selector.ToSelectClause(null);

            var param = command.AddParameter(_streamId);

            sql += $" where stream_id = :{param.ParameterName}";

            if (_version > 0)
            {
                var versionParam = command.AddParameter(_version);
                sql += " and version <= :" + versionParam.ParameterName;
            }

            if (_timestamp.HasValue)
            {
                var timestampParam = command.AddParameter(_timestamp.Value);
                sql += " and timestamp <= :" + timestampParam.ParameterName;
            }

            sql += " order by version";

            command.AppendQuery(sql);
        }