コード例 #1
0
ファイル: DmlfExtension.cs プロジェクト: timothydodd/dbshell
        public static void GenSql(this DmlfJoinType join, ISqlDumper dmp)
        {
            switch (join)
            {
            case DmlfJoinType.Inner:
                dmp.Put("^inner ^join");
                break;

            case DmlfJoinType.Left:
                dmp.Put("^left ^join");
                break;

            case DmlfJoinType.Right:
                dmp.Put("^right ^join");
                break;

            case DmlfJoinType.Outer:
                dmp.Put("^full ^outer ^join");
                break;

            case DmlfJoinType.CrossApply:
                dmp.Put("^cross ^apply");
                break;

            case DmlfJoinType.OuterApply:
                dmp.Put("^outer ^apply");
                break;
            }
        }
コード例 #2
0
ファイル: DmlfFromItem.cs プロジェクト: dbshell/dbshell
        private DmlfSource DoAddOrFindRelation(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier relationJoined, StructuredIdentifier relationToJoin, DatabaseInfo db, DmlfJoinType joinType)
        {
            if (relationToJoin.IsEmpty) return baseSource;
            string relName = relationToJoin.First;
            string alias = String.Format("_REF{0}_{1}", relationJoined.NameItems.Select(x => "_" + x).CreateDelimitedText(""), relName);
            var source = FindSourceByAlias(alias);
            if (source == null)
            {
                var baseTableInfo = db.FindTable(baseTable);
                var fk = baseTableInfo.ForeignKeys.FirstOrDefault(x => System.String.Compare(x.ConstraintName, relName, StringComparison.OrdinalIgnoreCase) == 0);
                if (fk == null)
                {
                    var column = baseTableInfo.FindColumn(relName);
                    if (column != null) fk = column.GetForeignKeys().FirstOrDefault(x => x.Columns.Count == 1);
                }
                if (fk == null) return null;

                source = new DmlfSource
                    {
                        TableOrView = fk.RefTableFullName,
                        Alias = alias,
                    };
                var relation = new DmlfRelation
                    {
                        Reference = source,
                        JoinType = joinType,
                    };
                for (int i = 0; i < fk.Columns.Count; i++)
                {
                    relation.Conditions.Add(new DmlfEqualCondition
                        {
                            LeftExpr = new DmlfColumnRefExpression
                                {
                                    Column = new DmlfColumnRef
                                        {
                                            ColumnName = fk.Columns[0].RefColumnName,
                                            Source = baseSource,
                                        }
                                },
                            RightExpr = new DmlfColumnRefExpression
                                {
                                    Column = new DmlfColumnRef
                                        {
                                            ColumnName = fk.RefColumns[0].RefColumnName,
                                            Source = source,
                                        }
                                },
                        });
                    Relations.Add(relation);
                }
            }
            if (relationToJoin.IsEmpty) return source;
            return DoAddOrFindRelation(source, source.TableOrView, relationJoined/relationToJoin.First, relationToJoin.WithoutFirst, db, joinType);
        }
コード例 #3
0
ファイル: DmlfFromItem.cs プロジェクト: dbshell/dbshell
 public DmlfColumnRef GetColumnRef(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier columnId, DatabaseInfo db, DmlfJoinType joinType)
 {
     var relationId = columnId.WithoutLast;
     string column = columnId.Last;
     var source = AddOrFindRelation(baseSource, baseTable, relationId, db, joinType);
     if (source == null) return null;
     return new DmlfColumnRef
         {
             ColumnName = column,
             Source = source,
         };
 }
コード例 #4
0
ファイル: DmlfFromItem.cs プロジェクト: dbshell/dbshell
 public DmlfSource AddOrFindRelation(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier relationId, DatabaseInfo db, DmlfJoinType joinType)
 {
     return DoAddOrFindRelation(baseSource, baseTable, new StructuredIdentifier(), relationId, db, joinType);
 }
コード例 #5
0
ファイル: DmlfFromItem.cs プロジェクト: timothydodd/dbshell
        private DmlfSource DoAddOrFindRelation(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier relationJoined, StructuredIdentifier relationToJoin, DatabaseInfo db, DmlfJoinType joinType)
        {
            if (relationToJoin.IsEmpty)
            {
                return(baseSource);
            }
            string relName = relationToJoin.First;
            string alias   = String.Format("_REF{0}_{1}", relationJoined.NameItems.Select(x => "_" + x).CreateDelimitedText(""), relName);
            var    source  = FindSourceByAlias(alias);

            if (source == null)
            {
                var baseTableInfo = db.FindTable(baseTable);
                var fk            = baseTableInfo.ForeignKeys.FirstOrDefault(x => System.String.Compare(x.ConstraintName, relName, StringComparison.OrdinalIgnoreCase) == 0);
                if (fk == null)
                {
                    var column = baseTableInfo.FindColumn(relName);
                    if (column != null)
                    {
                        fk = column.GetForeignKeys().FirstOrDefault(x => x.Columns.Count == 1);
                    }
                }
                if (fk == null)
                {
                    return(null);
                }

                source = new DmlfSource
                {
                    TableOrView = fk.RefTableFullName,
                    Alias       = alias,
                };
                var relation = new DmlfRelation
                {
                    Reference = source,
                    JoinType  = joinType,
                };
                for (int i = 0; i < fk.Columns.Count; i++)
                {
                    relation.Conditions.Add(new DmlfEqualCondition
                    {
                        LeftExpr = new DmlfColumnRefExpression
                        {
                            Column = new DmlfColumnRef
                            {
                                ColumnName = fk.Columns[0].RefColumnName,
                                Source     = baseSource,
                            }
                        },
                        RightExpr = new DmlfColumnRefExpression
                        {
                            Column = new DmlfColumnRef
                            {
                                ColumnName = fk.RefColumns[0].RefColumnName,
                                Source     = source,
                            }
                        },
                    });
                    Relations.Add(relation);
                }
            }
            if (relationToJoin.IsEmpty)
            {
                return(source);
            }
            return(DoAddOrFindRelation(source, source.TableOrView, relationJoined / relationToJoin.First, relationToJoin.WithoutFirst, db, joinType));
        }
コード例 #6
0
ファイル: DmlfFromItem.cs プロジェクト: timothydodd/dbshell
        public DmlfColumnRef GetColumnRef(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier columnId, DatabaseInfo db, DmlfJoinType joinType)
        {
            var    relationId = columnId.WithoutLast;
            string column     = columnId.Last;
            var    source     = AddOrFindRelation(baseSource, baseTable, relationId, db, joinType);

            if (source == null)
            {
                return(null);
            }
            return(new DmlfColumnRef
            {
                ColumnName = column,
                Source = source,
            });
        }
コード例 #7
0
ファイル: DmlfFromItem.cs プロジェクト: timothydodd/dbshell
 public DmlfSource AddOrFindRelation(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier relationId, DatabaseInfo db, DmlfJoinType joinType)
 {
     return(DoAddOrFindRelation(baseSource, baseTable, new StructuredIdentifier(), relationId, db, joinType));
 }