Skip to content

Commit

Permalink
Merge pull request #145 from artyom-smirnov/service_manager
Browse files Browse the repository at this point in the history
Firebird services support
  • Loading branch information
nakagami authored Nov 11, 2024
2 parents 0ec686e + 6d4edc9 commit b98b9d0
Show file tree
Hide file tree
Showing 20 changed files with 3,587 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test_fb3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
sudo systemctl restart firebird3.0
sudo chmod 0664 /etc/firebird/3.0/SYSDBA.password
grep '=' /etc/firebird/3.0/SYSDBA.password |sed 's/^/export /' >test_user.env
export FIREBIRD_LOG=/var/log/firebird/firebird3.0.log >> test_user.env
sudo touch /var/log/firebird/firebird3.0.log
sudo chmod 777 /var/log/firebird/firebird3.0.log
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v5
Expand Down
149 changes: 149 additions & 0 deletions _examples/service_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2023-2024 Artyom Smirnov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/

package main

import (
"database/sql"
"fmt"
"github.com/nakagami/firebirdsql"
"os"
)

func main() {
// Create some test database
conn, err := sql.Open("firebirdsql_createdb", "sysdba:test@localhost/tmp/gofirebirdsqltest.fdb")
defer conn.Close()
if err != nil || conn == nil {
panic(err)
}
err = conn.Ping()
if err != nil {
panic(err)
}
// Base service manager tools
sm, err := firebirdsql.NewServiceManager("localhost:3050", "sysdba", "test", firebirdsql.NewServiceManagerOptions())
defer sm.Close()
if err != nil {
panic(err)
}
// Extract some server info
version, err := sm.GetServerVersion()
if err != nil {
panic(err)
}
homeDir, err := sm.GetHomeDir()
if err != nil {
panic(err)
}
fmt.Printf("Server version: %s, installed in %s\n", version.Raw, homeDir)

//Database statistics
dbStats, err := sm.GetDbStatsString("/tmp/gofirebirdsqltest.fdb", firebirdsql.NewStatisticsOptions(firebirdsql.WithOnlyHeaderPages()))
if err != nil {
panic(err)
}
fmt.Println(dbStats)

// User management
um, err := firebirdsql.NewUserManager("localhost:3050", "sysdba", "test", firebirdsql.NewServiceManagerOptions(), firebirdsql.NewUserManagerOptions())
defer um.Close()
if err != nil {
panic(err)
}

// Create new user
_ = um.AddUser(firebirdsql.NewUser(firebirdsql.WithUsername("testuser"), firebirdsql.WithPassword("testpass"), firebirdsql.WithAdmin()))
// Obtain existing users
users, err := um.GetUsers()
if err != nil {
panic(err)
}
for _, user := range users {
fmt.Printf("User: %s, admin role: %v\n", *user.Username, *user.Admin)
}
// Drop user
_ = um.DeleteUser(firebirdsql.NewUser(firebirdsql.WithUsername("testuser")))

// Backup manager

// Backup manager (gbak utility)
bm, err := firebirdsql.NewBackupManager("localhost:3050", "sysdba", "test", firebirdsql.NewServiceManagerOptions())
// no need to close BackupManager, because it opens connection during Backup/Restore and closes it automatically
if err != nil {
panic(err)
}

// Create backup with gbak using logging from server to client
fmt.Println("\ngback:\n")
done := make(chan bool)
resChan := make(chan string)
go func() {
err = bm.Backup("/tmp/gofirebirdsqltest.fdb", "/tmp/gofirebirdsqltest.fbk", firebirdsql.NewBackupOptions(), resChan)
if err != nil {
panic(err)
}
done <- true
}()

cont := true
var s string
for cont {
select {
case s = <-resChan:
fmt.Println(s)
case <-done:
cont = false
break
}
}

// NBackup manager (nbackup utility)
nbk, err := firebirdsql.NewNBackupManager("localhost:3050", "sysdba", "test", firebirdsql.NewServiceManagerOptions())
// no need to close NBackupManager, because it opens connection during Backup/Restore and closes it automatically
if err != nil {
panic(err)
}

// Create zero level backup with nbackup using logging from server to client
os.Remove("/tmp/gofirebirdsqltest.nbk")
fmt.Println("\nNBackup:\n")
go func() {
err = nbk.Backup("/tmp/gofirebirdsqltest.fdb", "/tmp/gofirebirdsqltest.nbk", firebirdsql.NewNBackupOptions(), resChan)
if err != nil {
panic(err)
}
done <- true
}()

cont = true
for cont {
select {
case s = <-resChan:
fmt.Println(s)
case <-done:
cont = false
break
}
}
}
Loading

0 comments on commit b98b9d0

Please sign in to comment.