本文整理了Java中net.sf.jsqlparser.statement.select.Select
类的一些代码示例,展示了Select
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Select
类的具体详情如下:
包路径:net.sf.jsqlparser.statement.select.Select
类名称:Select
暂无
代码示例来源:origin: pagehelper/Mybatis-PageHelper
SelectBody selectBody = select.getSelectBody();
if (selectBody instanceof SetOperationList) {
selectBody = wrapSetOperationList((SetOperationList) selectBody);
if (((PlainSelect) selectBody).getTop() != null) {
throw new PageException("被分页的语句已经包含了Top,不能再通过分页插件进行分页查询!");
SelectItem orderByColumn = addRowNumber((PlainSelect) selectBody, autoItems);
((PlainSelect) selectBody).addSelectItems(autoItems.toArray(new SelectItem[autoItems.size()]));
PlainSelect innerSelectBody = new PlainSelect();
innerSelectBody.addSelectItems(orderByColumn);
Select newSelect = new Select();
PlainSelect newSelectBody = new PlainSelect();
newSelectBody.setFromItem(fromItem);
newSelect.setSelectBody(newSelectBody);
if (isNotEmptyList(select.getWithItemsList())) {
newSelect.setWithItemsList(select.getWithItemsList());
代码示例来源:origin: baomidou/mybatis-plus
try {
Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
Distinct distinct = plainSelect.getDistinct();
List<Expression> groupBy = plainSelect.getGroupByColumnReferences();
List<OrderByElement> orderBy = plainSelect.getOrderByElements();
for (SelectItem item : plainSelect.getSelectItems()) {
if (item.toString().contains(StringPool.QUESTION_MARK)) {
return sqlInfo.setSql(SqlParserUtils.getOriginalCountSql(selectStatement.toString()));
return sqlInfo.setSql(SqlParserUtils.getOriginalCountSql(selectStatement.toString()));
plainSelect.setSelectItems(COUNT_SELECT_ITEM);
return sqlInfo.setSql(selectStatement.toString());
} catch (Throwable e) {
代码示例来源:origin: JSQLParser/JSqlParser
public static Select buildSelectFromTableAndSelectItems(Table table, SelectItem... selectItems) {
Select select = new Select();
PlainSelect body = new PlainSelect();
body.addSelectItems(selectItems);
body.setFromItem(table);
select.setSelectBody(body);
return select;
}
代码示例来源:origin: JSQLParser/JSqlParser
@Override
public void visit(Select select) {
if (select.getWithItemsList() != null) {
for (WithItem withItem : select.getWithItemsList()) {
withItem.accept(this);
}
}
select.getSelectBody().accept(this);
}
代码示例来源:origin: JSQLParser/JSqlParser
final public Select Select() throws ParseException {Select select = new Select();
SelectBody selectBody = null;
List<WithItem> with = null;
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case K_WITH:{
with = WithList();
break;
}
default:
jj_la1[93] = jj_gen;
;
}
selectBody = SelectBody();
select.setWithItemsList(with);
select.setSelectBody(selectBody);
return select;
}
代码示例来源:origin: pagehelper/Mybatis-PageHelper
/**
* 将sql转换为count查询
*
* @param select
*/
public void sqlToCount(Select select, String name) {
SelectBody selectBody = select.getSelectBody();
// 是否能简化count查询
List<SelectItem> COUNT_ITEM = new ArrayList<SelectItem>();
COUNT_ITEM.add(new SelectExpressionItem(new Column("count(" + name +")")));
if (selectBody instanceof PlainSelect && isSimpleCount((PlainSelect) selectBody)) {
((PlainSelect) selectBody).setSelectItems(COUNT_ITEM);
} else {
PlainSelect plainSelect = new PlainSelect();
SubSelect subSelect = new SubSelect();
subSelect.setSelectBody(selectBody);
subSelect.setAlias(TABLE_ALIAS);
plainSelect.setFromItem(subSelect);
plainSelect.setSelectItems(COUNT_ITEM);
select.setSelectBody(plainSelect);
}
}
代码示例来源:origin: pagehelper/Mybatis-PageHelper
SelectBody selectBody = select.getSelectBody();
try {
processWithItemsList(select.getWithItemsList());
String result = select.toString();
return result;
代码示例来源:origin: it.unibz.inf.ontop/ontop-mapping-sql-core
/**
* Create a new query with the changed projection and selection
*/
private static String getInstantiatedSQL(String sql,
List<SelectItem> newColumns,
List<SelectExpressionItem> templateColumns,
List<String> values) throws JSQLParserException {
Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect)select.getSelectBody();
Expression where = plainSelect.getWhere();
int size = templateColumns.size(); // both lists have the same size
for (int i = 0; i < size; i++) {
BinaryExpression condition = new EqualsTo();
condition.setLeftExpression(templateColumns.get(i).getExpression());
condition.setRightExpression(new StringValue("'" + values.get(i) + "'"));
where = (where == null) ? condition : new AndExpression(where, condition);
}
plainSelect.setWhere(where); // where cannot be null
plainSelect.setSelectItems(newColumns);
return select.toString();
}
代码示例来源:origin: codingapi/tx-lcn
if (!(lockableSelect.statement().getSelectBody() instanceof PlainSelect)) {
throw new SQLException("non support this query when use control lock.");
PlainSelect plainSelect = (PlainSelect) lockableSelect.statement().getSelectBody();
if (!(plainSelect.getFromItem() instanceof Table)) {
throw new SQLException("non support this query when use control lock.");
Table leftTable = (Table) plainSelect.getFromItem();
List<SelectItem> selectItems = new ArrayList<>();
Connection connection = (Connection) DTXLocal.cur().getResource();
});
if (plainSelect.getJoins() != null) {
for (Join join : plainSelect.getJoins()) {
if (join.isSimple()) {
代码示例来源:origin: Sunybyjava/shoppingMall
/**
* 将sql转换为count查询
*
* @param select
*/
public void sqlToCount(Select select) {
SelectBody selectBody = select.getSelectBody();
// select中包含参数时在else中处理
// select中包含group by时在else中处理
if (selectBody instanceof PlainSelect
&& !selectItemsHashParameters(((PlainSelect) selectBody).getSelectItems())
&& ((PlainSelect) selectBody).getGroupByColumnReferences() == null) {
((PlainSelect) selectBody).setSelectItems(COUNT_ITEM);
} else {
PlainSelect plainSelect = new PlainSelect();
SubSelect subSelect = new SubSelect();
subSelect.setSelectBody(selectBody);
subSelect.setAlias(TABLE_ALIAS);
plainSelect.setFromItem(subSelect);
plainSelect.setSelectItems(COUNT_ITEM);
select.setSelectBody(plainSelect);
}
}
代码示例来源:origin: pagehelper/Mybatis-PageHelper
/**
* convert to order by sql
*
* @param sql
* @param orderBy
* @return
*/
public static String converToOrderBySql(String sql, String orderBy) {
//解析SQL
Statement stmt = null;
try {
stmt = CCJSqlParserUtil.parse(sql);
Select select = (Select) stmt;
SelectBody selectBody = select.getSelectBody();
//处理body-去最外层order by
List<OrderByElement> orderByElements = extraOrderBy(selectBody);
String defaultOrderBy = PlainSelect.orderByToString(orderByElements);
if (defaultOrderBy.indexOf('?') != -1) {
throw new PageException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!");
}
//新的sql
sql = select.toString();
} catch (Throwable e) {
throw new PageException("处理排序失败: " + e, e);
}
return sql + " order by " + orderBy;
}
代码示例来源:origin: baomidou/mybatis-plus
List<Join> joins = null;
if (statement instanceof Select) {
PlainSelect plainSelect = (PlainSelect) ((Select) statement).getSelectBody();
where = plainSelect.getWhere();
table = (Table) plainSelect.getFromItem();
joins = plainSelect.getJoins();
} else if (statement instanceof Update) {
Update update = (Update) statement;
代码示例来源:origin: baomidou/mybatis-plus
@Test
void parser() throws Exception {
Select select = (Select) CCJSqlParserUtil.parse("SELECT a,b,c FROM tableName t WHERE t.col = 9 and b=c LIMIT 3, ?");
PlainSelect ps = (PlainSelect) select.getSelectBody();
System.out.println(ps.getWhere().toString());
System.out.println(ps.getSelectItems().get(1).toString());
AndExpression e = (AndExpression) ps.getWhere();
System.out.println(e.getLeftExpression());
}
代码示例来源:origin: JSQLParser/JSqlParser
/**
* Adds an expression to select statements. E.g. a simple column is an expression.
*
* @param select
* @param expr
*/
public static void addExpression(Select select, final Expression expr) {
select.getSelectBody().accept(new SelectVisitor() {
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.getSelectItems().add(new SelectExpressionItem(expr));
}
@Override
public void visit(SetOperationList setOpList) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(WithItem withItem) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(ValuesStatement aThis) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
});
}
代码示例来源:origin: ontop/ontop
/**
The query for obtaining values of parameters is almost the same with the original source query,
except that we only need to distinct project the columns needed for the template expansion
*/
private static String getTemplateValuesQuery(String sql, List<SelectExpressionItem> templateColumns) throws JSQLParserException {
Select select = (Select)CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect)select.getSelectBody();
plainSelect.setDistinct(new Distinct());
plainSelect.setSelectItems(ImmutableList.copyOf(templateColumns)); // SelectExpressionItem -> SelectItem
return select.toString();
}
代码示例来源:origin: JSQLParser/JSqlParser
/**
* Adds a simple join to a select statement. The introduced join is returned for more
* configuration settings on it (e.g. left join, right join).
*
* @param select
* @param table
* @param onExpression
* @return
*/
public static Join addJoin(Select select, final Table table, final Expression onExpression) {
if (select.getSelectBody() instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
List<Join> joins = plainSelect.getJoins();
if (joins == null) {
joins = new ArrayList<Join>();
plainSelect.setJoins(joins);
}
Join join = new Join();
join.setRightItem(table);
join.setOnExpression(onExpression);
joins.add(join);
return join;
}
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
代码示例来源:origin: stackoverflow.com
public class MultiFromItems {
public static void main(String args[]) throws JSQLParserException {
String sql = "select t1.a, t2.b from t1, t2 where t1.id = t2.id";
Statement parse = CCJSqlParserUtil.parse(sql);
Select select = (Select)parse;
PlainSelect ps = (PlainSelect)select.getSelectBody();
System.out.println(ps);
System.out.println(ps.getFromItem());
System.out.println(ps.getJoins().get(0));
}
}
代码示例来源:origin: com.manydesigns/portofino-crud
protected String generateCountQuery(String queryString) throws JSQLParserException {
CCJSqlParserManager parserManager = new CCJSqlParserManager();
try {
PlainSelect plainSelect =
(PlainSelect) ((Select) parserManager.parse(new StringReader(queryString))).getSelectBody();
logger.debug("Query string {} contains select");
List items = plainSelect.getSelectItems();
if(items.size() != 1) {
logger.error("I don't know how to generate a count query for {}", queryString);
return null;
}
SelectExpressionItem item = (SelectExpressionItem) items.get(0);
Function function = new Function();
function.setName("count");
function.setParameters(new ExpressionList(Arrays.asList(item.getExpression())));
item.setExpression(function);
plainSelect.setOrderByElements(null);
return plainSelect.toString();
} catch(Exception e) {
logger.debug("Query string {} does not contain select", e);
queryString = "SELECT count(*) " + queryString;
PlainSelect plainSelect =
(PlainSelect) ((Select) parserManager.parse(new StringReader(queryString))).getSelectBody();
plainSelect.setOrderByElements(null);
return plainSelect.toString();
}
}
代码示例来源:origin: wxbty/meepo
public static String name_select_where(String sql) throws JSQLParserException {
CCJSqlParserManager parserManager = new CCJSqlParserManager();
Select select = (Select) parserManager.parse(new StringReader(sql));
PlainSelect plain = (PlainSelect) select.getSelectBody();
Expression where_expression = plain.getWhere();
if (where_expression == null)
return "";
String str = where_expression.toString();
return str;
}
代码示例来源:origin: JSQLParser/JSqlParser
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
if (select.getWithItemsList() != null) {
buffer.append("WITH ");
boolean first = true;
for (WithItem item : select.getWithItemsList()) {
if (!first) {
buffer.append(", ");
createView.getSelect().getSelectBody().accept(selectVisitor);
内容来源于网络,如有侵权,请联系作者删除!