/// <summary> /// Binds the compiled expression. /// </summary> /// <returns></returns> public IBinaryDataListEntry BindCompiledExpression() { // very short circuit if no items ;) if (_internalKeyMap.Keys.Count == 0) { CompiledExpression = null; return(null); } // short circuit the long eval for mix mode data ;) if (_internalMap.Keys.Count <= 1 && FetchEvaluationIterationCount(Expression) == 1 && CompiledExpression.Length == 3) { return(_internalKeyMap.Values.FirstOrDefault()); } var replaceValue = string.Empty; // Right now we assume there are not ;) foreach (var idx in _internalMap.Keys) { var token = BuildSubToken(idx); var otherKey = _internalMap[idx]; IBinaryDataListEntry value; if (_internalKeyMap.TryGetValue(otherKey, out value)) { if (value != null) { if (!value.IsRecordset) { var scalar = value.FetchScalar(); if (scalar != null) { if (_result == null) { var toReplace = scalar.TheValue; CompiledExpression = CompiledExpression.Replace(token, toReplace); } else { var itr = _result.FetchRecordsetIndexes(); string replaceVal; try { replaceVal = scalar.TheValue; } catch (NullValueInVariableException) { replaceVal = null; } while (itr.HasMore()) { var val = itr.FetchNextIndex(); // Fetch the next value from result ;) try { string error; string template = _result.TryFetchRecordsetColumnAtIndex(GlobalConstants.EvaluationRsField, val, out error).TheValue; Errors.AddError(error); template = template.Replace(token, replaceVal); _result.TryPutRecordItemAtIndex(new BinaryDataListItem(template, _ns, GlobalConstants.EvaluationRsField, val), val, out error); Errors.AddError(error); } catch (NullValueInVariableException) { //Do nothing got null } } CompiledExpression = CompiledExpression.Replace(token, replaceVal); } } } else { string error; // build up the complex expression result - this means debug will be out of sync of complex expressions ;) if (_result == null) { IList <Dev2Column> cols = new List <Dev2Column> { new Dev2Column(GlobalConstants.EvaluationRsField, enDev2ColumnArgumentDirection.Both) }; _result = Dev2BinaryDataListFactory.CreateEntry(_ns, string.Empty, cols, BinaryDataList.UID); var max = _internalKeyMap.Values.OrderByDescending(c => c.ItemCollectionSize()).FirstOrDefault(); if (max != null) { var itrToVal = max.ItemCollectionSize(); if (itrToVal == 0) { itrToVal = 1; } for (int i = 0; i < itrToVal; i++) { int idxT = (i + 1); _result.TryPutRecordItemAtIndex(new BinaryDataListItem(CompiledExpression, _ns, GlobalConstants.EvaluationRsField, idxT), idxT, out error); Errors.AddError(error); } } if (IsDebug) { // attach audit object for debug ;) _result.ComplexExpressionAuditor = new ComplexExpressionAuditor(); } } var idxItr = value.FetchRecordsetIndexes(); int expIdx = 1; // we need to treat this as a scalar ;) if (idxItr.Count == 1) { int curVal = idxItr.FetchNextIndex(); int amt = _result.ItemCollectionSize(); // ensure we always iterate once ;) if (amt == 0) { amt = 1; } idxItr = new LoopedIndexIterator(curVal, amt); } // else iterate across the recordset cuz it had a star ;) while (idxItr.HasMore()) { try { var val = idxItr.FetchNextIndex(); // Fetch the next value from result ;) var template = _result.TryFetchRecordsetColumnAtIndex(GlobalConstants.EvaluationRsField, expIdx, out error).TheValue; Errors.AddError(error); var binaryValue = value.TryFetchIndexedRecordsetUpsertPayload(val, out error); Errors.AddError(error); // now bind this result row with the correct data list data ;) if (binaryValue != null) { var preTemplate = template; var toReplace = binaryValue.TheValue; template = template.Replace(token, toReplace); // In cases when [[[{0}]] is the result, we need to inject the template value // In cases when [[rec({0}).a]] we need to replace the template pattern ;) var tmp = CompiledExpression.Replace("[", "").Replace("]", "").Replace(token, string.Empty); // ReSharper disable ConvertIfStatementToConditionalTernaryExpression if (tmp.Length > 0) // ReSharper restore ConvertIfStatementToConditionalTernaryExpression { // we have a [[rec({0}.a]] case ;) replaceValue = toReplace; } else { replaceValue = template; } _result.TryPutRecordItemAtIndex(new BinaryDataListItem(template, _ns, GlobalConstants.EvaluationRsField, expIdx), expIdx, out error); Errors.AddError(error); if (IsDebug) { var displayValue = DataListUtil.AddBracketsToValueIfNotExist(binaryValue.DisplayValue); _result.ComplexExpressionAuditor.AddAuditStep(preTemplate, displayValue, token, idx, template, Expression); _result.ComplexExpressionAuditor.SetMaxIndex(expIdx); } } expIdx++; // inc result index ;) } catch (NullValueInVariableException) { //Do Nothing got null value } } replaceValue = DataListUtil.RemoveLanguageBrackets(replaceValue); CompiledExpression = CompiledExpression.Replace(token, replaceValue); } } else { CompiledExpression = CompiledExpression.Replace(token, string.Empty); } } } return(_result); }
IList <IDataListItem> ConvertIBinaryDataListEntryToIDataListItem(IBinaryDataListEntry dataListEntry) { IList <IDataListItem> result = new List <IDataListItem>(); if (dataListEntry.IsRecordset) { var sizeOfCollection = dataListEntry.ItemCollectionSize(); if (sizeOfCollection == 0) { sizeOfCollection++; } var count = 0; var fields = dataListEntry.Columns.Where(c => c.ColumnIODirection == enDev2ColumnArgumentDirection.Both || c.ColumnIODirection == enDev2ColumnArgumentDirection.Input).ToList(); while (count < sizeOfCollection) { string error; var items = dataListEntry.FetchRecordAt(count + 1, out error); foreach (var item in items) { // check field mapping ;) if (fields.Any(f => f.ColumnName == item.FieldName)) { IDataListItem singleRes = new DataListItem(); singleRes.IsRecordset = true; singleRes.Recordset = item.Namespace; singleRes.Field = item.FieldName; singleRes.RecordsetIndex = (count + 1).ToString(CultureInfo.InvariantCulture); try { singleRes.Value = item.TheValue; } catch (Exception) { singleRes.Value = null; } singleRes.DisplayValue = item.DisplayValue; var desc = dataListEntry.Columns.FirstOrDefault(c => c.ColumnName == item.FieldName); singleRes.Description = desc == null ? null : desc.ColumnDescription; result.Add(singleRes); } } count++; } } else { var item = dataListEntry.FetchScalar(); if (item != null) { IDataListItem singleRes = new DataListItem(); singleRes.IsRecordset = false; singleRes.Field = item.FieldName; singleRes.DisplayValue = item.FieldName; try { singleRes.Value = item.TheValue; } catch (Exception) { singleRes.Value = null; } var desc = dataListEntry.Description; singleRes.Description = string.IsNullOrWhiteSpace(desc) ? null : desc; result.Add(singleRes); } } return(result); }
IList<IDataListItem> ConvertIBinaryDataListEntryToIDataListItem(IBinaryDataListEntry dataListEntry) { IList<IDataListItem> result = new List<IDataListItem>(); if(dataListEntry.IsRecordset) { var sizeOfCollection = dataListEntry.ItemCollectionSize(); if(sizeOfCollection == 0) { sizeOfCollection++; } var count = 0; var fields = dataListEntry.Columns.Where(c => c.ColumnIODirection == enDev2ColumnArgumentDirection.Both || c.ColumnIODirection == enDev2ColumnArgumentDirection.Input).ToList(); while(count < sizeOfCollection) { string error; var items = dataListEntry.FetchRecordAt(count + 1, out error); foreach(var item in items) { // check field mapping ;) if(fields.Any(f => f.ColumnName == item.FieldName)) { IDataListItem singleRes = new DataListItem(); singleRes.IsRecordset = true; singleRes.Recordset = item.Namespace; singleRes.Field = item.FieldName; singleRes.RecordsetIndex = (count + 1).ToString(CultureInfo.InvariantCulture); try { singleRes.Value = item.TheValue; } catch(Exception) { singleRes.Value = null; } singleRes.DisplayValue = item.DisplayValue; var desc = dataListEntry.Columns.FirstOrDefault(c => c.ColumnName == item.FieldName); singleRes.Description = desc == null ? null : desc.ColumnDescription; result.Add(singleRes); } } count++; } } else { var item = dataListEntry.FetchScalar(); if(item != null) { IDataListItem singleRes = new DataListItem(); singleRes.IsRecordset = false; singleRes.Field = item.FieldName; singleRes.DisplayValue = item.FieldName; try { singleRes.Value = item.TheValue; } catch(Exception) { singleRes.Value = null; } var desc = dataListEntry.Description; singleRes.Description = string.IsNullOrWhiteSpace(desc) ? null : desc; result.Add(singleRes); } } return result; }