예제 #1
0
        /// <summary>
        ///     Create a table of the results of the query definition.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="primaryKeys">The primary key field names.</param>
        /// <param name="copyLocally">if set to <c>true</c> if the data must be copied locally.</param>
        /// <param name="workspace">The workspace that contains the tables.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <returns>Returns a <see cref="ITable" /> representing the results of the query definition.</returns>
        public static ITable Evaluate(this IQueryDef source, string primaryKeys, bool copyLocally, IWorkspace workspace, string tableName)
        {
            IQueryName2 query = new TableQueryNameClass();

            query.QueryDef    = source;
            query.PrimaryKey  = (!copyLocally) ? primaryKeys : "";
            query.CopyLocally = copyLocally;

            IDatasetName ds = (IDatasetName)query;

            ds.WorkspaceName = (IWorkspaceName)((IDataset)workspace).FullName;
            ds.Name          = tableName;

            IName name = (IName)query;

            return((ITable)name.Open());
        }
예제 #2
0
            private static ITable GetQueryTable([NotNull] ITable referencingTable,
                                                [NotNull] string relation,
                                                [NotNull] out string foreignKeyFieldName)
            {
                IList <string> relationTokens = ParseRelationTokens(relation);

                int relationCount = relationTokens.Count;

                if (relationCount == 2)
                {
                    foreignKeyFieldName = relationTokens[1];
                    return(referencingTable);
                }

                if (relationCount == 5)
                {
                    string referencingTableName = DatasetUtils.GetName(referencingTable);

                    string relationReferencedTableFK  = relationTokens[1];
                    string relationTable              = relationTokens[2];
                    string relationReferencingTableFK = relationTokens[3];
                    string referencingTablePK         = relationTokens[4];

                    var workspace =
                        (IFeatureWorkspace)DatasetUtils.GetWorkspace(referencingTable);
                    IQueryDef queryDef = workspace.CreateQueryDef();

                    queryDef.Tables =
                        string.Format("{0},{1}", referencingTableName, relationTable);

                    queryDef.SubFields = string.Format("{0}.{1},{2}.{3}",
                                                       referencingTableName,
                                                       referencingTablePK,
                                                       relationTable,
                                                       relationReferencedTableFK);

                    queryDef.WhereClause = string.Format("{0}.{1} = {2}.{3}",
                                                         referencingTableName,
                                                         referencingTablePK,
                                                         relationTable,
                                                         relationReferencingTableFK);

                    IQueryName2 queryName = new TableQueryNameClass
                    {
                        CopyLocally = false,
                        QueryDef    = queryDef
                    };

                    var name = (IDatasetName)queryName;
                    name.WorkspaceName = WorkspaceUtils.GetWorkspaceName(workspace);
                    name.Name          = string.Format("{0}_{1}",
                                                       referencingTableName,
                                                       relationTable.Replace(".", "_"));

                    if (_msg.IsVerboseDebugEnabled)
                    {
                        _msg.Debug("Creating query-based feature class");

                        using (_msg.IncrementIndentation())
                        {
                            LogQueryName(queryName);
                        }
                    }

                    foreignKeyFieldName = string.Format("{0}.{1}", relationTable,
                                                        relationReferencedTableFK);

                    try
                    {
                        return((ITable)((IName)queryName).Open());
                    }
                    catch (Exception e)
                    {
                        _msg.DebugFormat("Error creating query-based table: {0}",
                                         e.Message);
                        LogQueryName(queryName);

                        throw;
                    }
                }

                throw new InvalidConfigurationException(
                          $"Cannot parse relation: {relation}");
            }