Skip to content

Commit 613720a

Browse files
committed
added docs to redhsift,bigquery,mssql and updated docs in whole codebase
Signed-off-by: adarsh-jaiss <[email protected]>
1 parent cdd4ccf commit 613720a

33 files changed

+631
-446
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ go get github.com/thesaas-company/xray@latest
2020
- Navigate to the `xray` directory.
2121
- Run this command to install the dependencies : `go mod install`
2222

23-
## Maintainer
24-
- [@Adarsh Jaiswal](https://github.com/adarsh-jaiss)
25-
- [@tqindia](https://github.com/tqindia)
23+
### Supported Databbases
2624

2725
## Show Your Support!
2826

cli/cmd/root.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,38 @@ import (
1717
"gopkg.in/yaml.v3"
1818
)
1919

20+
// Command line flags
2021
var (
2122
verbose bool
2223
cfgFile string
2324
dbType string
2425
)
2526

27+
// QueryResult represents the result of a database query.
2628
type QueryResult struct {
2729
Columns []string `json:"columns"`
2830
Rows [][]interface{} `json:"rows"`
2931
Time float64 `json:"time"`
3032
Error string `json:"error"`
3133
}
3234

35+
// Table represents a table with headers and rows.
3336
type Table struct {
3437
headers []string
3538
rows [][]string
3639
}
3740

41+
// NewTable creates a new Table with the given headers.
3842
func NewTable(headers []string) *Table {
3943
return &Table{headers: headers}
4044
}
4145

46+
// AddRow adds a row to the table.
4247
func (t *Table) AddRow(row []string) {
4348
t.rows = append(t.rows, row)
4449
}
4550

51+
// String returns a string representation of the table.
4652
func (t *Table) String() string {
4753
// Find the maximum width of each column
4854
columnWidths := make([]int, len(t.headers))
@@ -84,6 +90,7 @@ func (t *Table) String() string {
8490
return result.String()
8591
}
8692

93+
// toInterfaceSlice converts a slice of strings to a slice of interfaces.
8794
func toInterfaceSlice(strs []string) []interface{} {
8895
result := make([]interface{}, len(strs))
8996
for i, s := range strs {
@@ -97,6 +104,7 @@ var shellCmd = &cobra.Command{
97104
Use: "shell",
98105
Short: "Interact with databases",
99106
Run: func(cmd *cobra.Command, args []string) {
107+
// Set up logging
100108
if !verbose {
101109
logrus.SetOutput(io.Discard)
102110
} else {
@@ -114,7 +122,6 @@ var shellCmd = &cobra.Command{
114122
fmt.Printf("Error: Failed to read YAML file: %v\n", err)
115123
return
116124
}
117-
118125
var cfg config.Config
119126
err = yaml.Unmarshal(configData, &cfg)
120127
if err != nil {
@@ -187,6 +194,7 @@ var shellCmd = &cobra.Command{
187194
},
188195
}
189196

197+
// Execute runs the command line interface.
190198
func Execute() {
191199
rootCmd := &cobra.Command{Use: "xray"}
192200

@@ -215,6 +223,8 @@ func parseDbType(s string) xrayTypes.DbType {
215223
return xrayTypes.BigQuery
216224
case "redshift":
217225
return xrayTypes.Redshift
226+
case "mssql":
227+
return xrayTypes.MSSQL
218228
default:
219229
return xrayTypes.MySQL
220230
}

client.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -19,91 +19,92 @@ import (
1919
// NewClientWithConfig creates a new SQL client with the given configuration and database type.
2020
// It returns an error if the database type is not supported or if there is a problem creating the client.
2121
func NewClientWithConfig(dbConfig *config.Config, dbType types.DbType) (types.ISQL, error) {
22+
// Create a new SQL client based on the database type
2223
switch dbType {
2324
case types.MySQL:
24-
sqlClient, err := mysql.NewMySQLWithConfig(dbConfig)
25+
sqlClient, err := mysql.NewMySQLWithConfig(dbConfig) // NewMySQLWithConfig is a SQL client that connects to a MySQL database using the given configuration.
2526
if err != nil {
2627
return nil, err
2728
}
2829
return logger.NewLogger(sqlClient), nil
2930
case types.Postgres:
30-
sqlClient, err := postgres.NewPostgresWithConfig(dbConfig)
31+
sqlClient, err := postgres.NewPostgresWithConfig(dbConfig) // NewPostgresWithConfig is a SQL client that connects to a Postgres database using the given configuration.
3132
if err != nil {
3233
return nil, err
3334
}
3435
return logger.NewLogger(sqlClient), nil
3536
case types.Snowflake:
36-
sqlClient, err := snowflake.NewSnowflakeWithConfig(dbConfig)
37+
sqlClient, err := snowflake.NewSnowflakeWithConfig(dbConfig) // NewSnowflakeWithConfig is a SQL client that connects to a Snowflake database using the given configuration.
3738
if err != nil {
3839
return nil, err
3940
}
4041
return logger.NewLogger(sqlClient), nil
4142
case types.BigQuery:
42-
bigqueryClient, err := bigquery.NewBigQueryWithConfig(dbConfig)
43+
bigqueryClient, err := bigquery.NewBigQueryWithConfig(dbConfig) // NewBigQueryWithConfig is a SQL client that connects to a BigQuery database using the given configuration.
4344
if err != nil {
4445
return nil, err
4546
}
4647
return logger.NewLogger(bigqueryClient), nil
4748
case types.Redshift:
48-
redshiftClient, err := redshift.NewRedshiftWithConfig(dbConfig)
49+
redshiftClient, err := redshift.NewRedshiftWithConfig(dbConfig) // NewRedshiftWithConfig is a SQL client that connects to a Redshift database using the given configuration.
4950
if err != nil {
5051
return nil, err
5152
}
5253
return logger.NewLogger(redshiftClient), nil
5354
case types.MSSQL:
54-
mssqlClient, err := mssql.NewMSSQLFromConfig(dbConfig)
55+
mssqlClient, err := mssql.NewMSSQLFromConfig(dbConfig) // NewMSSQLFromConfig is a SQL client that connects to a MSSQL database using the given configuration.
5556
if err != nil {
5657
return nil, err
5758
}
5859
return logger.NewLogger(mssqlClient), nil
5960

6061
default:
61-
return nil, fmt.Errorf("unsupported database type: %s", dbType)
62+
return nil, fmt.Errorf("unsupported database type: %s", dbType) // Return an error if the database type is not supported.
6263
}
6364
}
6465

6566
// NewClient creates a new SQL client with the given database client and database type.
6667
// It returns an error if the database type is not supported or if there is a problem creating the client.
6768
func NewClient(dbClient *sql.DB, dbType types.DbType) (types.ISQL, error) {
68-
69+
// Create a new SQL client based on the database type
6970
switch dbType {
7071
case types.MySQL:
71-
sqlClient, err := mysql.NewMySQL(dbClient)
72+
sqlClient, err := mysql.NewMySQL(dbClient) // NewMySQL is a SQL client that connects to a MySQL database using the given database client.
7273
if err != nil {
7374
return nil, err
7475
}
7576
return logger.NewLogger(sqlClient), nil
7677
case types.Postgres:
77-
sqlClient, err := postgres.NewPostgres(dbClient)
78+
sqlClient, err := postgres.NewPostgres(dbClient) // NewPostgres is a SQL client that connects to a Postgres database using the given database client.
7879
if err != nil {
7980
return nil, err
8081
}
8182
return logger.NewLogger(sqlClient), nil
8283
case types.Snowflake:
83-
sqlClient, err := snowflake.NewSnowflake(dbClient)
84+
sqlClient, err := snowflake.NewSnowflake(dbClient) // NewSnowflake is a SQL client that connects to a Snowflake database using the given database client.
8485
if err != nil {
8586
return nil, err
8687
}
8788
return logger.NewLogger(sqlClient), nil
8889
case types.BigQuery:
89-
BigQueryClient, err := bigquery.NewBigQuery(dbClient)
90+
BigQueryClient, err := bigquery.NewBigQuery(dbClient) // NewBigQuery is a SQL client that connects to a BigQuery database using the given database client.
9091
if err != nil {
9192
return nil, err
9293
}
9394
return logger.NewLogger(BigQueryClient), nil
9495
case types.Redshift:
95-
redshiftClient, err := redshift.NewRedshift(dbClient)
96+
redshiftClient, err := redshift.NewRedshift(dbClient) // NewRedshift is a SQL client that connects to a Redshift database using the given database client.
9697
if err != nil {
9798
return nil, err
9899
}
99100
return logger.NewLogger(redshiftClient), nil
100101
case types.MSSQL:
101-
mssqlClient, err := mssql.NewMSSQL(dbClient)
102+
mssqlClient, err := mssql.NewMSSQL(dbClient) // NewMSSQL is a SQL client that connects to a MSSQL database using the given database client.
102103
if err != nil {
103104
return nil, err
104105
}
105106
return logger.NewLogger(mssqlClient), nil
106107
default:
107-
return nil, fmt.Errorf("unsupported database type: %s", dbType)
108+
return nil, fmt.Errorf("unsupported database type: %s", dbType) // Return an error if the database type is not supported.
108109
}
109110
}

config/config.go

-18
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,6 @@ type Config struct {
4747

4848
// Server is the MSSQL database server.
4949
Server string `yaml:"server" pflag:",Database server"`
50-
51-
// AWS holds the AWS configuration details.
52-
// AWS AWS `yaml:"aws"`
5350
}
5451

55-
// type AWS struct {
56-
// // Region is the AWS region.
57-
// Region string `yaml:"region" pflag:",AWS region"`
58-
59-
// // AccessKey is the AWS access key.
60-
// AccessKey string `yaml:"access_key" pflag:",AWS access key"`
61-
62-
// // SecretKey is the AWS secret key.
63-
// SecretAccessKey string `yaml:"secret_key" pflag:",AWS secret key"`
64-
65-
// // ClusterIdentifier is the AWS cluster identifier.
66-
// ClusterIdentifier string `yaml:"cluster_identifier" pflag:",AWS cluster identifier"`
6752

68-
// // SecretArn is the AWS secret ARN.
69-
// SecretArn string `yaml:"secret_arn" pflag:",AWS secret ARN"`
70-
// }

databases/bigquery/bigquery.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func NewBigQuery(client *sql.DB) (types.ISQL, error) {
3535
}
3636

3737
// NewBigQueryWithConfig creates a new instance of BigQuery with the provided configuration.
38+
// It returns an instance of types.ISQL and an error.
3839
func NewBigQueryWithConfig(cfg *config.Config) (types.ISQL, error) {
3940
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" || len(os.Getenv(GOOGLE_APPLICATION_CREDENTIALS)) == 0 {
4041
return nil, fmt.Errorf("please set %s env variable for the database", GOOGLE_APPLICATION_CREDENTIALS)
@@ -70,7 +71,7 @@ func (b *BigQuery) Schema(table string) (types.Table, error) {
7071
}
7172
}()
7273

73-
// scanning the result into and append it into a variable
74+
// scanning the result into a variable and append it into a the slice
7475
var columns []types.Column
7576
columnNames, err := rows.Columns()
7677
if err != nil {
@@ -104,6 +105,8 @@ func (b *BigQuery) Schema(table string) (types.Table, error) {
104105

105106
}
106107

108+
// Execute executes a query on BigQuery.
109+
// It takes a query string as input and returns the result as a byte slice and an error.
107110
func (b *BigQuery) Execute(query string) ([]byte, error) {
108111
rows, err := b.Client.Query(query)
109112
if err != nil {
@@ -161,7 +164,6 @@ func (b *BigQuery) Execute(query string) ([]byte, error) {
161164

162165
// Tables returns a list of tables in a dataset.
163166
// It takes a dataset name as input and returns a slice of strings and an error.
164-
165167
func (b *BigQuery) Tables(dataset string) ([]string, error) {
166168
// res, err := b.Client.Query("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + Dataset + "'")
167169

@@ -207,6 +209,7 @@ func (b *BigQuery) GenerateCreateTableQuery(table types.Table) string {
207209
return query
208210
}
209211

212+
// convertTypeToBigQuery converts a Data type to a BigQuery SQL Data type.
210213
func convertTypeToBigQuery(dataType string) string {
211214
// Map column types to BigQuery equivalents
212215
switch dataType {

databases/bigquery/bigquery_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ func TestSchema(t *testing.T) {
5858

5959
}
6060

61-
// TestExecute is a unit test function that tests the Execute method of the BigQuery struct.
61+
// TestTables is a unit test function that tests the Tables method of the BigQuery struct.
6262
// It creates a mock instance of BigQuery, sets the expected return values, and calls the method under test.
6363
// It then asserts the expected return values and checks if the method was called with the correct arguments.
64-
65-
func TestGetTableName(t *testing.T) {
64+
func TestTables(t *testing.T) {
6665
// create a new mock database connection
6766
db, mock := MockDB()
6867
defer func() {
@@ -96,6 +95,10 @@ func TestGetTableName(t *testing.T) {
9695
}
9796
}
9897

98+
99+
// TestExecute is a unit test function that tests the Execute method of the BigQuery struct.
100+
// It creates a mock instance of BigQuery, sets the expected return values, and calls the method under test.
101+
// It then asserts the expected return values and checks if the method was called with the correct arguments.
99102
func TestExecute(t *testing.T) {
100103
// create a new mock database connection
101104
db, mock := MockDB()
@@ -152,6 +155,9 @@ func TestExecute(t *testing.T) {
152155
}
153156
}
154157

158+
// TestGenerateCreateTablequeryis a unit test function that tests the TestGenerateCreateTablequery method of the BigQuery struct.
159+
// It creates a mock instance of BigQuery, sets the expected return values, and calls the method under test.
160+
// It then asserts the expected return values and checks if the method was called with the correct arguments.
155161
func TestGenerateCreateTablequery(t *testing.T) {
156162
db, mock := MockDB()
157163
defer func() {

databases/mssql/mssql.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ import (
1313
"github.com/thesaas-company/xray/types"
1414
)
1515

16+
// DB_PASSWORD is the name of the environment variable that stores the database password.
1617
var DB_PASSWORD = "DB_PASSWORD"
1718

18-
const (
19-
MSSQL_SCHEMA_QUERY = "SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, ORDINAL_POSITION, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s'"
20-
MSSQL_TABLES_QUERY = "USE %s; SELECT table_name FROM INFORMATION_SCHEMA.TABLES;"
21-
)
19+
// MSSQL_SCHEMA_QUERY is the SQL query for retrieving table schema.
20+
const MSSQL_SCHEMA_QUERY = "SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, ORDINAL_POSITION, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s'"
21+
22+
// MSSQL_TABLES_QUERY is the SQL query for listing tables within a database.
23+
const MSSQL_TABLES_QUERY = "USE %s; SELECT table_name FROM INFORMATION_SCHEMA.TABLES;"
2224

25+
// MSSQL represents the MSSQL database implementation.
2326
type MSSQL struct {
2427
Client *sql.DB
2528
Config *config.Config
2629
}
2730

31+
// NewMSSQL creates a new MSSQL instance with the given client.
2832
func NewMSSQL(client *sql.DB) (types.ISQL, error) {
2933
return &MSSQL{
3034
Client: client,
3135
Config: &config.Config{},
3236
}, nil
3337
}
3438

39+
// NewMSSQLFromConfig creates a new MSSQL instance with the given configuration.
3540
func NewMSSQLFromConfig(config *config.Config) (types.ISQL, error) {
36-
if os.Getenv(DB_PASSWORD) == "" || len(os.Getenv(DB_PASSWORD)) == 0 { // added mysql to be more verbose about the db type
41+
if os.Getenv(DB_PASSWORD) == "" || len(os.Getenv(DB_PASSWORD)) == 0 {
3742
return nil, fmt.Errorf("please set %s env variable for the database", DB_PASSWORD)
3843
}
3944

@@ -51,6 +56,8 @@ func NewMSSQLFromConfig(config *config.Config) (types.ISQL, error) {
5156
}, nil
5257
}
5358

59+
// Schema retrieves the table schema for the given table name.
60+
// It takes the table name as an argument and returns the table schema as a types.Table object.
5461
func (m *MSSQL) Schema(table string) (types.Table, error) {
5562
query := fmt.Sprintf(MSSQL_SCHEMA_QUERY, table)
5663
rows, err := m.Client.Query(query)
@@ -95,6 +102,8 @@ func (m *MSSQL) Schema(table string) (types.Table, error) {
95102
}, nil
96103
}
97104

105+
// Tables lists the tables within the given database in a bigquery.
106+
// It takes the database name as an argument and returns a slice of table names.
98107
func (m *MSSQL) Tables(databaseName string) ([]string, error) {
99108
query := fmt.Sprintf(MSSQL_TABLES_QUERY, databaseName)
100109
rows, err := m.Client.Query(query)
@@ -122,9 +131,10 @@ func (m *MSSQL) Tables(databaseName string) ([]string, error) {
122131
}
123132

124133
return tables, nil
125-
126134
}
127135

136+
// Execute executes the given SQL query and returns the result as JSON.
137+
// It takes the SQL query as an argument.
128138
func (m *MSSQL) Execute(query string) ([]byte, error) {
129139
rows, err := m.Client.Query(query)
130140
if err != nil {
@@ -174,6 +184,8 @@ func (m *MSSQL) Execute(query string) ([]byte, error) {
174184
return jsonData, nil
175185
}
176186

187+
// GenerateCreateTableQuery generates the SQL query for creating a table based on the given table definition.
188+
// It takes the table definition as an argument and returns the SQL query as a string.
177189
func (m *MSSQL) GenerateCreateTableQuery(table types.Table) string {
178190
query := "CREATE TABLE [" + table.Name + "] ("
179191
pk := ""

0 commit comments

Comments
 (0)