@@ -30,6 +30,12 @@ func NewService(db *sql.DB, cfg config.ServerConfig) *Service {
3030 return s
3131}
3232
33+ // Health checks the connection to SQL Server and returns server information.
34+ //
35+ // ctx is the context for the query timeout.
36+ //
37+ // Returns a map containing connection status, server name, database name,
38+ // and SQL Server version on success, or an error if the connection fails.
3339func (s * Service ) Health (ctx context.Context ) (map [string ]any , error ) {
3440 var serverName , version , databaseName string
3541 err := s .db .QueryRowContext (ctx , "SELECT @@SERVERNAME, @@VERSION, DB_NAME()" ).Scan (& serverName , & version , & databaseName )
@@ -44,6 +50,14 @@ func (s *Service) Health(ctx context.Context) (map[string]any, error) {
4450 }, nil
4551}
4652
53+ // Select executes a read-only SELECT or WITH query.
54+ //
55+ // ctx is the context for the query.
56+ // sqlText is the SELECT query to execute.
57+ // maxRows limits the number of rows returned (0 uses default).
58+ //
59+ // Returns the query result with columns and rows on success,
60+ // or an error if the query is invalid or execution fails.
4761func (s * Service ) Select (ctx context.Context , sqlText string , maxRows int ) (QueryResult , error ) {
4862 if err := validateSelectSQL (sqlText ); err != nil {
4963 return QueryResult {}, err
@@ -54,6 +68,13 @@ func (s *Service) Select(ctx context.Context, sqlText string, maxRows int) (Quer
5468 return s .query (ctx , sqlText , maxRows )
5569}
5670
71+ // Execute executes a write statement (INSERT, UPDATE, DELETE, MERGE).
72+ //
73+ // ctx is the context for the query.
74+ // sqlText is the write statement to execute.
75+ //
76+ // Returns the number of rows affected and a message on success,
77+ // or an error if the statement is invalid or not allowed.
5778func (s * Service ) Execute (ctx context.Context , sqlText string ) (ExecuteResult , error ) {
5879 if err := validateWriteSQL (sqlText , s .cfg .AllowDangerousSQL , s .cfg .AllowSchemaChanges ); err != nil {
5980 return ExecuteResult {}, err
@@ -65,16 +86,24 @@ func (s *Service) Execute(ctx context.Context, sqlText string) (ExecuteResult, e
6586 rowsAffected , _ := result .RowsAffected ()
6687 return ExecuteResult {
6788 RowsAffected : rowsAffected ,
68- Message : "statement executed" ,
89+ Message : "statement executed" ,
6990 }, nil
7091}
7192
93+ // query executes a raw SQL query and maps results to QueryResult.
94+ //
95+ // ctx is the context for the query.
96+ // sqlText is the SQL query to execute.
97+ // maxRows limits the number of rows returned.
98+ // args are optional query parameters.
99+ //
100+ // Returns the query result on success, or an error on failure.
72101func (s * Service ) query (ctx context.Context , sqlText string , maxRows int , args ... any ) (QueryResult , error ) {
73102 rows , err := s .db .QueryContext (ctx , sqlText , args ... )
74103 if err != nil {
75104 return QueryResult {}, err
76105 }
77- defer func () { _ = rows .Close () } ()
106+ defer rows .Close ()
78107
79108 columns , err := rows .Columns ()
80109 if err != nil {
0 commit comments