@@ -190,7 +190,7 @@ func (db *MSSQL) GetForeignKeys(database, table string) ([][]string, error) {
190190
191191func (db * MSSQL ) GetIndexes (database , table string ) ([][]string , error ) {
192192 query := `
193- SELECT
193+ SELECT
194194 t.name AS table_name,
195195 i.name AS index_name,
196196 i.is_unique AS is_unique,
@@ -201,33 +201,33 @@ func (db *MSSQL) GetIndexes(database, table string) ([][]string, error) {
201201 ic.is_included_column AS is_included,
202202 i.has_filter AS has_filter,
203203 i.filter_definition AS filter_definition
204- FROM
205- sys.tables t
204+ FROM
205+ sys.tables t
206206 INNER JOIN
207- sys.schemas s
207+ sys.schemas s
208208 ON
209209 t.schema_id = s.schema_id
210210 INNER JOIN
211- sys.indexes i
211+ sys.indexes i
212212 ON
213213 t.object_id = i.object_id
214214 INNER JOIN
215- sys.index_columns ic
215+ sys.index_columns ic
216216 ON
217217 i.object_id = ic.object_id
218218 AND
219219 i.index_id = ic.index_id
220220 INNER JOIN
221- sys.columns c
221+ sys.columns c
222222 ON
223223 t.object_id = c.object_id
224224 AND
225225 ic.column_id = c.column_id
226- WHERE
226+ WHERE
227227 DB_NAME() = @p1
228228 AND
229229 t.name = @p2
230- ORDER BY
230+ ORDER BY
231231 i.type_desc
232232 `
233233 return db .getTableInformations (query , database , table )
@@ -395,26 +395,24 @@ func (db *MSSQL) ExecuteDMLStatement(query string) (string, error) {
395395 return fmt .Sprintf ("%d rows affected" , rowsAffected ), nil
396396}
397397
398- func (db * MSSQL ) ExecuteQuery (query string ) ([][]string , error ) {
398+ func (db * MSSQL ) ExecuteQuery (query string ) ([][]string , int , error ) {
399399 if query == "" {
400- return nil , errors .New ("query can not be empty" )
400+ return nil , 0 , errors .New ("query can not be empty" )
401401 }
402402
403- results := make ([][]string , 0 )
404403 rows , err := db .Connection .Query (query )
405404 if err != nil {
406- return nil , err
405+ return nil , 0 , err
407406 }
408407
409408 defer rows .Close ()
410409
411410 columns , err := rows .Columns ()
412411 if err != nil {
413- return nil , err
412+ return nil , 0 , err
414413 }
415414
416- results = append (results , columns )
417-
415+ records := make ([][]string , 0 )
418416 for rows .Next () {
419417 rowValues := make ([]any , len (columns ))
420418
@@ -423,22 +421,25 @@ func (db *MSSQL) ExecuteQuery(query string) ([][]string, error) {
423421 }
424422
425423 if err := rows .Scan (rowValues ... ); err != nil {
426- return nil , err
424+ return nil , 0 , err
427425 }
428426
429427 var row []string
430428 for _ , col := range rowValues {
431429 row = append (row , string (* col .(* sql.RawBytes )))
432430 }
433431
434- results = append (results , row )
432+ records = append (records , row )
435433 }
436434
437435 if err := rows .Err (); err != nil {
438- return nil , err
436+ return nil , 0 , err
439437 }
440438
441- return results , nil
439+ // Prepend the columns to the records.
440+ results := append ([][]string {columns }, records ... )
441+
442+ return results , len (records ), nil
442443}
443444
444445func (db * MSSQL ) ExecutePendingChanges (changes []models.DBDMLChange ) error {
@@ -578,9 +579,9 @@ func (db *MSSQL) GetPrimaryKeyColumnNames(database, table string) ([]string, err
578579
579580 pkColumnName := make ([]string , 0 )
580581 query := `
581- SELECT
582+ SELECT
582583 c.name AS column_name
583- FROM
584+ FROM
584585 sys.tables t
585586 INNER JOIN
586587 sys.schemas s
@@ -604,7 +605,7 @@ func (db *MSSQL) GetPrimaryKeyColumnNames(database, table string) ([]string, err
604605 ic.column_id = c.column_id
605606 AND
606607 t.object_id = c.object_id
607- WHERE
608+ WHERE
608609 DB_NAME() = @p2
609610 AND
610611 t.name = @p3
0 commit comments