getTables fetches table info and returns structed table data.
(ctx context.Context, databaseName string)
| 106 | |
| 107 | // getTables fetches table info and returns structed table data. |
| 108 | func (d *Driver) getTables(ctx context.Context, databaseName string) ( |
| 109 | []*storepb.TableMetadata, |
| 110 | []*storepb.ExternalTableMetadata, |
| 111 | []*storepb.ViewMetadata, |
| 112 | []*storepb.MaterializedViewMetadata, |
| 113 | error, |
| 114 | ) { |
| 115 | var ( |
| 116 | tableMetadatas []*storepb.TableMetadata |
| 117 | extTableMetadatas []*storepb.ExternalTableMetadata |
| 118 | viewMetadatas []*storepb.ViewMetadata |
| 119 | mtViewMetadatas []*storepb.MaterializedViewMetadata |
| 120 | ) |
| 121 | |
| 122 | // list tables' names. |
| 123 | tableNames, err := d.listTablesNames(ctx, databaseName) |
| 124 | if err != nil { |
| 125 | return nil, nil, nil, nil, errors.Wrapf(err, "failed to list tables") |
| 126 | } |
| 127 | |
| 128 | // iterations in tables of certain database. |
| 129 | for _, tableName := range tableNames { |
| 130 | // filter out index table names. |
| 131 | if strings.HasSuffix(tableName, "__") { |
| 132 | continue |
| 133 | } |
| 134 | |
| 135 | tableInfo, err := d.getTableInfo(ctx, tableName, databaseName) |
| 136 | if err != nil { |
| 137 | return nil, nil, nil, nil, errors.Wrapf(err, "failed to describe table %s's type", tableName) |
| 138 | } |
| 139 | |
| 140 | // different processing way according to the type of the table. |
| 141 | switch tableInfo.tableType { |
| 142 | case "MATERIALIZED_VIEW": |
| 143 | mtViewMetadatas = append(mtViewMetadatas, &storepb.MaterializedViewMetadata{ |
| 144 | Name: tableName, |
| 145 | Definition: tableInfo.viewDef, |
| 146 | Comment: tableInfo.comment, |
| 147 | }) |
| 148 | case "VIRTUAL_VIEW": |
| 149 | viewMetadatas = append(viewMetadatas, &storepb.ViewMetadata{ |
| 150 | Name: tableName, |
| 151 | Definition: tableInfo.viewDef, |
| 152 | Comment: tableInfo.comment, |
| 153 | }) |
| 154 | case "EXTERNAL_TABLE": |
| 155 | extTableMetadatas = append(extTableMetadatas, &storepb.ExternalTableMetadata{ |
| 156 | Name: tableName, |
| 157 | Columns: tableInfo.colMetadatas, |
| 158 | }) |
| 159 | case "MANAGED_TABLE": |
| 160 | partitions, err := d.getPartitions(ctx, databaseName, tableName) |
| 161 | if err != nil { |
| 162 | // Ignore partitions error as some tables aren't partitioned. |
| 163 | slog.Debug("failed to get partitions", log.BBError(err)) |
| 164 | continue |
| 165 | } |
no test coverage detected