Skip to content

Commit

Permalink
Cache 'SHOW FULL TABLES' (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Schuch <[email protected]>
  • Loading branch information
nickschuch and nickschuch authored Nov 25, 2023
1 parent 4401c25 commit 875fcb3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (o Connection) Open(database string) (*sql.DB, error) {
type Client struct {
DB *sql.DB
Logger *log.Logger
// A field for caching a list of tables for this database.
cachedTables []string
}

// NewClient for dumping a full or single table from a database.
Expand Down
8 changes: 8 additions & 0 deletions internal/mysql/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (d *Client) ListTablesByGlob(globs []string) ([]string, error) {

// QueryTables will return a list of tables.
func (d *Client) QueryTables() ([]string, error) {
// Use the cached tables if we have them.
if len(d.cachedTables) > 0 {
return d.cachedTables, nil
}

tables := make([]string, 0)

rows, err := d.DB.Query("SHOW FULL TABLES")
Expand All @@ -56,6 +61,9 @@ func (d *Client) QueryTables() ([]string, error) {
}
}

// Set the cached tables for future executions.
d.cachedTables = tables

return tables, nil
}

Expand Down

0 comments on commit 875fcb3

Please sign in to comment.