Example #1
0
        public ExplorerItem EmitCodeAndGetExplorerItemTree(TypeBuilder dataContextTypeBuilder)
        {
            var query  = SqlHelper.LoadSql("QueryTables.sql");
            var tables = _connection.Query(query);

            //  The explorer items below the "Tables" explorer items can be either the
            // schemas or directly the tables, if only one schema is available (or selected).
            var rootExplorerItems = new List <ExplorerItem>();

            var schemas = _cxInfo.GetSchemas();

            var schemaGroups = schemas.Any()
            ? tables.GroupBy(t => t.TableSchema).Where(g => schemas.Contains(((string)g.Key).ToLower())).ToArray()
            : tables.GroupBy(t => t.TableSchema).ToArray();

            // If there is only one schema, then the schema explorer item can be skipped and
            // it is not required to add the schema to the table name.
            var onlyOneSchema = schemaGroups.Length == 1;

            foreach (var schemaGroup in schemaGroups)
            {
                var tableExplorerItems = new List <ExplorerItem>();
                var preparedTables     = new List <TableData>();

                foreach (var table in schemaGroup.OrderBy(t => t.TableName))
                {
                    var unmodifiedTableName   = (string)table.TableName;
                    var unmodifiedTableSchema = (string)table.TableSchema;

                    var tableName = onlyOneSchema
                  ? _cxInfo.GetTableName(unmodifiedTableName)
                  : _cxInfo.GetTableName(unmodifiedTableSchema + "_" + unmodifiedTableName);

                    var explorerItem = new ExplorerItem(tableName, ExplorerItemKind.QueryableObject, ExplorerIcon.Table)
                    {
                        IsEnumerable = true,
                        Children     = new List <ExplorerItem>(),
                        DragText     = tableName,
                        SqlName      = $"{unmodifiedTableSchema}.{unmodifiedTableName}"
                    };

                    var tableData = PrepareTableEntity(_cxInfo, _moduleBuilder, _connection, _nameSpace, table.TableCatalog,
                                                       unmodifiedTableSchema, unmodifiedTableName, explorerItem);

                    preparedTables.Add(tableData);
                }

                // build the associations before the types are created
                BuildAssociations(_connection, schemaGroup.Key, preparedTables);

                foreach (var tableData in preparedTables)
                {
                    dataContextTypeBuilder.CreateAndAddType(tableData);
                    tableExplorerItems.Add(tableData.ExplorerItem);
                }

                if (onlyOneSchema)
                {
                    rootExplorerItems.AddRange(tableExplorerItems);
                    continue;
                }

                var schemaExplorerItem = new ExplorerItem(schemaGroup.Key, ExplorerItemKind.Category, ExplorerIcon.Schema)
                {
                    IsEnumerable = true,
                    Children     = tableExplorerItems
                };

                rootExplorerItems.Add(schemaExplorerItem);
            }

            return(new ExplorerItem("Tables", ExplorerItemKind.Category, ExplorerIcon.Table)
            {
                IsEnumerable = true,
                Children = rootExplorerItems
            });
        }