/// <summary> /// Create a DataQuery instance. /// </summary> /// <param name='type'>Type of data query.</param> public DataQuery(DataQueryType type) { _type = type; }
/// <summary> /// Fills the type of the data query. /// </summary> /// <param name="queryType">Type of the query.</param> /// <param name="complexDataQuery">The complex data query.</param> /// <exception cref="Org.Sdmxsource.Sdmx.Api.Exception.SdmxException">Too many Selection Groups in ComplexDataQuery max one supported.</exception> public void FillDataQueryType(DataQueryType queryType, IComplexDataQuery complexDataQuery) { var dataReturnDetailsType = new DataReturnDetailsType(); queryType.ReturnDetails = dataReturnDetailsType; // TODO: check null in java if (complexDataQuery.DefaultLimit.HasValue && complexDataQuery.DefaultLimit.Value != 0) { dataReturnDetailsType.defaultLimit = complexDataQuery.DefaultLimit; } if (complexDataQuery.FirstNObservations.HasValue) { dataReturnDetailsType.FirstNObservations = complexDataQuery.FirstNObservations; } if (complexDataQuery.LastNObservations.HasValue) { dataReturnDetailsType.LastNObservations = complexDataQuery.LastNObservations; } if (complexDataQuery.DataQueryDetail != null) { dataReturnDetailsType.detail = complexDataQuery.DataQueryDetail.EnumType.ToString(); } if (complexDataQuery.ObservationAction != null) { dataReturnDetailsType.observationAction = complexDataQuery.ObservationAction.EnumType.ToString(); } HandleStructure(complexDataQuery, dataReturnDetailsType); var whereType = new DataParametersAndType(); queryType.DataWhere = whereType; //DataSetId at DataWhere Leveln - Optional if (complexDataQuery.DatasetId != null) { var datasetIdType = new QueryIDType { TypedValue = complexDataQuery.DatasetId, @operator = complexDataQuery.DatasetIdOperator.Operator }; whereType.DataSetID.Add(datasetIdType); } //DataProvider at DataWhere Level - Optional; if (complexDataQuery.DataProvider != null && complexDataQuery.DataProvider.Count != 0) { HandleDataProvider(complexDataQuery, whereType); } //Dataflow at DataWhere Level - Optional if (complexDataQuery.Dataflow != null) { HandleDataflow(complexDataQuery, whereType); } //ProvisionAgreement at DataWhere Level - Optional if (complexDataQuery.ProvisionAgreement != null) { HandleProvisionAgreement(complexDataQuery, whereType); } //Updated tag that can have 0 to 2 repetitions if (complexDataQuery.LastUpdatedDateTimeRange != null) { HandleLastUpdatedTime(complexDataQuery, whereType); } //Selection Groups - and clause (DataWhere) if (complexDataQuery.SelectionGroups != null && complexDataQuery.SelectionGroups.Count > 1) { throw new SdmxException("Too many Selection Groups in ComplexDataQuery max one supported.", SdmxErrorCode.GetFromEnum(SdmxErrorCodeEnumType.SemanticError)); } if (complexDataQuery.SelectionGroups != null && complexDataQuery.SelectionGroups.Count != 0) { HandleSelectionGroups(complexDataQuery, whereType); } }
/// <summary> /// Build complex data queries from the specified <paramref name="dataQueryType"/> /// </summary> /// <param name="dataQueryType"> /// The data query type. /// </param> /// <param name="structureRetrievalManager"> /// The structure retrieval manager. /// </param> /// <returns> /// The list of complex data query from the specified <paramref name="dataQueryType"/> /// </returns> public IList<IComplexDataQuery> BuildComplexDataQuery(DataQueryType dataQueryType, ISdmxObjectRetrievalManager structureRetrievalManager) { if (structureRetrievalManager == null) { throw new SystemException("ComplexDataQueryBuilder expectes a ISdmxObjectRetrievalManager"); } IList<IComplexDataQuery> returnList = new List<IComplexDataQuery>(); DataReturnDetailsType returnDetails = dataQueryType.ReturnDetails; DataParametersAndType dataWhere = dataQueryType.DataWhere; /**process the DataReturnDetailsType*/ DataQueryDetail queryDetail = GetReturnDetailsDetail(returnDetails); int? firstNObs = GetReturnDetailsFirstNobs(returnDetails); int? lastNObs = GetReturnDetailsLastNobs(returnDetails); int defaultLimit = GetReturnDetailsDefaultLimit(returnDetails); ObservationAction obsAction = GetReturnDetailsObsAction(returnDetails); IList<object> structureReferenceDetails = GetStructureRefDetails(returnDetails); string dimensionAtObservation = (structureReferenceDetails != null) ? (string)structureReferenceDetails[0] : null; bool hasExplicitMeasures = (structureReferenceDetails != null && structureReferenceDetails.Count > 1) ? (bool)structureReferenceDetails[1] : false; /**process the DataParametersAndType*/ //Get the Data Providers ISet<IDataProvider> dataProviders = GetDataWhereDataProviders(dataWhere, structureRetrievalManager); //Get the DatasetId string[] datasetArray = GetDataWhereDatasetId(dataWhere); string datasetId = (datasetArray != null) ? datasetArray[0] : null; TextSearch datasetIdOper = (datasetArray != null) ? TextSearch.ParseString(datasetArray[1]) : null; //Get the DataFlow IDataflowObject dataFlow = GetDataWhereDataFlow(dataWhere, structureRetrievalManager); //Get the Data Structure Definition IDataStructureObject dataStructure = GetDataWhereDataStrucuture(dataWhere, structureRetrievalManager, dataFlow); //Get the Provision Agreement IProvisionAgreementObject provisionAgreement = GetProvisionAgreement(dataWhere, structureRetrievalManager); //Get the Updated dates IList<ITimeRange> updatedDates = GetDataWhereUpdatedDates(dataWhere); //Get the ComplexDataQueryGoups ISet<IComplexDataQuerySelectionGroup> complexDataQuerySelectionGroups = BuildComplexDataQueryGroups(dataWhere, structureRetrievalManager, dataProviders); //Build the complex Query IComplexDataQuery complexQuery = new ComplexDataQueryImpl(datasetId, datasetIdOper, dataProviders, dataStructure, dataFlow, provisionAgreement, updatedDates, firstNObs, lastNObs, defaultLimit, obsAction, dimensionAtObservation, hasExplicitMeasures, queryDetail, complexDataQuerySelectionGroups); returnList.Add(complexQuery); return returnList; }
private static string GetCondition(DataQueryType dataQueryType, object propertyValue, string columnName, Operator optr) { if (propertyValue == null) { return(string.Empty); } if (optr == Operator.In) { #region In if (propertyValue is IList <long> ) { return(string.Format("{0} IN ({1})", columnName, GetSqlInConditions((IEnumerable <long>)propertyValue))); } else if (propertyValue is IList <int> ) { return(string.Format("{0} IN ({1})", columnName, GetSqlInConditions((IEnumerable <int>)propertyValue))); } else if (propertyValue is IList <string> ) { return(string.Format("{0} IN ({1})", columnName, GetSqlInConditions((IEnumerable <string>)propertyValue))); } else { return(string.Empty); } #endregion } if (optr == Operator.NotIn) { #region Not In if (propertyValue is IList <long> ) { return(string.Format("{0} Not IN ({1})", columnName, GetSqlInConditions((IEnumerable <long>)propertyValue))); } else if (propertyValue is IList <int> ) { return(string.Format("{0} Not IN ({1})", columnName, GetSqlInConditions((IEnumerable <int>)propertyValue))); } else if (propertyValue is IList <string> ) { return(string.Format("{0} Not IN ({1})", columnName, GetSqlInConditions((IEnumerable <string>)propertyValue))); } else { return(string.Empty); } #endregion } if (dataQueryType == DataQueryType.String) { #region string value = (string)propertyValue; value = value.Replace(@"\", @"\\"); if (string.IsNullOrEmpty(value)) { return(string.Empty); } switch (optr) { case Operator.Eq: return(string.Format("{0}='{1}'", columnName, value)); case Operator.NotEq: return(string.Format("{0}!='{1}'", columnName, value)); case Operator.Like: return(string.Format("{0} like '%{1}%'", columnName, value)); case Operator.LeftLike: return(string.Format("{0} like '{1}%'", columnName, value)); case Operator.RightLike: return(string.Format("{0} like '%{1}'", columnName, value)); default: throw new Exception(string.Format("当属性类型是string时,操作符{0}无效", optr)); } #endregion } else if (dataQueryType == DataQueryType.DateTime) { #region DateTime value = (DateTime)propertyValue; if (value == DateTime.MinValue) { return(string.Empty); } switch (optr) { case Operator.Eq: return(string.Format("{0}='{1}'", columnName, value.ToString(_DateTimeFormat))); case Operator.NotEq: return(string.Format("{0}!='{1}'", columnName, value.ToString(_DateTimeFormat))); case Operator.Gt: return(string.Format("{0}>'{1}'", columnName, value.ToString(_DateTimeFormat))); case Operator.Ge: return(string.Format("{0}>='{1}'", columnName, value.ToString(_DateTimeFormat))); case Operator.Lt: return(string.Format("{0}<'{1}'", columnName, value.ToString(_DateTimeFormat))); case Operator.Le: return(string.Format("{0}<='{1}'", columnName, value.ToString(_DateTimeFormat))); default: throw new Exception(string.Format("当属性类型是DateTime时,操作符{0}无效", optr)); } #endregion } else if (dataQueryType == DataQueryType.Number) { #region switch (optr) { case Operator.Eq: return(string.Format("{0}={1}", columnName, propertyValue.ToString())); case Operator.NotEq: return(string.Format("{0}!={1}", columnName, propertyValue.ToString())); case Operator.Gt: return(string.Format("{0}>{1}", columnName, propertyValue.ToString())); case Operator.Ge: return(string.Format("{0}>={1}", columnName, propertyValue.ToString())); case Operator.Lt: return(string.Format("{0}<{1}", columnName, propertyValue.ToString())); case Operator.Le: return(string.Format("{0}<={1}", columnName, propertyValue.ToString())); default: throw new Exception(string.Format("当属性类型是整数时,操作符{0}无效", optr)); } #endregion } else if (dataQueryType == DataQueryType.Enum) { #region if (EnumMetaDataCacheManager.GetEnumStorageType(propertyValue.GetType()) == EnumStorageType.EnumName) { string value = propertyValue.ToString(); switch (optr) { case Operator.Eq: return(string.Format("{0}='{1}'", columnName, value)); case Operator.NotEq: return(string.Format("{0}!='{1}'", columnName, value)); default: throw new Exception(string.Format("当属性类型是枚举时,操作符{0}无效", optr)); } } else { int value = (int)propertyValue; switch (optr) { case Operator.Eq: return(string.Format("{0}={1}", columnName, value)); case Operator.NotEq: return(string.Format("{0}!={1}", columnName, value)); default: throw new Exception(string.Format("当属性类型是枚举时,操作符{0}无效", optr)); } } #endregion } else { throw new Exception(string.Format("查询条件的属性类型{0}无效", dataQueryType)); } }