Skip to content

Commit 5263d31

Browse files
committed
feat(mysql): implement mysql in wasip2
Signed-off-by: Andrew Steurer <[email protected]>
1 parent a65f13a commit 5263d31

File tree

9 files changed

+458
-0
lines changed

9 files changed

+458
-0
lines changed

v3/examples/mysql-outbound/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main.wasm
2+
.spin/

v3/examples/mysql-outbound/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Requirements
2+
- Latest version of [TinyGo](https://tinygo.org/getting-started/)
3+
- Latest version of [Docker](https://docs.docker.com/get-started/get-docker/)
4+
5+
# Usage
6+
7+
TODO
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE pets (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, prey VARCHAR(100), is_finicky BOOL NOT NULL);
2+
INSERT INTO pets VALUES (1, 'Splodge', NULL, false);
3+
INSERT INTO pets VALUES (2, 'Kiki', 'Cicadas', false);
4+
INSERT INTO pets VALUES (3, 'Slats', 'Temptations', true);

v3/examples/mysql-outbound/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/spinframework/spin-go-sdk/v3/examples/mysql-outbound
2+
3+
go 1.24
4+
5+
require github.com/spinframework/spin-go-sdk/v3 v3.0.0
6+
7+
require (
8+
github.com/julienschmidt/httprouter v1.3.0 // indirect
9+
go.bytecodealliance.org/cm v0.2.2 // indirect
10+
)
11+
12+
replace github.com/spinframework/spin-go-sdk/v3 => ../../

v3/examples/mysql-outbound/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
2+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
3+
go.bytecodealliance.org/cm v0.2.2 h1:M9iHS6qs884mbQbIjtLX1OifgyPG9DuMs2iwz8G4WQA=
4+
go.bytecodealliance.org/cm v0.2.2/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=

v3/examples/mysql-outbound/main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
9+
spinhttp "github.com/spinframework/spin-go-sdk/v3/http"
10+
"github.com/spinframework/spin-go-sdk/v3/mysql"
11+
)
12+
13+
type Pet struct {
14+
ID int64
15+
Name string
16+
Prey *string // nullable field must be a pointer
17+
IsFinicky bool
18+
}
19+
20+
func init() {
21+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
22+
23+
// addr is the environment variable set in `spin.toml` that points to the
24+
// address of the Mysql server.
25+
addr := os.Getenv("DB_URL")
26+
27+
db := mysql.Open(addr)
28+
defer db.Close()
29+
30+
if _, err := db.Query("REPLACE INTO pets VALUES (?, 'Maya', ?, ?);", 4, "bananas", true); err != nil {
31+
http.Error(w, err.Error(), http.StatusInternalServerError)
32+
return
33+
}
34+
35+
rows, err := db.Query("SELECT * FROM pets")
36+
if err != nil {
37+
http.Error(w, err.Error(), http.StatusInternalServerError)
38+
return
39+
}
40+
41+
var pets []*Pet
42+
for rows.Next() {
43+
var pet Pet
44+
if err := rows.Scan(&pet.ID, &pet.Name, &pet.Prey, &pet.IsFinicky); err != nil {
45+
fmt.Println(err)
46+
}
47+
pets = append(pets, &pet)
48+
}
49+
json.NewEncoder(w).Encode(pets)
50+
})
51+
}
52+
53+
func main() {}

v3/examples/mysql-outbound/spin.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "go-mysql-outbound-example"
5+
version = "0.1.0"
6+
authors = ["Andrew Steurer <[email protected]>"]
7+
description = "Using Spin with MySQL"
8+
9+
[[trigger.http]]
10+
route = "/..."
11+
component = "mysql"
12+
13+
[component.mysql]
14+
environment = { DB_URL = "mysql://spin:[email protected]/spin_dev" }
15+
source = "main.wasm"
16+
allowed_outbound_hosts = ["mysql://127.0.0.1"]
17+
[component.mysql.build]
18+
command = "tinygo build -target=wasip2 --wit-package $(go list -mod=readonly -m -f '{{.Dir}}' github.com/spinframework/spin-go-sdk/v3)/wit --wit-world http-trigger -gc=leaking -o main.wasm main.go"
19+
watch = ["**/*.go", "go.mod"]

v3/internal/db/driver.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package db
2+
3+
import "database/sql/driver"
4+
5+
// GlobalParameterConverter is a global valueConverter instance to convert parameters.
6+
var GlobalParameterConverter = &valueConverter{}
7+
8+
var _ driver.ValueConverter = (*valueConverter)(nil)
9+
10+
// valueConverter is a no-op value converter.
11+
type valueConverter struct{}
12+
13+
func (c *valueConverter) ConvertValue(v any) (driver.Value, error) {
14+
return driver.Value(v), nil
15+
}

0 commit comments

Comments
 (0)