sql-table

Makes no-ORM querying of SQL databases more concise.

Cargo Documentation License

Examples

Basic usage:

```rust use sql_table::{ table, inject, ForeignKeyName, IndexName, Qualified, Table, TableColumn, Unquote, };

table!(Person: "person" { Name: "name", Country: "country", });

assert_eq!( inject!(" SELECT #{Person::Name}# FROM #{Person}# WHERE #{Person::Country}# = 'United States' "), format!(" SELECT name FROM person WHERE country = 'United States' ") ); ```

If you need more sophisticated behaviour:

```rust use sql_table::{ table, inject, ForeignKeyName, IndexName, Qualified, Table, TableColumn, Unquote, };

// If you want a specific table identifiers to be displayed by default as quoted // just add quote parameter after table definition. table!(SimCard: "sim card" { Owner: "owner", PhoneNumber: "phone number", }, quote: "`");

table!(Person: "person" { Name: "name", Country: "country", });

// Identifiers can also be unquoted using .unquoted() method, // or, in case of field names, they can be converted into qualified form // using .qualified() method of trait Qualified provided by this library // and implemented for all fields of each generated table. // Said methods can also be chained .unquoted().qualified(). assert_eq!( inject!(" SELECT #{SimCard::PhoneNumber.qualified()}# FROM #{SimCard}# JOIN #{Person}# ON #{Person::Name.qualified()}# = #{SimCard::Owner.unquoted().qualified()}# WHERE #{Person::Country.qualified()}# = 'United States' "), format!(" SELECT sim card.phone number FROM sim card JOIN person ON person.name = sim card.owner WHERE person.country = 'United States' ") );

// Format: fk // The whole name will get quoted if any of it's components (table/field names) are quoted. // In this case quotation will be done using the first found quote // of corresponding components listed in the key name. asserteq!( SimCard::Owner.foreignkeyname(Person::Name), "fk_sim card_owner_person_name" ); asserteq!( ::Unquoted::Owner.unquoted().foreignkeyname(Person::Name), "fksim cardownerpersonname" );

// Format: ix

... // All fields must be either quoted or unquoted. // The whole name will get quoted if either table or fields are quoted. asserteq!( Person::indexname(&[Person::Name]), "ixpersonname" ); asserteq!( SimCard::indexname(&[SimCard::Owner, SimCard::PhoneNumber]), "ix_sim card_owner_phone number" ); asserteq!( ::Unquoted::indexname(&[ ::Unquoted::Owner.unquoted(), ::Unquoted::PhoneNumber.unquoted() ]), "ixsim cardownerphone number" );

// If you need a different naming format for foreign keys, indices or anything else // you can implement custom IndexName, ForeignKeyName, etc. traits // using provided by this library default implementations as a reference. ```