示例#1
0
        /// <summary>
        /// Attempts to locate any rows of attribute data that have a specific key (this involves
        /// a select on all database tables that have been associated with Backsight).
        /// </summary>
        /// <param name="key">The key to look for</param>
        /// <returns>The rows found (may be an empty array)</returns>
        internal static DataRow[] FindByKey(string key)
        {
            // Locate information about the tables associated with Backsight
            ITable[] tables = EnvironmentContainer.Current.Tables;
            if (tables.Length == 0)
            {
                return(new DataRow[0]);
            }

            IDataServer ds = EditingController.Current.DataServer;

            if (ds == null)
            {
                return(new DataRow[0]);
            }

            List <DataRow> result = new List <DataRow>();

            foreach (ITable t in tables)
            {
                string sql = String.Format("SELECT * FROM {0} WHERE [{1}]='{2}'",
                                           t.TableName, t.IdColumnName, key);
                DataTable tab = ds.ExecuteSelect(sql);
                tab.TableName = t.TableName;
                result.AddRange(tab.Select());
            }

            return(result.ToArray());
        }
示例#2
0
        /// <summary>
        /// Searches a specific table to see whether it contains any rows with a specific key
        /// </summary>
        /// <param name="table">The table to select from</param>
        /// <param name="key">The key to look for</param>
        /// <returns>The rows found (may be an empty array). Normally, this array should contain
        /// no more than one row - however, it is possible that the spatial key is not the primary
        /// key of the table.</returns>
        internal static DataRow[] FindByKey(ITable table, string key)
        {
            IDataServer ds = EditingController.Current.DataServer;

            if (ds == null)
            {
                return(new DataRow[0]);
            }

            string sql = String.Format("SELECT * FROM {0} WHERE [{1}]='{2}'",
                                       table.TableName, table.IdColumnName, key);
            DataTable tab = ds.ExecuteSelect(sql);

            tab.TableName = table.TableName;
            return(tab.Select());
        }
示例#3
0
        /// <summary>
        /// Attaches miscellaneous attribute data to the features that have been loaded.
        /// </summary>
        /// <param name="keyIds">Index of the IDs to look for (indexed by formatted key)</param>
        /// <returns>The number of rows that were found (-1 if no database tables have
        /// been associated with Backsight)</returns>
        static int Load(Dictionary <string, FeatureId> keyIds)
        {
            // Locate information about the tables associated with Backsight
            ITable[] tables = EnvironmentContainer.Current.Tables;
            if (tables.Length == 0)
            {
                return(-1);
            }

            IDataServer ds = EditingController.Current.DataServer;

            if (ds == null)
            {
                return(-1);
            }

            // Copy the required keys into a temp table
            Trace.WriteLine(String.Format("Locating attributes for {0} feature IDs in {1} tables", keyIds.Count, tables.Length));

            int nFound = 0;

            ds.RunTransaction(delegate
            {
                IConnection ic = ds.GetConnection();
                const string KEYS_TABLE_NAME = "#Ids";
                CopyKeysToTable(ic, keyIds, KEYS_TABLE_NAME);

                foreach (ITable t in tables)
                {
                    string sql = String.Format("SELECT * FROM {0} WHERE [{1}] IN (SELECT [FeatureId] FROM [{2}])",
                                               t.TableName, t.IdColumnName, KEYS_TABLE_NAME);
                    DataTable tab = ds.ExecuteSelect(sql);
                    tab.TableName = t.TableName;

                    int featureIdIndex = tab.Columns[t.IdColumnName].Ordinal;
                    Debug.Assert(featureIdIndex >= 0);
                    foreach (DataRow row in tab.Select())
                    {
                        string key = row[featureIdIndex].ToString().TrimEnd();
                        FeatureId fid;
                        if (keyIds.TryGetValue(key, out fid))
                        {
                            // Don't create a row if the ID is already associated with the
                            // table (this is meant to cover situations where the edit has actively
                            // formed the attributes, and is calling this method only to cover the
                            // fact that further attributes may be involved).

                            if (!fid.RefersToTable(t))
                            {
                                Row r = new Row(fid, t, row);
                                nFound++;
                            }
                        }
                        else
                        {
                            string msg = String.Format("Cannot find '{0}' in dictionary", key);
                            throw new Exception(msg);
                        }
                    }
                }
            });

            return(nFound);
        }