/// <summary> /// InfluxDb client configuration. /// </summary> /// <param name="endpointUri">InfluxDb server URI.</param> /// <param name="username">InfluxDb server username.</param> /// <param name="password">InfluxDb server password.</param> /// <param name="influxVersion">InfluxDb server version.</param> /// <param name="queryLocation">Where queries are located in the request (URI params vs. Form Data) (optional).</param> /// <param name="httpClient">Custom HttpClient object (optional).</param> /// <param name="throwOnWarning">Should throw exception upon InfluxDb warning message (for debugging) (optional).</param> public InfluxDbClientConfiguration( Uri endpointUri, string username, string password, InfluxDbVersion influxVersion, QueryLocation queryLocation = QueryLocation.FormData, HttpClient httpClient = null, bool throwOnWarning = false) { Validate.IsNotNull(endpointUri, "Endpoint may not be null or empty."); EndpointUri = SanitizeEndpoint(endpointUri, false); Username = username; Password = password; InfluxVersion = influxVersion; QueryLocation = queryLocation; HttpClient = httpClient ?? new HttpClient(); ThrowOnWarning = throwOnWarning; }
/// <summary> /// InfluxDb client. /// </summary> /// <param name="endpointUri">InfluxDb server URI.</param> /// <param name="username">InfluxDb server username.</param> /// <param name="password">InfluxDb server password.</param> /// <param name="influxVersion">InfluxDb server version.</param> /// <param name="queryLocation">Where queries are located in the request (URI params vs. Form Data) (optional).</param> /// <param name="httpClient">Custom HttpClient object (optional).</param> /// <param name="throwOnWarning">Should throw exception upon InfluxDb warning message (for debugging) (optional).</param> public InfluxDbClient( string endpointUri, string username, string password, InfluxDbVersion influxVersion, QueryLocation queryLocation = QueryLocation.FormData, HttpClient httpClient = null, bool throwOnWarning = false ) : this( new InfluxDbClientConfiguration( new Uri(endpointUri), username, password, influxVersion, queryLocation, httpClient, throwOnWarning ) ) { }
/// <summary> /// Execute translation /// </summary> /// <param name="query">Query object</param> /// <param name="location">Query location</param> /// <param name="parameters">Parameters</param> /// <param name="objectName">Entity object name</param> /// <param name="useSort">Indicates whether use sort</param> /// <returns>Return a translation result</returns> public QueryTranslationResult ExecuteTranslation(IQuery query, QueryLocation location, CommandParameters parameters = null, string objectName = "", bool useSort = true) { if (query == null) { return(QueryTranslationResult.Empty); } StringBuilder conditionBuilder = new StringBuilder(); if (query.ExecutionMode == QueryExecutionMode.QueryObject) { StringBuilder sortBuilder = new StringBuilder(); parameters = parameters ?? new CommandParameters(); objectName = string.IsNullOrWhiteSpace(objectName) ? DefaultObjectPetName : objectName; List <string> withScripts = new List <string>(); string recurveTableName = string.Empty; string recurveTablePetName = string.Empty; #region condition if (!query.Conditions.IsNullOrEmpty()) { int index = 0; foreach (var condition in query.Conditions) { var queryItemCondition = TranslateCondition(query, condition, parameters, objectName); if (!queryItemCondition.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(queryItemCondition.WithScripts); recurveTableName = queryItemCondition.RecurveObjectName; recurveTablePetName = queryItemCondition.RecurvePetName; } conditionBuilder.Append($" {(index > 0 ? condition.Connector.ToString().ToUpper() : string.Empty)} {queryItemCondition.ConditionString}"); index++; } } #endregion #region sort if (useSort && !query.Sorts.IsNullOrEmpty()) { foreach (var orderItem in query.Sorts) { sortBuilder.Append($"{ConvertOrderCriterionName(query, objectName, orderItem)} {(orderItem.Desc ? DescKeyWord : AscKeyWord)},"); } } #endregion #region combine StringBuilder combineBuilder = new StringBuilder(); if (!query.Combines.IsNullOrEmpty()) { foreach (var combineEntry in query.Combines) { if (combineEntry?.Query == null) { continue; } switch (combineEntry.Type) { case CombineType.Except: var exceptFields = GetCombineFields(query, combineEntry.Query); var exceptQuery = QueryManager.Create().SetEntityType(query.GetEntityType()).IsNull(exceptFields.First()); var exceptJoinItem = new JoinEntry() { Type = JoinType.LeftJoin, JoinObjectFilter = combineEntry.Query, JoinCriteria = exceptFields.Select(pk => { var pkJoinField = new JoinField() { Name = pk, Type = JoinFieldType.Field }; return(RegularJoinCriterion.Create(FieldInfo.Create(pk), CriterionOperator.Equal, FieldInfo.Create(pk)) as IJoinCriterion); }).ToList(), JoinObjectExtraFilter = exceptQuery }; query.Join(exceptJoinItem); break; case CombineType.Intersect: var intersectFields = GetCombineFields(query, combineEntry.Query); query.Join(intersectFields.ToDictionary(c => c, c => c), JoinType.InnerJoin, CriterionOperator.Equal, combineEntry.Query); break; default: var combineObjectPetName = GetNewSubObjectPetName(); string combineObjectName = DataAccessContext.GetCombineEntityObjectName(combineEntry.Query); var combineQueryResult = ExecuteTranslation(combineEntry.Query, QueryLocation.Combine, parameters, combineObjectPetName, true); string combineConditionString = string.IsNullOrWhiteSpace(combineQueryResult.ConditionString) ? string.Empty : $"WHERE {combineQueryResult.ConditionString}"; combineBuilder.Append($" {GetCombineOperator(combineEntry.Type)} SELECT {string.Join(",", PostgreSqlManager.FormatQueryFields(combineObjectPetName, query, query.GetEntityType(), true, false))} FROM {PostgreSqlManager.WrapKeyword(combineObjectName)} AS {combineObjectPetName} {(combineQueryResult.AllowJoin ? combineQueryResult.JoinScript : string.Empty)} {combineConditionString}"); if (!combineQueryResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(combineQueryResult.WithScripts); recurveTableName = combineQueryResult.RecurveObjectName; recurveTablePetName = combineQueryResult.RecurvePetName; } break; } } } #endregion #region join bool allowJoin = true; StringBuilder joinBuilder = new StringBuilder(); StringBuilder joinExtraCondition = new StringBuilder(); if (!query.Joins.IsNullOrEmpty()) { foreach (var joinEntry in query.Joins) { if (joinEntry == null || joinEntry.JoinObjectFilter == null) { continue; } if (joinEntry.JoinObjectFilter.GetEntityType() == null) { throw new EZNEWException("IQuery object must set entity type if use in join operation"); } string joinObjectPetName = GetNewSubObjectPetName(); var joinQueryResult = ExecuteTranslation(joinEntry.JoinObjectFilter, QueryLocation.Join, parameters, joinObjectPetName, true); if (string.IsNullOrWhiteSpace(joinQueryResult.CombineScript)) { var joinResult = GetJoinCondition(query, joinEntry, parameters, objectName, joinObjectPetName); if (!joinResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(joinResult.WithScripts); recurveTableName = joinResult.RecurveObjectName; recurveTablePetName = joinResult.RecurvePetName; } var joinConnection = joinResult.ConditionString; if (!string.IsNullOrWhiteSpace(joinQueryResult.ConditionString)) { conditionBuilder.Append($"{(conditionBuilder.Length == 0 ? string.Empty : " AND ")}{joinQueryResult.ConditionString}"); } if (!string.IsNullOrWhiteSpace(joinQueryResult.JoinExtraConditionString)) { conditionBuilder.Append($"{(conditionBuilder.Length == 0 ? string.Empty : " AND ")}{joinQueryResult.JoinExtraConditionString}"); } joinBuilder.Append($" {GetJoinOperator(joinEntry.Type)} {PostgreSqlManager.WrapKeyword(DataAccessContext.GetJoinEntityObjectName(joinEntry.JoinObjectFilter))} AS {joinObjectPetName}{joinConnection}"); if (joinEntry.JoinObjectExtraFilter != null) { var extraQueryResult = ExecuteTranslation(joinEntry.JoinObjectExtraFilter, QueryLocation.Join, parameters, joinObjectPetName, true); if (!string.IsNullOrWhiteSpace(extraQueryResult.ConditionString)) { joinExtraCondition.Append(joinExtraCondition.Length > 0 ? $" AND {extraQueryResult.ConditionString}" : extraQueryResult.ConditionString); } } if (joinQueryResult.AllowJoin && !string.IsNullOrWhiteSpace(joinQueryResult.JoinScript)) { joinBuilder.Append($" {joinQueryResult.JoinScript}"); } } else { var combineJoinObjName = GetNewSubObjectPetName(); var joinResult = GetJoinCondition(query, joinEntry, parameters, objectName, combineJoinObjName); if (!joinResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(joinResult.WithScripts); recurveTableName = joinResult.RecurveObjectName; recurveTablePetName = joinResult.RecurvePetName; } var joinConnection = joinResult.ConditionString; joinBuilder.Append($" {GetJoinOperator(joinEntry.Type)} (SELECT {string.Join(",", PostgreSqlManager.FormatQueryFields(joinObjectPetName, joinEntry.JoinObjectFilter, joinEntry.JoinObjectFilter.GetEntityType(), false, false))} FROM {PostgreSqlManager.WrapKeyword(DataAccessContext.GetJoinEntityObjectName(joinEntry.JoinObjectFilter))} AS {joinObjectPetName} {(joinQueryResult.AllowJoin ? joinQueryResult.JoinScript : string.Empty)} {(string.IsNullOrWhiteSpace(joinQueryResult.ConditionString) ? string.Empty : "WHERE " + joinQueryResult.ConditionString)} {joinQueryResult.CombineScript}) AS {combineJoinObjName}{joinConnection}"); } if (!joinQueryResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(joinQueryResult.WithScripts); recurveTableName = joinQueryResult.RecurveObjectName; recurveTablePetName = joinQueryResult.RecurvePetName; } } } string joinScript = joinBuilder.ToString(); #endregion #region recurve script string conditionString = conditionBuilder.ToString(); string joinExtraConditionString = joinExtraCondition.ToString(); if (query.Recurve != null) { allowJoin = false; string nowConditionString = conditionString; if (!string.IsNullOrWhiteSpace(joinExtraConditionString)) { nowConditionString = string.IsNullOrWhiteSpace(nowConditionString) ? joinExtraConditionString : $"{nowConditionString} AND {joinExtraConditionString}"; joinExtraConditionString = string.Empty; } EntityField recurveField = DataManager.GetField(DatabaseServerType, query, query.Recurve.DataField); EntityField recurveRelationField = DataManager.GetField(DatabaseServerType, query, query.Recurve.RelationField); var recurveTable = GetNewRecurveTableName(); recurveTablePetName = recurveTable.Item1; recurveTableName = recurveTable.Item2; conditionString = $"{objectName}.{PostgreSqlManager.WrapKeyword(recurveField.FieldName)} IN (SELECT {recurveTablePetName}.{PostgreSqlManager.WrapKeyword(recurveField.FieldName)} FROM {recurveTableName} AS {recurveTablePetName})"; DataAccessContext.SetActivityQuery(query, location); string queryObjectName = DataManager.GetEntityObjectName(DataAccessContext); string withScript = $"{recurveTableName} AS (SELECT {objectName}.{PostgreSqlManager.WrapKeyword(recurveField.FieldName)},{objectName}.{PostgreSqlManager.WrapKeyword(recurveRelationField.FieldName)} FROM {PostgreSqlManager.WrapKeyword(queryObjectName)} AS {objectName} {joinScript} {(string.IsNullOrWhiteSpace(nowConditionString) ? string.Empty : $"WHERE {nowConditionString}")} " + $"UNION ALL SELECT {objectName}.{PostgreSqlManager.WrapKeyword(recurveField.FieldName)},{objectName}.{PostgreSqlManager.WrapKeyword(recurveRelationField.FieldName)} FROM {PostgreSqlManager.WrapKeyword(queryObjectName)} AS {objectName} JOIN {recurveTableName} AS {recurveTablePetName} " + $"ON {(query.Recurve.Direction == RecurveDirection.Up ? $"{objectName}.{PostgreSqlManager.WrapKeyword(recurveField.FieldName)}={recurveTablePetName}.{PostgreSqlManager.WrapKeyword(recurveRelationField.FieldName)}" : $"{objectName}.{PostgreSqlManager.WrapKeyword(recurveRelationField.FieldName)}={recurveTablePetName}.{PostgreSqlManager.WrapKeyword(recurveField.FieldName)}")})";
/// <summary> /// Execute translation /// </summary> /// <param name="query">Query object</param> /// <param name="location">Query location</param> /// <param name="parameters">Parameters</param> /// <param name="objectName">Entity object name</param> /// <param name="useSort">Indicates whether use sort</param> /// <returns>Return a translation result</returns> public QueryTranslationResult ExecuteTranslation(IQuery query, QueryLocation location, CommandParameters parameters = null, string objectName = "", bool useSort = true) { if (query == null) { return(QueryTranslationResult.Empty); } StringBuilder conditionBuilder = new StringBuilder(); if (query.ExecutionMode == QueryExecutionMode.QueryObject) { StringBuilder orderBuilder = new StringBuilder(); parameters = parameters ?? new CommandParameters(); objectName = string.IsNullOrWhiteSpace(objectName) ? DefaultObjectPetName : objectName; List <string> withScripts = new List <string>(); string recurveTableName = string.Empty; string recurveTablePetName = string.Empty; #region condition if (!query.Conditions.IsNullOrEmpty()) { int index = 0; foreach (var condition in query.Conditions) { var conditionResult = TranslateCondition(query, condition, parameters, objectName); if (!conditionResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(conditionResult.WithScripts); recurveTableName = conditionResult.RecurveObjectName; recurveTablePetName = conditionResult.RecurvePetName; } conditionBuilder.Append($" {(index > 0 ? condition.Connector.ToString().ToUpper() : string.Empty)} {conditionResult.ConditionString}"); index++; } } #endregion #region sort if (useSort && !query.Sorts.IsNullOrEmpty()) { foreach (var sortEntry in query.Sorts) { orderBuilder.Append($"{ConvertSortFieldName(query, objectName, sortEntry)} {(sortEntry.Desc ? DescKeyWord : AscKeyWord)},"); } } #endregion #region combine StringBuilder combineBuilder = new StringBuilder(); if (!query.Combines.IsNullOrEmpty()) { foreach (var combineEntry in query.Combines) { if (combineEntry?.Query == null) { continue; } var combineObjectPetName = GetNewSubObjectPetName(); string combineObjectName = DataAccessContext.GetCombineEntityObjectName(combineEntry.Query); var combineQueryResult = ExecuteTranslation(combineEntry.Query, QueryLocation.Combine, parameters, combineObjectPetName, true); string combineConditionString = string.IsNullOrWhiteSpace(combineQueryResult.ConditionString) ? string.Empty : $"WHERE {combineQueryResult.ConditionString}"; combineBuilder.Append($" {GetCombineOperator(combineEntry.Type)} SELECT {string.Join(",", SQLiteManager.FormatQueryFields(combineObjectPetName, query, query.GetEntityType(), true, false))} FROM {SQLiteManager.WrapKeyword(combineObjectName)} AS {combineObjectPetName} {(combineQueryResult.AllowJoin ? combineQueryResult.JoinScript : string.Empty)} {combineConditionString}"); if (!combineQueryResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(combineQueryResult.WithScripts); recurveTableName = combineQueryResult.RecurveObjectName; recurveTablePetName = combineQueryResult.RecurvePetName; } } } #endregion #region join bool allowJoin = true; StringBuilder joinBuilder = new StringBuilder(); StringBuilder joinExtraCondition = new StringBuilder(); if (!query.Joins.IsNullOrEmpty()) { foreach (var joinEntry in query.Joins) { if (joinEntry == null || joinEntry.JoinObjectFilter == null) { continue; } if (joinEntry.JoinObjectFilter.GetEntityType() == null) { throw new EZNEWException("IQuery object must set entity type if use in join operation"); } string joinObjectPetName = GetNewSubObjectPetName(); var joinQueryResult = ExecuteTranslation(joinEntry.JoinObjectFilter, QueryLocation.Join, parameters, joinObjectPetName, true); if (string.IsNullOrWhiteSpace(joinQueryResult.CombineScript)) { var joinResult = GetJoinCondition(query, joinEntry, parameters, objectName, joinObjectPetName); if (!joinResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(joinResult.WithScripts); recurveTableName = joinResult.RecurveObjectName; recurveTablePetName = joinResult.RecurvePetName; } var joinConnection = joinResult.ConditionString; if (!string.IsNullOrWhiteSpace(joinQueryResult.ConditionString)) { if (joinQueryResult.AllowJoin && PositionJoinConditionToConnection(joinEntry.Type)) { joinConnection += $"{(string.IsNullOrWhiteSpace(joinConnection) ? " ON" : " AND ")}{joinQueryResult.ConditionString}"; } else { conditionBuilder.Append($"{(conditionBuilder.Length == 0 ? string.Empty : " AND ")}{joinQueryResult.ConditionString}"); } } if (!string.IsNullOrWhiteSpace(joinQueryResult.JoinExtraConditionString)) { conditionBuilder.Append($"{(conditionBuilder.Length == 0 ? string.Empty : " AND ")}{joinQueryResult.JoinExtraConditionString}"); } joinBuilder.Append($" {GetJoinOperator(joinEntry.Type)} {SQLiteManager.WrapKeyword(DataAccessContext.GetJoinEntityObjectName(joinEntry.JoinObjectFilter))} AS {joinObjectPetName}{joinConnection}"); if (joinEntry.JoinObjectExtraFilter != null) { var extraQueryResult = ExecuteTranslation(joinEntry.JoinObjectExtraFilter, QueryLocation.Join, parameters, joinObjectPetName, true); if (!string.IsNullOrWhiteSpace(extraQueryResult.ConditionString)) { joinExtraCondition.Append(joinExtraCondition.Length > 0 ? $" AND {extraQueryResult.ConditionString}" : extraQueryResult.ConditionString); } } if (joinQueryResult.AllowJoin && !string.IsNullOrWhiteSpace(joinQueryResult.JoinScript)) { joinBuilder.Append($" {joinQueryResult.JoinScript}"); } } else { var combineJoinObjName = GetNewSubObjectPetName(); var joinResult = GetJoinCondition(query, joinEntry, parameters, objectName, combineJoinObjName); if (!joinResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(joinResult.WithScripts); recurveTableName = joinResult.RecurveObjectName; recurveTablePetName = joinResult.RecurvePetName; } var joinConnection = joinResult.ConditionString; joinBuilder.Append($" {GetJoinOperator(joinEntry.Type)} (SELECT {string.Join(",", SQLiteManager.FormatQueryFields(joinObjectPetName, joinEntry.JoinObjectFilter, joinEntry.JoinObjectFilter.GetEntityType(), false, false))} FROM {SQLiteManager.WrapKeyword(DataAccessContext.GetJoinEntityObjectName(joinEntry.JoinObjectFilter))} AS {joinObjectPetName} {(joinQueryResult.AllowJoin ? joinQueryResult.JoinScript : string.Empty)} {(string.IsNullOrWhiteSpace(joinQueryResult.ConditionString) ? string.Empty : "WHERE " + joinQueryResult.ConditionString)} {joinQueryResult.CombineScript}) AS {combineJoinObjName}{joinConnection}"); } if (!joinQueryResult.WithScripts.IsNullOrEmpty()) { withScripts.AddRange(joinQueryResult.WithScripts); recurveTableName = joinQueryResult.RecurveObjectName; recurveTablePetName = joinQueryResult.RecurvePetName; } } } string joinScript = joinBuilder.ToString(); #endregion #region recurve script string conditionString = conditionBuilder.ToString(); string joinExtraConditionString = joinExtraCondition.ToString(); if (query.Recurve != null) { allowJoin = false; string nowConditionString = conditionString; if (!string.IsNullOrWhiteSpace(joinExtraConditionString)) { nowConditionString = string.IsNullOrWhiteSpace(nowConditionString) ? joinExtraConditionString : $"{nowConditionString} AND {joinExtraConditionString}"; joinExtraConditionString = string.Empty; } EntityField recurveField = DataManager.GetField(DatabaseServerType, query, query.Recurve.DataField); EntityField recurveRelationField = DataManager.GetField(DatabaseServerType, query, query.Recurve.RelationField); var recurveTable = GetNewRecurveTableName(); recurveTablePetName = recurveTable.Item1; recurveTableName = recurveTable.Item2; conditionString = $"{objectName}.{SQLiteManager.WrapKeyword(recurveField.FieldName)} IN (SELECT {recurveTablePetName}.{SQLiteManager.WrapKeyword(recurveField.FieldName)} FROM {SQLiteManager.WrapKeyword(recurveTableName)} AS {recurveTablePetName})"; DataAccessContext.SetActivityQuery(query, location); string queryObjectName = DataManager.GetEntityObjectName(DataAccessContext); string withScript = $"{recurveTableName} AS (SELECT {objectName}.{SQLiteManager.WrapKeyword(recurveField.FieldName)},{objectName}.{SQLiteManager.WrapKeyword(recurveRelationField.FieldName)} FROM {SQLiteManager.WrapKeyword(queryObjectName)} AS {objectName} {joinScript} {(string.IsNullOrWhiteSpace(nowConditionString) ? string.Empty : $"WHERE {nowConditionString}")} " + $"UNION ALL SELECT {objectName}.{SQLiteManager.WrapKeyword(recurveField.FieldName)},{objectName}.{SQLiteManager.WrapKeyword(recurveRelationField.FieldName)} FROM {SQLiteManager.WrapKeyword(queryObjectName)} AS {objectName} JOIN {recurveTableName} AS {recurveTablePetName} " + $"ON {(query.Recurve.Direction == RecurveDirection.Up ? $"{objectName}.{SQLiteManager.WrapKeyword(recurveField.FieldName)}={recurveTablePetName}.{SQLiteManager.WrapKeyword(recurveRelationField.FieldName)}" : $"{objectName}.{SQLiteManager.WrapKeyword(recurveRelationField.FieldName)}={recurveTablePetName}.{SQLiteManager.WrapKeyword(recurveField.FieldName)}")})";