Exemplo n.º 1
0
        private void AddReference(List <ColumnValue> list, ForeignKeyConstraint constraint)
        {
            Db2SourceContext dataSet = Table.Context;
            Table            refTbl  = dataSet.Tables[constraint.ReferenceSchemaName, constraint.ReferenceTableName];

            if (refTbl == null)
            {
                return;
            }
            ColumnInfo[] cols = Row.GetForeignKeyColumns(constraint);
            //List<IDbDataParameter> prms = new List<IDbDataParameter>(cols.Length);
            StringBuilder buf  = new StringBuilder();
            string        join = string.Empty;

            for (int i = 0, n = cols.Length; i < n; i++)
            {
                ColumnInfo col = cols[i];
                buf.Append(join);
                buf.Append(constraint.RefColumns[i]);
                buf.Append(" = :");
                buf.Append(col.Name);
                buf.Append("::");
                buf.Append(Table.Columns[col.Name].DataType);
                //prms.Add(dataSet.CreateParameterByFieldInfo(col, Row[col.Index], false));
                join = " and ";
            }
            using (IDbConnection conn = dataSet.NewConnection(true))
            {
                using (IDbCommand cmd = dataSet.GetSqlCommand(refTbl.GetSelectSQL(string.Empty, buf.ToString(), string.Empty, null, HiddenLevel.Visible), null, conn))
                {
                    foreach (ColumnInfo col in cols)
                    {
                        dataSet.ApplyParameterByFieldInfo(cmd.Parameters, col, Row[col.Index], false);
                    }
                    IDataReader reader = cmd.ExecuteReader();
                    if (!reader.Read())
                    {
                        return;
                    }
                    list.Add(new ColumnValue()
                    {
                        IsHeader = true, ColumnName = refTbl.Name, Value = null
                    });
                    for (int i = 0, n = reader.FieldCount; i < n; i++)
                    {
                        object v = null;
                        try
                        {
                            v = reader.GetValue(i);
                        }
                        catch (OverflowException)
                        {
                            v = "(OVERFLOW)";
                        }
                        list.Add(new ColumnValue()
                        {
                            IsHeader = false, ColumnName = reader.GetName(i), Value = v
                        });
                    }
                }
            }
        }