-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(client): add an interface for database queries handler * feat(client): add a postgres database querier that implements database querier interface * feat(client): add mysql struct that implements database querier * feat(client): add a sqlite struct that implements the database querier interface * feat(client): add the database querier and its implementations to the client * test(client): add some validations to the test
- Loading branch information
1 parent
1b43eac
commit b1eefd1
Showing
5 changed files
with
298 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
) | ||
|
||
// mysql struct is in charge of perform all the mysql related queries, | ||
// without the client knowing. | ||
type mysql struct{} | ||
|
||
// a validation to see if mysql is implementing databaseQuerier. | ||
var _ databaseQuerier = (*mysql)(nil) | ||
|
||
// returns a pointer to a mysql. | ||
func newMySQL() *mysql { | ||
m := mysql{} | ||
return &m | ||
} | ||
|
||
// ShowTables returns a query to retrieve all the tables. | ||
func (m *mysql) ShowTables() (string, []interface{}, error) { | ||
query := "SHOW TABLES;" | ||
return query, nil, nil | ||
} | ||
|
||
// TableStructure returns a query string to retrieve all the relevant information of a given table. | ||
func (m *mysql) TableStructure(tableName string) (string, []interface{}, error) { | ||
query := fmt.Sprintf("DESCRIBE %s;", tableName) | ||
return query, nil, nil | ||
} | ||
|
||
// Constraints returns all the constraints of a given table. | ||
func (m *mysql) Constraints(tableName string) (string, []interface{}, error) { | ||
query := sq.Select( | ||
`tc.constraint_name`, | ||
`tc.table_name`, | ||
`tc.constraint_type`, | ||
). | ||
From("information_schema.table_constraints AS tc"). | ||
Where("tc.table_name = ?", tableName) | ||
|
||
sql, args, err := query.ToSql() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
return sql, args, err | ||
} | ||
|
||
// Indexes returns a query to get all the indexes of a table. | ||
func (m *mysql) Indexes(tableName string) (string, []interface{}, error) { | ||
query := fmt.Sprintf("SHOW INDEX FROM %s", tableName) | ||
return query, nil, nil | ||
} |
Oops, something went wrong.