66 sqlorig "database/sql"
77 "encoding/binary"
88 "fmt"
9+ "strings"
910 "sync"
1011 "sync/atomic"
1112 "time"
@@ -188,10 +189,28 @@ func (c *StmtCache) getStmt(query string) (*sqlorig.Stmt, bool) {
188189 return stmt , exists
189190}
190191
192+ func isVersionAtLeast (version string , targetVersion string ) bool {
193+ result , err := mysql .CompareServerVersions (version , targetVersion )
194+ if err != nil {
195+ return false
196+ }
197+ return result >= 0
198+ }
199+
200+ func getBinlogStatusCommand (db * sql.DB ) string {
201+ version , _ := db .QueryMySQLVersion ()
202+ if isVersionAtLeast (version , "8.4.0" ) {
203+ return "SHOW BINARY LOG STATUS"
204+ } else {
205+ return "SHOW MASTER STATUS"
206+ }
207+ }
208+
191209func ShowMasterStatusBinlogPosition (db * sql.DB ) (mysql.Position , error ) {
192- rows , err := db .Query ("SHOW MASTER STATUS" )
210+ query := getBinlogStatusCommand (db )
211+ rows , err := db .Query (query )
193212 if err != nil {
194- return NewMysqlPosition ("" , 0 , err )
213+ return NewMysqlPosition ("" , 0 , err , db )
195214 }
196215 defer rows .Close ()
197216 var file string
@@ -201,7 +220,7 @@ func ShowMasterStatusBinlogPosition(db *sql.DB) (mysql.Position, error) {
201220 if rows .Next () {
202221 cols , err = rows .Columns ()
203222 if err != nil {
204- return NewMysqlPosition (file , position , err )
223+ return NewMysqlPosition (file , position , err , db )
205224 }
206225 switch len (cols ) {
207226 case 4 :
@@ -210,18 +229,20 @@ func ShowMasterStatusBinlogPosition(db *sql.DB) (mysql.Position, error) {
210229 err = rows .Scan (& file , & position , & binlog_do_db , & binlog_ignore_db , & executed_gtid_set )
211230 }
212231 }
213- return NewMysqlPosition (file , position , err )
232+ return NewMysqlPosition (file , position , err , db )
214233}
215234
216- func NewMysqlPosition (file string , position uint32 , err error ) (mysql.Position , error ) {
235+ func NewMysqlPosition (file string , position uint32 , err error , db * sql.DB ) (mysql.Position , error ) {
236+ var binlogStatusCmd string
237+ binlogStatusCmd = getBinlogStatusCommand (db )
217238 switch {
218239 case err == sqlorig .ErrNoRows :
219- return mysql.Position {}, fmt .Errorf ("no results from show master status" )
240+ return mysql.Position {}, fmt .Errorf ("no results from %s" , strings . ToLower ( binlogStatusCmd ) )
220241 case err != nil :
221242 return mysql.Position {}, err
222243 default :
223244 if file == "" {
224- return mysql.Position {}, fmt .Errorf ("show master status does not show a file" )
245+ return mysql.Position {}, fmt .Errorf ("%s does not show a file" , strings . ToLower ( binlogStatusCmd ) )
225246 }
226247
227248 return mysql.Position {
0 commit comments