private SqlString GetSqlStringWithLimitsIfNeeded(QueryWriter queryWriter) { Parameter skipParameter = null; Parameter takeParameter = null; if (queryWriter.SkipParameter != null) { queryWriter.SkipParameter.ExpectedType = NHibernateUtil.Int32; queryWriter.SkipParameter.IsSkipParameter(); skipParameter = Parameter.Placeholder; skipParameter.BackTrack = queryWriter.SkipParameter.GetIdsForBackTrack(sessionFactory).First(); } if (queryWriter.TakeParameter != null) { queryWriter.TakeParameter.ExpectedType = NHibernateUtil.Int32; queryWriter.TakeParameter.IsTakeParameterWithSkipParameter(queryWriter.SkipParameter); takeParameter = Parameter.Placeholder; takeParameter.BackTrack = queryWriter.TakeParameter.GetIdsForBackTrack(sessionFactory).First(); } // We allow the user to specify either constants or parameters for their limits. // The dialect can move the given parameters where he need, what it can't do is generates new parameters, losing the BackTrack. var dialect = sessionFactory.Dialect; return(dialect.GetLimitString(queryWriter.ToSqlString(), queryWriter.Skip.HasValue ? (int?)dialect.GetOffsetValue(queryWriter.Skip.Value) : null, queryWriter.Take.HasValue ? (int?)dialect.GetLimitValue(queryWriter.Skip ?? 0, queryWriter.Take.Value) : null, skipParameter, takeParameter)); }
public void CreateParameters(IDictionary <string, object> parameters) { foreach (var param in parameters) { QueryWriter.CreateParameter(param.Key, param.Value); } }
public void EmptyQueryForNoClauses() { var writer = new QueryWriter(); var query = writer.ToCypherQuery(); Assert.Equal("", query.QueryText); Assert.Equal(0, query.QueryParameters.Count); }
public static string ConvertToSql(QueryExpression queryExpression, out ParameterBag valueParameters) { var queryWriter = new QueryWriter(); var converter = new QueryConverter(); converter.WriteQuery(queryWriter, queryExpression); valueParameters = queryWriter.Parameters; return(queryWriter.ToString().ReduceWhitespace().Trim()); }
public void AppendClause() { var writer = new QueryWriter(); writer.AppendClause("foo"); var query = writer.ToCypherQuery(); Assert.Equal("foo", query.QueryText); Assert.Equal(0, query.QueryParameters.Count); }
public void AppendMultipleClauses() { var writer = new QueryWriter(); writer.AppendClause("foo"); writer.AppendClause("bar"); var query = writer.ToCypherQuery(); Assert.AreEqual("foo\r\nbar", query.QueryText); Assert.AreEqual(0, query.QueryParameters.Count); }
public void AppendClauseWithParameter() { var writer = new QueryWriter(); writer.AppendClause("foo {0}", "bar"); var query = writer.ToCypherQuery(); Assert.Equal("foo $p0", query.QueryText); Assert.Equal(1, query.QueryParameters.Count); Assert.Equal("bar", query.QueryParameters["p0"]); }
public void AppendMultipleClauses() { var writer = new QueryWriter(); writer.AppendClause("foo"); writer.AppendClause("bar"); var query = writer.ToCypherQuery(); Assert.Equal("foo" + Environment.NewLine + "bar", query.QueryText); Assert.Equal(0, query.QueryParameters.Count); }
public void AppendClauseWithMultipleParameters() { var writer = new QueryWriter(); writer.AppendClause("foo {0} bar {1}", "baz", "qak"); var query = writer.ToCypherQuery(); Assert.Equal("foo $p0 bar $p1", query.QueryText); Assert.Equal(2, query.QueryParameters.Count); Assert.Equal("baz", query.QueryParameters["p0"]); Assert.Equal("qak", query.QueryParameters["p1"]); }
private void EndQuery() { QueryWriter queryWriter = ((QueryWriter)writer); SqlString sqlString = queryWriter.ToSqlString(); if (queryWriter.Take.HasValue || queryWriter.Skip.HasValue) { sqlString = sessionFactory.Dialect.GetLimitString(sqlString, queryWriter.Skip ?? 0, queryWriter.Take ?? int.MaxValue); } writer = outputStack[0]; outputStack.RemoveAt(0); Out(sqlString); }
public void AppendMultipleClausesWithMultipleParameters() { var writer = new QueryWriter(); writer.AppendClause("foo {0} bar {1}", "baz", "qak"); writer.AppendClause("{0} qoo {1} zoo", "abc", "xyz"); var query = writer.ToCypherQuery(); var expectedText = string.Format("foo $p0 bar $p1{0}$p2 qoo $p3 zoo", Environment.NewLine); Assert.Equal(expectedText, query.QueryText); Assert.Equal(4, query.QueryParameters.Count); Assert.Equal("baz", query.QueryParameters["p0"]); Assert.Equal("qak", query.QueryParameters["p1"]); Assert.Equal("abc", query.QueryParameters["p2"]); Assert.Equal("xyz", query.QueryParameters["p3"]); }
public void AppendMultipleClausesWithMultipleParameters() { var writer = new QueryWriter(); writer.AppendClause("foo {0} bar {1}", "baz", "qak"); writer.AppendClause("{0} qoo {1} zoo", "abc", "xyz"); var query = writer.ToCypherQuery(); const string expectedText = @"foo {p0} bar {p1} {p2} qoo {p3} zoo"; Assert.AreEqual(expectedText, query.QueryText); Assert.AreEqual(4, query.QueryParameters.Count); Assert.AreEqual("baz", query.QueryParameters["p0"]); Assert.AreEqual("qak", query.QueryParameters["p1"]); Assert.AreEqual("abc", query.QueryParameters["p2"]); Assert.AreEqual("xyz", query.QueryParameters["p3"]); }
public void ToCypherQueryShouldNotIncrementParamCountsWhenGeneratedTwice() { var writer = new QueryWriter(); writer.AppendClause("foo {0}", "bar"); var query1 = writer.ToCypherQuery(); Assert.Equal("foo $p0", query1.QueryText); Assert.Equal(1, query1.QueryParameters.Count); Assert.Equal("bar", query1.QueryParameters["p0"]); var query2 = writer.ToCypherQuery(); Assert.Equal("foo $p0", query2.QueryText); Assert.Equal(1, query2.QueryParameters.Count); Assert.Equal("bar", query2.QueryParameters["p0"]); }
public void ToCypherQueryShouldNotLeakNewParamsIntoPreviouslyBuiltQuery() { var writer = new QueryWriter(); writer.AppendClause("foo {0}", "bar"); var query1 = writer.ToCypherQuery(); writer.AppendClause("baz {0}", "qak"); var query2 = writer.ToCypherQuery(); Assert.Equal("foo $p0", query1.QueryText); Assert.Equal(1, query1.QueryParameters.Count); Assert.Equal("bar", query1.QueryParameters["p0"]); Assert.Equal("foo $p0" + Environment.NewLine + "baz $p1", query2.QueryText); Assert.Equal(2, query2.QueryParameters.Count); Assert.Equal("bar", query2.QueryParameters["p0"]); Assert.Equal("qak", query2.QueryParameters["p1"]); }
public CypherQuery ToCypherQuery(IContractResolver contractResolver = null, bool isWrite = true) { return(QueryWriter.ToCypherQuery(contractResolver, isWrite)); }
private SqlString GetSqlStringWithLimitsIfNeeded(QueryWriter queryWriter) { Parameter skipParameter = null; Parameter takeParameter = null; if(queryWriter.SkipParameter != null) { queryWriter.SkipParameter.ExpectedType = NHibernateUtil.Int32; skipParameter = Parameter.Placeholder; skipParameter.BackTrack = queryWriter.SkipParameter.GetIdsForBackTrack(sessionFactory).First(); } if (queryWriter.TakeParameter != null) { queryWriter.TakeParameter.ExpectedType = NHibernateUtil.Int32; takeParameter = Parameter.Placeholder; takeParameter.BackTrack = queryWriter.TakeParameter.GetIdsForBackTrack(sessionFactory).First(); } // We allow the user to specify either constants or parameters for their limits. // The dialect can move the given parameters where he need, what it can't do is generates new parameters, losing the BackTrack. var dialect = sessionFactory.Dialect; return dialect.GetLimitString(queryWriter.ToSqlString(), queryWriter.Skip.HasValue ? (int?) dialect.GetOffsetValue(queryWriter.Skip.Value) : null, queryWriter.Take.HasValue ? (int?) dialect.GetLimitValue(queryWriter.Skip ?? 0, queryWriter.Take.Value) : null, skipParameter, takeParameter); }
public static void Write(OutputModel outputModel, DatabasePlan databasePlan, PartitionPlan partitionPlan) { var s = $@" namespace {databasePlan.Namespace}; public class {partitionPlan.ClassName} : Cosmogenesis.Core.DbPartitionBase {{ protected virtual {databasePlan.Namespace}.{databasePlan.DbClassName} {databasePlan.DbClassName} {{ get; }} = default!; {PkClass(partitionPlan)} /// <summary>Mocking constructor</summary> protected {partitionPlan.ClassName}() {{ }} internal protected {partitionPlan.ClassName}( {new[] { ConstructorClassParameter(databasePlan), ConstructorKeyParameter(partitionPlan) }.Where(x => !string.IsNullOrEmpty(x)).JoinNonEmpty()}) : base( db: {databasePlan.DbClassNameArgument}, partitionKey: {partitionPlan.GetPkPlan.FullMethodName}({partitionPlan.GetPkPlan.DocumentToParametersMapping("pkData")}), serializer: {databasePlan.Namespace}.{databasePlan.SerializerClassName}.Instance) {{ this.{databasePlan.DbClassName} = {databasePlan.DbClassNameArgument} ?? throw new System.ArgumentNullException(nameof({databasePlan.DbClassNameArgument})); {PkClassSetter(partitionPlan)} }} {databasePlan.Namespace}.{partitionPlan.QueryBuilderClassName}? queryBuilder; /// <summary> /// Methods to build queries for later execution. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.QueryBuilderClassName} QueryBuilder => this.queryBuilder ??= new( {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName}, partitionKey: this.PartitionKey); {databasePlan.Namespace}.{partitionPlan.QueryClassName}? query; /// <summary> /// Methods to execute queries. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.QueryClassName} Query => this.query ??= new( {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName}, {partitionPlan.QueryBuilderClassNameArgument}: this.QueryBuilder); /// <summary> /// A batch of operations to be executed atomically (or not at all) within a {partitionPlan.Name} in the {databasePlan.Name} database. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.BatchClassName} CreateBatch() => new( transactionalBatch: this.CreateBatchForPartition(), partitionKey: this.PartitionKeyString, validateStateBeforeSave: this.{databasePlan.DbClassName}.ValidateStateBeforeSave, {partitionPlan.ClassNameArgument}: this); {databasePlan.Namespace}.{partitionPlan.ReadClassName}? read; /// <summary> /// Methods to read documents. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.ReadClassName} Read => this.read ??= new( {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName}, partitionKey: this.PartitionKey); {databasePlan.Namespace}.{partitionPlan.ReadOrThrowClassName}? readOrThrow; /// <summary> /// Methods to read documents, or throw DbConflictException is they are not found. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.ReadOrThrowClassName} ReadOrThrow => this.readOrThrow ??= new( {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName}, partitionKey: this.PartitionKey); {databasePlan.Namespace}.{partitionPlan.CreateClassName}? create; /// <summary> /// Methods to create documents. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.CreateClassName} Create => this.create ??= new(this); {databasePlan.Namespace}.{partitionPlan.ReadOrCreateClassName}? readOrCreate; /// <summary> /// Methods to read documents, or create them if they did not yet exist. /// </summary> public virtual {databasePlan.Namespace}.{partitionPlan.ReadOrCreateClassName} ReadOrCreate => this.readOrCreate ??= new(this); {ReadMany(databasePlan, partitionPlan)} {CreateOrReplace(databasePlan, partitionPlan)} {string.Concat(partitionPlan.Documents.Select(x => Create(partitionPlan, x)))} {string.Concat(partitionPlan.Documents.Select(x => CreateOrReplace(partitionPlan, x)))} {string.Concat(partitionPlan.Documents.Select(x => ReadOrCreate(partitionPlan, x)))} {string.Concat(partitionPlan.Documents.Select(ReplaceIfMutable))} {string.Concat(partitionPlan.Documents.Select(DeleteIfTransient))} }} "; outputModel.Context.AddSource($"partition_{partitionPlan.ClassName}.cs", s); BatchWriter.Write(outputModel, databasePlan, partitionPlan); CreateWriter.Write(outputModel, databasePlan, partitionPlan); ReadOrCreateWriter.Write(outputModel, databasePlan, partitionPlan); CreateOrReplaceWriter.Write(outputModel, databasePlan, partitionPlan); ReadWriter.Write(outputModel, databasePlan, partitionPlan); ReadOrThrowWriter.Write(outputModel, databasePlan, partitionPlan); ReadManyWriter.Write(outputModel, databasePlan, partitionPlan); QueryBuilderWriter.Write(outputModel, databasePlan, partitionPlan); QueryWriter.Write(outputModel, databasePlan, partitionPlan); }
public QueryWriterWrapper(QueryWriter queryWriter, AnnotationsContext annotationsContext) { QueryWriter = queryWriter ?? throw new ArgumentNullException(nameof(queryWriter)); AnnotationsContext = annotationsContext ?? throw new ArgumentNullException(nameof(annotationsContext)); IsBoltClient = annotationsContext.IsBoltClient; }
public string CreateParameter(object paramValue) { return(QueryWriter.CreateParameter(GetTransformedParameterValue(paramValue))); }
public void CreateParameter(string key, object value) { QueryWriter.CreateParameter(key, GetTransformedParameterValue(value)); }
public bool ContainsParameterWithKey(string key) { return(QueryWriter.ContainsParameterWithKey(key)); }