Skip to content

Commit

Permalink
feat: upgrade to latest xk6-sql: query function is now part of the Da…
Browse files Browse the repository at this point in the history
…tabase interface
  • Loading branch information
szkiba committed Oct 21, 2024
1 parent f6f3371 commit d668533
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,35 @@ import driver from "k6/x/sql/driver/azuresql";
const db = sql.open(driver, "");

export function setup() {
db.exec(`IF object_id('keyvalues') is null
CREATE TABLE keyvalues (
[id] INT IDENTITY PRIMARY KEY,
[key] varchar(50) NOT NULL,
[value] varchar(50));`);
db.exec(`
IF object_id('roster') is null
CREATE TABLE roster
(
[id] INT IDENTITY PRIMARY KEY,
[given_name] varchar(50) NOT NULL,
[family_name] varchar(50) NOT NULL);
`);
}

export function teardown() {
db.close();
}

export default function () {
db.exec("INSERT INTO keyvalues ([key], [value]) VALUES('plugin-name', 'k6-plugin-sql');");

let results = sql.query(db, "SELECT * FROM keyvalues WHERE [key] = @p1;", "plugin-name");
for (const row of results) {
console.log(`key: ${row.key}, value: ${row.value}`);
let result = db.exec(`
INSERT INTO roster
([given_name], [family_name])
VALUES
('Peter', 'Pan'),
('Wendy', 'Darling'),
('Tinker', 'Bell'),
('James', 'Hook');
`);
console.log(`${result.rowsAffected()} rows inserted`);

let rows = db.query("SELECT * FROM roster WHERE [key] = @p1;", "Peter");
for (const row of rows) {
console.log(`${row.family_name}, ${row.given_name}`);
}
}
```
Expand Down
30 changes: 21 additions & 9 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,34 @@ import driver from "k6/x/sql/driver/azuresql";
const db = sql.open(driver, "");

export function setup() {
db.exec(`IF object_id('keyvalues') is null
CREATE TABLE keyvalues (
[id] INT IDENTITY PRIMARY KEY,
[key] varchar(50) NOT NULL,
[value] varchar(50));`);
db.exec(`
IF object_id('roster') is null
CREATE TABLE roster
(
[id] INT IDENTITY PRIMARY KEY,
[given_name] varchar(50) NOT NULL,
[family_name] varchar(50) NOT NULL);
`);
}

export function teardown() {
db.close();
}

export default function () {
db.exec("INSERT INTO keyvalues ([key], [value]) VALUES('plugin-name', 'k6-plugin-sql');");
let result = db.exec(`
INSERT INTO roster
([given_name], [family_name])
VALUES
('Peter', 'Pan'),
('Wendy', 'Darling'),
('Tinker', 'Bell'),
('James', 'Hook');
`);
console.log(`${result.rowsAffected()} rows inserted`);

let results = sql.query(db, "SELECT * FROM keyvalues WHERE [key] = @p1;", "plugin-name");
for (const row of results) {
console.log(`key: ${row.key}, value: ${row.value}`);
let rows = db.query("SELECT * FROM roster WHERE [key] = @p1;", "Peter");
for (const row of rows) {
console.log(`${row.family_name}, ${row.given_name}`);
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/grafana/xk6-sql-driver-azuresql
go 1.22

require (
github.com/grafana/xk6-sql v0.5.0-alpha.3
github.com/grafana/xk6-sql v0.5.0-alpha.4
github.com/microsoft/go-mssqldb v1.7.2
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f h1:9r05Uxs+Pq1UqZqGG/PoCUWd6xk2ab+mRWAM5xZgM9I=
github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f/go.mod h1:FmcutBFPLiGgroH42I4/HBahv7GxVjODcVWFTw1ISes=
github.com/grafana/xk6-sql v0.5.0-alpha.3 h1:rnCMuFl8QiOzwlbOmCOizOQlJV/371OfJRep0gdx89s=
github.com/grafana/xk6-sql v0.5.0-alpha.3/go.mod h1:+RhqH/hFEeqhv7viOrDtVNPtKMxRiQunIQtKlx6KCMo=
github.com/grafana/xk6-sql v0.5.0-alpha.4 h1:WRerxW/gwADz/laOLmHR4ftgzt3jg2g5ljHY/ueRumw=
github.com/grafana/xk6-sql v0.5.0-alpha.4/go.mod h1:qUzzuSiYE4fIknBxRVqLMhdhu+RAU0o7nequbL0Kv9E=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down

0 comments on commit d668533

Please sign in to comment.