Skip to content

Commit

Permalink
adding automatic service check on query
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jun 14, 2024
1 parent 51b1d7c commit 472b872
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
18 changes: 18 additions & 0 deletions pkg/js/libs/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ func (c *MySQLClient) ExecuteQueryWithOpts(opts MySQLOptions, query string) (*ut
return nil, err
}

// executing queries implies the remote mysql service
ok, err := c.IsMySQL(opts.Host, opts.Port)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("not a mysql service")
}

db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
Expand Down Expand Up @@ -220,6 +229,15 @@ func (c *MySQLClient) ExecuteQueryWithOpts(opts MySQLOptions, query string) (*ut
// log(to_json(result));
// ```
func (c *MySQLClient) ExecuteQuery(host string, port int, username, password, query string) (*utils.SQLResult, error) {
// executing queries implies the remote mysql service
ok, err := c.IsMySQL(host, port)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("not a mysql service")
}

return c.ExecuteQueryWithOpts(MySQLOptions{
Host: host,
Port: port,
Expand Down
17 changes: 13 additions & 4 deletions pkg/js/libs/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ func (c *PGClient) Connect(host string, port int, username, password string) (bo
// log(to_json(result));
// ```
func (c *PGClient) ExecuteQuery(host string, port int, username, password, dbName, query string) (*utils.SQLResult, error) {
// executing queries implies the remote postgres service
ok, err := c.IsPostgres(host, port)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("not a postgres service")
}

return memoizedexecuteQuery(host, port, username, password, dbName, query)
}

Expand Down Expand Up @@ -149,10 +158,10 @@ func connect(host string, port int, username string, password string, dbName str
defer cancel()

db := pg.Connect(&pg.Options{
Addr: target,
User: username,
Password: password,
Database: dbName,
Addr: target,
User: username,
Password: password,
Database: dbName,
Dialer: func(network, addr string) (net.Conn, error) {
return protocolstate.Dialer.Dial(context.Background(), network, addr)
},
Expand Down

0 comments on commit 472b872

Please sign in to comment.