107 lines
2.9 KiB
Rust
Raw Normal View History

2020-09-17 21:46:53 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-06-16 09:37:45 +03:00
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
2021-03-12 20:04:47 +03:00
2021-05-10 12:51:22 +03:00
use marine_sqlite_connector::State;
2020-09-17 21:46:53 +03:00
2021-03-12 20:04:47 +03:00
module_manifest!();
2020-09-17 21:46:53 +03:00
pub fn main() {}
2021-05-11 15:44:11 +03:00
#[marine]
2020-11-09 06:14:07 +03:00
pub fn test1(age: i64) {
2021-05-11 15:44:11 +03:00
let connection = marine_sqlite_connector::open(":memory:").unwrap();
2020-09-17 21:46:53 +03:00
connection
.execute(
"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users VALUES ('Alice', 42);
INSERT INTO users VALUES ('Bob', 69);
",
)
.unwrap();
let mut statement = connection
.prepare("SELECT * FROM users WHERE age > ?")
.unwrap();
2020-11-09 06:14:07 +03:00
statement.bind(1, age).unwrap();
2020-09-17 21:46:53 +03:00
while let State::Row = statement.next().unwrap() {
println!("name = {}", statement.read::<String>(0).unwrap());
println!("age = {}", statement.read::<i64>(1).unwrap());
}
}
2021-05-11 15:44:11 +03:00
#[marine]
2020-11-09 06:14:07 +03:00
pub fn test2(age: i64) {
2021-05-10 12:51:22 +03:00
use marine_sqlite_connector::Value;
2020-09-17 21:46:53 +03:00
2021-05-11 15:44:11 +03:00
let connection = marine_sqlite_connector::open(":memory:").unwrap();
2020-09-17 21:46:53 +03:00
connection
.execute(
"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users VALUES ('Alice', 42);
INSERT INTO users VALUES ('Bob', 69);
",
)
.unwrap();
let mut cursor = connection
.prepare("SELECT * FROM users WHERE age > ?")
.unwrap()
.cursor();
2020-11-09 06:14:07 +03:00
cursor.bind(&[Value::Integer(age)]).unwrap();
2020-09-17 21:46:53 +03:00
while let Some(row) = cursor.next().unwrap() {
println!(
"name = {}",
row[0].as_string().expect("error on row[0] parsing")
);
println!(
"age = {}",
row[1].as_integer().expect("error on row[1] parsing")
);
2020-09-17 21:46:53 +03:00
}
}
2020-09-23 01:14:54 +03:00
2021-05-11 15:44:11 +03:00
#[marine]
2020-09-23 01:14:54 +03:00
pub fn test3() {
2021-03-04 14:09:58 +03:00
let db_path = "/tmp/users.sqlite";
2021-05-11 15:44:11 +03:00
let connection = marine_sqlite_connector::open(db_path).expect("db should be opened");
2020-09-23 01:14:54 +03:00
2021-03-04 14:09:58 +03:00
connection
.execute(
"
CREATE TABLE IF NOT EXISTS users (name TEXT, age INTEGER);
2020-09-23 01:14:54 +03:00
INSERT INTO users VALUES ('Alice', 42);
INSERT INTO users VALUES ('Bob', 69);
",
2021-03-04 14:09:58 +03:00
)
.expect("table should be created successfully");
2020-09-23 01:14:54 +03:00
2021-05-11 15:44:11 +03:00
let connection = marine_sqlite_connector::open(db_path).expect("db should be opened");
2021-03-04 14:09:58 +03:00
let cursor = connection.prepare("SELECT * FROM users").unwrap().cursor();
2020-09-23 01:14:54 +03:00
2021-03-04 14:09:58 +03:00
println!("table size is: {:?}", cursor.count());
2020-09-23 01:14:54 +03:00
}