Rustic bindings for sqlite3

Overview

Rust-Sqlite3

Rustic bindings for sqlite3. OBSOLETE in favor of jgallagher/rusqlite

Copyright (c) 2014-2017 Dan Connolly and contributors

Share and enjoy. LICENSE: MIT.

Documentation, Status

View the documentation online

Three layers of API are provided:

  • mod ffi provides exhaustive, though unsafe, bindgen bindings for libsqlite.h
  • mod core provides a minimal safe interface to the basic sqlite3 API
  • mod types provides ToSql/FromSql traits, and the library provides convenient query() and update() APIs.

The API design is perhaps stabilizing, though testing is uneven and I have not used the library beyond trivial integration tests.

Build Status

TODO

  • another thorough read-through of the sqlite API intro, with unit tests to match; especially...
    • unit testing other than the happy-paths
  • ToSql/FromSql can now be implemented by clients, but the types module probably doesn't hit the 80% mark yet; e.g. it's missing uint and &[u8].
  • investigate test coverage tools for rust
  • basic benchmarking

Motivation and Acknowledgements

I was looking into sandstorm, a personal cloud platform with an architecture based on the wonderful capability security paradigm, and I found a rust application, acronymy, that uses the native API rather than the traditional POSIX environment.

I started poring over the code and followed the dependency link to linuxfood's rustsqlite. I started working on a memory safety issue etc. but soon found a number of large-scale API design issues that I wasn't sure how to approach with the upstream developers. I was also inspired by FromSql, ToSql and such from sfackler's rust-postgres API.

So I started from scratch, using bindgen, Result (sum types) etc.

Comments
  • Does DatabaseConnection::exec() need to be &mut self ?

    Does DatabaseConnection::exec() need to be &mut self ?

    Life gets much better if I change this to:

    pub fn exec(&self, sql: &str) -> SqliteResult<()>
    

    The consistency-based argument here is that exec() and prepare() should be the same.

    The not-very-compelling argument is that exec(&mut self) means that I can only call exec() from one reference and that's a huge pain.

    But I'll admit that whenever I think about the boundary between "safety assurances provided by SQLite" and "additional safety assurances provided by Rust", I get myself tied up in knots.

    wontfix 
    opened by ericsink 6
  • retire these bindings in favor of rusqlite?

    retire these bindings in favor of rusqlite?

    Now that rusqlite has addressed the lifetime issues with query_map(), there's not much reason to maintain these bindings.

    I'm inclined to give this project a once-over and raise issues there for anything that's novel here.

    I made a note about the expose() method on one of their issues... it's a closed issue, though, so maybe I should look into that some more.

    cc @jgallagher @PeterReid @dgilman @AndiDog @ericsink

    opened by dckc 4
  • Cargo build error: expected &str, found &[collections::string::String]

    Cargo build error: expected &str, found &[collections::string::String]

    Hello,

    I have troubles cargo building your library (using nightly Rust and Cargo from 2014-10-14):

    /Users/olda/Code/rust/rust-sqlite3/src/types.rs:111:25: 111:81 error: mismatched types: expected `&str`, found `&[collections::string::String]` (expected str, found unsized array)
    /Users/olda/Code/rust/rust-sqlite3/src/types.rs:111         s.bind_text(ix, time::at_utc(*self).strftime(SQLITE_TIME_FMT).as_slice())
    

    I tried fixing it, but failed. :-) I understand that &str is the same as &[u8] (http://doc.rust-lang.org/std/str/index.html#representation) but when I did this, It complained that &[u8] isn't &str.:

    impl ToSql for time::Timespec {
        fn to_sql(&self, s: &mut PreparedStatement, ix: uint) -> SqliteResult<()> {
            let time_string = time::at_utc(*self).strftime(SQLITE_TIME_FMT).unwrap();
            let time_str    = time_string.as_bytes();
            s.bind_text(ix, time_str)
        }
    }
    

    I'm curious what the fix is.

    Thanks, Ollie

    opened by ollie 4
  • Fix specification of dependency version

    Fix specification of dependency version

    Was always incorrect, and from Rust 1.9 or so, Cargo will actually warn about it (will become an error at some point).

    It must either be

    [dependencies.time]
    version = "^0.1.5"
    

    or just plain below "[dependencies]":

    [dependencies]
    time = "^0.1.5"
    
    opened by AndiDog 3
  • required sqlite3 version

    required sqlite3 version

    In fbf297b, I stopped using sqlite_close_v2 for compatibility with sqlite 3.7.9, provided by Ubuntu 12.04, used by travis. #11 is a pull request to get sqlite3_errstr via SQLite3 3.7.15.1.

    I'd like to use this issue to consider the options.

    opened by dckc 2
  • Move to travis-cargo, fix docs

    Move to travis-cargo, fix docs

    You'll need to add a commit to change a few things on top of this:

    • add your encrypted Github access token to ./.travis.yml again (I followed the Giving Travis Permissions section here http://www.hoverbear.org/2015/03/07/rust-travis-github-pages/ )
    • change documentation link in Cargo.toml to your github pages URL
    • change docs and Build Status url/badge in README.md to your github/travis URLs
    opened by dgilman 1
  • On the relationship between ResultSet and PreparedStatement

    On the relationship between ResultSet and PreparedStatement

    I've worked myself into an unhappy spot.

    Basically, I'm building an API which wants to return a set of rows. The API is not sqlite-specific, but this implementation of it is.

    Normally I would just return a struct that wraps the sqlite3_stmt. Something like this:

    struct MyRows {
        stmt: PreparedStatement,
    }
    

    Currently I am trying to code against the core layer of rust-sqlite3. So in order to return a struct that contains what I need, it has to look kinda like this:

    struct MyRows {
        stmt: PreparedStatement,
        rset: ResultSet,
    }
    

    But ResultSet is a reference to a PreparedStatement, so Rust isn't going to allow this. (Google for things talking about a struct which contains both data and a reference to it.)

    Basically, right now I wish that ResultSet owned its PreparedStatement instead of having a reference to same. Then I would just do this:

    struct MyRows {
        rset: ResultSet,
    }
    

    But I also realize that if it were designed this way, I would very likely be wishing for the other approach, since I would want the ability to reuse the statement handle after the current result set has been processed.

    I think maybe I wish that ResultSet didn't exist at all, which means I would end up with an API more similar to the sqlite3 API to which I am accustomed. But I haven't worked through the implications of that here in Rust land.

    I could get around this by coding against the ffi layer instead. And I might end up doing that. But for now I wanted to raise this issue about the design of the core layer.

    wontfix 
    opened by ericsink 1
  • Rows need not be mutable to extract data from them

    Rows need not be mutable to extract data from them

    FromSql and column_text/blob/str/slice unnecessarily required rows to be mutable. This prevented having references to two slices from the same row as once.

    opened by PeterReid 1
  • Add zero-copy column getters and fix potential non-UTF8 Strings

    Add zero-copy column getters and fix potential non-UTF8 Strings

    Add zero-copy column getters and fix potential non-UTF8 Strings

    Sqlite3's notion of text corresponds more closely to Rust's notion of [u8]s than strs in two important ways:

    • Sqlite3's text, like [u8]s and unlike strs, can contain non-UTF8 sequences.
    • Sqlite3's text, unlike strs, must be null-terminated.

    This makes sqlite3_column_blob work at least as well as sqlite3_column_text for getting a str into Rust. Never using sqlite3_column_text has another advantage: mixing calls to sqlite3_column_blob and sqlite3_column_text may cause the underlying buffer to be reallocated, invalidating the old buffer. By only ever calling the _blob variant, the buffer returned will be valid for as long as the row. This makes it possible to safely expose zero-copy column getters for &[u8] and &str.

    And I added myself to the authors list.

    opened by PeterReid 1
  • Fix build for rustc changes.

    Fix build for rustc changes.

    Previously, PreparedStatement did not actually use its 'db lifetime, so the lifetime checks behaved as if there was no bound on the lifetime. A rustc change made that a compile error. Adding a PhantomData there, and so making the lifetime have to be respected, make a number of other changes necessary. There are now separate lifetime parameters for statements, result sets, and rows. Not having separate lifetime parameters would cut off, (for example) a PreparedStatement's lifetime when it got executed, even after the ResultSet that came from that execute went out of scope. I believe that was because the lifetimes got somehow fused together when the ResultSet was made.

    A second change is that prepare() takes a reference instead of a mutable reference. The compiler was treating the PreparedStatement that came from the prepare as if it kept ownership of the DatabaseConnection without that. It seems like there ought to be a way around this, but I didn't find it.

    opened by PeterReid 1
  • Fix for rustc update, fix a test on Windows

    Fix for rustc update, fix a test on Windows

    The open_file_db test didn't work on Windows, where there is not usually a /tmp directory. The rustc changes were: Show has changed to Display and io is now old_io.

    opened by PeterReid 1
  • Documentation is out of date

    Documentation is out of date

    The documentation on rust-ci.org (linked to from the README) seems to have fallen out of date.

    For example, http://www.rust-ci.org/dckc/rust-sqlite3/doc/sqlite3/trait.DatabaseUpdate.html seems to have been removed from the source (in a good way), and http://www.rust-ci.org/dckc/rust-sqlite3/doc/sqlite3/access/fn.open.html is documented as taking one argument but actually takes two.

    This does seem like a nice API to Sqlite3, but I'm getting thrown off a bit by the outdated documentation. I'll switch to locally-generated docs for now.

    opened by aij 2
Owner
Dan Connolly
Open Web advocate, Open source developer since 1993
Dan Connolly
Example sqlite3 Dynamic Loadable Extension in Rust - vfs and vtab modules - port of vfsstat.c

Example sqlite3 Dynamic Loadable Extension in Rust - vfs and vtab modules The vfs and vtab This is a port of the official ext/misc/vfsstat.c sqlite3 e

Manos Pitsidianakis 28 Oct 10, 2022
sqlite3 Rewritten in RiiR Rust 🦀🦀🦀

sqlite3 Rewritten in RiiR Rust ?? ?? ?? Finally, one of the best written software paired with one of the best writable programming language‽ Fearless

Manos Pitsidianakis 142 Jan 3, 2023
leveldb LevelDB LevelDB skade/leveldb — LevelDB bindings

Rust leveldb bindings Almost-complete bindings for leveldb for Rust. Documentation Rust version policy leveldb is built and tested on stable releases

Florian Gilcher 161 Nov 20, 2022
Rust bindings for LMDB

lmdb-rs Rust bindings for LMDB Documentation (master branch) Building LMDB is bundled as submodule so update submodules first: git submodule update --

Valerii Hiora 104 Dec 8, 2022
Ergonomic bindings to SQLite for Rust

Rusqlite Rusqlite is an ergonomic wrapper for using SQLite from Rust. It attempts to expose an interface similar to rust-postgres. use rusqlite::{para

Rusqlite 1.9k Jan 5, 2023
"KakaoTalk" internal api bindings

talk-api-client "KakaoTalk" internal api bindings Examples Auth See examples/auth.rs To run example, clone this repository and run command cargo run -

storycraft 5 Sep 7, 2022
Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

null 294 Dec 23, 2022
SQLite3 Bindings for Rust

SQLite3 Bindings for Rust To compile use rustc src/sqlite3.rs or if you have Cargo installed cargo build. The interface is currently evolving both alo

null 74 Aug 6, 2022
Example sqlite3 Dynamic Loadable Extension in Rust - vfs and vtab modules - port of vfsstat.c

Example sqlite3 Dynamic Loadable Extension in Rust - vfs and vtab modules The vfs and vtab This is a port of the official ext/misc/vfsstat.c sqlite3 e

Manos Pitsidianakis 28 Oct 10, 2022
sqlite3 Rewritten in RiiR Rust 🦀🦀🦀

sqlite3 Rewritten in RiiR Rust ?? ?? ?? Finally, one of the best written software paired with one of the best writable programming language‽ Fearless

Manos Pitsidianakis 142 Jan 3, 2023
A rustic tcp + serialization abstraction.

Wire An abstraction over TCP and Serialization "put a struct in one side and it comes out the other end" Wire is a library that makes writing applicat

Ty Overby 33 May 12, 2021
Rustic - a backup tool that provides fast, encrypted, deduplicated backups

Rustic is a backup tool that provides fast, encrypted, deduplicated backups. It can read the restic repo format desribed in the design document and writes a compatible repo format which can also be read by restic.

null 266 Jan 2, 2023
rustic_core - library for fast, encrypted, deduplicated backups that powers rustic-rs

Library for fast, encrypted, and deduplicated backups About This library is powering rustic-rs. A backup tool that provides fast, encrypted, deduplica

rustic 9 Sep 29, 2023
Rust bindings for libinjection

libinjection-rs Rust bindings for libinjection. How to use Add libinjection to dependencies of Cargo.toml: libinjection = "0.2" Import crate: extern c

ArvanCloud 35 Sep 24, 2022
A project for generating C bindings from Rust code

cbindgen   Read the full user docs here! cbindgen creates C/C++11 headers for Rust libraries which expose a public C API. While you could do this by h

Ryan Hunt 1.7k Jan 3, 2023
Automatically generates Rust FFI bindings to C (and some C++) libraries.

bindgen bindgen automatically generates Rust FFI bindings to C (and some C++) libraries. For example, given the C header doggo.h: typedef struct Doggo

The Rust Programming Language 3.2k Jan 4, 2023
Rust-JDBC bindings

jdbc A Rust library that allows you to use JDBC and JDBC drivers. Usage First, add the following to your Cargo.toml: [dependencies] jdbc = "0.1" Next,

Aurora 18 Feb 9, 2022
Lua 5.3 bindings for Rust

rust-lua53 Aims to be complete Rust bindings for Lua 5.3 and beyond. Currently, master is tracking Lua 5.3.3. Requires a Unix-like environment. On Win

J.C. Moyer 150 Dec 14, 2022
Safe Rust bindings to Lua 5.1

rust-lua Copyright 2014 Lily Ballard Description This is a set of Rust bindings to Lua 5.1. The goal is to provide a (relatively) safe interface to Lu

Lily Ballard 124 Jan 5, 2023
mruby safe bindings for Rust

mrusty. mruby safe bindings for Rust mrusty lets you: run Ruby 1.9 files with a very restricted API (without having to install Ruby) reflect Rust stru

Anima 200 Oct 12, 2022