1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Procedural macros for sqlw.
//!
//! This crate provides the macros used by [`sqlw`]. You typically
//! don't need to depend on it directly; use the re-exports from `sqlw`.
//!
//! [`sqlw`]: https://docs.rs/sqlw
use TokenStream;
use parse_macro_input;
use crate::;
/// Compiles a SQL query with `?` placeholders.
///
/// # Syntax
///
/// - `Schema::FIELD` - Field references resolved via `.desc()`
/// - `{expr}` - Variable binding (emits `?` placeholder)
///
/// # Example
///
/// ```ignore
/// use sqlw::query_qmark;
///
/// let name = "Alice";
/// let query = query_qmark!(
/// SELECT User::NAME, User::EMAIL
/// FROM User::TABLE
/// WHERE User::NAME = {name}
/// );
/// // Result: "SELECT name, email FROM users WHERE name = ?"
/// ```
/// Compiles a SQL query with `$1, $2` placeholders.
///
/// # Syntax
///
/// - `Schema::FIELD` - Field references resolved via `.desc()`
/// - `{expr}` - Variable binding (emits `$1, $2` placeholders)
///
/// # Example
///
/// ```ignore
/// use sqlw::query_numbered;
///
/// let name = "Alice";
/// let query = query_numbered!(
/// SELECT User::NAME, User::EMAIL
/// FROM User::TABLE
/// WHERE User::NAME = {name}
/// );
/// // Result: "SELECT name, email FROM users WHERE name = $1"
/// ```
/// Derives [`FromRow`] for a struct.
///
/// # Attributes
///
/// - `#[field = "column_name"]` - Map to a specific column name
/// - `#[field(Schema::FIELD)]` - Map to a schema field constant
/// - `#[optional]` - Allow the column to be missing (returns `None`)
///
/// # Example
///
/// ```ignore
/// use sqlw::FromRow;
///
/// #[derive(FromRow)]
/// struct User {
/// id: i64,
/// #[field = "user_name"]
/// name: String,
/// #[optional]
/// bio: Option<String>,
/// }
/// ```
/// Derives [`TryFrom<ValueRef>`] for a single-field tuple struct (newtype).
///
/// Delegates to the inner type's [`TryFrom<ValueRef>`] implementation.
///
/// # Example
///
/// ```ignore
/// use sqlw::TryFromValueRef;
///
/// #[derive(TryFromValueRef)]
/// struct UserName(String);
/// ```
/// Derives [`From<T> for Value`] for newtypes and enums.
///
/// For newtypes, delegates to the inner type's [`Into<Value>`].
/// For enums, maps variants to strings via `#[value = "..."]` (defaults to lowercased name).
///
/// # Examples
///
/// ```ignore
/// use sqlw::IntoValue;
///
/// #[derive(IntoValue)]
/// struct UserName(String);
///
/// #[derive(IntoValue)]
/// enum Role {
/// #[value = "user"]
/// User,
/// Admin,
/// }
/// ```
/// Defines a table schema with typed field constants and a value struct.
///
/// Generates:
/// - A struct with one `pub` field per column (named from the column string)
/// - `TABLE` and field constants for compile-time-safe SQL references
/// - A `Default` impl so you can create zero-value instances
///
/// Each row in the macro body is: `NAME: Type "column_name"`.
/// The UPPER_CASE `NAME` becomes a `const` for SQL generation;
/// the `column_name` string becomes the struct field name.
///
/// # Example
///
/// ```ignore
/// schema!(User "users" {
/// /// Primary key
/// ID: i64 "id",
/// NAME: String "name",
/// EMAIL: String "email",
/// });
///
/// // Generated value struct:
/// // pub struct User {
/// // pub id: i64,
/// // pub name: String,
/// // pub email: String,
/// // }
/// // impl Default for User { ... }
///
/// // Generated constants:
/// // User::TABLE -> Def<User>
/// // User::ID -> Def<User, Typed<i64>>
/// // User::NAME -> Def<User, Typed<String>>
/// // User::EMAIL -> Def<User, Typed<String>>
/// ```
///
/// # Untyped fields
///
/// When you only need a column constant without a corresponding struct field
/// (e.g. for computed columns or foreign key references), omit the type:
///
/// ```ignore
/// schema!(User "users" {
/// ID "id", // const only, no struct field
/// NAME: String "name", // const and struct field
/// });
/// ```