A command line utility for generating rust mappings to databases.
Connects to a PostgreSQL database and creates a rust module representing all the schemas complete with mappings for stored functions/procedures
Maps SQL table, views, and functions to rust structs and functions using tokio-postgres
and postgres
Once generated the generated code does not contain additional checks that the database schema hasn't changed. While some type conversions will fail on the call care should be taken to update the generated code at the same time as the database
All functions generated take the client used to connect to the database as the first argument
SQL procedures/functons which are overloaded (two with the same name and different arguments) are mapped to functions which take a single tuple i,e, my_func((client, id, "hello")) and my_func((client, id))
this means overloading a previously not overloaded SQL procedure would be a breaking change with regards to the generated code (unless use-tuples with options all or one are used)
``` sqldbmapper 0.1.0 Generate a rust wrapper for a PostgreSQL database
USAGE:
sqldbmapper [FLAGS] [OPTIONS] --conn
FLAGS: -d, --debug Activate debug mode --dir Program will treat output as a directory name rather than a file and generate a whole crate. If output is not provided code is printed as usual -h, --help Prints help information --no-functions Only make mappings for tables and views --rust-case Convert names from the database to rust standard (i.e. table names in CamelCase, fields and functions in snake_case) -u, --ugly Skip running output through rustfmt -V, --version Prints version information
OPTIONS:
--conn
ARGS:
| cannot find type `????` in module `super::pg_catalog`
The type specified is only mapped by one of the third party crates. postgres_types::FromSql
lists all the types that can be mapped except for Numeric
which is mapped with rust_decimal::Decimal
Contains trait TryFromRow for converting from tokio-postgres Rows to Rust types and implements it for several common types
Reexports types that are convertable to/from sql types
Features a derive macro from TryFromRow (defined in sqldbmapper_core)
build.rs
scriptMake a new library crate and make your Cargo.toml look like the following ```toml [package] name = "rust_test" version = "0.1.0" edition = "2018"
[dependencies] sqldbmappercore = { version = "0.1", features = ["with-bit-vec-06", "with-chrono-04", "with-eui48-04", "with-geo-types-06", "with-rustdecimal-1", "with-serdejson-1", "with-time-02", "with-uuid-0_8", ] } postgres-types = { version = "0.2", features = ["derive"] } async-trait = { version = "0.1", optional = true }
serde = { version = "1.0", features = ["derive"] }
[build-dependencies] sqldbmapper = { path = "../sqldbmapper/sqldbmapper" }
[features]
sync = []
async = ["async-trait"]
``
Change the name and version of the crate as well as the features for
sqldbmapper_core` to your liking
Create a build.rs
file with the following contents
```rust
use sqldbmapper::{ Opt, Tuples, ThirdParty };
fn main() { let options = Opt { debug: false, ugly: false, dir: false, // this should be false rustcase: true, rustfmtconfig: None, rustfmtconfigpath: None, nofunctions: false, usetuples: Tuples::ForOverloads, thirdparty: vec![ ThirdParty::Chrono, ThirdParty::Time, ThirdParty::Eui48, ThirdParty::GeoTypes, ThirdParty::SerdeJson, ThirdParty::Uuid, ThirdParty::BitVec, ThirdParty::RustDecimal, ], conn: std::env::var("DATABASEURL").expect("Must provide connection string in environment variable 'DATABASE_URL'"), output: Some("./src/lib.rs".into()) };
let mut client = options.get_client();
let full_db = client.get_all(options.no_functions);
full_db.make_output(&options);
} ``` That should be enough to get started.
COMMENT ON
and stick it in doc commentsLicensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.