Bootstrap

JSqlParser生成修改表定义SQL语句

依赖

        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>4.3</version>
        </dependency>

删除表

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.drop.Drop;

public class DropTableDemo {
    public static void main(String[] args) {
        Drop drop = new Drop();
        drop.setIfExists(true);
        drop.setType("TABLE");
        Table table = new Table("t_one");
        drop.setName(table);
        System.out.println(drop);
    }
}

语句:

DROP TABLE IF EXISTS t_one

修改字段定义

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;
import net.sf.jsqlparser.statement.create.table.ColDataType;

import java.util.Collections;

public class AlterTableDemo {
    public static void main(String[] args)throws Exception {
        Alter alter = new Alter();
        alter.setTable(new Table("t_one"));
        // 修改姓名
        ColDataType varchar = new ColDataType("VARCHAR");
        varchar.setArgumentsStringList(Collections.singletonList("128"));
        AlterExpression.ColumnDataType name = new AlterExpression.ColumnDataType("name", false, varchar, Collections.singletonList("COMMENT '道号'"));
        AlterExpression alterExpression = new AlterExpression();
        alterExpression.setOperation(AlterOperation.MODIFY);
        alterExpression.addColDataType(name);
        alter.addAlterExpression(alterExpression);
        // 修改仙龄
        ColDataType anInt = new ColDataType("INT");
        anInt.setArgumentsStringList(Collections.singletonList("10"));
        AlterExpression.ColumnDataType age = new AlterExpression.ColumnDataType("age", false, anInt, Collections.singletonList("COMMENT '仙龄'"));
        AlterExpression ageAlterexpression = new AlterExpression();
        ageAlterexpression.setOperation(AlterOperation.MODIFY);
        ageAlterexpression.addColDataType(age);
        alter.addAlterExpression(ageAlterexpression);
        System.out.println(alter);
    }
}

语句:

ALTER TABLE t_one MODIFY name VARCHAR (128) COMMENT '道号', MODIFY age INT (10) COMMENT '仙龄'

新增字段

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;
import net.sf.jsqlparser.statement.create.table.ColDataType;

import java.util.Collections;

public class AddColumnDemo {
    public static void main(String[] args) {
        Alter alter = new Alter();
        alter.setTable(new Table("t_one"));
        AlterExpression alterExpression = new AlterExpression();
        alterExpression.setOperation(AlterOperation.ADD);
        ColDataType bigint = new ColDataType("BIGINT");
        bigint.setArgumentsStringList(Collections.singletonList("20"));
        AlterExpression.ColumnDataType fightingCapacity = new AlterExpression.ColumnDataType("fighting_capacity", false, bigint, Collections.singletonList("COMMENT '战力'"));
        alterExpression.addColDataType(fightingCapacity);
        alter.addAlterExpression(alterExpression);
        System.out.println(alter);
    }
}

语句:

ALTER TABLE t_one ADD fighting_capacity BIGINT (20) COMMENT '战力'

删除字段

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;


public class DropColumnDemo {
    public static void main(String[] args)throws Exception {
        Alter alter = new Alter();
        alter.setTable(new Table("t_one"));
        AlterExpression alterExpression = new AlterExpression();
        alterExpression.setOperation(AlterOperation.DROP);
        alterExpression.setColumnName("fighting_capacity");
        alter.addAlterExpression(alterExpression);
        System.out.println(alter);
    }
}

语句:

ALTER TABLE t_one DROP fighting_capacity
;