github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqlserver/dolt_server.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sqlserver
    16  
    17  import (
    18  	"fmt"
    19  	"sync"
    20  
    21  	"github.com/dolthub/go-mysql-server/server"
    22  )
    23  
    24  var theServer *server.Server
    25  var mutex sync.Mutex
    26  
    27  // RunningInServerMode returns true if the current process is running a SQL server.
    28  func RunningInServerMode() bool {
    29  	mutex.Lock()
    30  	defer mutex.Unlock()
    31  	return theServer != nil
    32  }
    33  
    34  // GetRunningServer returns the Server instance running in this process, or nil if no SQL server is running.
    35  func GetRunningServer() *server.Server {
    36  	mutex.Lock()
    37  	defer mutex.Unlock()
    38  	return theServer
    39  }
    40  
    41  // SetRunningServer sets the specified Server as the running SQL server for this process.
    42  func SetRunningServer(server *server.Server) error {
    43  	if server == nil {
    44  		return fmt.Errorf("server must be non-nil")
    45  	}
    46  	mutex.Lock()
    47  	defer mutex.Unlock()
    48  	theServer = server
    49  	return nil
    50  }
    51  
    52  func UnsetRunningServer() {
    53  	mutex.Lock()
    54  	defer mutex.Unlock()
    55  	theServer = nil
    56  }