Exemplo n.º 1
0
        internal void Load(DataTable table, DataSet dbset, int flag, int level)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (dbset == null)
            {
                throw new ArgumentNullException("dbset");
            }

            var value = Value;

            if (value == null)
            {
                value = Type.Assembly.CreateInstance(Type.FullName) as DBRefBase;
                _pinfo.SetValue(_host.Entity, value);
            }
            value.Clear();
            var rows = table.Rows.Cast <DataRow>().Where(row =>
            {
                var result = 1;
                _keys.ToList().ForEach(k =>
                {
                    var column = _host.GetColumn(k.Key);
                    var pk0    = column.Value;
                    var pk1    = TableSchema.ValueTypeConvert(row[k.Value], column.Type);
                    result    *= pk0.Equals(pk1) ? 1 : 0;
                });
                return(result == 1);
            });


            if (rows.Count() > 0)
            {
                if (value.Type.Equals("DBRefEntity"))
                {
                    var ent = value.NewTableEntity();
                    ent.Fill(rows.First(), dbset, flag, level);
                }

                if (value.Type.Equals("DBRefList"))
                {
                    rows.Cast <DataRow>().ToList().ForEach(row =>
                    {
                        var ent = value.NewTableEntity();
                        ent.Fill(row, dbset, flag, level);
                    });
                }
            }
        }
Exemplo n.º 2
0
Arquivo: Join.cs Projeto: eleooo/App
        /// <summary>
        /// Initializes a new instance of the <see cref="Join"/> class.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="joinType">Type of the join.</param>
        public Join(TableSchema.Table from, TableSchema.Table to, JoinType joinType,params string[] joinExpressions)
        {
            TableSchema.TableColumn fromCol = null;
            TableSchema.TableColumn toCol = null;
            if (joinExpressions != null && joinExpressions.Length > 0)
                JoinExpressions.AddRange(joinExpressions);
            foreach(TableSchema.TableColumn col in from.Columns)
            {
                if(col.IsForeignKey && !String.IsNullOrEmpty(col.ForeignKeyTableName))
                {
                    //TableSchema.Table fkTable = col.Table.Provider.GetTableSchema(col.ForeignKeyTableName, col.Table.TableType);
                    TableSchema.Table fkTable = DataService.GetSchema(col.ForeignKeyTableName, col.Table.Provider.Name, col.Table.TableType);
                    if(Utility.IsMatch(fkTable.Name, to.Name))
                    {
                        fromCol = col;
                        //found it - use the PK
                        toCol = fkTable.PrimaryKey;
                        break;
                    }
                }
            }

            //reverse it - just in case we can't find a match
            if(fromCol == null || toCol == null)
            {
                foreach(TableSchema.TableColumn col in to.Columns)
                {
                    if(col.IsForeignKey && !String.IsNullOrEmpty(col.ForeignKeyTableName))
                    {
                        //TableSchema.Table fkTable = col.Table.Provider.GetTableSchema(col.ForeignKeyTableName, col.Table.TableType);
                        TableSchema.Table fkTable = DataService.GetSchema(col.ForeignKeyTableName, col.Table.Provider.Name, col.Table.TableType);
                        if(Utility.IsMatch(fkTable.Name, from.Name))
                        {
                            toCol = col;
                            //found it - use the PK
                            fromCol = fkTable.PrimaryKey;
                            break;
                        }
                    }
                }
            }

            //if that fails, see if there is a matching column name
            if(fromCol == null || toCol == null)
            {
                //first, try to match the PK on the from table
                //to a column in the "to" table
                if(to.Columns.Contains(from.PrimaryKey.ColumnName))
                {
                    FromColumn = from.PrimaryKey;
                    ToColumn = to.GetColumn(from.PrimaryKey.ColumnName);
                    //if that doesn't work, see if the PK of the "to" table has a
                    //matching column in the "from" table
                }
                else if(from.Columns.Contains(to.PrimaryKey.ColumnName))
                {
                    FromColumn = from.GetColumn(to.PrimaryKey.ColumnName);
                    ToColumn = to.PrimaryKey;
                }
            }

            //if that fails - run a match on any column that matches in "from" to "to"
            if(fromCol == null || toCol == null)
            {
                foreach(TableSchema.TableColumn col in from.Columns)
                {
                    if(to.Columns.Contains(col.ColumnName))
                    {
                        toCol = to.GetColumn(col.ColumnName);
                        fromCol = col;
                        break;
                    }
                }
            }

            //still null? this seems exhausting, but the good thing is that these are indexed loops
            //so they execute fast :)
            if(fromCol == null || toCol == null)
            {
                foreach(TableSchema.TableColumn col in to.Columns)
                {
                    if(from.Columns.Contains(col.ColumnName))
                    {
                        fromCol = from.GetColumn(col.ColumnName);
                        toCol = col;
                        break;
                    }
                }
            }

            //if it's still null, throw since the join can't be made
            //and that's a failure of this method
            if(fromCol == null || toCol == null)
            {
                throw new SqlQueryException("Can't create a join for " + from.TableName + " to " + to.TableName +
                                            " - can't determine the columns to link on. Try specifying the columns (using their schema) or specifying the table/column pair");
            }
            FromColumn = fromCol;
            ToColumn = toCol;
            _joinType = joinType;
        }