Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,52 @@ jobs:
exit 1
fi

go:
name: Go backend (PostgreSQL ${{ matrix.postgres }})
runs-on: ubuntu-22.04
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
postgres: ["13", "17"]
services:
postgres:
image: postgres:${{ matrix.postgres }}
env:
POSTGRES_PASSWORD: lowquery
ports: ["5432:5432"]
options: >-
--health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 10
env:
LQ_PG_HOST: localhost
LQ_PG_PORT: "5432"
LQ_PG_USER: postgres
LQ_PG_PASSWORD: lowquery
LQ_PG_DB: postgres
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version-file: go.mod
- name: Formatting
run: test -z "$(gofmt -l internal main.go)"
- run: go vet ./internal/...
- name: Unit and integration tests
run: go test -count=1 ./internal/...

go-app:
name: Go app build (Windows)
runs-on: windows-2022
timeout-minutes: 20
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version-file: go.mod
- run: bun install --frozen-lockfile
- run: bun run build:app

lint:
name: Rust format and lint
runs-on: ubuntu-22.04
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bun-debug.log*
# --- Vite ---
/dist
/build
/bin
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.vite/
Expand Down
3 changes: 3 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module github.com/LowBloat/LowQuery

go 1.26.4

require (
github.com/danieljoos/wincred v1.2.3
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.13.0
github.com/ncruces/go-sqlite3 v0.38.2
github.com/wailsapp/wails/v3 v3.1.0
github.com/zalando/go-keyring v0.2.8
golang.org/x/sys v0.50.0
)

require (
github.com/coder/websocket v1.8.14 // indirect
github.com/ebitengine/purego v0.11.1 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/godbus/dbus/v5 v5.2.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/ncruces/go-sqlite3-wasm/v6 v6.7.35401 // indirect
github.com/ncruces/julianday v1.0.0 // indirect
golang.org/x/term v0.46.0 // indirect
golang.org/x/text v0.44.0 // indirect
)
55 changes: 55 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
github.com/danieljoos/wincred v1.2.3 h1:v7dZC2x32Ut3nEfRH+vhoZGvN72+dQ/snVXo/vMFLdQ=
github.com/danieljoos/wincred v1.2.3/go.mod h1:6qqX0WNrS4RzPZ1tnroDzq9kY3fu1KwE7MRLQK4X0bs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.11.1 h1:2zpWRSQNVKN4eKsKO9eM1ILDgWfYMY9GwqRmK6XeQ/0=
github.com/ebitengine/purego v0.11.1/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ=
github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.13.0 h1:Hl08k64maU0nrRyVUEbwCnK6/gT0EhMrtAqBHz9/gA4=
github.com/jackc/pgx/v5 v5.13.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=
github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao=
github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/ncruces/go-sqlite3 v0.38.2 h1:jYcxFaXfFCNE2PAnRzVEccmn5HvC4cZwy04RGnkJy7E=
github.com/ncruces/go-sqlite3 v0.38.2/go.mod h1:qPQrrHbHYP7FQsgp9rAD0xr14DCDOXZwk8jeU6Y+dUg=
github.com/ncruces/go-sqlite3-wasm/v6 v6.7.35401 h1:BlrYMg6wi+S0oxZ8IeGMHXi2nfjK+06KLQ3w85bqxOM=
github.com/ncruces/go-sqlite3-wasm/v6 v6.7.35401/go.mod h1:K/Z0eKUURoWW9cJ9rHeMGDF0G4pr0A6WizdC+Ohzj54=
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/wailsapp/wails/v3 v3.1.0 h1:ld0AXSicXiTTsIvmTq3qNn9FzCP8vIHRSJdkRLgaAo8=
github.com/wailsapp/wails/v3 v3.1.0/go.mod h1:tzDY3W1jzN3ksSqfGalTt7K3PwnGfIqtl+IgIXlXz0I=
github.com/zalando/go-keyring v0.2.8 h1:6sD/Ucpl7jNq10rM2pgqTs0sZ9V3qMrqfIIy5YPccHs=
github.com/zalando/go-keyring v0.2.8/go.mod h1:tsMo+VpRq5NGyKfxoBVjCuMrG47yj8cmakZDO5QGii0=
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
205 changes: 205 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Package app is the backend the interface talks to: the session registry,
// password resolution and one handler per command the interface sends
// (src/lib/api.ts). Commands keep the names and named arguments of the Tauri
// commands they replace, so the interface code stays the same.
package app

import (
"context"
"strings"
"sync"

"github.com/google/uuid"

"github.com/LowBloat/LowQuery/internal/apperr"
"github.com/LowBloat/LowQuery/internal/drivers"
"github.com/LowBloat/LowQuery/internal/model"
"github.com/LowBloat/LowQuery/internal/profiles"
)

type App struct {
version string
profiles *profiles.Store
window Window

mu sync.RWMutex
sessions map[string]drivers.Session
}

// Window is what the backend needs from the main window.
type Window interface {
// Painted is called once the page has painted and whenever the theme
// changes, with the page background as #rrggbb.
Painted(background string)
}

func New(version string, store *profiles.Store) *App {
return &App{
version: version,
profiles: store,
sessions: map[string]drivers.Session{},
}
}

// SetWindow connects the main window once it exists.
func (a *App) SetWindow(w Window) { a.window = w }

func (a *App) session(id string) (drivers.Session, error) {
a.mu.RLock()
defer a.mu.RUnlock()
s, ok := a.sessions[id]
if !ok {
return nil, apperr.New(apperr.NotFound, "Session closed. Connect again.")
}
return s, nil
}

// CloseAll closes every open session (on exit).
func (a *App) CloseAll() {
a.mu.Lock()
all := a.sessions
a.sessions = map[string]drivers.Session{}
a.mu.Unlock()
for _, s := range all {
_ = s.Close()
}
}

var authFailureCodes = map[string]bool{
"28P01": true, "28000": true, "ORA-01017": true, "ORA-01005": true,
"1045 (28000)": true, "1044 (42000)": true, "1698 (28000)": true,
"NOAUTH": true, "WRONGPASS": true, "AuthenticationError": true,
}

func isAuthFailure(e *apperr.Error) bool {
return authFailureCodes[e.Code] ||
strings.HasPrefix(e.Code, "Msg 18456,") ||
strings.Contains(e.Message, "password")
}

// openWithPassword resolves the password: the one provided now, the one in
// the credential store, or none (trust authentication, for example). Asks
// for it when the user chose not to save it.
func openWithPassword(ctx context.Context, profile model.ConnectionProfile, password *string) (drivers.Session, error) {
// SQLite files and Windows-authenticated SQL Server need no password.
if profile.Engine == model.SQLite || (profile.Engine == model.SQLServer && profile.WindowsAuth) {
return openSession(ctx, profile, nil)
}
provided := password != nil
if password == nil {
password, _ = profiles.StoredPassword(profile.ID)
}
if password == nil && !profile.SavePassword {
return nil, apperr.New(apperr.PasswordRequired, "Enter the password.")
}
hadPassword := password != nil
s, err := openSession(ctx, profile, password)
if err != nil && !provided {
if e := apperr.From(err); isAuthFailure(e) {
msg := "Enter the password."
if hadPassword {
msg = "The saved password was rejected."
}
out := apperr.New(apperr.PasswordRequired, msg)
out.Detail = e.Error()
return nil, out
}
}
return s, err
}

func (a *App) testConnection(ctx context.Context, profile model.ConnectionProfile, password *string) (model.ServerInfo, error) {
s, err := openWithPassword(ctx, profile, password)
if err != nil {
return model.ServerInfo{}, err
}
defer s.Close()
return model.ServerInfo{ServerVersion: s.ServerVersion()}, nil
}

func (a *App) openSession(ctx context.Context, profileID string, password *string) (model.SessionInfo, error) {
profile, err := a.profiles.Get(profileID)
if err != nil {
return model.SessionInfo{}, err
}
s, err := openWithPassword(ctx, profile, password)
if err != nil {
return model.SessionInfo{}, err
}
// A password typed at connect time is kept when the profile saves
// passwords, so the next connection does not ask again.
if password != nil && profile.SavePassword {
_ = profiles.RememberPassword(profile.ID, *password)
}
id := uuid.NewString()
info := model.SessionInfo{
SessionID: id,
ProfileID: profileID,
Engine: s.Engine(),
ServerVersion: s.ServerVersion(),
CurrentUser: s.CurrentUser(),
CurrentSchema: s.CurrentSchema(),
Autocommit: s.TxState().Autocommit,
}
a.mu.Lock()
a.sessions[id] = s
a.mu.Unlock()
return info, nil
}

func (a *App) closeSession(id string) {
a.mu.Lock()
s, ok := a.sessions[id]
delete(a.sessions, id)
a.mu.Unlock()
if ok {
// A running statement keeps the driver busy; close once it returns.
go s.Close()
}
}

func (a *App) execute(ctx context.Context, sessionID string, req model.ExecRequest) (*model.ExecResult, error) {
s, err := a.session(sessionID)
if err != nil {
return nil, err
}
if strings.TrimSpace(req.SQL) == "" {
return nil, apperr.New(apperr.Invalid, "Nothing to execute.")
}
r, err := s.Execute(ctx, req)
if err != nil {
return nil, err
}
r.Normalize()
return r, nil
}

func (a *App) applyChanges(ctx context.Context, sessionID string, req model.ApplyRequest) (*model.ApplyResult, error) {
s, err := a.session(sessionID)
if err != nil {
return nil, err
}
if len(req.Changes) == 0 {
return &model.ApplyResult{InTransaction: s.TxState().InTransaction}, nil
}
if len(req.Target.Key) == 0 {
return nil, apperr.New(apperr.Invalid, "This result has no key to identify its rows.")
}
return s.ApplyChanges(ctx, req)
}

func (a *App) objectDDL(ctx context.Context, sessionID, schema, name, kind string) (string, error) {
s, err := a.session(sessionID)
if err != nil {
return "", err
}
ddl, err := s.ObjectDDL(ctx, schema, name, kind)
if err != nil {
return "", err
}
if strings.TrimSpace(ddl) == "" {
return "", apperr.Newf(apperr.NotFound,
"The database returned no definition for %s. It may be encrypted, or this user may lack the privilege to read it.", name)
}
return ddl, nil
}
Loading
Loading