Skip to content

Commit

Permalink
feat(code): add derive "QueryableByName" to read-structs
Browse files Browse the repository at this point in the history
fixes Wulf#97
  • Loading branch information
hasezoey committed Nov 6, 2023
1 parent 5062ebe commit 3f039b5
Show file tree
Hide file tree
Showing 35 changed files with 44 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- list changes to files (unchanged, modified, deleted)
- generate doc-comments for generated structs, fields and functions
- change the diesel import to be `use diesel` (instead of the previous `use crate::diesel`)
- add derive `QueryableByName` to read-structs

## 0.0.17 (yanked)

Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ authors = [
edition = "2021"

[features]
default = ["tsync", "backtrace"]
default = ["tsync", "backtrace", "derive-queryablebyname"]
# enable support for tsync (https://github.com/Wulf/tsync)
tsync = []
# enable support for diesel_async (https://github.com/weiznich/diesel_async)
async = []
# enable support for backtraces on errors
backtrace = []
# enable derive "QueryableByName"
derive-queryablebyname = []

[dependencies]
clap = { version = "4.4", features = ["derive", "wrap_help"] }
Expand Down
9 changes: 8 additions & 1 deletion src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub mod derives {
pub const SELECTABLE: &str = "Selectable";
pub const IDENTIFIABLE: &str = "Identifiable";
pub const ASSOCIATIONS: &str = "Associations";
#[cfg(feature = "derive-queryablebyname")]
pub const QUERYABLEBYNAME: &str = "QueryableByName";
}

impl<'a> Struct<'a> {
Expand Down Expand Up @@ -186,7 +188,12 @@ impl<'a> Struct<'a> {
match self.ty {
StructType::Read => {
// derives that always exist, regardless of extra conditions
derives_vec.extend_from_slice(&[derives::QUERYABLE, derives::SELECTABLE]);
derives_vec.extend_from_slice(&[
derives::QUERYABLE,
derives::SELECTABLE,
#[cfg(feature = "derive-queryablebyname")]
derives::QUERYABLEBYNAME,
]);

if !self.table.foreign_keys.is_empty() {
derives_vec.extend_from_slice(&[derives::ASSOCIATIONS, derives::IDENTIFIABLE]);
Expand Down
2 changes: 1 addition & 1 deletion test/autogenerated_all/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/autogenerated_attributes/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/autogenerated_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/cleanup_generated_content/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/create_update_str_cow/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(text))]
pub struct Todos {
/// Field representing column `text`
Expand Down
2 changes: 1 addition & 1 deletion test/create_update_str_str/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(text))]
pub struct Todos {
/// Field representing column `text`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `tableA`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=tableA, primary_key(_id))]
pub struct TableA {
/// Field representing column `_id`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `tableB`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, Associations, Identifiable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName, Associations, Identifiable)]
#[diesel(table_name=tableB, primary_key(_id), belongs_to(TableA, foreign_key=link))]
pub struct TableB {
/// Field representing column `_id`
Expand Down
2 changes: 1 addition & 1 deletion test/custom_model_path/models/tableA/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `tableA`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=tableA, primary_key(_id))]
pub struct TableA {
/// Field representing column `_id`
Expand Down
2 changes: 1 addition & 1 deletion test/custom_model_path/models/tableB/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `tableB`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, Associations, Identifiable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName, Associations, Identifiable)]
#[diesel(table_name=tableB, primary_key(_id), belongs_to(TableA, foreign_key=link))]
pub struct TableB {
/// Field representing column `_id`
Expand Down
2 changes: 1 addition & 1 deletion test/manual_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/multiple_primary_keys/models/users/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `users`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=users, primary_key(name,address))]
pub struct Users {
/// Field representing column `name`
Expand Down
2 changes: 1 addition & 1 deletion test/once_common_structs/models/table1/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::models::common::*;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `table1`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table1, primary_key(id))]
pub struct Table1 {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/once_common_structs/models/table2/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::models::common::*;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `table2`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table2, primary_key(id))]
pub struct Table2 {
/// Field representing column `id`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
use crate::models::common::*;

/// Struct representing a row in table `table1`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table1, primary_key(id))]
pub struct Table1 {
/// Field representing column `id`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
use crate::models::common::*;

/// Struct representing a row in table `table2`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table2, primary_key(id))]
pub struct Table2 {
/// Field representing column `id`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
use crate::models::common::*;

/// Struct representing a row in table `table1`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table1, primary_key(id))]
pub struct Table1 {
/// Field representing column `id`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
use crate::models::common::*;

/// Struct representing a row in table `table2`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table2, primary_key(id))]
pub struct Table2 {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/once_connection_type/models/table1/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
use crate::models::common::*;

/// Struct representing a row in table `table1`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table1, primary_key(id))]
pub struct Table1 {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/once_connection_type/models/table2/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
use crate::models::common::*;

/// Struct representing a row in table `table2`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table2, primary_key(id))]
pub struct Table2 {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/readonly/models/normal/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `normal`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=normal, primary_key(id))]
pub struct Normal {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/readonly/models/prefixTable/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `prefixTable`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=prefixTable, primary_key(id))]
pub struct PrefixTable {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/readonly/models/prefixTableSuffix/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `prefixTableSuffix`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=prefixTableSuffix, primary_key(id))]
pub struct PrefixTableSuffix {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/readonly/models/tableSuffix/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `tableSuffix`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=tableSuffix, primary_key(id))]
pub struct TableSuffix {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table_async/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use diesel::QueryResult;
type ConnectionType = diesel_async::pooled_connection::deadpool::Object<diesel_async::AsyncPgConnection>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table_no_crud/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::schema::*;
use serde::{Deserialize, Serialize};

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table_no_serde/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `todos`
#[derive(Debug, Clone, Queryable, Selectable)]
#[derive(Debug, Clone, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=todos, primary_key(id))]
pub struct Todos {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/single_model_file/models/table1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `table1`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table1, primary_key(id))]
pub struct Table1 {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/single_model_file/models/table2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `table2`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=table2, primary_key(id))]
pub struct Table2 {
/// Field representing column `id`
Expand Down
2 changes: 1 addition & 1 deletion test/use_statements/models/fang_tasks/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use diesel::QueryResult;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

/// Struct representing a row in table `fang_tasks`
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Selectable, QueryableByName)]
#[diesel(table_name=fang_tasks, primary_key(id))]
pub struct FangTasks {
/// Field representing column `id`
Expand Down

0 comments on commit 3f039b5

Please sign in to comment.