This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ballerina/io; | |
import ballerina/mysql; | |
import ballerina/sql; | |
mysql:Client testDB = new({ | |
host: "localhost", | |
port: 3306, | |
name: "", | |
username: "root", | |
password: "root", | |
poolOptions: { maximumPoolSize: 5 }, | |
dbOptions: { useSSL: false } | |
}); | |
public function main(string... args) { | |
createDatabase(); | |
var ret = testDB->update("CREATE TABLE testdb.bittypes( | |
id INT, | |
bitVal1 BIT(1), | |
bitVal2 BIT(64), | |
PRIMARY KEY (id) | |
)"); | |
handleUpdate(ret, "Create bittypes table"); | |
int id = 1; | |
boolean bitVal1 = true; | |
byte[] bitVal2 = [1, 2]; | |
var ret = testDB->update("INSERT INTO testdb.bittypes( | |
row_id, | |
bit_val_1, | |
bit_val_2 | |
) VALUES ( | |
?, | |
?, | |
? | |
)", id, bitVal1, bitVal2); | |
handleUpdate(ret, "Update bittypes table"); | |
dropDatabase(); | |
} | |
function createDatabase() { | |
var ret = testDB->update("CREATE DATABASE testdb"); | |
handleUpdate(ret, "Create database testdb"); | |
} | |
function dropDatabase() { | |
var ret = testDB->update("DROP DATABASE testdb"); | |
handleUpdate(ret, "Drop Database testdb"); | |
} | |
function handleUpdate(sql:UpdateResult|error returned, string message) { | |
if (returned is sql:UpdateResult) { | |
io:println(message + " status: " + returned.updatedRowCount); | |
} else { | |
io:println(message + " failed: " + <string>returned.detail().message); | |
} | |
} |