public void Set(object fromKey, IEnumerable toKeys) { // load existing keys var currentToKeys = GetToKeys(fromKey); // remove missed relations var fromCondition = ComposeFromCondition(fromKey); var deleteCondition = fromCondition; var toKeysArr = toKeys.Cast<object>().ToArray(); if (toKeysArr.Length>0) { deleteCondition = new QueryConditionNode((QField)ToFieldName, Conditions.In | Conditions.Not, new QConst(toKeysArr)) & deleteCondition; } Dalc.Delete(new Query(RelationSourceName, deleteCondition)); var data = ExtraKeys == null ? new Hashtable() : new Hashtable( new DictionaryWrapper<string,object>(ExtraKeys) ); data[FromFieldName] = fromKey; int pos = 1; foreach (var toKey in toKeys) { data[ToFieldName] = toKey; if (PositionFieldName != null) data[PositionFieldName] = pos++; if (Contains(toKey, currentToKeys)) { if (PositionFieldName!=null) Dalc.Update( new Hashtable() { {PositionFieldName, data[PositionFieldName]} }, new Query(RelationSourceName, (QField)ToFieldName == new QConst(toKey) & fromCondition)); } else { Dalc.Insert(data, RelationSourceName); } } }
protected override string BuildCondition(QueryConditionNode node) { string lvalue = BuildValue(node.LValue); string rvalue = BuildValue(node.RValue); Conditions condition = (node.Condition | Conditions.Not) ^ Conditions.Not; string res = null; for (int i = 0; i < enumConditions.Length; i++) { if (enumConditions[i] == condition) { res = stringConditions[i]; break; // first match } } if (res == null) { throw new ArgumentException("Invalid conditions set", condition.ToString()); } if ((node.Condition & Conditions.Not) == Conditions.Not) { res = "!" + res; } if ((node.Condition & Conditions.Null) == Conditions.Null) { rvalue = "null"; } string result = String.Format("{0}{1}{2}", lvalue, res, rvalue); if (!String.IsNullOrEmpty(node.Name)) { result = String.Format("(<{0}> {1})", node.Name, result); } return(result); }
public void Set(object fromKey, IEnumerable toKeys) { // load existing keys var currentToKeys = GetToKeys(fromKey); // remove missed relations var fromCondition = ComposeFromCondition(fromKey); var deleteCondition = fromCondition; var toKeysArr = toKeys.Cast <object>().ToArray(); if (toKeysArr.Length > 0) { deleteCondition = new QueryConditionNode((QField)ToFieldName, Conditions.In | Conditions.Not, new QConst(toKeysArr)) & deleteCondition; } Dalc.Delete(new Query(RelationSourceName, deleteCondition)); var data = ExtraKeys == null ? new Hashtable() : new Hashtable(new DictionaryWrapper <string, object>(ExtraKeys)); data[FromFieldName] = fromKey; int pos = 1; foreach (var toKey in toKeys) { data[ToFieldName] = toKey; if (PositionFieldName != null) { data[PositionFieldName] = pos++; } if (Contains(toKey, currentToKeys)) { if (PositionFieldName != null) { Dalc.Update(new Hashtable() { { PositionFieldName, data[PositionFieldName] } }, new Query(RelationSourceName, (QField)ToFieldName == new QConst(toKey) & fromCondition)); } } else { Dalc.Insert(data, RelationSourceName); } } }
private void SaveInternal(IDictionary data, IFileObject fileObject, string tableName, string keyFieldName) { QueryConditionNode conditions = new QueryConditionNode((QField)keyFieldName, Conditions.Equal, (QConst)fileObject.Name); Hashtable recordData = new Hashtable(); int result = Dalc.RecordsCount(new Query(tableName, conditions)); if (result > 0) { if (data.Contains(keyFieldName)) { data.Remove(keyFieldName); // fixed DB bug on update } Dalc.Update(new Query(tableName, conditions), data); } else { Dalc.Insert(tableName, data); } }
protected QueryNode ParseCondition(string input, int startIdx, out int endIdx) { IQueryValue leftValue = ParseInternal(input, startIdx, out endIdx); int nextEndIdx; Conditions conditions = Conditions.Equal; LexemType nextLexemType = GetLexemType(input, endIdx, out nextEndIdx); if (!GetCondition(nextLexemType, input, endIdx, ref nextEndIdx, ref conditions)) { throw new RelExParseException( String.Format("Invalid syntax (position: {0}, expression: {1})", startIdx, input)); } IQueryValue rightValue = ParseInternal(input, nextEndIdx, out endIdx); QueryNode node; if (IsNullValue(rightValue)) { if ((conditions & Conditions.Equal) != 0) { node = new QueryConditionNode(leftValue, Conditions.Null | (conditions & ~Conditions.Equal), null); } else { throw new RelExParseException( String.Format("Invalid syntax - such condition cannot be used with 'null' (position: {0}, expression: {1})", startIdx, input)); } } else { node = new QueryConditionNode(leftValue, conditions, rightValue); } return(node); }
protected QueryNode ComposeCondition(Expression expression) { if (expression is UnaryExpression) { UnaryExpression unExpr = (UnaryExpression)expression; QueryNode qNode = ComposeCondition(unExpr.Operand); return(unExpr.NodeType == ExpressionType.Not ? new QueryNegationNode(qNode) : qNode); } if (expression is LambdaExpression) { LambdaExpression lambdaExpr = (LambdaExpression)expression; return(ComposeCondition(lambdaExpr.Body)); } if (expression is BinaryExpression) { BinaryExpression binExpr = (BinaryExpression)expression; if (binExpr.NodeType == ExpressionType.AndAlso || binExpr.NodeType == ExpressionType.OrElse) { QueryGroupNode qGroup = new QueryGroupNode(binExpr.NodeType == ExpressionType.AndAlso ? QueryGroupNodeType.And : QueryGroupNodeType.Or); qGroup.Nodes.Add(ComposeCondition(binExpr.Left)); qGroup.Nodes.Add(ComposeCondition(binExpr.Right)); return(qGroup); } if (conditionMapping.ContainsKey(binExpr.NodeType)) { IQueryValue rightValue = ComposeValue(binExpr.Right); IQueryValue leftValue = ComposeValue(binExpr.Left); Conditions qCond = conditionMapping[binExpr.NodeType]; // process == and != null/DBNull.Value specifically if (rightValue is QConst && (Convert.IsDBNull(((QConst)rightValue).Value) || ((QConst)rightValue).Value == null)) { qCond = nullConditionMapping[binExpr.NodeType]; } QueryConditionNode qCondNode = new QueryConditionNode(leftValue, qCond, rightValue); return(qCondNode); } } else if (expression is MethodCallExpression) { MethodCallExpression methodExpr = (MethodCallExpression)expression; // check for special method call like 'In' or 'Like' if (methodExpr.Method.Name == "In") { QField fldValue = ComposeFieldValue(methodExpr.Object); IQueryValue inValue = ComposeValue(methodExpr.Arguments[0]); // possible conversion to IList if (inValue is QConst) { var inConstValue = (QConst)inValue; if (!(inConstValue.Value is IList)) { IList constList = new ArrayList(); foreach (object o in ((IEnumerable)inConstValue.Value)) { constList.Add(o); } if (constList.Count == 0) // means 'nothing'? { return(new QueryConditionNode((QConst)"1", Conditions.Equal, (QConst)"2")); } inValue = new QConst(constList); } } return(new QueryConditionNode(fldValue, Conditions.In, inValue)); } else if (methodExpr.Method.Name == "Like") { QField fldValue = ComposeFieldValue(methodExpr.Object); IQueryValue likeValue = ComposeValue(methodExpr.Arguments[0]); return(new QueryConditionNode(fldValue, Conditions.Like, likeValue)); } } throw new NotSupportedException(); }
protected QueryNode TranslateConditionNode(Class dataClass, QueryConditionNode node) { if (node.LValue is QField && node.RValue is QField) { throw new NotSupportedException("Cannot compare 2 class properties"); } if (node.LValue is QField) { var lFld = (QField)node.LValue; // check for related property if (lFld.Prefix != null) { var rel = dataClass.Schema.FindRelationshipByID(lFld.Prefix); if (rel == null) { rel = dataClass.Schema.InferRelationshipByID(lFld.Prefix, dataClass); } if (rel != null) { return(ComposeRelatedPropertyCondition(dataClass, rel, lFld, node.Condition, TranslateQueryValue(node.RValue))); } } var lValProperty = dataClass.FindPropertyByID(lFld.Name); if (lValProperty != null) { return(ComposePropertyCondition(dataClass, lValProperty, node.Condition, TranslateQueryValue(node.RValue))); } } if (node.RValue is QField) { var rFld = (QField)node.RValue; var cnd = node.Condition; if ((cnd & Conditions.GreaterThan) == Conditions.GreaterThan) { cnd = (cnd & ~Conditions.GreaterThan) | Conditions.LessThan; } else if ((cnd & Conditions.LessThan) == Conditions.LessThan) { cnd = (cnd & ~Conditions.LessThan) | Conditions.GreaterThan; } if (rFld.Prefix != null) { var rel = dataClass.Schema.FindRelationshipByID(rFld.Prefix); if (rel != null) { return(ComposeRelatedPropertyCondition(dataClass, rel, rFld, cnd, TranslateQueryValue(node.LValue))); } } var rValProperty = dataClass.FindPropertyByID(rFld.Name); if (rValProperty != null) { return(ComposePropertyCondition(dataClass, rValProperty, cnd, TranslateQueryValue(node.LValue))); } } var translatedNode = new QueryConditionNode( TranslateQueryValue(node.LValue), node.Condition, TranslateQueryValue(node.RValue)); return(translatedNode); }