Major plugin updates

This commit is contained in:
Lea Anthony
2024-04-14 21:41:33 +10:00
parent cf130a6e25
commit c7ed7e72d4
52 changed files with 1155 additions and 326 deletions
+15 -26
View File
@@ -2,22 +2,16 @@ package sqlite
import (
"database/sql"
"embed"
_ "embed"
"errors"
"fmt"
"github.com/wailsapp/wails/v3/pkg/application"
"io/fs"
_ "modernc.org/sqlite"
"strings"
)
//go:embed sqlite_close.js
var closejs string
//go:embed sqlite_open.js
var openjs string
//go:embed sqlite_execute_select.js
var executeselectjs string
//go:embed assets/*
var assets embed.FS
// ---------------- Plugin Setup ----------------
// This is the main plugin struct. It can be named anything you like.
@@ -25,10 +19,9 @@ var executeselectjs string
// Both the Init() and Shutdown() methods are called synchronously when the app starts and stops.
type Config struct {
// Add any configuration options here
CanOpenFromJS bool
CanCloseFromJS bool
DBFile string
DBFile string
CanCallOpen bool
CanCallClose bool
}
type Plugin struct {
@@ -63,14 +56,11 @@ func (p *Plugin) Name() string {
// initialise any resources you need.
func (p *Plugin) Init(api application.PluginAPI) error {
p.callableMethods = []string{"Execute", "Select"}
p.js = executeselectjs
if p.config.CanOpenFromJS {
if p.config.CanCallOpen {
p.callableMethods = append(p.callableMethods, "Open")
p.js += openjs
}
if p.config.CanCloseFromJS {
if p.config.CanCallClose {
p.callableMethods = append(p.callableMethods, "Close")
p.js += closejs
}
if p.config.DBFile == "" {
return errors.New(`no database file specified. Please set DBFile in the config to either a filename or use ":memory:" to use an in-memory database`)
@@ -80,7 +70,6 @@ func (p *Plugin) Init(api application.PluginAPI) error {
return err
}
p.js += fmt.Sprintf("\nwindow.sqlite = { %s };", strings.Join(p.callableMethods, ", "))
return nil
}
@@ -89,8 +78,8 @@ func (p *Plugin) CallableByJS() []string {
return p.callableMethods
}
func (p *Plugin) InjectJS() string {
return p.js
func (p *Plugin) Assets() fs.FS {
return assets
}
// ---------------- Plugin Methods ----------------
@@ -157,15 +146,15 @@ func (p *Plugin) Select(query string, args ...any) ([]map[string]any, error) {
return results, nil
}
func (p *Plugin) Close() error {
func (p *Plugin) Close() (string, error) {
if p.conn == nil {
return errors.New("no open database connection")
return "", errors.New("no open database connection")
}
err := p.conn.Close()
if err != nil {
return err
return "", err
}
p.conn = nil
return nil
return "Database connection closed", nil
}