Derive Macro geekorm_derive::GeekTable
source · #[derive(GeekTable)]
{
// Attributes available to this derive:
#[geekorm]
}
Expand description
Derive macro for GeekTable
trait.
This macro will generate the implementation of GeekTable
trait for the given struct.
The struct must have named fields.
§Example
This macro generates a number of methods for the struct, including table
and table_name
methods.
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;
#[derive(GeekTable)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}
// This will get you the underlying table information.
let table = Users::table();
assert_eq!(Users::table_name(), "Users");
§Generate New Rows
When the new
feature is enabled, the following methods are generated for the struct:
PrimaryKey<T>
fields are not generatedOption<T>
fields are not generated
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;
#[derive(GeekTable)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
country: Option<String>,
}
let user = Users::new(
String::from("geekmasher"),
42,
String::from("Software Developer")
);
§Generated Query Methods
The following methods are generated for the struct:
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;
#[derive(GeekTable)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}
// Create a new table query
let create = Users::create().build()
.expect("Failed to build CREATE TABLE query");
// Select data from the table
let select = Users::select()
.where_eq("name", "geekmasher")
.build()
.expect("Failed to build SELECT query");
§Generated Helper Methods
When the helpers
feature is enabled, the following helper methods are generated for the struct:
Note: This is a very experimental feature and might change in the future.
use geekorm::{GeekTable, PrimaryKeyInteger};
use geekorm::prelude::*;
#[derive(GeekTable)]
struct Users {
id: PrimaryKeyInteger,
name: String,
age: i32,
occupation: String,
}
// Select by column helper function
let user = Users::select_by_name("geekmasher");
let user = Users::select_by_age(69);
let user = Users::select_by_occupation("Software Developer");