mirror of
https://github.com/Phundrak/georm.git
synced 2025-08-30 22:25:35 +00:00
This commit introduces support for PostgreSQL generated columns by adding two new field attributes to the `Georm` derive macro: `#[georm(generated)]` and `#[georm(generated_always)]`. The `#[georm(generated_always)]` attribute is for fields that are always generated by the database, such as `GENERATED ALWAYS AS IDENTITY` columns or columns with a `GENERATED ALWAYS AS (expression) STORED` clause. These fields are now excluded from `INSERT` and `UPDATE` statements, preventing accidental writes and ensuring data integrity at compile time. The `#[georm(generated)]` attribute is for fields that have a default value generated by the database but can also be manually overridden, such as `GENERATED BY DEFAULT AS IDENTITY` columns. These fields behave similarly to `#[georm(defaultable)]` fields, allowing them to be omitted from `INSERT` statements to use the database-generated value. For now, the behaviour is the same between `#[georm(generated)]` and `#[georm(defaultable)]`, but the addition of the former now will be useful for future features. Key changes: - Added `generated` and `generated_always` attributes to `GeormFieldAttributes`. - Introduced `GeneratedType` enum in the IR to represent the different generation strategies. - Modified the `create` and `update` query generation to exclude fields marked with `#[georm(generated_always)]`. - Integrated `#[georm(generated)]` fields with the existing defaultable struct logic. - Added validation to prevent conflicting attribute usage, namely `#[georm(generated)]` and `#[georm(generated_always)]` on the same field. Implements #3
132 lines
3.2 KiB
Rust
132 lines
3.2 KiB
Rust
use georm::Georm;
|
|
use sqlx::types::BigDecimal;
|
|
|
|
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
|
#[georm(
|
|
table = "biographies",
|
|
one_to_one = [{
|
|
name = "author", remote_id = "biography_id", table = "authors", entity = Author
|
|
}]
|
|
)]
|
|
pub struct Biography {
|
|
#[georm(id)]
|
|
pub id: i32,
|
|
pub content: String,
|
|
}
|
|
|
|
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
|
#[georm(table = "authors")]
|
|
pub struct Author {
|
|
#[georm(id)]
|
|
pub id: i32,
|
|
pub name: String,
|
|
#[georm(relation = {entity = Biography, table = "biographies", name = "biography", nullable = true})]
|
|
pub biography_id: Option<i32>,
|
|
}
|
|
|
|
impl PartialOrd for Author {
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
Some(self.id.cmp(&other.id))
|
|
}
|
|
}
|
|
|
|
impl Ord for Author {
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
self.id.cmp(&other.id)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
|
#[georm(
|
|
table = "books",
|
|
one_to_many = [
|
|
{ name = "reviews", remote_id = "book_id", table = "reviews", entity = Review }
|
|
],
|
|
many_to_many = [{
|
|
name = "genres",
|
|
table = "genres",
|
|
entity = Genre,
|
|
link = { table = "book_genres", from = "book_id", to = "genre_id" }
|
|
}]
|
|
)]
|
|
pub struct Book {
|
|
#[georm(id)]
|
|
ident: i32,
|
|
title: String,
|
|
#[georm(relation = {entity = Author, table = "authors", name = "author"})]
|
|
author_id: i32,
|
|
}
|
|
|
|
impl PartialOrd for Book {
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
Some(self.ident.cmp(&other.ident))
|
|
}
|
|
}
|
|
|
|
impl Ord for Book {
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
self.ident.cmp(&other.ident)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Georm, PartialEq, Eq)]
|
|
#[georm(table = "reviews")]
|
|
pub struct Review {
|
|
#[georm(id)]
|
|
pub id: i32,
|
|
#[georm(relation = {entity = Book, table = "books", remote_id = "ident", name = "book"})]
|
|
pub book_id: i32,
|
|
pub review: String,
|
|
}
|
|
|
|
#[derive(Debug, Georm, PartialEq, Eq)]
|
|
#[georm(
|
|
table = "genres",
|
|
many_to_many = [{
|
|
name = "books",
|
|
table = "books",
|
|
entity = Book,
|
|
remote_id = "ident",
|
|
link = { table = "book_genres", from = "genre_id", to = "book_id" }
|
|
}]
|
|
)]
|
|
pub struct Genre {
|
|
#[georm(id)]
|
|
id: i32,
|
|
name: String,
|
|
}
|
|
|
|
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
|
#[georm(table = "UserRoles")]
|
|
pub struct UserRole {
|
|
#[georm(id)]
|
|
pub user_id: i32,
|
|
#[georm(id)]
|
|
pub role_id: i32,
|
|
#[georm(defaultable)]
|
|
pub assigned_at: chrono::DateTime<chrono::Utc>,
|
|
}
|
|
|
|
#[derive(Debug, Georm, PartialEq, Default, Clone)]
|
|
#[georm(table = "products")]
|
|
pub struct Product {
|
|
#[georm(id, generated_always)]
|
|
pub id: i32,
|
|
#[georm(generated)]
|
|
pub sku_number: i32,
|
|
pub name: String,
|
|
pub price: BigDecimal,
|
|
pub discount_percent: i32,
|
|
#[georm(generated_always)]
|
|
pub final_price: Option<BigDecimal>, // Apparently this can be null ?
|
|
}
|
|
|
|
impl Product {
|
|
#[allow(dead_code)]
|
|
pub async fn find_by_name(name: String, pool: &sqlx::PgPool) -> ::sqlx::Result<Self> {
|
|
::sqlx::query_as!(Self, "SELECT * FROM products WHERE name = $1", name)
|
|
.fetch_one(pool)
|
|
.await
|
|
}
|
|
}
|