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

com.novartis.opensource.yada.YADASelectDeParser Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2016 Novartis Institutes for BioMedical Research Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.novartis.opensource.yada;

import java.util.ArrayList;
import java.util.List;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.YADAMarkupParameter;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.ValuesList;
import net.sf.jsqlparser.util.deparser.SelectDeParser;

/**
 * A subclass of {@link net.sf.jsqlparser.util.deparser.SelectDeParser} with methods to account for JDBC-related columns and 
 * query-parsing-state management (i.e., flag setting/resetting)
 * @author David Varon
 *
 */
public class YADASelectDeParser extends SelectDeParser {

	/**
	 * Flag to mark when expression is aliased
	 */
	private boolean expressionHasAlias = false;
	/**
	 * Flag to mark when expression contains a jdbc parameter symbol
	 */
	private boolean hasJdbcParameter   = false;
  /**
   * values expression for downstream expansion
   * @since 9.3.6
   */
  private ValuesList valuesList;
  /**
   * container of alias.column values for downstream reference
   * @since 9.3.6
   */
  private List valuesColumns = new ArrayList<>();

	
	/**
	 * Default no-arg constructor
	 */
	public YADASelectDeParser() {}
	
	/**
	 * Creates a new instance, as well as sets vars for arguments.
	 * @param yadaExpressionDeParser the object for processing SQL expressions
	 * @param builder the container for expression processing metadata
	 */
	public YADASelectDeParser(YADAExpressionDeParser yadaExpressionDeParser,
			StringBuilder builder) {
		super(yadaExpressionDeParser,builder);
	}

	/**
	 * Sets flags as needed, then calls handler.
	 * @see net.sf.jsqlparser.util.deparser.SelectDeParser#visit(net.sf.jsqlparser.statement.select.SelectExpressionItem)
	 */
	@Override
	public void visit(SelectExpressionItem selectExpressionItem) {
		super.visit(selectExpressionItem);
		this.expressionHasAlias = selectExpressionItem.getAlias() != null;		
		YADAExpressionDeParser expDeParser = (YADAExpressionDeParser)this.getExpressionVisitor(); 
		this.hasJdbcParameter   = expDeParser.hasJdbcParameter();						
		((YADAExpressionDeParser)this.getExpressionVisitor()).setInExpression(true);
		handleSelectExpressionItem(selectExpressionItem);
	}
	
	@Override
	public void deparseJoin(Join join) {
	  super.deparseJoin(join);
	}
	
	@Override
  public void visit(ValuesList valuesList) {
    super.visit(valuesList);
    String alias = valuesList.getAlias().toString();
    List columns = valuesList.getColumnNames();
    for(String column : columns)
    {
      this.getValuesColumns().add(String.format("%s.%s", alias, column));
    }
    this.setValuesList(valuesList);
    MultiExpressionList mel = valuesList.getMultiExpressionList();
    ExpressionList el = mel.getExprList().get(0);
    List exprs = el.getExpressions();
    Table tab = new Table(alias);
    for(int i=0;i getValuesColumns() {
    return valuesColumns;
  }

  /**
   * Standard mutator
   * @param valuesColumns the {@link List} returned by the parser
   * @since 9.3.6
   */
  public void setValuesColumns(List valuesColumns) {
    this.valuesColumns = valuesColumns;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy