
v.java-api-for-mysql.1.5.1.source-code.module-info Maven / Gradle / Ivy
Show all versions of java-api-for-mysql Show documentation
/*
* Copyright (c) Petr Panteleyev. All rights reserved.
* Licensed under the BSD license. See LICENSE file in the project root for full license information.
*/
import org.panteleyev.mysqlapi.annotations.Column;
import org.panteleyev.mysqlapi.annotations.Table;
import org.panteleyev.mysqlapi.annotations.RecordBuilder;
import org.panteleyev.mysqlapi.TableRecord;
import org.panteleyev.mysqlapi.MySqlClient;
/**
Persistence library provides annotation classes for database access.
Database
Database manipulation is beyond the scope of this API. Calling code must supply correct {@link javax.sql.DataSource}
and ensure database does exist and proper access control is established.
Table
Class implementing database table is defined by the annotation {@link Table}.
Such class must also implement interface {@link TableRecord}.
API currently supports the following primary key types:
- {@link java.lang.Integer}, int
- {@link java.lang.Long}, long
- {@link java.lang.String}
- {@link java.util.UUID}
In case of {@link java.lang.Integer} one may use {@link MySqlClient#generatePrimaryKey} to generate unique values for the
appropriate table classes. Also make sure that application calls {@link MySqlClient#preload} first.
Deserialization
API supports two ways of object deserialization: by constructor and direct field assignment. Constructor must be
used for objects with final fields or in case of additional initialization.
Field Assignment
There must be no-argument constructor, either default or explicit. Setters are not used, i.e. there is no way
to define additional deserialization logic in case of field assignment.
{@literal @}Table("book")
class Book implements Record {
{@literal @}PrimaryKey
{@literal @}Column(Column.ID)
private int id;
{@literal @}Column("title")
private String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title
}
public void setTitle(String title) {
this.title = title;
}
}
Constructor
Constructor deserialization is triggered by {@link RecordBuilder}
as shown below. Such constructor must have parameters corresponding to table columns.
{@literal @}Table("book")
class Book implements TableRecord {
{@literal @}PrimaryKey
{@literal @}Column(Field.ID)
private final int id;
{@literal @}Column("title")
private final String title;
{@literal @}RecordBuilder
public Book ({@literal @}Column(Column.ID) int id, {@literal @}Column("title") String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
}
Java Records
Java records deserialization is always performed by means of canonical constructor. {@link RecordBuilder}
annotation is ignored even if it is present on any of the available constructors.
All record components must be annotated with {@link Column}, i.e., record must be an exact representation of the
database table.
{@literal @}Table("book")
record Book(
{@literal @}PrimaryKey
{@literal @}Column(Field.ID)
int id,
{@literal @}Column("title")
String title
) implements TableRecord {
}
Data Types
The following data types are supported:
Data Types
Java MySQL Comment
int
{@link java.lang.Integer} INTEGER
long
{@link java.lang.Long} BIGINT
boolean
{@link java.lang.Boolean} BOOLEAN
{@link java.lang.String}
VARCHAR ( {@link Column#length} )
{@link java.lang.String} with {@link Column#isJson()} = true
JSON
{@link java.math.BigDecimal}
DECIMAL ( {@link Column#precision}, {@link Column#scale} )
MySQL representation does not guarantee that retrieved value will be equal to original one by means of
{@link java.lang.Object#equals}. Use {@link java.math.BigDecimal#compareTo} instead.
{@link java.util.Date}
BIGINT
Dates are stored as long using {@link java.util.Date#getTime}
{@link java.time.LocalDate}
BIGINT
Local dates are stored as long using {@link java.time.LocalDate#toEpochDay}
byte[]
VARBINARY ( {@link Column#length} )
{@link java.util.UUID}
BINARY(16) or VARCHAR(36) depending on {@link Column#storeUuidAsBinary()}
The following conversion functions are used between string and binary representation:
BIN_TO_UUID()
and UUID_TO_BIN()
Indexes and Foreign Keys
{@literal @}Table("parent_table")
public class ParentTable implements TableRecord {
{@literal @}Column("data")
{@literal @}Index(value = "data", unique = true)
private String data;
public String getData() {
return data;
}
}
This will produce the following SQL for indexed column:
CREATE UNIQUE INDEX data ON parent_table(data)
{@literal @}Table("child_table")
public class ChildTable implements TableRecord {
{@literal @}Column("parent_data")
{@literal @}ForeignKey(table = ParentTable.class, column = "data",
onDelete = ReferenceOption.RESTRICT, onUpdate = ReferenceOption.CASCADE)
private final String parentData;
public String getParentData() {
return parentData;
}
}
This will produce the following SQL for the foreign key:
CREATE FOREIGN KEY(parent_data) REFERENCES parent_table(data) ON DELETE RESTRICT ON UPDATE CASCADE
@see MySQL Data Types
*/
open module org.panteleyev.mysqlapi {
requires java.base;
requires java.sql;
requires java.naming;
exports org.panteleyev.mysqlapi;
exports org.panteleyev.mysqlapi.annotations;
}