0
# Schema Objects
1
2
Type-safe representations of database schema objects including fields, tables, constraints, and relationships. Provides metadata access and supports both generated and dynamic schema definitions.
3
4
## Capabilities
5
6
### Field Interface
7
8
Represents column expressions and database fields with type safety and extensive operator support.
9
10
```java { .api }
11
public interface Field<T> extends SelectField<T>, GroupField, OrderField<T> {
12
/**
13
* Get the field name
14
* @return Field name as string
15
*/
16
String getName();
17
18
/**
19
* Get the field's Java type
20
* @return Class representing the field type
21
*/
22
Class<T> getType();
23
24
/**
25
* Get the field's data type information
26
* @return DataType with detailed type information
27
*/
28
DataType<T> getDataType();
29
30
/**
31
* Get the field comment
32
* @return Field comment or null
33
*/
34
String getComment();
35
36
/**
37
* Create an alias for this field
38
* @param alias Alias name
39
* @return Field with alias applied
40
*/
41
Field<T> as(String alias);
42
43
/**
44
* Create an alias using a Name object
45
* @param alias Alias as Name object
46
* @return Field with alias applied
47
*/
48
Field<T> as(Name alias);
49
50
/**
51
* Create an equality condition
52
* @param value Value to compare with
53
* @return Condition representing field = value
54
*/
55
Condition eq(T value);
56
57
/**
58
* Create a not-equal condition
59
* @param value Value to compare with
60
* @return Condition representing field <> value
61
*/
62
Condition ne(T value);
63
64
/**
65
* Create an IN condition with multiple values
66
* @param values Values for IN clause
67
* @return Condition representing field IN (values)
68
*/
69
Condition in(T... values);
70
71
/**
72
* Create a NOT IN condition
73
* @param values Values for NOT IN clause
74
* @return Condition representing field NOT IN (values)
75
*/
76
Condition notIn(T... values);
77
78
/**
79
* Create an IN condition with subquery
80
* @param select Subquery providing values
81
* @return Condition representing field IN (subquery)
82
*/
83
Condition in(Select<? extends Record1<T>> select);
84
85
/**
86
* Create an ascending sort field
87
* @return SortField for ascending order
88
*/
89
SortField<T> asc();
90
91
/**
92
* Create a descending sort field
93
* @return SortField for descending order
94
*/
95
SortField<T> desc();
96
}
97
```
98
99
### Numeric Field Operations
100
101
Additional operations available for numeric fields.
102
103
```java { .api }
104
public interface Field<T extends Number> {
105
/**
106
* Create a greater-than condition
107
* @param value Value to compare with
108
* @return Condition representing field > value
109
*/
110
Condition gt(T value);
111
112
/**
113
* Create a greater-than-or-equal condition
114
* @param value Value to compare with
115
* @return Condition representing field >= value
116
*/
117
Condition ge(T value);
118
119
/**
120
* Create a less-than condition
121
* @param value Value to compare with
122
* @return Condition representing field < value
123
*/
124
Condition lt(T value);
125
126
/**
127
* Create a less-than-or-equal condition
128
* @param value Value to compare with
129
* @return Condition representing field <= value
130
*/
131
Condition le(T value);
132
133
/**
134
* Create a BETWEEN condition
135
* @param minValue Minimum value (inclusive)
136
* @param maxValue Maximum value (inclusive)
137
* @return Condition representing field BETWEEN min AND max
138
*/
139
Condition between(T minValue, T maxValue);
140
141
/**
142
* Add this field to another
143
* @param value Value to add
144
* @return Field representing field + value
145
*/
146
Field<T> add(T value);
147
148
/**
149
* Subtract a value from this field
150
* @param value Value to subtract
151
* @return Field representing field - value
152
*/
153
Field<T> sub(T value);
154
155
/**
156
* Multiply this field by a value
157
* @param value Value to multiply by
158
* @return Field representing field * value
159
*/
160
Field<T> mul(T value);
161
162
/**
163
* Divide this field by a value
164
* @param value Value to divide by
165
* @return Field representing field / value
166
*/
167
Field<T> div(T value);
168
}
169
```
170
171
### String Field Operations
172
173
Additional operations available for string fields.
174
175
```java { .api }
176
public interface Field<String> {
177
/**
178
* Create a LIKE condition
179
* @param pattern Pattern for LIKE comparison
180
* @return Condition representing field LIKE pattern
181
*/
182
Condition like(String pattern);
183
184
/**
185
* Create a NOT LIKE condition
186
* @param pattern Pattern for NOT LIKE comparison
187
* @return Condition representing field NOT LIKE pattern
188
*/
189
Condition notLike(String pattern);
190
191
/**
192
* Create a case-insensitive LIKE condition
193
* @param pattern Pattern for ILIKE comparison
194
* @return Condition representing field ILIKE pattern
195
*/
196
Condition likeIgnoreCase(String pattern);
197
198
/**
199
* Create a SIMILAR TO condition (regex-like)
200
* @param pattern Regular expression pattern
201
* @return Condition representing field SIMILAR TO pattern
202
*/
203
Condition similarTo(String pattern);
204
205
/**
206
* Concatenate this field with another value
207
* @param value Value to concatenate
208
* @return Field representing field || value
209
*/
210
Field<String> concat(String value);
211
212
/**
213
* Create an uppercase version of this field
214
* @return Field representing UPPER(field)
215
*/
216
Field<String> upper();
217
218
/**
219
* Create a lowercase version of this field
220
* @return Field representing LOWER(field)
221
*/
222
Field<String> lower();
223
224
/**
225
* Trim whitespace from this field
226
* @return Field representing TRIM(field)
227
*/
228
Field<String> trim();
229
}
230
```
231
232
**Usage Examples:**
233
234
```java
235
// Field comparisons and conditions
236
Condition authorFilter = AUTHOR.FIRST_NAME.eq("John")
237
.and(AUTHOR.LAST_NAME.like("D%"))
238
.and(AUTHOR.BIRTH_YEAR.between(1950, 1990));
239
240
// Numeric operations
241
Field<Integer> adjustedPages = BOOK.PAGES.add(50).mul(2);
242
Select<Record2<String, Integer>> query = create
243
.select(BOOK.TITLE, adjustedPages.as("adjusted_pages"))
244
.from(BOOK);
245
246
// String operations
247
Select<Record1<String>> authors = create
248
.select(AUTHOR.FIRST_NAME.concat(" ").concat(AUTHOR.LAST_NAME).as("full_name"))
249
.from(AUTHOR)
250
.where(AUTHOR.LAST_NAME.upper().like("SMITH%"));
251
252
// Field aliases and sorting
253
Result<Record> result = create
254
.select(
255
BOOK.TITLE.as("book_title"),
256
BOOK.PAGES.as("page_count")
257
)
258
.from(BOOK)
259
.orderBy(BOOK.PAGES.desc(), BOOK.TITLE.asc())
260
.fetch();
261
```
262
263
### Table Interface
264
265
Represents database tables with metadata and relationship information.
266
267
```java { .api }
268
public interface Table<R extends Record> extends TableLike<R> {
269
/**
270
* Get the table's record type information
271
* @return RecordType describing the table's structure
272
*/
273
RecordType<R> recordType();
274
275
/**
276
* Get the table's schema
277
* @return Schema containing this table
278
*/
279
Schema getSchema();
280
281
/**
282
* Get the table name
283
* @return Table name as string
284
*/
285
String getName();
286
287
/**
288
* Get the table comment
289
* @return Table comment or null
290
*/
291
String getComment();
292
293
/**
294
* Get the primary key constraint
295
* @return UniqueKey representing the primary key
296
*/
297
UniqueKey<R> getPrimaryKey();
298
299
/**
300
* Get all unique key constraints
301
* @return List of unique keys including primary key
302
*/
303
List<UniqueKey<R>> getUniqueKeys();
304
305
/**
306
* Get all foreign key references from this table
307
* @return List of foreign keys referencing other tables
308
*/
309
List<ForeignKey<R, ?>> getReferences();
310
311
/**
312
* Get all foreign keys referencing this table
313
* @return List of foreign keys from other tables
314
*/
315
List<ForeignKey<?, R>> getReferencedBy();
316
317
/**
318
* Get the identity column (auto-increment)
319
* @return Identity representing the auto-increment column
320
*/
321
Identity<R, ?> getIdentity();
322
323
/**
324
* Get all indexes on this table
325
* @return List of indexes
326
*/
327
List<Index> getIndexes();
328
329
/**
330
* Create an alias for this table
331
* @param alias Alias name
332
* @return Table with alias applied
333
*/
334
Table<R> as(String alias);
335
336
/**
337
* Create an alias with field aliases
338
* @param alias Table alias name
339
* @param fieldAliases Aliases for the table's fields
340
* @return Table with table and field aliases applied
341
*/
342
Table<R> as(String alias, String... fieldAliases);
343
344
/**
345
* Get a field from this table by name
346
* @param name Field name
347
* @return Field from this table
348
*/
349
Field<?> field(String name);
350
351
/**
352
* Get a field from this table by index
353
* @param index Field index (0-based)
354
* @return Field at the specified index
355
*/
356
Field<?> field(int index);
357
}
358
```
359
360
**Usage Examples:**
361
362
```java
363
// Table metadata access
364
Table<AuthorRecord> authors = AUTHOR;
365
Schema schema = authors.getSchema();
366
String tableName = authors.getName();
367
UniqueKey<AuthorRecord> pk = authors.getPrimaryKey();
368
List<ForeignKey<AuthorRecord, ?>> fks = authors.getReferences();
369
370
// Table aliases
371
Table<AuthorRecord> a = AUTHOR.as("a");
372
Table<BookRecord> b = BOOK.as("b");
373
374
Result<Record> result = create
375
.select()
376
.from(a)
377
.join(b).on(a.field("ID").eq(b.field("AUTHOR_ID")))
378
.fetch();
379
380
// Dynamic field access
381
Field<?> nameField = AUTHOR.field("FIRST_NAME");
382
Field<?> firstField = AUTHOR.field(0);
383
```
384
385
### Schema Interface
386
387
Represents database schemas containing tables, procedures, and other objects.
388
389
```java { .api }
390
public interface Schema extends Named {
391
/**
392
* Get all tables in this schema
393
* @return List of tables
394
*/
395
List<Table<?>> getTables();
396
397
/**
398
* Get a table by name
399
* @param name Table name
400
* @return Table with the specified name
401
*/
402
Table<Record> getTable(String name);
403
404
/**
405
* Get all sequences in this schema
406
* @return List of sequences
407
*/
408
List<Sequence<?>> getSequences();
409
410
/**
411
* Get all user-defined types in this schema
412
* @return List of UDTs
413
*/
414
List<UDT<?>> getUDTs();
415
416
/**
417
* Get the catalog containing this schema
418
* @return Catalog or null for default
419
*/
420
Catalog getCatalog();
421
}
422
```
423
424
### Field and Table Creation
425
426
Static methods for creating fields and tables dynamically.
427
428
```java { .api }
429
/**
430
* Create a field from a string name
431
* @param name Field name
432
* @return Field with Object type
433
*/
434
public static Field<Object> field(String name);
435
436
/**
437
* Create a typed field from a string name
438
* @param name Field name
439
* @param type Field type class
440
* @return Field with specified type
441
*/
442
public static <T> Field<T> field(String name, Class<T> type);
443
444
/**
445
* Create a field from a Name object
446
* @param name Field name as Name
447
* @param type Field data type
448
* @return Field with specified name and type
449
*/
450
public static <T> Field<T> field(Name name, DataType<T> type);
451
452
/**
453
* Create a table from a string name
454
* @param name Table name
455
* @return Table with Record type
456
*/
457
public static Table<Record> table(String name);
458
459
/**
460
* Create a table from a Name object
461
* @param name Table name as Name
462
* @return Table with Record type
463
*/
464
public static Table<Record> table(Name name);
465
466
/**
467
* Create a derived table from a SELECT
468
* @param select SELECT statement
469
* @return Table derived from the SELECT
470
*/
471
public static Table<Record> table(Select<?> select);
472
```
473
474
**Usage Examples:**
475
476
```java
477
// Dynamic field and table creation
478
Field<String> nameField = field("name", String.class);
479
Field<Integer> ageField = field("age", Integer.class);
480
Table<Record> usersTable = table("users");
481
482
// Query with dynamic objects
483
Result<Record> result = create
484
.select(nameField, ageField)
485
.from(usersTable)
486
.where(ageField.gt(18))
487
.fetch();
488
489
// Derived table from subquery
490
Table<Record> youngAuthors = table(
491
select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
492
.from(AUTHOR)
493
.where(AUTHOR.DATE_OF_BIRTH.gt(LocalDate.of(1980, 1, 1)))
494
).as("young_authors");
495
496
Result<Record> result = create
497
.select()
498
.from(youngAuthors)
499
.fetch();
500
```
501
502
## Data Types
503
504
```java { .api }
505
public interface DataType<T> {
506
/**
507
* Get the SQL data type name
508
* @return SQL type name (e.g., "VARCHAR", "INTEGER")
509
*/
510
String getTypeName();
511
512
/**
513
* Get the Java type class
514
* @return Java class for this data type
515
*/
516
Class<T> getType();
517
518
/**
519
* Check if this type is nullable
520
* @return true if NULL values are allowed
521
*/
522
boolean nullable();
523
524
/**
525
* Create a non-nullable version of this type
526
* @return DataType that doesn't allow NULL
527
*/
528
DataType<T> notNull();
529
530
/**
531
* Get the default value for this type
532
* @return Default value or null
533
*/
534
T getDefaultValue();
535
}
536
```