Skip to content

JavaMethod

Yong Zhu edited this page Aug 14, 2018 · 2 revisions

Java method configuration to create DDL

Java method configuration is more flexible than Annotation, because you can use same Java methods to modify TableModel at run-time.

Java method configuration Example:

	@Test
	public void sampleTest() {// An example used to put on README.md
		TableModel t1 = new TableModel("customers");
		t1.column("name").STRING(20).pkey();
		t1.column("email").STRING(20).pkey().entityField("email").updatable(true).insertable(false);
		t1.column("address").VARCHAR(50).defaultValue("'Beijing'").comment("address comment");
		t1.column("phoneNumber").VARCHAR(50).singleIndex("IDX2");
		t1.column("age").INTEGER().notNull().check("'>0'");
		t1.index("idx3").columns("address", "phoneNumber").unique();

		TableModel t2 = new TableModel("orders").comment("order comment");
		t2.column("id").LONG().autoId().pkey();
		t2.column("name").STRING(20);
		t2.column("email").STRING(20);
		t2.column("name2").STRING(20).pkey().tail(" default 'Sam'");
		t2.column("email2").STRING(20);
		t2.fkey().columns("name2", "email2").refs("customers", "name", "email");
		t2.fkey("fk1").columns("name", "email").refs("customers", "name", "email");
		t2.unique("uk1").columns("name2", "email2");

		TableModel t3 = new TableModel("sampletable");
		t3.column("id").LONG().identityId().pkey();
		t3.tableGenerator("table_gen1", "tb1", "pkcol2", "valcol", "pkval", 1, 10);
		t3.column("id1").INTEGER().idGenerator("table_gen1");
		t3.sequenceGenerator("seq1", "seq_1", 1, 1);
		t3.column("id2").INTEGER().idGenerator("seq1");
		t3.engineTail(" DEFAULT CHARSET=utf8");

		String[] dropAndCreateDDL = Dialect.H2Dialect.toDropAndCreateDDL(t1, t2, t3);
		for (String ddl : dropAndCreateDDL)
			System.out.println(ddl);
	}

Explanation:

  • LONG()、STRING()...methods: column type definition:
    Common: BOOLEAN,DOUBLE,FLOAT,INTEGER,LONG(=BIGINT),SHORT(=SMALLINT),BIGDECIMAL(=NUMERIC),STRING(=VARCHAR),DATE,TIME,TIMESTAMP,BIGINT,VARCHAR
    Un-common:BINARY,BIT,BLOB,CHAR,CLOB,DECIMAL,LONGNVARCHAR,LONGVARBINARY,LONGVARCHAR,NCHAR,NCLOB,NUMERIC,NVARCHAR,REAL,SMALLINT,TINYINT,VARBINARY
  • pkey(): Primary key definition.
  • identityId(): Identity column, note some databases do not support identity.
  • unique: Unique constraint definition.
  • index(): Index definition.
  • notNull(): Not null column definition.
  • check(): Check constraint definition.
  • defaultValue(): default value definition.
  • fkey() method: foreign key definition, format: column.fkey(ftable,fcol1, fcol2...).
  • SingleXxx() methods: shortcut method to build foreign key, index, unique constraints but only works for 1 column
  • comment(): comment definition.
  • autoID(): Similar like JPA's @Auto type, in program using dialect.getNextAutoID(connection) to get a Long type ID.
  • tableGenerator() and sequenceGenerator(): Simliar like JPA's @TableGenerator and @SequenceGenerator type.
  • dialect's toDropDDL(), toCreateDDL() and toDropAndCreateDDL() methods: build DDL String Array。DDLFormatter.format() can be used format DDL.
  • toCreateDDL() method will run reserved words checking, if found reserved words like "user"、"order" will throw an Exception.
  • table.engineTail() method: put extra String behind DDL tail only when database(like MySql) support engine。column.tail() add extra String on column definition.
  • FKeyConst and ColumnsModel has tail() method to add extra String piece at end of DDL definition.
  • entityField() method: mark a column be mapped to a Java POJO's field, this is designed for ORM tool

Result of above example:

alter table orders  drop constraint  fk1
alter table orders  drop constraint  fk_orders_name2_email2
drop table tb1 if exists
drop sequence if exists seq_1
drop sequence if exists jdia_seq_autoid
drop table customers if exists
drop table orders if exists
drop table sampletable if exists
create table customers ( name varchar(20),email varchar(20),address varchar(50) default 'Beijing',phoneNumber varchar(50),age integer not null check ('>0'), primary key (name,email))
create  index IDX2 on customers (phoneNumber)
create unique index idx3 on customers (address,phoneNumber)
create table orders ( id bigint,name varchar(20),email varchar(20),name2 varchar(20) default 'Sam',email2 varchar(20), primary key (id,name2))
alter table orders add constraint uk1 unique (name2,email2)
create table sampletable ( id bigint generated by default as identity,id1 integer,id2 integer, primary key (id))
create sequence jdia_seq_autoid start with 1 increment by 1
create sequence seq_1 start with 1 increment by 1
create table tb1 (pkcol2 varchar(100),valcol bigint )
alter table orders  add constraint fk_orders_name2_email2 foreign key (name2,email2) references customers (name,email)
alter table orders  add constraint fk1 foreign key (name,email) references customers (name,email)
Clone this wiki locally