一、前言
关于jsqlparser(0.7.0)源码包中通过定义数据SQL的DDL/DML/DQL语句net.sf.jsqlparser.statement接口,分别创建表实现类(net.sf.jsqlparser.statement.create.table.CreateTable)、删除表结构(net.sf.jsqlparser.statement.drop.Drop)、表数据的增删改查(net.sf.jsqlparser.statement.insert.Insert/net.sf.jsqlparser.statement.delete.Delete/net.sf.jsqlparser.statement.update.Update/net.sf.jsqlparser.statement.select.Select),并通过解析工具类对操作进行对于解析处理(net.sf.jsqlparser.util.deparser.CreateTableDeParser/DeleteDeParser/InsertDeParser/SelectDeParser/UpdateDeParser等)。
二、源码说明
1. 操作语句接口
package net.sf.jsqlparser.statement;@b@@b@public abstract interface Statement@b@{@b@ public abstract void accept(StatementVisitor paramStatementVisitor);@b@}
2. 表结构&数据操作接口实现类
package net.sf.jsqlparser.statement.create.table;@b@@b@import java.util.List;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.Statement;@b@import net.sf.jsqlparser.statement.StatementVisitor;@b@import net.sf.jsqlparser.statement.select.PlainSelect;@b@@b@public class CreateTable@b@ implements Statement@b@{@b@ private Table table;@b@ private List tableOptionsStrings;@b@ private List columnDefinitions;@b@ private List indexes;@b@@b@ public void accept(StatementVisitor statementVisitor)@b@ {@b@ statementVisitor.visit(this);@b@ }@b@@b@ public Table getTable()@b@ {@b@ return this.table;@b@ }@b@@b@ public void setTable(Table table) {@b@ this.table = table;@b@ }@b@@b@ public List getColumnDefinitions()@b@ {@b@ return this.columnDefinitions;@b@ }@b@@b@ public void setColumnDefinitions(List list) {@b@ this.columnDefinitions = list;@b@ }@b@@b@ public List getTableOptionsStrings()@b@ {@b@ return this.tableOptionsStrings;@b@ }@b@@b@ public void setTableOptionsStrings(List list) {@b@ this.tableOptionsStrings = list;@b@ }@b@@b@ public List getIndexes()@b@ {@b@ return this.indexes;@b@ }@b@@b@ public void setIndexes(List list) {@b@ this.indexes = list;@b@ }@b@@b@ public String toString() {@b@ String sql = "";@b@@b@ sql = "CREATE TABLE " + this.table + " (";@b@@b@ sql = sql + PlainSelect.getStringList(this.columnDefinitions, true, false);@b@ if ((this.indexes != null) && (this.indexes.size() != 0)) {@b@ sql = sql + ", ";@b@ sql = sql + PlainSelect.getStringList(this.indexes);@b@ }@b@ sql = sql + ") ";@b@ sql = sql + PlainSelect.getStringList(this.tableOptionsStrings, false, false);@b@@b@ return sql;@b@ }@b@}
package net.sf.jsqlparser.statement.drop;@b@@b@import java.util.List;@b@import net.sf.jsqlparser.statement.Statement;@b@import net.sf.jsqlparser.statement.StatementVisitor;@b@import net.sf.jsqlparser.statement.select.PlainSelect;@b@@b@public class Drop@b@ implements Statement@b@{@b@ private String type;@b@ private String name;@b@ private List parameters;@b@@b@ public void accept(StatementVisitor statementVisitor)@b@ {@b@ statementVisitor.visit(this);@b@ }@b@@b@ public String getName() {@b@ return this.name;@b@ }@b@@b@ public List getParameters() {@b@ return this.parameters;@b@ }@b@@b@ public String getType() {@b@ return this.type;@b@ }@b@@b@ public void setName(String string) {@b@ this.name = string;@b@ }@b@@b@ public void setParameters(List list) {@b@ this.parameters = list;@b@ }@b@@b@ public void setType(String string) {@b@ this.type = string;@b@ }@b@@b@ public String toString() {@b@ String sql = "DROP " + this.type + " " + this.name;@b@@b@ if ((this.parameters != null) && (this.parameters.size() > 0)) {@b@ sql = sql + " " + PlainSelect.getStringList(this.parameters);@b@ }@b@@b@ return sql;@b@ }@b@}
package net.sf.jsqlparser.statement.insert;@b@@b@import java.util.List;@b@import net.sf.jsqlparser.expression.operators.relational.ItemsList;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.Statement;@b@import net.sf.jsqlparser.statement.StatementVisitor;@b@import net.sf.jsqlparser.statement.select.PlainSelect;@b@@b@public class Insert@b@ implements Statement@b@{@b@ private Table table;@b@ private List columns;@b@ private ItemsList itemsList;@b@ private boolean useValues;@b@@b@ public Insert()@b@ {@b@ this.useValues = true; }@b@@b@ public void accept(StatementVisitor statementVisitor) {@b@ statementVisitor.visit(this);@b@ }@b@@b@ public Table getTable() {@b@ return this.table;@b@ }@b@@b@ public void setTable(Table name) {@b@ this.table = name;@b@ }@b@@b@ public List getColumns()@b@ {@b@ return this.columns;@b@ }@b@@b@ public void setColumns(List list) {@b@ this.columns = list;@b@ }@b@@b@ public ItemsList getItemsList()@b@ {@b@ return this.itemsList;@b@ }@b@@b@ public void setItemsList(ItemsList list) {@b@ this.itemsList = list;@b@ }@b@@b@ public boolean isUseValues() {@b@ return this.useValues;@b@ }@b@@b@ public void setUseValues(boolean useValues) {@b@ this.useValues = useValues;@b@ }@b@@b@ public String toString() {@b@ String sql = "";@b@@b@ sql = "INSERT INTO ";@b@ sql = new StringBuilder().append(sql).append(this.table).append(" ").toString();@b@ sql = new StringBuilder().append(sql).append((this.columns != null) ? new StringBuilder().append(PlainSelect.getStringList(this.columns, true, true)).append(" ").toString() : "").toString();@b@@b@ if (this.useValues)@b@ sql = new StringBuilder().append(sql).append("VALUES ").append(this.itemsList).append("").toString();@b@ else {@b@ sql = new StringBuilder().append(sql).append("").append(this.itemsList).append("").toString();@b@ }@b@@b@ return sql;@b@ }@b@}
package net.sf.jsqlparser.statement.delete;@b@@b@import net.sf.jsqlparser.expression.Expression;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.Statement;@b@import net.sf.jsqlparser.statement.StatementVisitor;@b@@b@public class Delete@b@ implements Statement@b@{@b@ private Table table;@b@ private Expression where;@b@@b@ public void accept(StatementVisitor statementVisitor)@b@ {@b@ statementVisitor.visit(this);@b@ }@b@@b@ public Table getTable() {@b@ return this.table;@b@ }@b@@b@ public Expression getWhere() {@b@ return this.where;@b@ }@b@@b@ public void setTable(Table name) {@b@ this.table = name;@b@ }@b@@b@ public void setWhere(Expression expression) {@b@ this.where = expression;@b@ }@b@@b@ public String toString() {@b@ return new StringBuilder().append("DELETE FROM ").append(this.table).append((this.where != null) ? new StringBuilder().append(" WHERE ").append(this.where).toString() : "").toString();@b@ }@b@}
package net.sf.jsqlparser.statement.update;@b@@b@import java.util.List;@b@import net.sf.jsqlparser.expression.Expression;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.Statement;@b@import net.sf.jsqlparser.statement.StatementVisitor;@b@@b@public class Update@b@ implements Statement@b@{@b@ private Table table;@b@ private Expression where;@b@ private List columns;@b@ private List expressions;@b@@b@ public void accept(StatementVisitor statementVisitor)@b@ {@b@ statementVisitor.visit(this);@b@ }@b@@b@ public Table getTable() {@b@ return this.table;@b@ }@b@@b@ public Expression getWhere() {@b@ return this.where;@b@ }@b@@b@ public void setTable(Table name) {@b@ this.table = name;@b@ }@b@@b@ public void setWhere(Expression expression) {@b@ this.where = expression;@b@ }@b@@b@ public List getColumns()@b@ {@b@ return this.columns;@b@ }@b@@b@ public List getExpressions()@b@ {@b@ return this.expressions;@b@ }@b@@b@ public void setColumns(List list) {@b@ this.columns = list;@b@ }@b@@b@ public void setExpressions(List list) {@b@ this.expressions = list;@b@ }@b@}
package net.sf.jsqlparser.statement.select;@b@@b@import java.util.Iterator;@b@import java.util.List;@b@import net.sf.jsqlparser.statement.Statement;@b@import net.sf.jsqlparser.statement.StatementVisitor;@b@@b@public class Select@b@ implements Statement@b@{@b@ private SelectBody selectBody;@b@ private List withItemsList;@b@@b@ public void accept(StatementVisitor statementVisitor)@b@ {@b@ statementVisitor.visit(this);@b@ }@b@@b@ public SelectBody getSelectBody() {@b@ return this.selectBody;@b@ }@b@@b@ public void setSelectBody(SelectBody body) {@b@ this.selectBody = body;@b@ }@b@@b@ public String toString() {@b@ Iterator iter;@b@ StringBuffer retval = new StringBuffer();@b@ if ((this.withItemsList != null) && (!(this.withItemsList.isEmpty()))) {@b@ retval.append("WITH ");@b@ for (iter = this.withItemsList.iterator(); iter.hasNext(); ) {@b@ WithItem withItem = (WithItem)iter.next();@b@ retval.append(withItem);@b@ if (iter.hasNext())@b@ retval.append(",");@b@ retval.append(" ");@b@ }@b@ }@b@ retval.append(this.selectBody);@b@ return retval.toString();@b@ }@b@@b@ public List getWithItemsList() {@b@ return this.withItemsList;@b@ }@b@@b@ public void setWithItemsList(List withItemsList) {@b@ this.withItemsList = withItemsList;@b@ }@b@}
3. 解析语句工具类
package net.sf.jsqlparser.util.deparser;@b@@b@import java.util.Iterator;@b@import java.util.List;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.create.table.ColDataType;@b@import net.sf.jsqlparser.statement.create.table.ColumnDefinition;@b@import net.sf.jsqlparser.statement.create.table.CreateTable;@b@import net.sf.jsqlparser.statement.create.table.Index;@b@@b@public class CreateTableDeParser@b@{@b@ protected StringBuffer buffer;@b@@b@ public CreateTableDeParser(StringBuffer buffer)@b@ {@b@ this.buffer = buffer;@b@ }@b@@b@ public void deParse(CreateTable createTable) {@b@ this.buffer.append("CREATE TABLE " + createTable.getTable().getWholeTableName());@b@ if (createTable.getColumnDefinitions() != null) {@b@ Iterator iterator;@b@ this.buffer.append(" { ");@b@ for (Iterator iter = createTable.getColumnDefinitions().iterator(); iter.hasNext(); ) {@b@ ColumnDefinition columnDefinition = (ColumnDefinition)iter.next();@b@ this.buffer.append(columnDefinition.getColumnName());@b@ this.buffer.append(" ");@b@ this.buffer.append(columnDefinition.getColDataType().getDataType());@b@ if (columnDefinition.getColDataType().getArgumentsStringList() != null)@b@ for (iterator = columnDefinition.getColDataType().getArgumentsStringList().iterator(); iterator.hasNext(); ) {@b@ this.buffer.append(" ");@b@ this.buffer.append((String)iterator.next());@b@ }@b@@b@ if (columnDefinition.getColumnSpecStrings() != null)@b@ for (iterator = columnDefinition.getColumnSpecStrings().iterator(); iterator.hasNext(); ) {@b@ this.buffer.append(" ");@b@ this.buffer.append((String)iterator.next());@b@ }@b@@b@@b@ if (iter.hasNext())@b@ this.buffer.append(",\n");@b@@b@ }@b@@b@ for (iter = createTable.getIndexes().iterator(); iter.hasNext(); ) {@b@ this.buffer.append(",\n");@b@ Index index = (Index)iter.next();@b@ this.buffer.append(index.getType() + " " + index.getName());@b@ this.buffer.append("(");@b@ iterator = index.getColumnsNames().iterator();@b@ while (true) { do { if (!(iterator.hasNext())) break label410;@b@ this.buffer.append((String)iterator.next()); }@b@ while (!(iterator.hasNext()));@b@ this.buffer.append(", ");@b@ }@b@@b@ this.buffer.append(")");@b@@b@ if (iter.hasNext())@b@ this.buffer.append(",\n");@b@ }@b@@b@ label410: this.buffer.append(" \n} ");@b@ }@b@ }@b@@b@ public StringBuffer getBuffer() {@b@ return this.buffer;@b@ }@b@@b@ public void setBuffer(StringBuffer buffer) {@b@ this.buffer = buffer;@b@ }@b@}
package net.sf.jsqlparser.util.deparser;@b@@b@import java.util.Iterator;@b@import java.util.List;@b@import net.sf.jsqlparser.expression.Expression;@b@import net.sf.jsqlparser.expression.ExpressionVisitor;@b@import net.sf.jsqlparser.expression.operators.relational.ExpressionList;@b@import net.sf.jsqlparser.expression.operators.relational.ItemsList;@b@import net.sf.jsqlparser.expression.operators.relational.ItemsListVisitor;@b@import net.sf.jsqlparser.schema.Column;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.insert.Insert;@b@import net.sf.jsqlparser.statement.select.SelectBody;@b@import net.sf.jsqlparser.statement.select.SelectVisitor;@b@import net.sf.jsqlparser.statement.select.SubSelect;@b@@b@public class InsertDeParser@b@ implements ItemsListVisitor@b@{@b@ protected StringBuffer buffer;@b@ protected ExpressionVisitor expressionVisitor;@b@ protected SelectVisitor selectVisitor;@b@@b@ public InsertDeParser()@b@ {@b@ }@b@@b@ public InsertDeParser(ExpressionVisitor expressionVisitor, SelectVisitor selectVisitor, StringBuffer buffer)@b@ {@b@ this.buffer = buffer;@b@ this.expressionVisitor = expressionVisitor;@b@ this.selectVisitor = selectVisitor;@b@ }@b@@b@ public StringBuffer getBuffer() {@b@ return this.buffer;@b@ }@b@@b@ public void setBuffer(StringBuffer buffer) {@b@ this.buffer = buffer;@b@ }@b@@b@ public void deParse(Insert insert) {@b@ this.buffer.append("INSERT INTO ");@b@ this.buffer.append(insert.getTable().getWholeTableName());@b@ if (insert.getColumns() != null) {@b@ this.buffer.append("(");@b@ for (Iterator iter = insert.getColumns().iterator(); iter.hasNext(); ) {@b@ Column column = (Column)iter.next();@b@ this.buffer.append(column.getColumnName());@b@ if (iter.hasNext())@b@ this.buffer.append(", ");@b@ }@b@@b@ this.buffer.append(")");@b@ }@b@@b@ insert.getItemsList().accept(this);@b@ }@b@@b@ public void visit(ExpressionList expressionList)@b@ {@b@ this.buffer.append(" VALUES (");@b@ for (Iterator iter = expressionList.getExpressions().iterator(); iter.hasNext(); ) {@b@ Expression expression = (Expression)iter.next();@b@ expression.accept(this.expressionVisitor);@b@ if (iter.hasNext())@b@ this.buffer.append(", ");@b@ }@b@ this.buffer.append(")");@b@ }@b@@b@ public void visit(SubSelect subSelect) {@b@ subSelect.getSelectBody().accept(this.selectVisitor); }@b@@b@ public ExpressionVisitor getExpressionVisitor() {@b@ return this.expressionVisitor;@b@ }@b@@b@ public SelectVisitor getSelectVisitor() {@b@ return this.selectVisitor;@b@ }@b@@b@ public void setExpressionVisitor(ExpressionVisitor visitor) {@b@ this.expressionVisitor = visitor;@b@ }@b@@b@ public void setSelectVisitor(SelectVisitor visitor) {@b@ this.selectVisitor = visitor;@b@ }@b@}
package net.sf.jsqlparser.util.deparser;@b@@b@import net.sf.jsqlparser.expression.Expression;@b@import net.sf.jsqlparser.expression.ExpressionVisitor;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.delete.Delete;@b@@b@public class DeleteDeParser@b@{@b@ protected StringBuffer buffer;@b@ protected ExpressionVisitor expressionVisitor;@b@@b@ public DeleteDeParser()@b@ {@b@ }@b@@b@ public DeleteDeParser(ExpressionVisitor expressionVisitor, StringBuffer buffer)@b@ {@b@ this.buffer = buffer;@b@ this.expressionVisitor = expressionVisitor;@b@ }@b@@b@ public StringBuffer getBuffer() {@b@ return this.buffer;@b@ }@b@@b@ public void setBuffer(StringBuffer buffer) {@b@ this.buffer = buffer;@b@ }@b@@b@ public void deParse(Delete delete) {@b@ this.buffer.append("DELETE FROM " + delete.getTable().getWholeTableName());@b@ if (delete.getWhere() != null) {@b@ this.buffer.append(" WHERE ");@b@ delete.getWhere().accept(this.expressionVisitor);@b@ }@b@ }@b@@b@ public ExpressionVisitor getExpressionVisitor() {@b@ return this.expressionVisitor;@b@ }@b@@b@ public void setExpressionVisitor(ExpressionVisitor visitor) {@b@ this.expressionVisitor = visitor;@b@ }@b@}
package net.sf.jsqlparser.util.deparser;@b@@b@import java.util.List;@b@import net.sf.jsqlparser.expression.Expression;@b@import net.sf.jsqlparser.expression.ExpressionVisitor;@b@import net.sf.jsqlparser.schema.Column;@b@import net.sf.jsqlparser.schema.Table;@b@import net.sf.jsqlparser.statement.update.Update;@b@@b@public class UpdateDeParser@b@{@b@ protected StringBuffer buffer;@b@ protected ExpressionVisitor expressionVisitor;@b@@b@ public UpdateDeParser()@b@ {@b@ }@b@@b@ public UpdateDeParser(ExpressionVisitor expressionVisitor, StringBuffer buffer)@b@ {@b@ this.buffer = buffer;@b@ this.expressionVisitor = expressionVisitor;@b@ }@b@@b@ public StringBuffer getBuffer() {@b@ return this.buffer;@b@ }@b@@b@ public void setBuffer(StringBuffer buffer) {@b@ this.buffer = buffer;@b@ }@b@@b@ public void deParse(Update update) {@b@ this.buffer.append("UPDATE " + update.getTable().getWholeTableName() + " SET ");@b@ for (int i = 0; i < update.getColumns().size(); ++i) {@b@ Column column = (Column)update.getColumns().get(i);@b@ this.buffer.append(column.getWholeColumnName() + "=");@b@@b@ Expression expression = (Expression)update.getExpressions().get(i);@b@ expression.accept(this.expressionVisitor);@b@ if (i < update.getColumns().size() - 1) {@b@ this.buffer.append(", ");@b@ }@b@@b@ }@b@@b@ if (update.getWhere() != null) {@b@ this.buffer.append(" WHERE ");@b@ update.getWhere().accept(this.expressionVisitor);@b@ }@b@ }@b@@b@ public ExpressionVisitor getExpressionVisitor()@b@ {@b@ return this.expressionVisitor;@b@ }@b@@b@ public void setExpressionVisitor(ExpressionVisitor visitor) {@b@ this.expressionVisitor = visitor;@b@ }@b@}