(ctx context.Context, tableName string, databaseName string)
| 236 | ) |
| 237 | |
| 238 | func (d *Driver) getTableInfo(ctx context.Context, tableName string, databaseName string) ( |
| 239 | *TableInfo, |
| 240 | error, |
| 241 | ) { |
| 242 | tableInfo := &TableInfo{} |
| 243 | |
| 244 | query := fmt.Sprintf("DESCRIBE FORMATTED `%s`.`%s`", databaseName, tableName) |
| 245 | rows, err := d.db.QueryContext(ctx, query) |
| 246 | if err != nil { |
| 247 | return nil, errors.Wrapf(err, "failed to describe table %s", tableName) |
| 248 | } |
| 249 | defer rows.Close() |
| 250 | |
| 251 | section := columnSection |
| 252 | for rows.Next() { |
| 253 | var colName, dataType, comment string |
| 254 | if err := rows.Scan(&colName, &dataType, &comment); err != nil { |
| 255 | return nil, errors.Wrap(err, "failed to scan row") |
| 256 | } |
| 257 | |
| 258 | // The first rows are column metadata, followed by "# Detailed Table Information" and "# Storage Information" |
| 259 | switch { |
| 260 | case strings.HasPrefix(colName, "# col_name"): |
| 261 | continue |
| 262 | case strings.HasPrefix(colName, "# Detailed Table Information"): |
| 263 | section = tableInfoSection |
| 264 | case strings.HasPrefix(colName, "# Storage Information"): |
| 265 | section = storageSection |
| 266 | case strings.HasPrefix(colName, "# View Information"): |
| 267 | section = viewSection |
| 268 | default: |
| 269 | // No action needed for other cases |
| 270 | } |
| 271 | switch section { |
| 272 | case columnSection: |
| 273 | if colName != "" && dataType != "" { |
| 274 | // Column metadata. |
| 275 | position := len(tableInfo.colMetadatas) + 1 |
| 276 | tableInfo.colMetadatas = append(tableInfo.colMetadatas, &storepb.ColumnMetadata{ |
| 277 | Name: colName, |
| 278 | Type: dataType, |
| 279 | Comment: comment, |
| 280 | Position: int32(position), |
| 281 | }) |
| 282 | } |
| 283 | case tableInfoSection: |
| 284 | switch { |
| 285 | case trimField(colName) == "Table Type:": |
| 286 | tableInfo.tableType = trimField(dataType) |
| 287 | case trimField(dataType) == "numRows": |
| 288 | n, err := strconv.Atoi(trimField(comment)) |
| 289 | if err != nil { |
| 290 | return nil, errors.Wrapf(err, "failed to parse row count") |
| 291 | } |
| 292 | tableInfo.numRows = n |
| 293 | case trimField(dataType) == "totalSize": |
| 294 | n, err := strconv.Atoi(trimField(comment)) |
| 295 | if err != nil { |
no test coverage detected