Beispiel #1
0
 /// <summary>
 /// Determines whether two specified values are equal.
 /// </summary>
 /// <param name="tableA">The first value to compare.</param>
 /// <param name="tableB">The second value to compare.</param>
 internal bool Equals(DB3 tableA, DB3 tableB)
 {
     return
         ((TableA.Equals(tableA) && TableB.Equals(tableB))
          ||
          (TableA.Equals(tableB) && TableB.Equals(tableA)));
 }
Beispiel #2
0
        internal static ColumnMap GetColumnMap(DB3 columnID)
        {
            var tableMap = GetNodeMap(columnID.TableID);

            return(tableMap.Columns.Where(a => a.ID.Equals(columnID))
                   .Single());
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the DatabaseMap class.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="mappingDate"></param>
 /// <param name="isWhole"></param>
 public DatabaseMap(Identifier name, DateTime mappingDate, bool isWhole)
     : base(DB3.Get(DbMapping.NewDatabaseID))
 {
     Name        = name;
     MappingDate = mappingDate;
     IsWhole     = isWhole;
 }
Beispiel #4
0
        internal static DataParamChainer AddNodeColumns(Chainer prev, DB3 nodeID, ColumnSelector selector, string prefix)
        {
            var node = DbMapping.GetNodeMap(nodeID);

            ColumnMap[] columns;

            if (selector == ColumnSelector.All)
            {
                if (node.HasRowversion)
                {
                    return(((IParam)prev).Param(prefix.AsParam(1), node.RowversionColumn.DataType));
                }

                columns = node.SortedColumns;
            }
            else if (selector == ColumnSelector.RK)
            {
                columns = node.SortedRKColumns;
            }
            else if (selector == ColumnSelector.InsertableWithIdentity)
            {
                columns = node.GetInsertableColumns(true);
            }
            else
            {
                columns = node.GetInsertableColumns(false);
            }

            return(AddNodeColumns(prev, columns, prefix));
        }
Beispiel #5
0
        internal Relation AddGraphPair(DB3 foreignKey, DbNode predecessorNode, DbNode relatedNode, Link link)
        {
            SetRX(relatedNode);

            // We need to replace foreignKey with the invoker's one.
            //   attention:
            //     This is needed because the default foreign key is the first column. Since it is allowed
            //     that any column represents the foreign key, the user's foreign key must be replaced with
            //     the default one in order to fit the graph invoker's default foreign key.
            DB3 invokerForeignKey;

            GraphPair pair;
            Relation  relation;

            if (link.IsFKTable(predecessorNode.NodeID)) // => predecessor node is FK, related node is REFERENCE
            {
                invokerForeignKey = _GetInvokerForeignKey(link, foreignKey, out relation);
                pair = new GraphPair(invokerForeignKey, relatedNode.NodeID, predecessorNode.RX, relatedNode.RX);
            }
            else
            {
                invokerForeignKey = _GetInvokerForeignKey(link, foreignKey, out relation);
                pair = new GraphPair(invokerForeignKey, predecessorNode.NodeID, relatedNode.RX, predecessorNode.RX);
            }

            _graphPairs.Add(pair);

            return(relation);
        }
Beispiel #6
0
 /// <summary>
 /// Determines whether the specified argument is equal to this instance.
 /// </summary>
 /// <param name="other">The argument to compare with this instance.</param>
 public bool Equals(DB3 other)
 {
     if (other.DbX == _dbX && other.TableY == _tableY && other.ColumnZ == _columnZ)
     {
         return(true);
     }
     return(false);
 }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of the NodeMap class.
 /// </summary>
 /// <param name="id">A DB3 identifier of a database object.</param>
 /// <param name="name">A name of a database object.</param>
 /// <param name="hasGenuineRK">A flag to indicate whether a database object has a genuine row key (PK or UK).</param>
 public NodeMap(DB3 id, Identifier name, bool hasGenuineRK = true)
     : base(id)
 {
     Name         = name;
     Columns      = new HashSet <ColumnMap>(new MapEqualityComparer());
     Params       = new HashSet <ParamMap>(new MapEqualityComparer());
     HasGenuineRK = hasGenuineRK;
 }
Beispiel #8
0
        internal bool IsInnerJoin(DB3 firstTable)
        {
            if (IsRefTable(firstTable))
            {
                return(true);
            }

            return(!DbMapping.GetColumnMap(FKColumns[0]).IsNullable);
        }
Beispiel #9
0
        internal static void ThrowForeignKeyNotFoundException(DB3 foreignKey, DB3 table)
        {
            var foreignKeyFullName = DbMapping.GetForeignKeyName(foreignKey);

            throw new QueryTalkException("Link.TryGetRelation", QueryTalkExceptionType.ForeignKeyNotFound,
                                         String.Format("table = {0}{1}   foreign key = {2}",
                                                       DbMapping.GetNodeMap(table).Name.Sql,
                                                       Environment.NewLine,
                                                       foreignKeyFullName), Text.Method.By);
        }
Beispiel #10
0
 private static Procedure ReloadProc(DB3 nodeID, ColumnSelector selector)
 {
     return(Designer.GetNewDesigner(Text.Method.ReloadGo, true, true)
            .ParamNodeColumns(nodeID, selector)
            .Param("@Table", Designer.Inliner.Table)
            .Param("@Where", Designer.Inliner.Expression)
            .From("@Table")
            .Where("@Where")
            .Select()
            .EndProc());
 }
Beispiel #11
0
        // invoke a specified node's static ctor
        private static void _Invoke(DB3 nodeID)
        {
            var invoker = _invokers.Where(a => a.ID.Equals(nodeID)).FirstOrDefault();

            if (invoker == null)
            {
                ThrowNodeNotFoundException(nodeID);
            }

            invoker.Invoke();
        }
Beispiel #12
0
        /// <summary>
        /// Initializes a new instance of the NodeTree class.
        /// </summary>
        /// <param name="parent">Is a DB3 identifier of a parent.</param>
        /// <param name="child">Is a DB3 identifier of a child.</param>
        /// <param name="level">A tree level.</param>
        /// <param name="maxLevels">level should not exceed maxLevels.</param>
        public NodeTree(DB3 parent, DB3 child, int level, int maxLevels)
        {
            if (maxLevels <= level)
            {
                throw new QueryTalkException("NodeTree.ctor", QueryTalkExceptionType.MaxLevelsExceeded,
                                             String.Format("maxLevels = {0}", maxLevels), Text.Method.DeleteCascadeGo);
            }

            Parent = parent;
            Child  = child;
            Level  = level;
        }
Beispiel #13
0
        internal static string GetForeignKeyName(DB3 foreignKey)
        {
            ColumnMap foreignKeyMap;
            string    foreignKeyFullName = Text.Unknown;

            if (!foreignKey.Equals(DB3.Default))
            {
                foreignKeyMap      = GetColumnMap(foreignKey);
                foreignKeyFullName = foreignKeyMap.FullName;
            }

            return(foreignKeyFullName);
        }
Beispiel #14
0
 private static Procedure DeleteProc(DB3 nodeID, ColumnSelector selector)
 {
     return(Designer.GetNewDesigner(Text.Method.DeleteGo, true, true)
            .ParamNodeColumns(nodeID, selector)
            .Param("@Table", Designer.Inliner.Table)
            .Param("@Where", Designer.Inliner.Expression)
            .From("@Table")
            .Where("@Where")
            .Delete()
            .Declare <int>("@r").Set(Designer.RowCount)
            .Return("@r")
            .EndProc());
 }
Beispiel #15
0
        // invoke certain graph creator
        internal static void GraphInvoke(DB3 foreignkeyID, DB3 referenceID, IEnumerable foreignKeyTable, IEnumerable referenceTable)
        {
            var invoker = _graphInvokers.Where(a => a.ForeignKeyID.Equals(foreignkeyID) &&
                                               a.ReferenceID.Equals(referenceID)).FirstOrDefault();

            if (invoker != null)
            {
                invoker.Invoke(foreignKeyTable, referenceTable);
            }
            else
            {
                throw new QueryTalkException("DbMapping.GraphInvoke", QueryTalkExceptionType.InvokerNotFoundInnerException,
                                             String.Format("foreignKeyID = {0}{1}   referenceID = {2}", foreignkeyID, Environment.NewLine, referenceID));
            }
        }
Beispiel #16
0
        private static void ThrowNodeNotFoundException(DB3 nodeID)
        {
            // try get database
            var database = _databases.Where(a => a.ID == nodeID.DatabaseID).FirstOrDefault();

            if (database != null)
            {
                throw new QueryTalkException("DbMapping.GetNodeMap", QueryTalkExceptionType.NodeNotFoundInnerException,
                                             String.Format("database = {0}{1}   requested node ID = {2}", database.Name, Environment.NewLine, nodeID));
            }
            else
            {
                throw new QueryTalkException("DbMapping.GetNodeMap", QueryTalkExceptionType.NodeNotFoundInnerException,
                                             String.Format("requested node ID = {0}", nodeID));
            }
        }
Beispiel #17
0
        internal Relation TryGetRelation(DB3 foreignKey, string method = null)
        {
            if (HasIntermediate)
            {
                throw new QueryTalkException("Link.TryGetRelation", QueryTalkExceptionType.IntermediateTableDisallowed,
                                             String.Format("linked tables = {0}:{1}{2}   intermediate table = {3}",
                                                           DbMapping.GetNodeMap(TableA).Name.Sql,
                                                           DbMapping.GetNodeMap(TableB).Name.Sql,
                                                           Environment.NewLine, DbMapping.GetNodeMap(Intermediate).Name.Sql),
                                             method);
            }

            Relation relation = null;

            if (foreignKey.Equals(DB3.Default))
            {
                // single relation
                if (_relations.Count == 1)
                {
                    return(_relations.First());
                }
                // many relations - missing FK
                else
                {
                    throw new QueryTalkException("Link.TryGetRelation", QueryTalkExceptionType.MissingForeignKey,
                                                 String.Format("linked tables = {0}:{1}",
                                                               DbMapping.GetNodeMap(TableA).Name.Sql,
                                                               DbMapping.GetNodeMap(TableB).Name.Sql), method);
                }
            }
            // foreign key is defined:
            else
            {
                relation = _relations.Where(a => a.FKColumns.Contains(foreignKey))
                           .FirstOrDefault();

                if (relation == null)
                {
                    DbMapping.ThrowForeignKeyNotFoundException(foreignKey, TableA);
                }
            }

            return(relation);
        }
Beispiel #18
0
        internal static NodeMap GetNodeMap(DB3 nodeID)
        {
            var node = _nodes.Where(a => a.ID.Equals(nodeID))
                       .SingleOrDefault();

            if (node == null)
            {
                _Invoke(nodeID);
                node = _nodes.Where(a => a.ID.Equals(nodeID))
                       .SingleOrDefault();
            }

            if (node == null)
            {
                ThrowNodeNotFoundException(nodeID);
            }

            return(node);
        }
Beispiel #19
0
        // builds a relation SQL clause
        internal static Relation GetRelation(DB3 tableA, DB3 tableB, DB3 foreignKey, string method = null)
        {
            var link = TryFindLink(tableA, tableB, method);

            if (foreignKey.Equals(DB3.Default))
            {
                if (link.HasManyRelations)
                {
                    throw new QueryTalkException("DbMapping.BuildRelation", QueryTalkExceptionType.MissingForeignKey,
                                                 String.Format("linked tables = {0}:{1}", GetNodeMap(tableA).Name.Sql, GetNodeMap(tableB).Name.Sql), method);
                }
                else
                {
                    foreignKey = link.DefaultForeignKey;
                }
            }

            return(link.TryGetRelation(foreignKey, method));
        }
Beispiel #20
0
        internal static Link TryFindLink(DB3 tableA, DB3 tableB, string method = null)
        {
            var link = GetLink(tableA, tableB);

            if (link == null && !tableA.Equals(tableB))
            {
                link = TryFindIntermediate(tableA, tableB, method);
            }

            // second check
            if (link == null)
            {
                throw new QueryTalkException("DbMapping.TryFindLink", QueryTalkExceptionType.LinkNotFound,
                                             String.Format("table A = {0}{1}   table B = {2}",
                                                           DbMapping.GetNodeMap(tableA).Name.Sql, Environment.NewLine,
                                                           DbMapping.GetNodeMap(tableB).Name.Sql),
                                             method);
            }

            return(link);
        }
Beispiel #21
0
 /// <summary>
 /// Initializes a new instance of the ColumnMap class.
 /// </summary>
 /// <param name="id">A DB3 identifier of a column.</param>
 /// <param name="columnOrdinal">A column ordinal.</param>
 /// <param name="name">A name of a column.</param>
 /// <param name="dataType">A data type.</param>
 /// <param name="isNullable">A flag to indicate if a column is nullable.</param>
 /// <param name="isRK">A flag to indicate if a column is part of a row key.</param>
 /// <param name="isUK">A flag to indicate if a column is part of a unique key.</param>
 /// <param name="isFK">A flag to indicate if a column is part of a foreign key.</param>
 /// <param name="columnType">A column type.</param>
 /// <param name="hasDefault">A flag to indicate if a column has a default constraint.</param>
 public ColumnMap(
     DB3 id,
     int columnOrdinal,
     Identifier name,
     DataType dataType,
     bool isNullable,
     bool isRK             = false,
     bool isUK             = false,
     bool isFK             = false,
     ColumnType columnType = Wall.ColumnType.Regular,
     bool hasDefault       = false)
     : base(id)
 {
     ColumnOrdinal = columnOrdinal;
     Name          = name;
     DataType      = dataType;
     IsNullable    = isNullable;
     IsRK          = isRK;
     IsUK          = isUK;
     IsFK          = isFK;
     ColumnType    = columnType;
     HasDefault    = hasDefault;
 }
Beispiel #22
0
        /// <summary>
        /// Initializes a new instance of the DbColumn class.
        /// </summary>
        /// <param name="parent">A parent.</param>
        /// <param name="columnID">A DB3 identifier of a column.</param>
        public DbColumn(DbNode parent, DB3 columnID)
        {
            _parent  = parent;
            ColumnID = columnID;

            Build = (buildContext, buildArgs) =>
            {
                _parent.CheckReuseAndThrow();

                // note:
                //   If the current object is a PivotChainer, then the previous FromChainer is not accessable
                //   by the select statement any more
                if (buildContext.Current is PivotChainer)
                {
                    return(ColumnMap.Name.Sql);
                }

                string alias = Text.Zero;

                if (buildContext.Current.QueryPart != null && buildContext.Current.QueryPart.Joiner != null)
                {
                    alias = buildContext.Current.QueryPart.Joiner.ProcessColumn(this);
                }

                if (alias != Text.ZeroAlias)
                {
                    return(String.Format("{0}.{1}",
                                         Filter.Delimit(alias),
                                         IsAllColumns ? Text.Asterisk : ColumnMap.Name.Sql));
                }
                else
                {
                    return(String.Format("{0}",
                                         IsAllColumns ? Text.Asterisk : ColumnMap.Name.Sql));
                }
            };
        }
Beispiel #23
0
        internal string BuildRelation(DB3 table1, string alias1, string alias2)
        {
            string aliasA, aliasB;

            if (table1.TableID.Equals(ID.TableID) &&
                !IsSelfRelation)
            {
                aliasA = alias1;
                aliasB = alias2;
            }
            else
            {
                aliasA = alias2;
                aliasB = alias1;
            }

            var sql = Text.GenerateSql(70);

            for (int i = 0; i < FKColumns.Count(); ++i)
            {
                if (i > 0)
                {
                    sql.Append(Text._And_);
                }

                var columnA = DbMapping.GetColumnMap(FKColumns[i]);
                var columnB = DbMapping.GetColumnMap(RefColumns[i]);

                sql
                .AppendFormat("[{0}].{1} = [{2}].{3}",
                              aliasA, columnA.Name.Sql,
                              aliasB, columnB.Name.Sql);
            }

            return(sql.ToString());
        }
Beispiel #24
0
 /// <summary>
 /// Adds a row type to the mapping data.
 /// </summary>
 /// <param name="type">A type of the row.</param>
 /// <param name="nodeID">A node mapping identifier that row belongs to.</param>
 public static void AddRowType(Type type, DB3 nodeID)
 {
     DbMapping.AddRowType(type, nodeID);
 }
Beispiel #25
0
 internal DbColumns(DbNode parent, DB3 columnID, string alias)
 {
     Column = new DbColumn(parent, columnID, alias);
 }
Beispiel #26
0
 internal DbColumns(DbNode parent, DB3 columnID)
 {
     Column = new DbColumn(parent, columnID);
 }
Beispiel #27
0
 /// <summary>
 /// Initializes a new instance of the Relation class.
 /// </summary>
 /// <param name="foreignKey">The specified column of the relation which represents a unique identifier of the relation between the two tables.</param>
 /// <param name="foreignKeyColumns">The columns which are part of the key in table A.</param>
 /// <param name="referenceKeyColumns">The columns which are part of the key in table B.</param>
 public Relation(DB3 foreignKey, DB3[] foreignKeyColumns, DB3[] referenceKeyColumns)
     : base(foreignKey)
 {
     FKColumns  = foreignKeyColumns;
     RefColumns = referenceKeyColumns;
 }
Beispiel #28
0
 // returns true if the table is on foreign key side in the relationship
 internal bool IsFKTable(DB3 table)
 {
     return(table.Equals(FKTable));
 }
Beispiel #29
0
 // returns true if the table is on reference side in the relationship
 internal bool IsRefTable(DB3 table)
 {
     return(table.Equals(RefTable));
 }
Beispiel #30
0
 internal string BuildRelation(DB3 table1, int alias1, int alias2)
 {
     return(BuildRelation(table1, alias1.ToString(), alias2.ToString()));
 }