Exemplo n.º 1
0
        internal static List <PrefetchField> GetPrefetchFieldsOne(CSTable table, List <string> fieldList, List <string> aliasList, CSJoinList joinList, string[] prefetchPaths)
        {
            List <PrefetchField> prefetchFields = new List <PrefetchField>();

            foreach (CSSchemaField schemaField in table.Schema.Fields)
            {
                bool prefetch = schemaField.Prefetch;

                string fieldName = schemaField.Name;

                prefetch |= (prefetchPaths != null && prefetchPaths.Any(s =>
                {
                    if (s.IndexOf('.') > 0)
                    {
                        s = s.Substring(0, s.IndexOf('.'));
                    }

                    return(s == fieldName);
                }));

                if (schemaField.Relation != null && schemaField.Relation.RelationType == CSSchemaRelationType.ManyToOne && prefetch)
                {
                    CSJoin join = new CSJoin(schemaField.Relation, table.TableAlias);

                    joinList.Add(join);

                    PrefetchField prefetchField = new PrefetchField();

                    prefetchField.SchemaField = schemaField;

                    foreach (string columnName in schemaField.Relation.ForeignSchema.ColumnsToRead)
                    {
                        string alias = CSNameGenerator.NextFieldAlias;

                        fieldList.Add(join.RightAlias + "." + columnName);
                        aliasList.Add(alias);

                        prefetchField.AliasMap[alias] = columnName;
                    }

                    prefetchFields.Add(prefetchField);
                }
            }

            return(prefetchFields);
        }
Exemplo n.º 2
0
        private static object EvalVariable(string var, QueryExpression parentQuery)
        {
            Match match = _regexVar.Match(var);

            if (!match.Success)
            {
                throw new CSExpressionException("Object [" + var + "] is unknown");
            }

            List <string> fieldNames = new List <string>();

            foreach (Capture capture in match.Groups["Object"].Captures)
            {
                fieldNames.Add(capture.Value);
            }

            QueryExpression subExpression = null;

            CSTable currentTable = parentQuery.Table;

            string fieldName = null;

            for (int i = 0; i < fieldNames.Count; i++)
            {
                bool isLast = (i == fieldNames.Count - 1);

                CSJoin        currentJoin = null;
                CSSchemaField schemaField = currentTable.Schema.Fields[fieldNames[i]];

                if (schemaField == null)
                {
                    throw new CSException("Error in expression. [" + fieldNames[i] + "] is not a valid field name");
                }

                if (schemaField.Relation != null)
                {
                    switch (schemaField.Relation.RelationType)
                    {
                    case CSSchemaRelationType.OneToMany:
                    {
                        if (subExpression != null)
                        {
                            throw new CSExpressionException("Error in expression [" + var + "]: Multiple *ToMany relations");
                        }

                        subExpression       = new QueryExpression(schemaField.Relation.ForeignSchema);
                        subExpression.Joins = new CSJoinList();

                        subExpression.Expression = currentTable.TableAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.LocalKey) + "=" + subExpression.Table.TableAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.ForeignKey);
                    }
                    break;

                    case CSSchemaRelationType.ManyToMany:
                    {
                        if (subExpression != null)
                        {
                            throw new CSExpressionException("Error in expression [" + var + "]: Multiple *ToMany relations");
                        }

                        subExpression       = new QueryExpression(schemaField.Relation.ForeignSchema);
                        subExpression.Joins = new CSJoinList();

                        currentJoin = new CSJoin();

                        currentJoin.LeftSchema  = schemaField.Relation.ForeignSchema;
                        currentJoin.LeftTable   = subExpression.Table.TableName;
                        currentJoin.LeftAlias   = subExpression.Table.TableAlias;
                        currentJoin.RightTable  = schemaField.Relation.LinkTable;
                        currentJoin.RightAlias  = CSNameGenerator.NextTableAlias;
                        currentJoin.LeftColumn  = schemaField.Relation.ForeignKey;
                        currentJoin.RightColumn = schemaField.Relation.ForeignLinkKey;

                        currentJoin.Type = CSJoinType.Inner;

                        subExpression.Joins.Add(currentJoin);

                        subExpression.Expression = currentJoin.RightAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.LocalLinkKey) + "=" + currentTable.TableAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.LocalKey);

                        currentJoin = null;
                    }
                    break;

                    case CSSchemaRelationType.ManyToOne:
                    {
                        if (!isLast)
                        {
                            if (subExpression != null)                                             // meaning we already encountered a *toMany relation
                            {
                                currentJoin = new CSJoin(schemaField.Relation, subExpression.Table.TableAlias);

                                if (subExpression.Joins.Contains(currentJoin))
                                {
                                    currentJoin = subExpression.Joins.GetExistingJoin(currentJoin);
                                }
                                else
                                {
                                    subExpression.Joins.Add(currentJoin);
                                }
                            }
                            else
                            {
                                currentJoin = new CSJoin(schemaField.Relation, currentTable.TableAlias);

                                if (parentQuery.Joins.Contains(currentJoin))
                                {
                                    currentJoin = parentQuery.Joins.GetExistingJoin(currentJoin);
                                }
                                else
                                {
                                    parentQuery.Joins.Add(currentJoin);
                                }
                            }
                        }
                    }
                    break;
                    }

                    if (!isLast)
                    {
                        if (currentJoin != null)
                        {
                            currentTable = new CSTable(schemaField.Relation.ForeignSchema, currentJoin.RightAlias);
                        }
                        else
                        {
                            if (subExpression != null)
                            {
                                currentTable = new CSTable(schemaField.Relation.ForeignSchema, subExpression.Table.TableAlias);
                            }
                            else
                            {
                                currentTable = new CSTable(schemaField.Relation.ForeignSchema);
                            }
                        }
                    }
                    else
                    {
                        fieldName = currentTable.Schema.DB.QuoteField(currentTable.TableAlias + "." + schemaField.Relation.LocalKey);
                    }
                }
                else
                {
                    fieldName = currentTable.Schema.DB.QuoteField(currentTable.TableAlias + "." + schemaField.MappedColumn.Name);
                }
            }

            if (subExpression != null)
            {
                subExpression.FieldName = fieldName;

                return(subExpression);
            }
            else
            {
                //if (fieldName != null)
                return(fieldName);
            }
        }