All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wtbian.util.SqlParserUtil Maven / Gradle / Ivy

Go to download

The mvc-generator makes it easier to write code with spring-mvc applications. mvc-generator generate code with freemarker template of controller, service and dao etc level object using maven plugin. Simplicity is the biggest advantage of the generator code over generating code tools.

There is a newer version: 1.0.4.1
Show newest version
package com.github.wtbian.util;

import com.github.wtbian.core.Context;
import com.github.wtbian.core.Entity;
import com.github.wtbian.core.Property;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bianwentao on 2019/1/27.
 */
public class SqlParserUtil {
    /**
     * parse sql statement
     * @param sql sql statement
     * @return {@link Context}
     * @throws JSQLParserException
     */
    public static Context parseCreateTable(String sql) throws JSQLParserException {
        Context ctx = Context.getInstance();
        CCJSqlParserManager parser = new CCJSqlParserManager();
        Statement stmt = parser.parse(new StringReader(sql.replace("`","")));
        if (stmt instanceof CreateTable) {
            CreateTable createTable = (CreateTable) stmt;
            List columnDefinitionList = createTable.getColumnDefinitions();
            Table table = createTable.getTable();
            String str = table.getName();
            ctx.setTableName(str);
            List propertyList = new ArrayList();
            for (ColumnDefinition columnDefinition : columnDefinitionList) {
                Property attr = new Property();
                attr.setJavaType(columnDefinition.getColDataType().getDataType());
                attr.setPropertyName(columnDefinition.getColumnName());
                List comments = columnDefinition.getColumnSpecStrings();
                attr.setComment(comments.get(comments.size() - 1).replaceAll("\'",""));
                propertyList.add(attr);
            }
            ctx.setProperties(propertyList);
        }
        return ctx;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy