Example #1
0
        /// <summary>
        /// Attempts to expand a domain lookup value
        /// </summary>
        /// <param name="shortValue">The short lookup string</param>
        /// <param name="columnName">The name of the column the lookup value was obtained from</param>
        /// <param name="table">The table containing the column</param>
        /// <returns>The expanded value in the domain associated with the column. If the column is
        /// not associated with a domain table, or the lookup string cannot be found, you get back
        /// the supplied <paramref name="shortValue"/>.</returns>
        /// <remarks>There is some scope for efficiency improvement here. Rather than doing this
        /// every time some RowTextGeometry is drawn, it would make sense to hold something like
        /// a <c>PreparedTemplate</c> object that holds a parsed template that points more directly
        /// to any domains involved.</remarks>
        static string GetDomainValue(string shortValue, string columnName, ITable table)
        {
            // Attempt to locate the column in question
            IColumnDomain[] cds = table.ColumnDomains;
            IColumnDomain   cd  = Array.Find <IColumnDomain>(cds, t => String.Compare(t.ColumnName, columnName, true) == 0);

            if (cd == null)
            {
                return(shortValue);
            }

            // Obtain the database server
            IDataServer ds = EditingController.Current.DataServer;

            if (ds == null)
            {
                return(shortValue);
            }

            // Perform a lookup on the domain
            IDomainTable dt        = cd.Domain;
            string       longValue = dt.Lookup(ds.ConnectionString, shortValue);

            // If it's not there, it SHOULD be blank
            if (String.IsNullOrEmpty(longValue))
            {
                return(shortValue);
            }
            else
            {
                return(longValue);
            }
        }
 /// <summary>
 /// Creates a new row in the <c>ColumnDomains</c> table
 /// </summary>
 /// <param name="cd">Information for the row to add</param>
 /// <returns>The row inserted into this table</returns>
 internal ColumnDomainRow AddColumnDomainRow(IColumnDomain cd)
 {
     ColumnDomainRow result = NewColumnDomainRow();
     result.TableId = (cd.ParentTable == null ? 0 : cd.ParentTable.Id);
     result.ColumnName = cd.ColumnName;
     result.DomainId = (cd.Domain == null ? 0 : cd.Domain.Id);
     AddColumnDomainRow(result);
     return result;
 }
Example #3
0
            /// <summary>
            /// Creates a new row in the <c>ColumnDomains</c> table
            /// </summary>
            /// <param name="cd">Information for the row to add</param>
            /// <returns>The row inserted into this table</returns>
            internal ColumnDomainRow AddColumnDomainRow(IColumnDomain cd)
            {
                ColumnDomainRow result = NewColumnDomainRow();

                result.TableId    = (cd.ParentTable == null ? 0 : cd.ParentTable.Id);
                result.ColumnName = cd.ColumnName;
                result.DomainId   = (cd.Domain == null ? 0 : cd.Domain.Id);
                AddColumnDomainRow(result);
                return(result);
            }
        /// <summary>
        /// Initializes a new instance of the <see cref="ColumnDomainConverter"/> class.
        /// </summary>
        /// <param name="ds">The database holding domain data</param>
        /// <param name="cd">The column domain that is being converted (not null)</param>
        public ColumnDomainConverter(IDataServer ds, IColumnDomain cd)
        {
            if (ds == null || cd == null)
                throw new ArgumentNullException();

            m_DataServer = ds;
            m_ColumnDomain = cd;
            IDomainTable dt = cd.Domain;
            string[] lookups = dt.GetLookupValues(ds.ConnectionString);
            m_Values = new StandardValuesCollection(lookups);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ColumnDomainConverter"/> class.
        /// </summary>
        /// <param name="ds">The database holding domain data</param>
        /// <param name="cd">The column domain that is being converted (not null)</param>
        public ColumnDomainConverter(IDataServer ds, IColumnDomain cd)
        {
            if (ds == null || cd == null)
            {
                throw new ArgumentNullException();
            }

            m_DataServer   = ds;
            m_ColumnDomain = cd;
            IDomainTable dt = cd.Domain;

            string[] lookups = dt.GetLookupValues(ds.ConnectionString);
            m_Values = new StandardValuesCollection(lookups);
        }
Example #6
0
        /// <summary>
        /// Display the attributes of a specific row on this property page
        /// </summary>
        /// <param name="row">The row of interest (not null)</param>
        /// <exception cref="ArgumentNullException">If the supplied row is null</exception>
        internal void SetRow(Row row)
        {
            if (row == null)
            {
                throw new ArgumentNullException();
            }

            m_Row = row;

            // Set the text on the tab
            this.Text = row.Table.TableName;

            // We'll display any expanded domain values if we can
            IColumnDomain[] cds = row.Table.ColumnDomains;

            // Hmm, there isn't a PropertyGrid.DataSource property, so do it the
            // hard way (is there a better way?)

            DataRow   data  = row.Data;
            DataTable table = data.Table;

            object[]          items = data.ItemArray;
            AdhocPropertyList props = new AdhocPropertyList(items.Length);

            for (int i = 0; i < items.Length; i++)
            {
                DataColumn    dc         = table.Columns[i];
                string        columnName = dc.ColumnName;
                AdhocProperty item       = new AdhocProperty(columnName, items[i]);
                item.ReadOnly = true;

                // If the column is associated with a domain, lookup the expanded value and
                // record as the item's description

                IColumnDomain cd = Array.Find <IColumnDomain>(cds, t => String.Compare(t.ColumnName, columnName, true) == 0);

                if (cd != null)
                {
                    string shortValue = items[i].ToString();
                    string longValue  = cd.Domain.Lookup(m_DataServer.ConnectionString, shortValue);
                    item.Description = longValue;
                }

                props.Add(item);
            }

            propertyGrid.SelectedObject = props;
        }
Example #7
0
            /// <summary>
            /// Checks an array of <c>ColumnDomainRow</c> to see if any element refers
            /// to a specific database column and domain
            /// </summary>
            /// <param name="cd">The item to look for</param>
            /// <param name="rows">The rows to check</param>
            /// <returns>True if any row matches the search item</returns>
            /// <remarks>This helps to avoid the clumsy syntax of Array.Find, making
            /// calling code a bit more readable.</remarks>
            internal static bool HasMatchInArray(IColumnDomain cd, ColumnDomainRow[] rows)
            {
                int    tableId       = (cd.ParentTable == null ? 0 : cd.ParentTable.Id);
                int    domainTableId = (cd.Domain == null ? 0 : cd.Domain.Id);
                string columnName    = cd.ColumnName;

                foreach (ColumnDomainRow row in rows)
                {
                    if (row.TableId == tableId &&
                        row.DomainId == domainTableId &&
                        row.ColumnName == columnName)
                    {
                        return(true);
                    }
                }

                return(false);
            }
Example #8
0
        /// <summary>
        /// Assigns default values to fields in a new row of attribute data. Numeric fields are
        /// assigned a value of 0 (even though this could conceivably break check constraints).
        /// Fields associated with a domain table are assigned the first lookup value (whatever
        /// that is). Any other [var]char field that is not nullable will be assigned a blank value.
        /// </summary>
        /// <param name="data">The data to assign defaults to</param>
        void AssignDefaultValues(DataRow data)
        {
            DataTable dt = data.Table;

            IColumnDomain[] cds = m_Table.ColumnDomains;

            foreach (DataColumn dc in dt.Columns)
            {
                Type t = dc.DataType;
                if (t == typeof(int) || t == typeof(short) || t == typeof(byte))
                {
                    data[dc] = 0;
                }
                else if (t == typeof(double) || t == typeof(float))
                {
                    data[dc] = 0.0;
                }
                else if (t == typeof(string))
                {
                    IColumnDomain cd = FindColumnDomain(dc.ColumnName);

                    if (cd != null)
                    {
                        string[] vals = cd.Domain.GetLookupValues(m_DataServer.ConnectionString);
                        if (vals.Length > 0)
                        {
                            data[dc] = vals[0];
                        }
                    }
                    else if (!dc.AllowDBNull)
                    {
                        // Default to a blank string is the field isn't nullable
                        data[dc] = String.Empty;
                    }
                }
            }
        }
Example #9
0
        private void columnsPage_ShowFromNext(object sender, EventArgs e)
        {
            columnsGrid.Rows.Clear();
            idColumnComboBox.Items.Clear();

            TableFactory tf        = new TableFactory();
            string       tableName = m_Edit.TableName;

            Smo.Table t = tf.FindTableByName(tableName);

            if (t == null)
            {
                return;
            }

            // Get any domains already associated with the table
            IColumnDomain[] curDomains = m_Edit.ColumnDomains;

            columnsGrid.RowCount = t.Columns.Count;

            for (int i = 0; i < columnsGrid.RowCount; i++)
            {
                Smo.Column c = t.Columns[i];
                idColumnComboBox.Items.Add(c.Name);

                DataGridViewRow row = columnsGrid.Rows[i];
                row.Cells["dgcColumnName"].Value = c.Name;

                Smo.DataType dt       = c.DataType;
                string       dataType = dt.SqlDataType.ToString().ToLower();

                if (dt.SqlDataType == Smo.SqlDataType.Char ||
                    dt.SqlDataType == Smo.SqlDataType.NChar ||
                    dt.SqlDataType == Smo.SqlDataType.VarChar ||
                    dt.SqlDataType == Smo.SqlDataType.NVarChar)
                {
                    dataType += String.Format("({0})", dt.MaximumLength);
                }

                if (!c.Nullable)
                {
                    dataType += " not null";
                }

                row.Cells["dgcDataType"].Value = dataType;

                // Display any domain previously associated with the column
                IColumnDomain cd = Array.Find <IColumnDomain>(curDomains,
                                                              delegate(IColumnDomain tcd) { return(tcd.ColumnName == c.Name); });
                if (cd != null)
                {
                    row.Cells["dgcDomain"].Value = cd.Domain;
                }

                row.Tag = c;
            }

            // Nothing initially selected
            columnsGrid.CurrentCell = null;

            // If we have a simple primary key, assume it's the feature ID column
            if (String.IsNullOrEmpty(m_Edit.IdColumnName))
            {
                Smo.Column pk = TableFactory.GetSimplePrimaryKeyColumn(t);
                if (pk != null)
                {
                    idColumnComboBox.SelectedItem = pk.Name;
                }
            }
            else
            {
                idColumnComboBox.SelectedItem = m_Edit.IdColumnName;
            }
        }
Example #10
0
        private void grid_SelectionChanged(object sender, EventArgs e)
        {
            domainGrid.Tag = null;
            domainGrid.Rows.Clear();
            domainGrid.Enabled = false;

            if (!grid.Enabled)
            {
                return;
            }

            DataGridViewRow row = GetSelectedGridRow();

            if (row == null)
            {
                return;
            }

            // Show the data type
            DataColumn dc = (DataColumn)row.Tag;

            dataTypeLabel.Text = dc.DataType.Name;

            if (!dc.AllowDBNull)
            {
                dataTypeLabel.Text += " not null";
            }

            if (dc.DataType == typeof(string))
            {
                if (dc.MaxLength == 1)
                {
                    dataTypeLabel.Text += " (1 character)";
                }
                else
                {
                    dataTypeLabel.Text += String.Format(" (up to {0} characters)", dc.MaxLength);
                }
            }

            // Show any domain values
            IColumnDomain cd = FindColumnDomain(dc.ColumnName);

            domainValuesLabel.Enabled = (cd != null);

            if (cd != null)
            {
                // Note the currently defined value
                string           currentValue = row.Cells["dgcValue"].FormattedValue.ToString();
                DataGridViewCell currentCell  = null;

                IDomainTable domainTable = cd.Domain;
                string[]     lookups     = domainTable.GetLookupValues(m_DataServer.ConnectionString);
                domainGrid.RowCount = lookups.Length;
                for (int i = 0; i < lookups.Length; i++)
                {
                    string          shortValue = lookups[i];
                    DataGridViewRow r          = domainGrid.Rows[i];
                    r.Tag = shortValue;
                    r.Cells["dgcShortValue"].Value = shortValue;
                    r.Cells["dgcLongValue"].Value  = domainTable.Lookup(m_DataServer.ConnectionString, shortValue);

                    // If we have just defined the current data value, remember the cell so
                    // that we can set it once the grid has been loaded.
                    if (shortValue == currentValue)
                    {
                        currentCell = r.Cells["dgcShortValue"];
                    }
                }

                domainGrid.CurrentCell = currentCell;
                domainGrid.Enabled     = true;
                domainGrid.Tag         = domainTable;
            }
        }
            /// <summary>
            /// Checks an array of <c>ColumnDomainRow</c> to see if any element refers
            /// to a specific database column and domain
            /// </summary>
            /// <param name="cd">The item to look for</param>
            /// <param name="rows">The rows to check</param>
            /// <returns>True if any row matches the search item</returns>
            /// <remarks>This helps to avoid the clumsy syntax of Array.Find, making
            /// calling code a bit more readable.</remarks>
            internal static bool HasMatchInArray(IColumnDomain cd, ColumnDomainRow[] rows)
            {
                int tableId = (cd.ParentTable == null ? 0 : cd.ParentTable.Id);
                int domainTableId = (cd.Domain == null ? 0 : cd.Domain.Id);
                string columnName = cd.ColumnName;

                foreach (ColumnDomainRow row in rows)
                {
                    if (row.TableId == tableId &&
                        row.DomainId == domainTableId &&
                        row.ColumnName == columnName)
                        return true;
                }

                return false;
            }