|
72 | 72 |
|
73 | 73 | (define-template-metafunction (make-fld-maker stx) |
74 | 74 | (syntax-parse stx |
75 | | - [(_ struct-id fld-id fld-name fld-type fld-pk? fld-ai? fld-nullable? fld-unique? fld-virtual?) |
| 75 | + [(_ struct-id fld-id fld-name fld-type fld-pk? fld-fk? fld-fk fld-ai? fld-nullable? fld-unique? fld-virtual?) |
76 | 76 | (with-syntax ([getter-id (format-id #'struct-id "~a-~a" #'struct-id #'fld-id)] |
77 | 77 | [setter-id (format-id #'struct-id "set-~a-~a" #'struct-id #'fld-id)] |
78 | 78 | [updater-id (format-id #'struct-id "update-~a-~a" #'struct-id #'fld-id)]) |
|
83 | 83 | #:setter setter-id |
84 | 84 | #:updater updater-id |
85 | 85 | #:primary-key? fld-pk? |
| 86 | + #:foreign-key? fld-fk? |
| 87 | + #:foreign-key fld-fk |
86 | 88 | #:auto-increment? fld-ai? |
87 | 89 | #:nullable? fld-nullable? |
88 | 90 | #:unique? fld-unique? |
|
111 | 113 | (optional-arg ... ...) |
112 | 114 | struct-pred-id))])) |
113 | 115 |
|
| 116 | + (define-syntax-class fk |
| 117 | + (pattern (schema:id field:id) |
| 118 | + #:with ctor-arg (with-syntax* ([schema-id (format-id #'schema "~a-schema" #'schema)]) |
| 119 | + #'(foreign-key (schema-table schema-id) (id->column-name 'field))))) |
| 120 | + |
114 | 121 | (define-syntax-class fld |
115 | 122 | (pattern (id:id type:expr (~alt (~optional (~and #:primary-key primary-key)) |
| 123 | + (~optional (~seq #:foreign-key foreign-key:fk)) |
116 | 124 | (~optional (~and #:auto-increment auto-increment)) |
117 | 125 | (~optional (~and #:nullable nullable)) |
118 | 126 | (~optional (~and #:unique unique)) |
|
126 | 134 | #:with required? (if (or (attribute auto-increment) |
127 | 135 | (attribute nullable)) #'#f #'t) |
128 | 136 | #:with primary-key? (if (attribute primary-key) #'#t #'#f) |
| 137 | + #:with foreign-key? (if (attribute foreign-key) #'#t #'#f) |
| 138 | + #:with foreign-key-ctor-arg (if (attribute foreign-key) |
| 139 | + (attribute foreign-key.ctor-arg) |
| 140 | + #'#f) |
129 | 141 | #:with auto-increment? (if (attribute auto-increment) #'#t #'#f) |
130 | 142 | #:with nullable? (if (attribute nullable) #'#t #'#f) |
131 | 143 | #:with unique? (if (attribute unique) #'#t #'#f) |
|
143 | 155 | [else #'(ctor-kwd id)])) |
144 | 156 |
|
145 | 157 | (pattern ((id:id default:expr) type:expr (~alt (~optional (~and #:primary-key primary-key)) |
| 158 | + (~optional (~seq #:foreign-key foreign-key:fk)) |
146 | 159 | (~optional (~and #:auto-increment auto-increment)) |
147 | 160 | (~optional (~and #:nullable nullable)) |
148 | 161 | (~optional (~and #:unique unique)) |
|
164 | 177 |
|
165 | 178 | #:with required? #'#f |
166 | 179 | #:with primary-key? (if (attribute primary-key) #'#t #'#f) |
| 180 | + #:with foreign-key? (if (attribute foreign-key) #'#t #'#f) |
| 181 | + #:with foreign-key-ctor-arg (if (attribute foreign-key) |
| 182 | + (attribute foreign-key.ctor-arg) |
| 183 | + #'#f) |
167 | 184 | #:with auto-increment? (if (attribute auto-increment) #'#t #'#f) |
168 | 185 | #:with nullable? (if (attribute nullable) #'#t #'#f) |
169 | 186 | #:with unique? (if (attribute unique) #'#t #'#f) |
|
239 | 256 | f.name |
240 | 257 | f.type |
241 | 258 | f.primary-key? |
| 259 | + f.foreign-key? |
| 260 | + f.foreign-key-ctor-arg |
242 | 261 | f.auto-increment? |
243 | 262 | f.nullable? |
244 | 263 | f.unique? |
|
0 commit comments