Exemple #1
0
		public override void Visit (Column column)
		{
			if (column.TableName != String.Empty)
				_Query.Append (column.TableName).Append (".");

			_Query.Append (FormatAttribute (column.ColumnName)).Append (" ");

			if (column.Alias != null && column.Alias != string.Empty)
				_Query.AppendFormat ("AS {0} ", FormatAttribute (column.Alias));
		}
Exemple #2
0
        /// <summary>
        /// Processes the attributes.
        /// </summary>
        /// <param name="entityMap">Entity mapping whitch contains the attribute</param>
        /// <param name="name">Name of the attribute</param>
        /// <param name="loadAttributes"></param>
        /// <param name="checkAlias">Indicate if returned column must have an computed alias name or not</param>
        /// <returns>Return a <see cref="Column"/></returns>
        public ISQLExpression ProcessAttributes(EntityMapping entityMap, string name, bool loadAttributes, bool checkAlias)
        {
            if (name == "Id")
            {
                Table currentTable = (Table)tableContext.Peek();
                return new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0]));
            }
            else
            {
                // Exception management : attribute mapping
                AttributeMapping attributeMapping = entityMap.Attributes[name];

                //Attempt to get the attribute in the children types
                if (attributeMapping == null)
                    foreach (Evaluant.Uss.Models.Entity e in _Model.GetTree(entityMap.Type))
                    {
                        EntityMapping em = _Mapping.Entities[e.Type];
                        if ((attributeMapping = em.Attributes[name]) != null)
                            break;
                    }

                //Attempt to get the attribute in the parent types
                if (attributeMapping == null)
                {
                    Evaluant.Uss.Models.Entity cur = _Model.GetEntity(entityMap.Type);

                    while (_Model.GetParent(cur) != null)
                    {
                        cur = _Model.GetParent(cur);
                        EntityMapping current = _Mapping.Entities[cur.Type];

                        if (current.Attributes[name] != null)
                            attributeMapping = current.Attributes[name];
                    }
                }

                if (attributeMapping == null)
                    throw new UniversalStorageException(String.Format("The attribute '{0}' of the entity '{1}' is not defined in your mapping file", name, entityMap.Type));

                // If the attribute is contained in entity's table : the current query doesn't change
                if (attributeMapping.Table == entityMap.Table)
                {
                    Table currentTable = (Table)tableContext.Peek();

                    Column c = new Column(attributeMapping, currentTable.TableAlias, attributeMapping.Field);
                    //if (entityMap.Ids[attributeMapping.Field] != null)
                    //    c.ColumnName = entityMap.GetIdField(entityMap.Ids[attributeMapping.Field]);

                    if (checkAlias)
                    {
                        string attributeFullName = _Model.GetFullAttributeName(attributeMapping.Name, attributeMapping.ParentEntity.Type);
                        //if (entityMap.Ids[attributeMapping.Field] != null)
                        //    attributeFullName = entityMap.GetIdFieldAs(entityMap.Ids[attributeMapping.Field]);
                        //c.ColumnName = (_AliasColumnMapping.Contains(attributeFullName)) ? _AliasColumnMapping[attributeFullName].ToString() : "";
                        if (_AliasColumnMapping.Contains(attributeFullName))
                            c.ColumnName = _AliasColumnMapping[attributeFullName].ToString();
                    }
                    return c;
                }
                // Else : create a new select query
                else
                {
                    // Get the current context
                    Table currentTable = (Table)tableContext.Pop();
                    SelectStatement mainQuery = (SelectStatement)queryContext.Pop();

                    // Create a new query
                    SelectStatement currentQuery = new SelectStatement(attributeMapping);

                    Table attributeTable = new TableSource(attributeMapping, attributeMapping.Table, CreateUniqueAttributeAlias());
                    string atrbId = attributeMapping.Table == null || attributeMapping.Table == entityMap.Table ? entityMap.GetIdField(entityMap.Ids[0]) : attributeMapping.ParentField;

                    if (loadAttributes)
                    {
                        return this.ProcessAttributesWithLoadAttribute(mainQuery, currentQuery, currentTable, entityMap, attributeTable, attributeMapping, atrbId);
                    }
                    else
                    {
                        if (_IsFirstAttribute)
                        {
                            _IsFirstAttribute = false;

                            // if current table is a JoinedTable : 
                            //		from clause = mainQuery INNER JOIN attributeTable
                            if (currentTable.GetType() == typeof(JoinedTable))
                            {
                                JoinedTable lastJoinTable = new JoinedTable(mainQuery, loadAttributes ? TypeJoinedTable.LeftOuter : TypeJoinedTable.Inner, attributeTable);
                                lastJoinTable.TableAlias = mainQuery.TableAlias;

                                BinaryLogicExpression onCondition = new BinaryLogicExpression(
                                    new Column(entityMap.Ids[0], mainQuery.TableAlias, entityMap.GetIdField(entityMap.Ids[0])),
                                    BinaryLogicOperator.Equals,
                                    new Column(attributeMapping, attributeTable.TableAlias, atrbId));

                                if (mainQuery.WhereClause.SearchCondition.Count != 0)
                                {
                                    foreach (ILogicExpression expression in mainQuery.WhereClause.SearchCondition)
                                    {
                                        onCondition = new BinaryLogicExpression(onCondition, BinaryLogicOperator.And, expression);
                                    }
                                }

                                lastJoinTable.SearchConditions.Add(new BinaryLogicExpression(
                                    new Column(entityMap.Ids[0], mainQuery.TableAlias, entityMap.GetIdField(entityMap.Ids[0])),
                                    BinaryLogicOperator.Equals,
                                    new Column(entityMap.Ids[0], attributeTable.TableAlias, atrbId)));

                                currentQuery.TableAlias = mainQuery.TableAlias;
                                currentQuery.FromClause.Add(lastJoinTable);

                                currentQuery.WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                                    new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Discriminator),
                                    BinaryLogicOperator.Equals,
                                    new Constant(name, DbType.AnsiString)));


                                tableContext.Push(lastJoinTable);
                                queryContext.Push(currentQuery);

                                return new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Field);

                            }
                            // else 
                            //		from clause = currentTable INNER JOIN attributeTable
                            else
                            {

                                JoinedTable joinTable = new JoinedTable(currentTable, loadAttributes ? TypeJoinedTable.LeftOuter : TypeJoinedTable.Inner, attributeTable);
                                joinTable.SearchConditions.Add(new BinaryLogicExpression(
                                    new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0])),
                                    BinaryLogicOperator.Equals,
                                    new Column(attributeMapping, attributeTable.TableAlias, atrbId)));


                                currentQuery.TableAlias = CreateUniqueTableAlias();
                                currentQuery.FromClause.Add(joinTable);
                                currentQuery.WhereClause.SearchCondition = mainQuery.WhereClause.SearchCondition;

                                tableContext.Push(joinTable);
                            }
                        }
                        else
                        {
                            Table rightTable = ((JoinedTable)currentTable).RigthTable;

                            JoinedTable joinTable = new JoinedTable(attributeTable, TypeJoinedTable.Inner, rightTable);
                            joinTable.SearchConditions.Add(new BinaryLogicExpression(
                                new Column(attributeMapping, rightTable.TableAlias, atrbId),
                                BinaryLogicOperator.Equals,
                                new Column(attributeMapping, attributeTable.TableAlias, atrbId)));

                            ((JoinedTable)currentTable).RigthTable = joinTable;

                            currentQuery.SelectList.Add(new Column(entityMap.Ids[0], currentTable.TableAlias, entityMap.GetIdField(entityMap.Ids[0])));

                            currentQuery.TableAlias = currentTable.TableAlias;
                            currentQuery.FromClause.Add(currentTable);

                            tableContext.Push(currentTable);
                        }

                        queryContext.Push(currentQuery);
                        return new Column(attributeMapping, attributeTable.TableAlias, attributeMapping.Field);

                    }
                }
            }
        }
        public void Process(DeleteEntityCommand c)
        {
            EntityMapping deleteEntityMapping = _Mapping.Entities[c.Type, true];
            Hashtable cmds = new Hashtable();

            foreach (ReferenceMapping refm in deleteEntityMapping.References)
            {
                Evaluant.Uss.Models.Reference reference = _Engine.Model.GetReference(c.Type, refm.Name);
                EntityMapping entityChildMapping = _Mapping.Entities[refm.EntityChild];

                if (!reference.ToMany)
                    continue;
                foreach (RuleMapping rulem in refm.Rules)
                {
                    if (rulem.ParentTable == deleteEntityMapping.Table)
                    {
                        // update child table
                        SQLCommand cmd = (SQLCommand)cmds[rulem.ChildTable];
                        EntityMapping childEntity = _Mapping.Entities[refm.EntityChild];
                        if (!reference.IsComposition && rulem.ChildTable == childEntity.Table)
                        {
                            if (cmd == null)
                            {
                                cmd = new UpdateCommand(rulem, rulem.ChildTable);
                                cmds.Add(rulem.ChildTable, cmd);
                            }
                        }
                        else
                        {
                            if (cmd == null)
                            {
                                cmd = new DeleteCommand(rulem, rulem.ChildTable);
                                cmds.Add(rulem.ChildTable, cmd);
                            }
                        }

                        //if (cmd is DeleteCommand)
                        //    continue;

                        for (int i = 0; i < rulem.ParentFields.Length; i++)
                        {
                            DbType dbType;
                            if (deleteEntityMapping.IdFields.Contains(refm.Rules[0].ParentFields[i]))
                            {
                                dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[refm.Rules[0].ParentFields[i]].Generator);
                            }
                            else
                            {
                                dbType = _Dialect.GetDbTypeToPrimaryKey(childEntity.Ids[refm.Rules[0].ChildFields[i]].Generator);
                            }


                            Constant newValue;
                            if (rulem.ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rulem.ParentDefaultValues[i]))
                                newValue = new Constant(rulem.ParentDefaultValues[i], dbType);
                            else
                                newValue = new Constant(DBNull.Value, dbType);

                            // SSX 28/01/08: add contains test
                            if (entityChildMapping.Ids[rulem.ChildFields[i]] == null && cmd is UpdateCommand
                                && !((UpdateCommand)cmd).ColumnValueCollection.Contains(rulem.ChildFields[i]))
                                ((UpdateCommand)cmd).ColumnValueCollection.Add(rulem.ChildFields[i], newValue);

                            Column childField = new Column(rulem, rulem.ChildFields[i]);
                            Constant childParameter = new Constant(ConvertId(deleteEntityMapping, rulem.ParentFields[i], c.ParentId), dbType);
                            BinaryLogicExpression parentClause = new BinaryLogicExpression(childField, BinaryLogicOperator.Equals, childParameter);
                            if (cmd is UpdateCommand)
                                ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(parentClause);
                            else
                                ((DeleteCommand)cmd).Condition.SearchCondition.Add(parentClause);
                        }

                        if (!string.IsNullOrEmpty(rulem.Constraint))
                            DisableForeignKeys(rulem.ChildTable);
                    }
                }
            }

            // Deletes all references to and from it first
            foreach (EntityMapping em in _Mapping.Entities)
            {
                foreach (ReferenceMapping refm in em.References)
                {
                    //Evaluant.Uss.Models.Model childModel = _Engine.Model.GetEntity(

                    if (refm.EntityChild == deleteEntityMapping.Type) // To
                    {
                        for (int indexRule = 0; indexRule < refm.Rules.Count; indexRule++)
                        {
                            RuleMapping rulem = refm.Rules[indexRule];
                            if ((rulem.ChildField == deleteEntityMapping.IdFields &&
                                deleteEntityMapping.DiscriminatorField == null) ||
                                (rulem.ChildField + ";" + deleteEntityMapping.DiscriminatorField == deleteEntityMapping.IdFields &&
                                deleteEntityMapping.DiscriminatorField != null))
                            {
                                // delete childtable

                                if (cmds.ContainsKey(rulem.ChildTable))
                                    continue;

                                DeleteCommand cmd = new DeleteCommand(rulem, rulem.ChildTable);
                                cmds.Add(rulem.ChildTable, cmd);

                                for (int index = 0; index < rulem.ChildFields.Length; index++)
                                {
                                    DbType dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[index].Generator);

                                    cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                                        new Column(rulem, rulem.ChildFields[index]),
                                        BinaryLogicOperator.Equals,
                                        new Constant(ConvertId(deleteEntityMapping, rulem.ChildFields[index], c.ParentId), dbType)
                                    ));
                                }


                                if (!string.IsNullOrEmpty(refm.DiscriminatorField) && indexRule == refm.Rules.Count - 1)
                                {
                                    cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                                        new Column(rulem, refm.DiscriminatorField),
                                        BinaryLogicOperator.Equals,
                                        new Parameter(rulem, refm.DiscriminatorValue)
                                        ));
                                }

                                if (!string.IsNullOrEmpty(rulem.Constraint))
                                    DisableForeignKeys(rulem.ChildTable);
                            }

                            if (rulem.ParentField != em.IdFields && em.DiscriminatorField != null
                                || rulem.ParentTable != em.Table)
                            {
                                // update parent table
                                SQLCommand cmd = (SQLCommand)cmds[rulem.ParentTable];
                                if (cmd == null)
                                {
                                    cmd = new UpdateCommand(rulem, rulem.ParentTable);
                                    cmds.Add(rulem.ParentTable, cmd);
                                }

                                if (cmd is DeleteCommand)
                                    continue;

                                for (int i = 0; i < rulem.ParentFields.Length; i++)
                                {
                                    DbType dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[i].Generator);

                                    Constant newValue = new Constant(DBNull.Value, dbType);
                                    if (rulem.ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rulem.ParentDefaultValues[i]))
                                        newValue = new Constant(rulem.ParentDefaultValues[i], dbType);

                                    // SSX 28/01/08: add contains test
                                    if (em.Ids[rulem.ParentFields[i]] == null
                                        && !((UpdateCommand)cmd).ColumnValueCollection.Contains(rulem.ParentFields[i]))
                                        ((UpdateCommand)cmd).ColumnValueCollection.Add(rulem.ParentFields[i], newValue);

                                    Column parentField = new Column(rulem, rulem.ParentFields[i]);
                                    Constant parentParameter = new Constant(ConvertId(deleteEntityMapping, rulem.ParentFields[i], c.ParentId), dbType);
                                    BinaryLogicExpression parentClause = new BinaryLogicExpression(parentField, BinaryLogicOperator.Equals, parentParameter);
                                    ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(parentClause);
                                }

                                // UPDATE parentTable SET parentField = NULL WHERE (parentField = @parentValue AND discriminator = 'name's relation')
                                if (!string.IsNullOrEmpty(refm.DiscriminatorField) && indexRule == refm.Rules.Count - 1)
                                {
                                    Column discriminatorField = new Column(rulem, rulem.ParentField);
                                    Constant discriminatorValue = new Constant(refm.DiscriminatorValue, DbType.AnsiString);
                                    BinaryLogicExpression discriminatorClause = new BinaryLogicExpression(discriminatorField, BinaryLogicOperator.Equals, discriminatorValue);
                                    ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(discriminatorClause);
                                }

                                if (!string.IsNullOrEmpty(rulem.Constraint))
                                    DisableForeignKeys(rulem.ParentTable);

                            }
                        }

                    }

                }
            }


            // Deletes all attributes if they are in a separate table
            if (deleteEntityMapping.Attributes != null)
            {
                foreach (AttributeMapping am in deleteEntityMapping.Attributes)
                {
                    if (am.Table != deleteEntityMapping.Table)
                    {
                        DeleteCommand cmd = new DeleteCommand(am, am.Table);
                        for (int index = 0; index < deleteEntityMapping.Ids.Count; index++)
                        {
                            PrimaryKeyMapping pkm = deleteEntityMapping.Ids[index];
                            cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                                new Column(am, am.ParentFields[index]),
                                BinaryLogicOperator.Equals,
                                new Constant(ConvertId(deleteEntityMapping, pkm.Field, c.ParentId), _Dialect.GetDbTypeToPrimaryKey(pkm.Generator))));
                        }
                        cmds.Add(am.Table, cmd);
                    }
                }
            }

            // SSX 28/01/08: remove table if already existing
            if (cmds.Contains(deleteEntityMapping.Table))
            {
                cmds.Remove(deleteEntityMapping.Table);
            }

            if (!cmds.ContainsKey(deleteEntityMapping.Table))
            {
                DeleteCommand cmd = new DeleteCommand(deleteEntityMapping, deleteEntityMapping.Table);
                for (int index = 0; index < deleteEntityMapping.Ids.Count; index++)
                {
                    PrimaryKeyMapping pkm = deleteEntityMapping.Ids[index];
                    cmd.Condition.SearchCondition.Add(new BinaryLogicExpression(
                        new Column(deleteEntityMapping, pkm.Field),
                        BinaryLogicOperator.Equals,
                        new Constant(ConvertId(deleteEntityMapping, pkm.Field, c.ParentId), _Dialect.GetDbTypeToPrimaryKey(pkm.Generator))));
                }
                cmds.Add(deleteEntityMapping.Table, cmd);
            }

            foreach (string tableName in cmds.Keys)
            {

                foreach (string query in _Dialect.RenderQueries((ISQLExpression)cmds[tableName]))
                {
                    if (!string.IsNullOrEmpty(query))
                    {
                        IDbCommand cmd = _Driver.CreateCommand(query, _Connection, _Transaction);
                        if (_Engine.TraceSqlSwitch.Enabled)
                            TraceHelpler.Trace(cmd, _Dialect);

                        cmd.ExecuteNonQuery();
                    }
                }


            }


        }
Exemple #4
0
        public override void Visit(BinaryOperator binaryop)
        {
            bool firstIsType = ((int)_ExprLevel.Peek() == EXPR);
            _ExprLevel.Push(_ExprLevel.Peek());

            ISQLExpression expression = null;

            EntityMapping entityMap = null;
            if (!firstIsType)
                entityMap = (EntityMapping)entityMappingContext.Peek();

            //	Try to get the queried attribute. If it is a date type and the 
            //	corresponding mapping is generic, we have to convert the value as ticks
            //	instead of the standard representation

            Value val = binaryop.LeftOperand as Value;
            Path path = binaryop.LeftOperand as Path;

            if (val == null)
                val = binaryop.RightOperand as Value;

            if (path == null)
                path = binaryop.RightOperand as Path;

            if (!firstIsType)
            {
                if (val != null && path != null && path.Identifiers.Count == 1)
                {
                    AttributeMapping am = entityMap.Attributes[path.Identifiers[path.Identifiers.Count - 1].Value, true];

                    if (val.Type == ValueEnum.Date)
                    {
                        //	If the field type isn't date type, convert the value as ticks
                        if (am.DbType != DbType.Date && am.DbType != DbType.DateTime && am.DbType != DbType.Time)
                        {
                            DateTime dt = Convert.ToDateTime(val.Text);
                            val.Text = Common.Utils.ConvertToString(dt, dt.GetType());
                        }
                    }
                }
            }

            ISQLExpression discriminatorConstraint = null;
            Path p = binaryop.LeftOperand as Path;
            if (p == null)
                p = binaryop.RightOperand as Path;

            if (p != null && !firstIsType)
            {
                AttributeMapping am = entityMap.Attributes[p.Identifiers[p.Identifiers.Count - 1].Value];

                if (am != null &&
                    am.Discriminator != null &&
                    am.Discriminator != string.Empty &&
                    am.DiscriminatorValue != null &&
                    am.DiscriminatorValue != string.Empty)
                {
                    Column left = new Column(am, am.Discriminator);
                    Constant right = new Constant(path.Identifiers[path.Identifiers.Count - 1].Value, DbType.AnsiStringFixedLength);
                    discriminatorConstraint = new BinaryLogicExpression(
                        left,
                        BinaryLogicOperator.Equals,
                        right);
                }
            }

            switch (binaryop.Type)
            {
                case BinaryOperatorEnum.And:
                    binaryop.LeftOperand.Accept(this);
                    ISQLExpression leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    ISQLExpression rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.And, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);

                    break;
                case BinaryOperatorEnum.Or:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.Or, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);

                    break;
                case BinaryOperatorEnum.Equal:

                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.Equals, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);

                    break;
                case BinaryOperatorEnum.Greater:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.Greater, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;

                case BinaryOperatorEnum.GreaterOrEqual:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.GreaterOrEquals, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;
                case BinaryOperatorEnum.Lesser:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.Lesser, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;
                case BinaryOperatorEnum.LesserOrEqual:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.LesserOrEquals, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;

                case BinaryOperatorEnum.NotEqual:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryLogicOperator.NotEquals, rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;

                case BinaryOperatorEnum.Plus:
                case BinaryOperatorEnum.Times:
                case BinaryOperatorEnum.Div:
                case BinaryOperatorEnum.Minus:
                case BinaryOperatorEnum.Modulo:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new BinaryLogicExpression(leftValueExpression, BinaryOperatorToBinaryLogicOperator(binaryop.Type), rightValueExpression);

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(expression, BinaryLogicOperator.And, discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;

                case BinaryOperatorEnum.Contains:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new LikePredicate(leftValueExpression, String.Format("%{0}%", (string)((Constant)rightValueExpression).Value));

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);
                    break;

                case BinaryOperatorEnum.BeginsWith:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new LikePredicate(leftValueExpression, String.Format("{0}%", (string)((Constant)rightValueExpression).Value));

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);

                    break;
                case BinaryOperatorEnum.EndsWith:
                    binaryop.LeftOperand.Accept(this);
                    leftValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    binaryop.RightOperand.Accept(this);
                    rightValueExpression = (ISQLExpression)sqlExpressionContext.Pop();

                    expression = new LikePredicate(leftValueExpression, String.Format("%{0}", (string)((Constant)rightValueExpression).Value));

                    if (discriminatorConstraint != null)
                        expression = new BinaryLogicExpression(
                            expression,
                            BinaryLogicOperator.And,
                            discriminatorConstraint);

                    sqlExpressionContext.Push(expression);

                    break;
            }

            _ExprLevel.Pop();
        }
Exemple #5
0
        public override void Visit(Call call)
        {
            if (call.Name == "id")
            {
                Table table = (Table)tableContext.Peek();

                EntityMapping em = (EntityMapping)entityMappingContext.Peek();

                if (em.Ids.Count == 1)
                {
                    Column opRight = new Column(em.Ids[0], table.TableAlias, em.GetIdField(em.Ids[0]));

                    InPredicate ip = new InPredicate(opRight);

                    DbType type = DbType.Object;
                    if (em.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.guid)
                        type = DbType.AnsiString;
                    else
                    {
                        // If generator is inherited => Get the parameters of the parent type
                        EntityMapping parentEM = em;
                        if (em.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.inherited)
                        {
                            Evaluant.Uss.Models.Entity current = Model.GetEntity(em.Type);

                            while (Model.GetParent(current) != null)
                            {
                                current = Model.GetParent(current);
                                parentEM = _Mapping.Entities[current.Type];
                                if (parentEM.Ids[0].Generator.Name != GeneratorMapping.GeneratorType.inherited)
                                {
                                    parentEM.Ids[0].Generator.Params = _Mapping.Entities[current.Type].Ids[0].Generator.Params;
                                    break;
                                }
                            }
                        }

                        if (parentEM.Ids[0].Generator.Name == GeneratorMapping.GeneratorType.guid)
                            type = DbType.AnsiString;
                        else
                            type = (DbType)Enum.Parse(typeof(DbType), parentEM.Ids[0].Generator.GetParam(DBDialect.DBTYPE).Value);
                    }

                    foreach (Evaluant.OPath.Expressions.Constraint constraint in call.Operands)
                    {
                        if ((constraint as Value) != null)
                        {
                            if (((Value)constraint).Text == string.Empty)
                            {
                                ((Value)constraint).Text = null;
                                ((Value)constraint).Type = GetValueEnum(type);
                            }
                        }

                        constraint.Accept(this);

                        if (((Constant)sqlExpressionContext.Peek()).Value == null)
                        {
                            sqlExpressionContext.Pop();
                            sqlExpressionContext.Push(new Constant(DBNull.Value, type));
                        }

                        ip.SubQueries.Add(new Constant(((Constant)sqlExpressionContext.Pop()).Value, type));
                    }

                    sqlExpressionContext.Push(ip);
                }
                else
                {
                    // TODO : Logique des clés primaires
                    for (int indexConstraint = 0; indexConstraint < call.Operands.Count; indexConstraint++)
                    {
                        Evaluant.OPath.Expressions.Constraint constraint = call.Operands[indexConstraint];

                        for (int i = 0; i < em.Ids.Count; i++)
                        {
                            PrimaryKeyMapping pkm = em.Ids[i];
                            DbType dbType = _Dialect.GetDbTypeToPrimaryKey(pkm.Generator);

                            Value value = constraint as Value;
                            if ((constraint as Value) != null)
                            {
                                if (value.Text == string.Empty)
                                {
                                    value.Text = null;
                                    value.Type = GetValueEnum(dbType);
                                }
                            }

                            string[] values = value.Text.Split(SqlMapperProvider.IDSEP);

                            if (i >= values.Length)
                                break;
                            Value newValue = new Value(values[i], GetValueEnum(_Dialect.GetDbTypeToPrimaryKey(pkm.Generator)));
                            newValue.Accept(this);

                            if (((Constant)sqlExpressionContext.Peek()).Value == null)
                            {
                                sqlExpressionContext.Pop();
                                sqlExpressionContext.Push(new Constant(DBNull.Value, dbType));
                            }

                            Column field = new Column(pkm, table.TableAlias, em.GetIdField(pkm));

                            sqlExpressionContext.Push(new BinaryLogicExpression(field, BinaryLogicOperator.Equals, (Constant)sqlExpressionContext.Pop()));

                            if (i != 0)
                            {
                                BinaryLogicExpression leftOperand = (BinaryLogicExpression)sqlExpressionContext.Pop();
                                BinaryLogicExpression rightOperand = (BinaryLogicExpression)sqlExpressionContext.Pop();

                                sqlExpressionContext.Push(new BinaryLogicExpression(leftOperand, BinaryLogicOperator.And, rightOperand));
                            }

                        }

                        if (indexConstraint != 0)
                        {
                            BinaryLogicExpression leftOp = (BinaryLogicExpression)sqlExpressionContext.Pop();
                            BinaryLogicExpression rightOp = (BinaryLogicExpression)sqlExpressionContext.Pop();

                            sqlExpressionContext.Push(new BinaryLogicExpression(leftOp, BinaryLogicOperator.Or, rightOp));
                        }
                    }
                }
            }
        }
Exemple #6
0
		public InPredicate(Column c)
		{
			_SubQueries = new ExpressionCollection();
			_Column = c;
		}
Exemple #7
0
        public void ProcessEntity(Identifier ident, bool isInRef)
        {
            // Restore current entity mapping context
            EntityMapping entityMapping = (EntityMapping)entityMappingContext.Peek();

            // Initialization
            Table currentTable = new TableSource(entityMapping, entityMapping.Table, CreateUniqueTableAlias());
            Column currentItem = new Column(entityMapping.Ids[0], currentTable.TableAlias, entityMapping.GetIdField(entityMapping.Ids[0]), entityMapping.GetIdFieldAs(entityMapping.Ids[0]));
            SelectStatement currentQuery = new SelectStatement(entityMapping);
            if (!isInRef)
                currentQuery.TableAlias = currentTable.TableAlias;

            // Save current context
            tableContext.Push(currentTable);
            sqlExpressionContext.Push(currentItem);
            queryContext.Push(currentQuery);

            if (entityMapping.DiscriminatorField != null && !isInRef)
            {
                InPredicate ip = new InPredicate(new Column(entityMapping, currentTable.TableAlias, entityMapping.DiscriminatorField));

                string parentType = entityMapping.Type;

                if (_BaseType != null && _BaseType != parentType)
                {
                    Evaluant.Uss.Models.Entity parentEntity = Model.GetEntity(_BaseType);

                    while (Model.GetParent(parentEntity) != null)
                        parentEntity = Model.GetParent(parentEntity);

                    parentType = parentEntity.Type;
                }

                //Evaluant.Uss.Models.Entity parentType = Model.GetEntity(parentType);

                //Get all the children of the queried type
                IList children = _Model.GetTree(parentType);

                ip.SubQueries.Add(new Constant(entityMapping.DiscriminatorValue, DbType.AnsiString));

                StringCollection processed = new StringCollection();

                foreach (Evaluant.Uss.Models.Entity e in children)
                {
                    if (e.Type == parentType)
                        continue;

                    EntityMapping current = _Mapping.Entities[e.Type, true];

                    string currentType = current.DiscriminatorValue;
                    if (processed.Contains(currentType))
                        continue;

                    processed.Add(currentType);

                    ip.SubQueries.Add(new Constant(currentType, DbType.AnsiString));
                }

                currentQuery.WhereClause.SearchCondition.Add(ip);
            }

            // Process constraint : an attribute constraint can modify the context
            if (ident.Constraint != null)
            {
                int stackSizeBefore = entityMappingContext.Count;

                ProcessConstraint(ident.Constraint);

                int stackSizeAfter = entityMappingContext.Count;

                for (int i = stackSizeAfter - 1; i > stackSizeBefore - 1; i--)
                {
                    entityMappingContext.Pop();
                    entityModelContext.Pop();
                }
            }
        }
Exemple #8
0
		/// <summary>
		/// Removes the first occurrence of a specific Column from this ColumnCollection.
		/// </summary>
		/// <param name="value">
		/// The Column value to remove from this ColumnCollection.
		/// </param>
		public virtual void Remove(Column value)
		{
			this.List.Remove(value);
		}
Exemple #9
0
		public InPredicate(Column c, ExpressionCollection expressions)
		{
			_SubQueries = expressions;
			_Column = c;
		}
Exemple #10
0
		/// <summary>
		/// Return the zero-based index of the first occurrence of a specific value
		/// in this ColumnCollection
		/// </summary>
		/// <param name="value">
		/// The Column value to locate in the ColumnCollection.
		/// </param>
		/// <returns>
		/// The zero-based index of the first occurrence of the _ELEMENT value if found;
		/// -1 otherwise.
		/// </returns>
		public virtual int IndexOf(Column value)
		{
			return this.List.IndexOf(value);
		}
Exemple #11
0
		/// <summary>
		/// Inserts an element into the ColumnCollection at the specified index
		/// </summary>
		/// <param name="index">
		/// The index at which the Column is to be inserted.
		/// </param>
		/// <param name="value">
		/// The Column to insert.
		/// </param>
		public virtual void Insert(int index, Column value)
		{
			this.List.Insert(index, value);
		}
Exemple #12
0
		/// <summary>
		/// Determines whether a specfic Column value is in this ColumnCollection.
		/// </summary>
		/// <param name="value">
		/// The Column value to locate in this ColumnCollection.
		/// </param>
		/// <returns>
		/// true if value is found in this ColumnCollection;
		/// false otherwise.
		/// </returns>
		public virtual bool Contains(Column value)
		{
			return this.List.Contains(value);
		}
Exemple #13
0
		/// <summary>
		/// Adds an instance of type Column to the end of this ColumnCollection.
		/// </summary>
		/// <param name="value">
		/// The Column to be added to the end of this ColumnCollection.
		/// </param>
		public virtual void Add(Column value)
		{
			this.List.Add(value);
		}
Exemple #14
0
		/// <summary>
		/// Adds the elements of an array to the end of this ColumnCollection.
		/// </summary>
		/// <param name="items">
		/// The array whose elements are to be added to the end of this ColumnCollection.
		/// </param>
		public virtual void AddRange(Column[] items)
		{
			foreach (Column item in items)
			{
				this.List.Add(item);
			}
		}
Exemple #15
0
		/// <summary>
		/// Initializes a new instance of the ColumnCollection class, containing elements
		/// copied from an array.
		/// </summary>
		/// <param name="items">
		/// The array whose elements are to be added to the new ColumnCollection.
		/// </param>
		public ColumnCollection(Column[] items)
		{
			this.AddRange(items);
		}
Exemple #16
0
		public virtual void Visit(Column column)
		{
            if (column.TableName != String.Empty)
            {
                //if(!string.IsNullOrEmpty(schema))
                //    _Query.Append(FormatAttribute(schema + DOT + column.TableName)).Append(".");
                //else
                    _Query.Append(FormatAttribute(column.TableName)).Append(".");
            }
			
			_Query.Append(FormatAttribute(column.ColumnName));
            _Query.Append(SPACE);

			if(column.Alias != null && column.Alias != string.Empty)
                _Query.Append(AS).Append(FormatAttribute(column.Alias));

		}