func GetSeedingMode(db *sql.DB, agName string) (seedingMode SeedingMode, seedingModeDesc string, err error) {
	err = db.QueryRow(`
		SELECT ar.seeding_mode, ar.seeding_mode_desc
		FROM
			sys.availability_groups ag
			INNER JOIN sys.dm_hadr_availability_replica_states ars ON ars.group_id = ag.group_id AND ars.is_local = 1
			INNER JOIN sys.availability_replicas ar ON ar.replica_id = ars.replica_id
		WHERE
			ag.name = ?`, agName).Scan(&seedingMode, &seedingModeDesc)
	return
}
--------------------------------------------------------------------------------------
Function: GetSeedingMode

Description:
Gets the seeding mode of the current replica of the given Availability Group

Params:
db: A connection to a SQL Server instance hosting a replica of the AG.
agName: The name of the AG.

Returns:
The numeric value and string name of the seeding mode, or an error if the AG was not found.
package ag
import (
	"database/sql"
	"fmt"
	"strings"
)
type AvailabilityMode byte
const (
	AmASYNCHRONOUS_COMMIT AvailabilityMode = 0
	AmSYNCHRONOUS_COMMIT AvailabilityMode = 1
	AmCONFIGURATION_ONLY AvailabilityMode = 4
)
type Role byte
const (
	RoleRESOLVING Role = 0
	RolePRIMARY Role = 1
	RoleSECONDARY Role = 2
)
type SeedingMode byte
const (
	SmAUTOMATIC SeedingMode = 0
	SmMANUAL SeedingMode = 1
)
func Drop(db *sql.DB, agName string) error {
	_, err := db.Exec(fmt.Sprintf("DROP AVAILABILITY GROUP %s", quoteName(agName)))
	return err
}
func Failover(db *sql.DB, agName string) error {
	_, err := db.Exec(fmt.Sprintf("ALTER AVAILABILITY GROUP %s FAILOVER", quoteName(agName)))
	return err
}
func FailoverWithDataLoss(db *sql.DB, agName string) error {
	_, err := db.Exec(fmt.Sprintf("ALTER AVAILABILITY GROUP %s FORCE_FAILOVER_ALLOW_DATA_LOSS", quoteName(agName)))
	return err
}
func GetAvailabilityMode(db *sql.DB, agName string) (availabilityMode AvailabilityMode, availabilityModeDesc string, err error) {
	err = db.QueryRow(`
		SELECT ar.availability_mode, ar.availability_mode_desc
		FROM
			sys.availability_groups ag
			INNER JOIN sys.dm_hadr_availability_replica_states ars ON ars.group_id = ag.group_id AND ars.is_local = 1
			INNER JOIN sys.availability_replicas ar ON ar.replica_id = ars.replica_id
		WHERE
			ag.name = ?`, agName).Scan(&availabilityMode, &availabilityModeDesc)
	return
}
func GetCurrentReplicaName(db *sql.DB, agName string) (currentReplicaName string, err error) {
	err = db.QueryRow(`
		SELECT ar.replica_server_name
		FROM
			sys.availability_groups ag
			INNER JOIN sys.dm_hadr_availability_replica_states ars ON ars.group_id = ag.group_id AND ars.is_local = 1
			INNER JOIN sys.availability_replicas ar ON ar.replica_id = ars.replica_id
		WHERE
			ag.name = ?`, agName).Scan(&currentReplicaName)
	return
}
func GetDatabaseStates(db *sql.DB, agName string) (result string, err error) {
	stmt, err := db.Prepare(`
		SELECT d.state, d.state_desc, COUNT(*) FROM
			sys.availability_groups ag
			INNER JOIN sys.dm_hadr_database_replica_states drs ON drs.group_id = ag.group_id AND drs.is_local = 1
			INNER JOIN sys.databases d on d.database_id = drs.database_id
		WHERE
			ag.name = ? AND d.state <> 0
		GROUP BY d.state, d.state_desc`)
	if err != nil {
		return
	}
	defer stmt.Close()
	rows, err := stmt.Query(agName)
	if err != nil {
		return
	}
	defer rows.Close()
	for rows.Next() {
		var state byte
		var stateDesc string
		var numDatabases int
		err = rows.Scan(&state, &stateDesc, &numDatabases)
		if err != nil {
			return
		}
		result += fmt.Sprintf("%d databases are %s, ", numDatabases, stateDesc)
	}
	result = strings.TrimSuffix(result, ", ")
	err = rows.Err()
	return
}
func GetDBFailoverMode(db *sql.DB, agName string) (dbFailoverMode bool, err error) {
	err = db.QueryRow(`
		SELECT ag.db_failover
		FROM
			sys.availability_groups ag
		WHERE
			ag.name = ?`, agName).Scan(&dbFailoverMode)
	return
}
func GetNumSyncCommitReplicas(db *sql.DB, agName string) (numReplicas uint, err error) {
	err = db.QueryRow(`
		SELECT COUNT(*)
		FROM
			sys.availability_replicas ar
			INNER JOIN sys.availability_groups ag ON ar.group_id = ag.group_id
		WHERE ag.name = ? AND ar.availability_mode = ?`, agName, AmSYNCHRONOUS_COMMIT).Scan(&numReplicas)
	return
}
func GetPrimaryReplicaName(db *sql.DB, agName string) (primaryReplicaName string, err error) {
	err = db.QueryRow(`
		SELECT ags.primary_replica
		FROM
			sys.availability_groups ag
			INNER JOIN sys.dm_hadr_availability_group_states ags ON ags.group_id = ag.group_id
		WHERE
			ag.name = ?`, agName).Scan(&primaryReplicaName)
	return
}
func GetRole(db *sql.DB, agName string) (role Role, roleDesc string, err error) {
	err = db.QueryRow(`
		SELECT ars.role, ars.role_desc
		FROM
			sys.availability_groups ag
			INNER JOIN sys.dm_hadr_availability_replica_states ars ON ars.group_id = ag.group_id AND ars.is_local = 1
		WHERE
			ag.name = ?`, agName).Scan(&role, &roleDesc)
	return
}