net.sf.jsqlparser.schema.Column Java Examples
The following examples show how to use
net.sf.jsqlparser.schema.Column.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 7 votes |
@Override public void visit(InExpression inExpression) { if (inExpression.isNot()) { visit0(inExpression); return; } Column column = (Column) inExpression.getLeftExpression(); if (inExpression.getRightItemsList() instanceof ExpressionList) { TableWrapper tab = getTableFromContext(column); if (tab == null) { visit0(inExpression); return; } ExpressionList itemsList = (ExpressionList) inExpression.getRightItemsList(); List<Expression> list = itemsList.getExpressions(); if (list == null || list.isEmpty()) { visit0(inExpression); } for (Expression exp : list) { routeTable(tab, column, exp); } } else { visit0(inExpression); return; } }
Example #2
Source File: QueryConverter.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
private void validate() throws ParseException { List<SelectItem> selectItems = sqlCommandInfoHolder.getSelectItems(); List<SelectItem> filteredItems = Lists.newArrayList(Iterables.filter(selectItems, new Predicate<SelectItem>() { @Override public boolean apply(SelectItem selectItem) { try { if (SelectExpressionItem.class.isInstance(selectItem) && Column.class.isInstance(((SelectExpressionItem) selectItem).getExpression())) { return true; } } catch (NullPointerException e) { return false; } return false; } })); SqlUtils.isFalse((selectItems.size() >1 || SqlUtils.isSelectAll(selectItems)) && sqlCommandInfoHolder.isDistinct(),"cannot run distinct one more than one column"); SqlUtils.isFalse(sqlCommandInfoHolder.getGoupBys().size() == 0 && selectItems.size()!=filteredItems.size() && !SqlUtils.isSelectAll(selectItems) && !SqlUtils.isCountAll(selectItems)&& !sqlCommandInfoHolder.isTotalGroup(),"illegal expression(s) found in select clause. Only column names supported"); }
Example #3
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 6 votes |
@Override public void visit(Insert insert) { this.getStack().push(new FrameContext(StatementType.INSERT)); visit0(insert); // route table List<Column> columns = insert.getColumns(); if (columns != null) { ItemsList itemsList = insert.getItemsList(); if (itemsList instanceof ExpressionList) { procInsertColumns(columns, (ExpressionList) itemsList); } else if (itemsList instanceof MultiExpressionList) { for (ExpressionList expressionList : ((MultiExpressionList) itemsList).getExprList()) { procInsertColumns(columns, expressionList); } } else { throw new UnsupportedSQLExpressionException(insert.toString()); } } afterVisitBaseStatement(); }
Example #4
Source File: QueryTranslator.java From foxtrot with Apache License 2.0 | 6 votes |
private ColumnData setupColumn(Expression expression) { if(expression instanceof Function) { Function function = (Function)expression; if(function.getName() .equalsIgnoreCase("temporal")) { List parameters = function.getParameters() .getExpressions(); if(parameters.size() != 1 || !(parameters.get(0) instanceof Column)) { throw new FqlParsingException("temporal function must have a fieldname as parameter"); } return ColumnData.temporal(((Column)parameters.get(0)).getFullyQualifiedName()); } throw new FqlParsingException("Only the function 'temporal' is supported in where clause"); } if(expression instanceof Column) { return new ColumnData(((Column)expression).getFullyQualifiedName()); } throw new FqlParsingException("Only the function 'temporal([fieldname)' and fieldname is supported in where clause"); }
Example #5
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 6 votes |
private void routeTable(TableWrapper tab, Column column, Object sdValue) { if (tab == null) {// return; } if (tab == AMBIGUOUS_TABLE) { throw new RuntimeException("Shard value '" + column.toString() + "' in where clause is ambiguous. Sql is [" + sql + "]"); } ShardRouteInfo routeInfo = getRouteInfo(tab, sdValue); // 当没有设置别名,但分表字段使用了表前缀时,别前缀需要根据路由结果进行重写; // 这里之所有没有采用将table取名的方式是因为update/delete/insert不全支持别名; // 由于select都是支持别名的,所有都会被添加别名,因此不会执行下面的操作; Table columnTable = column.getTable(); if (tab.getAlias() == null && columnTable != null && columnTable.getName() != null && columnTable.getName().length() > 0) { if (columnTable.getSchemaName() != null) { columnTable.setSchemaName(routeInfo.getScName()); } columnTable.setName(routeInfo.getTbName()); } route0(tab, routeInfo); }
Example #6
Source File: CloudSpannerResultSetMetaData.java From spanner-jdbc with MIT License | 6 votes |
@Override public int isNullable(int column) throws SQLException { Column col = getColumn(column); if (col != null && col.getTable() != null) { String schema = Strings.isNullOrEmpty(col.getTable().getSchemaName()) ? "" : CloudSpannerDriver.unquoteIdentifier(col.getTable().getSchemaName()); String tableName = CloudSpannerDriver.unquoteIdentifier(col.getTable().getName()); String colName = CloudSpannerDriver.unquoteIdentifier(col.getColumnName()); try (java.sql.ResultSet rs = statement.getConnection().getMetaData().getColumns("", schema, tableName, colName)) { if (rs.next()) { return rs.getInt("NULLABLE"); } } } return columnNullableUnknown; }
Example #7
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private void setWhereParameters(Expression where, com.google.cloud.spanner.Statement.Builder builder) { if (where != null) { where.accept(new ExpressionVisitorAdapter() { private String currentCol = null; @Override public void visit(Column col) { currentCol = unquoteIdentifier(col.getFullyQualifiedName()); } @Override public void visit(JdbcParameter parameter) { parameter.accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.bind("p" + parameter.getIndex()), currentCol)); currentCol = null; } @Override public void visit(SubSelect subSelect) { setSelectParameters(subSelect.getSelectBody(), builder); } }); } }
Example #8
Source File: UsedColumnExtractorVisitor.java From evosql with Apache License 2.0 | 6 votes |
@Override public void visit(SelectExpressionItem arg0) { if (currentState.stateSource != null) { String columnName = null; if (arg0.getAlias() != null) columnName = arg0.getAlias().getName(); else if (arg0.getExpression() instanceof Column) { columnName = ((Column)arg0.getExpression()).getColumnName(); } // Strip quotes if (columnName != null) columnName = columnName.replaceAll("^\"|\"$", ""); currentState.columnSource = new ColumnSource(currentState.stateSource, columnName); } arg0.getExpression().accept(this); // Get ColumnSource and store in the current state source if (currentState.stateSource != null) { currentState.stateSource.add(currentState.columnSource); currentState.columnSource = null; } }
Example #9
Source File: OnVisitorMatchLookupBuilder.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
@Override public void visit(Column column) { if(SqlUtils.isColumn(column)) { String columnName; if(column.getTable() != null) { columnName = SqlUtils.getColumnNameFromColumn(column); } else { columnName = column.getColumnName(); } if(!SqlUtils.isTableAliasOfColumn(column, joinAliasTable) ) { if(column.getTable() == null || SqlUtils.isTableAliasOfColumn(column, baseAliasTable)) {//we know let var don't have table inside column.setColumnName("$$" + columnName.replace(".", "_").toLowerCase()); } else { column.setColumnName("$$" + column.getName(false).replace(".", "_").toLowerCase()); } column.setTable(null); } else { column.setTable(null); column.setColumnName("$" + columnName); } } }
Example #10
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private Mutation createUpdateMutation(Update update, boolean generateParameterMetaData) throws SQLException { if (update.getTables().isEmpty()) throw new CloudSpannerSQLException("No table found in update statement", Code.INVALID_ARGUMENT); if (update.getTables().size() > 1) throw new CloudSpannerSQLException( "Update statements for multiple tables at once are not supported", Code.INVALID_ARGUMENT); String table = unquoteIdentifier(update.getTables().get(0).getFullyQualifiedName()); getParameterStore().setTable(table); List<Expression> expressions = update.getExpressions(); WriteBuilder builder = Mutation.newUpdateBuilder(table); int index = 0; for (Column col : update.getColumns()) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName)); index++; } visitUpdateWhereClause(update.getWhere(), builder, generateParameterMetaData); return builder.build(); }
Example #11
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private void visitDeleteWhereClause(Expression where, DeleteKeyBuilder keyBuilder, boolean generateParameterMetaData) throws SQLException { if (where != null) { DMLWhereClauseVisitor whereClauseVisitor = new DMLWhereClauseVisitor(getParameterStore()) { @Override protected void visitExpression(Column col, Expression expression) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); keyBuilder.set(columnName); expression.accept( new KeyBuilderExpressionVisitorAdapter(getParameterStore(), columnName, keyBuilder)); } }; where.accept(whereClauseVisitor); if (!generateParameterMetaData && !whereClauseVisitor.isValid()) { throw new CloudSpannerSQLException(INVALID_WHERE_CLAUSE_DELETE_MESSAGE, Code.INVALID_ARGUMENT); } } }
Example #12
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
public static Object getValue(Expression incomingExpression, Expression otherSide, FieldType defaultFieldType, Map<String, FieldType> fieldNameToFieldTypeMapping) throws ParseException { FieldType fieldType = otherSide !=null ? firstNonNull(fieldNameToFieldTypeMapping.get(getStringValue(otherSide)), defaultFieldType) : FieldType.UNKNOWN; if (LongValue.class.isInstance(incomingExpression)) { return normalizeValue((((LongValue)incomingExpression).getValue()),fieldType); } else if (SignedExpression.class.isInstance(incomingExpression)) { return normalizeValue((((SignedExpression)incomingExpression).toString()),fieldType); } else if (StringValue.class.isInstance(incomingExpression)) { return normalizeValue((((StringValue)incomingExpression).getValue()),fieldType); } else if (Column.class.isInstance(incomingExpression)) { return normalizeValue(getStringValue(incomingExpression),fieldType); } else { throw new ParseException("can not parseNaturalLanguageDate: " + incomingExpression.toString()); } }
Example #13
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private boolean isSingleRowWhereClause(TableKeyMetaData table, Expression where) { if (where != null) { SingleRowWhereClauseValidator validator = new SingleRowWhereClauseValidator(table); DMLWhereClauseVisitor whereClauseVisitor = new DMLWhereClauseVisitor(getParameterStore()) { @Override protected void visitExpression(Column col, Expression expression) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); validator.set(columnName); expression.accept(new SingleRowWhereClauseValidatorExpressionVisitorAdapter( getParameterStore(), validator)); } }; where.accept(whereClauseVisitor); return whereClauseVisitor.isValid() && validator.isValid(); } return false; }
Example #14
Source File: CTEToNestedQueryConverter.java From quetzal with Eclipse Public License 2.0 | 6 votes |
@Override public void visit(Column tableColumn) { clear(); boolean prevIsTopLevel = isTopLevel; isTopLevel = false; if (tableColumn.getTable()!=null && tableColumn.getTable().getName()!=null) { if (tableColumn.getTable().getName().equalsIgnoreCase(leftTable.getAlias()!=null? leftTable.getAlias().getName(): leftTable.getName())) { leftTableFound = true; } if (tableColumn.getTable().getName().equalsIgnoreCase(rightTable.getAlias()!=null? rightTable.getAlias().getName(): rightTable.getName())) { rightTableFound = true; } } isTopLevel = prevIsTopLevel; defaultTopLevelProcessing(tableColumn); }
Example #15
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private void visitUpdateWhereClause(Expression where, WriteBuilder builder, boolean generateParameterMetaData) throws SQLException { if (where != null) { DMLWhereClauseVisitor whereClauseVisitor = new DMLWhereClauseVisitor(getParameterStore()) { @Override protected void visitExpression(Column col, Expression expression) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); expression.accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName)); } }; where.accept(whereClauseVisitor); if (!generateParameterMetaData && !whereClauseVisitor.isValid()) { throw new CloudSpannerSQLException(INVALID_WHERE_CLAUSE_UPDATE_MESSAGE, Code.INVALID_ARGUMENT); } } else { throw new SQLException(INVALID_WHERE_CLAUSE_UPDATE_MESSAGE); } }
Example #16
Source File: CountSqlParser.java From Mybatis-PageHelper with MIT License | 6 votes |
/** * 将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); } }
Example #17
Source File: CountSqlParser.java From FastSQL with Apache License 2.0 | 6 votes |
/** * 将sql转换为count查询 * */ 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); } }
Example #18
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
public static String getStringValue(Expression expression) { if (StringValue.class.isInstance(expression)) { return ((StringValue)expression).getValue(); } else if (Column.class.isInstance(expression)) { String columnName = expression.toString(); Matcher matcher = SURROUNDED_IN_QUOTES.matcher(columnName); if (matcher.matches()) { return matcher.group(1); } return columnName; } return expression.toString(); }
Example #19
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
public static String getColumnNameFromColumn(Column c){ String [] splitedNestedField = c.getName(false).split("\\."); if(splitedNestedField.length > 2) { return String.join(".", Arrays.copyOfRange(splitedNestedField, 1, splitedNestedField.length)); } else { return splitedNestedField[splitedNestedField.length-1]; } }
Example #20
Source File: PaginationInterceptor.java From platform with Apache License 2.0 | 5 votes |
private static List<OrderByElement> addOrderByElements(List<Sort.Order> orderList, List<OrderByElement> orderByElements) { orderByElements = CollectionUtils.isEmpty(orderByElements) ? new ArrayList<>(orderList.size()) : orderByElements; List<OrderByElement> orderByElementList = orderList.stream().map(item -> { OrderByElement element = new OrderByElement(); element.setExpression(new Column(item.getProperty())); element.setAsc(item.getDirection().isAscending()); return element; }).collect(Collectors.toList()); orderByElements.addAll(orderByElementList); return orderByElements; }
Example #21
Source File: JSqlParsers.java From sqlhelper with GNU Lesser General Public License v3.0 | 5 votes |
public static boolean expressionEquals(Expression expr1, Expression expr2) { if (expr1 == null && expr2 == null) { return true; } if (expr1 == null || expr2 == null) { return false; } if (expr1 instanceof Column && expr2 instanceof Column) { return columnEquals((Column) expr1, (Column) expr2); } return expr1.toString().equalsIgnoreCase(expr2.toString()); }
Example #22
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 5 votes |
public SqlParam(Column column, Expression expression) { this.column = column; this.expression = expression; if (expression instanceof JdbcParameter) { value = ((JdbcParameter) expression).getIndex(); jdbcParamType = true; } else if (expression instanceof JdbcNamedParameter) { value = ((JdbcNamedParameter) expression).getName(); jdbcParamType = true; } else { value = getRouteValue(column, expression); jdbcParamType = false; } }
Example #23
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 5 votes |
@Override public void visit(Between between) { if (between.isNot()) { visit0(between); return; } Column column = (Column) between.getLeftExpression(); TableWrapper tab = getTableFromContext(column); if (tab == null) { visit0(between); return; } Expression begin = between.getBetweenExpressionStart(); Expression end = between.getBetweenExpressionEnd(); if (begin instanceof SubSelect || end instanceof SubSelect) { visit0(between); return; } else if ((begin instanceof JdbcParameter || begin instanceof JdbcNamedParameter) // || (end instanceof JdbcParameter || end instanceof JdbcNamedParameter)) { tab.getJdbcParamKeys().add(new RangeParam(new SqlParam(column, begin), new SqlParam(column, end))); return; } else { long s1 = ((Number) getRouteValue(column, begin)).longValue(); long e1 = ((Number) getRouteValue(column, end)).longValue(); if (s1 > e1) { long temp = s1; s1 = e1; e1 = temp; } routeTable(tab, column, new RangeShardValue(s1, e1)); } }
Example #24
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 5 votes |
private void routeTable(TableWrapper tab, Column column, Expression routeValueExpression) { // jdbc参数 if (routeValueExpression != null && routeValueExpression instanceof JdbcParameter || routeValueExpression instanceof JdbcNamedParameter) { tab.getJdbcParamKeys().add(new SqlParam(column, routeValueExpression)); return; } else {// 普通sql参数 Object sdValue = getRouteValue(column, routeValueExpression);// routeTable(tab, column, sdValue); } }
Example #25
Source File: QueryUtils.java From foxtrot with Apache License 2.0 | 5 votes |
public static String expressionToString(Expression expression) { if(expression instanceof Column) { return ((Column)expression).getFullyQualifiedName(); } if(expression instanceof StringValue) { return ((StringValue)expression).getValue(); } return null; }
Example #26
Source File: QueryTranslator.java From foxtrot with Apache License 2.0 | 5 votes |
private ResultSort generateResultSort(List<OrderByElement> orderByElements) { if(CollectionUtils.isEmpty(orderByElements)) { return null; } OrderByElement orderByElement = orderByElements.get(0); Column sortColumn = (Column)orderByElement.getExpression(); ResultSort resultSortColumn = new ResultSort(); resultSortColumn.setField(sortColumn.getFullyQualifiedName()); resultSortColumn.setOrder(orderByElement.isAsc() ? ResultSort.Order.asc : ResultSort.Order.desc); logger.info("ResultSort: {}", resultSortColumn); return resultSortColumn; }
Example #27
Source File: OnVisitorLetsBuilder.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
@Override public void visit(Column column) { if(!SqlUtils.isTableAliasOfColumn(column, joinAliasTable)) { String columnName; if(SqlUtils.isTableAliasOfColumn(column, baseAliasTable)) { columnName = SqlUtils.getColumnNameFromColumn(column); } else { columnName = column.getName(false); } onDocument.put(columnName.replace(".", "_").toLowerCase(), "$" + columnName); } }
Example #28
Source File: JSQLParserAdapter.java From ddal with Apache License 2.0 | 5 votes |
private void procInsertColumns(List<Column> columns, ExpressionList expressionList) { List<Expression> valueList = expressionList.getExpressions(); for (int i = 0; i < columns.size(); i++) { Column column = columns.get(i); TableWrapper tab = getTableFromContext(column); if (tab != null) { Expression expression = valueList.get(i); routeTable(tab, column, expression); } } }
Example #29
Source File: SqlSecurerVisitor.java From evosql with Apache License 2.0 | 5 votes |
@Override public void visit(Column arg0) { Table table = arg0.getTable(); if (table != null) { table.accept(this); } arg0.setColumnName(secureName(arg0.getColumnName())); }
Example #30
Source File: UsedColumnExtractorVisitor.java From evosql with Apache License 2.0 | 5 votes |
@Override public void visit(Column arg0) { String tableName = null; if (arg0.getTable() != null) { if (arg0.getTable().getAlias() != null) tableName = arg0.getTable().getAlias().getName(); else tableName = arg0.getTable().getName(); } // If it is outer select, don't add, otherwise do add if (!(currentState.isOuterState && currentState.phase == SelectPhase.SELECT)) { addUsedColumn(arg0.getColumnName(), tableName); } }