Skip to content

Commit

Permalink
0.0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Dec 1, 2023
1 parent 7b3e384 commit b0cc6a2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libsql-python"
version = "0.0.19"
version = "0.0.20"
edition = "2021"

[lib]
Expand All @@ -12,3 +12,4 @@ pyo3 = "0.19.0"
libsql = { git = "https://github.com/tursodatabase/libsql/", rev = "365d92124321de378cf1aaf5100c08efb8f046dd" }
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
tracing-subscriber = "0.3"
log = "0.4.20"
19 changes: 9 additions & 10 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import libsql_experimental
import os

con = libsql_experimental.connect("hello.db", sync_url="http://localhost:8080",
auth_token="")
import libsql_experimental as libsql

con.sync()
print(F"syncing with {os.getenv('LIBSQL_URL')}")
conn = libsql.connect("hello_sync.db", sync_url=os.getenv("LIBSQL_URL"),
auth_token=os.getenv("LIBSQL_AUTH_TOKEN"))
conn.execute("CREATE TABLE IF NOT EXISTS users_sync (id INTEGER);")
conn.execute("INSERT INTO users_sync(id) VALUES (1);")
conn.commit()

cur = con.cursor()

cur.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER, email TEXT);")
cur.execute("INSERT INTO users VALUES (1, '[email protected]')")

print(cur.execute("SELECT * FROM users").fetchone())
print(conn.execute("select * from users_sync").fetchall())
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pyo3::prelude::*;
use pyo3::types::{PyList, PyTuple};
use std::cell::RefCell;
use std::sync::Arc;
use log::trace;

fn to_py_err(error: libsql_core::errors::Error) -> PyErr {
let msg = match error {
Expand Down Expand Up @@ -155,6 +156,7 @@ impl Cursor {
sql: String,
parameters: Option<&PyTuple>,
) -> PyResult<pyo3::PyRef<'a, Self>> {
trace!("execute: sql={}", sql);
self_.rt.block_on(execute(&self_, sql, parameters))?;
Ok(self_)
}
Expand All @@ -164,6 +166,7 @@ impl Cursor {
sql: String,
parameters: Option<&PyList>,
) -> PyResult<pyo3::PyRef<'a, Cursor>> {
trace!("executemany: sql={}", sql);
for parameters in parameters.unwrap().iter() {
let parameters = parameters.extract::<&PyTuple>()?;
self_
Expand All @@ -175,6 +178,7 @@ impl Cursor {

#[getter]
fn description(self_: PyRef<'_, Self>) -> PyResult<Option<&PyTuple>> {
trace!("description");
let stmt = self_.stmt.borrow();
let mut elements: Vec<Py<PyAny>> = vec![];
for column in stmt.as_ref().unwrap().columns() {
Expand All @@ -196,6 +200,7 @@ impl Cursor {
}

fn fetchone(self_: PyRef<'_, Self>) -> PyResult<Option<&PyTuple>> {
trace!("fetchone");
let mut rows = self_.rows.borrow_mut();
match rows.as_mut() {
Some(rows) => {
Expand All @@ -213,6 +218,7 @@ impl Cursor {
}

fn fetchall(self_: PyRef<'_, Self>) -> PyResult<Option<&PyList>> {
trace!("fetchall");
let mut rows = self_.rows.borrow_mut();
match rows.as_mut() {
Some(rows) => {
Expand All @@ -235,6 +241,7 @@ impl Cursor {

#[getter]
fn lastrowid(self_: PyRef<'_, Self>) -> PyResult<Option<i64>> {
trace!("lastrowid");
let stmt = self_.stmt.borrow();
match stmt.as_ref() {
Some(_) => Ok(Some(self_.conn.last_insert_rowid())),
Expand All @@ -244,16 +251,19 @@ impl Cursor {

#[getter]
fn rowcount(self_: PyRef<'_, Self>) -> PyResult<i64> {
trace!("rowcount");
Ok(*self_.rowcount.borrow())
}

fn close(_self: PyRef<'_, Self>) -> PyResult<()> {
trace!("close");
// TODO
Ok(())
}
}

async fn begin_transaction(conn: &libsql_core::Connection) -> PyResult<()> {
trace!("begin_transaction");
conn.execute("BEGIN", ()).await.map_err(to_py_err)?;
Ok(())
}
Expand Down Expand Up @@ -284,15 +294,15 @@ async fn execute(cursor: &Cursor, sql: String, parameters: Option<&PyTuple>) ->
None => libsql_core::params::Params::None,
};
let mut stmt = cursor.conn.prepare(&sql).await.map_err(to_py_err)?;
let rows = stmt.query(params).await.map_err(to_py_err)?;
let rows = stmt.execute(params).await.map_err(to_py_err)?;
if stmt_is_dml {
let mut rowcount = cursor.rowcount.borrow_mut();
*rowcount += cursor.conn.changes() as i64;
} else {
cursor.rowcount.replace(-1);
}
cursor.stmt.replace(Some(stmt));
cursor.rows.replace(Some(rows));
//cursor.rows.replace(Some(rows));
Ok(())
}

Expand Down

0 comments on commit b0cc6a2

Please sign in to comment.