mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Major plugin updates
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
|
||||
import {Call} from '/wails/runtime.js';
|
||||
|
||||
/**
|
||||
* Open a sqlite DB.
|
||||
* @param filename {string} - file to open.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export function Open(filename) {
|
||||
return Call.ByID(147348976, filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a sqlite DB.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export function Close() {
|
||||
return Call.ByID(3998329564);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL statement.
|
||||
* @param statement {string} - SQL statement to execute.
|
||||
* @param args {...any} - Arguments to pass to the statement.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export function Execute(statement, ...args) {
|
||||
return Call.ByID(2804887383, statement, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a select query.
|
||||
* @param statement {string} - Select SQL statement.
|
||||
* @param args {...any} - Arguments to pass to the statement.
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
export function Select(statement, ...args) {
|
||||
return Call.ByID(2209315040, statement, ...args);
|
||||
}
|
||||
+15
-26
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
/**
|
||||
* Close a sqlite DB.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
function Close() {
|
||||
return wails.CallByID(3998329564);
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
|
||||
/**
|
||||
* Execute a SQL statement.
|
||||
* @param statement {string} - SQL statement to execute.
|
||||
@param args {...any} - Arguments to pass to the statement.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
function Execute(statement, ...args) {
|
||||
return wails.CallByID(2804887383, statement, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a select query.
|
||||
* @param statement {string} - Select SQL statement.
|
||||
* @param args {...any} - Arguments to pass to the statement.
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
function Select(statement, ...args) {
|
||||
return wails.CallByID(2209315040, statement, ...args);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
/**
|
||||
* Open a sqlite DB.
|
||||
* @param filename {string} - file to open.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
function Open(filename) {
|
||||
return wails.CallByID(147348976, filename);
|
||||
}
|
||||
Reference in New Issue
Block a user