package com.innovaatio.query.parser;
import java.util.ArrayList;
import java.util.List;
import com.innovation.model.Criteria;
import com.innovation.model.Field;
import com.innovation.model.Query;
public class QueryParser {
Query query = new Query();
public Query Parse(String strQuery){
final int selectIndex = (strQuery.indexOf("SELECT") != -1) ? strQuery.indexOf("SELECT") : strQuery.indexOf("select");
final int fromIndex = (strQuery.indexOf("FROM") != -1) ? strQuery.indexOf("FROM") : strQuery.indexOf("from");;
final int whereIndex = (strQuery.indexOf("WHERE") != -1) ? strQuery.indexOf("WHERE") : strQuery.indexOf("where");
final int orderByIndex = (strQuery.indexOf("ORDERBY") != -1) ? strQuery.indexOf("ORDERBY") : strQuery.indexOf("orderby");
if (selectIndex == -1){
query.setSelect(false);
return query;
}
if (fromIndex == -1){
query.setParseMessage("Invalid SQL, missing the from clause");
return query;
}
String tableName = strQuery.substring(fromIndex, whereIndex).trim().substring(4);
query.setTableName(tableName);
String criteriaStr = null;
if(orderByIndex != -1){
String orderByField = strQuery.substring(orderByIndex).trim().substring(7).trim();
criteriaStr = strQuery.substring(whereIndex, orderByIndex).trim().substring(5);
query.setSortByFieldName(orderByField);
}else{
criteriaStr = strQuery.substring(whereIndex).trim();
}
query.setCriteriaList(parseCriteriaList(criteriaStr));
return query;
}
private String getOperator(String condition){
if(condition.contains("=")){
return "=";
}else if(condition.contains("<")){
return "<";
}else if(condition.contains(">")){
return ">";
}else if(condition.contains("<>")){
return "<>";
}
return "";
}
private Criteria getCriteria(String criteria){
Criteria criteriaObj = null;
if(criteria.contains("=") || criteria.contains("<") || criteria.contains(">") || criteria.contains("<>")){
String[] fields = criteria.split(getOperator(criteria));
if(fields.length == 2){
criteriaObj = new Criteria(getOperator(criteria), new Field(fields[0],fields[1]));
}
}
return criteriaObj;
}
public List<Criteria> parseCriteriaList(String criteriaStr){
List<Criteria> criteriaList = new ArrayList<Criteria>();
if(criteriaStr.indexOf("AND") != -1 || criteriaStr.indexOf("and") != -1){
int andIndex = (criteriaStr.indexOf("AND") != -1) ? criteriaStr.indexOf("AND") : criteriaStr.indexOf("and");
if(andIndex != -1){
query.setLogicalOperator("AND");
String[] criteriaArr = (criteriaStr.split("AND").length == 2)?criteriaStr.split("AND"):criteriaStr.split("and");
for(String criteria: criteriaArr){
criteriaList.add(getCriteria(criteria));
}
}
}else if(criteriaStr.indexOf("OR") != -1 || criteriaStr.indexOf("or") != -1){
int orIndex = (criteriaStr.indexOf("OR") != -1) ? criteriaStr.indexOf("OR") : criteriaStr.indexOf("or");
if(orIndex != -1){
query.setLogicalOperator("OR");
String[] criteriaArr = (criteriaStr.split("OR").length == 2)?criteriaStr.split("OR"):criteriaStr.split("or");
for(String criteria: criteriaArr){
criteriaList.add(getCriteria(criteria));
}
}
}
return criteriaList;
}
}
import java.util.ArrayList;
import java.util.List;
import com.innovation.model.Criteria;
import com.innovation.model.Field;
import com.innovation.model.Query;
public class QueryParser {
Query query = new Query();
public Query Parse(String strQuery){
final int selectIndex = (strQuery.indexOf("SELECT") != -1) ? strQuery.indexOf("SELECT") : strQuery.indexOf("select");
final int fromIndex = (strQuery.indexOf("FROM") != -1) ? strQuery.indexOf("FROM") : strQuery.indexOf("from");;
final int whereIndex = (strQuery.indexOf("WHERE") != -1) ? strQuery.indexOf("WHERE") : strQuery.indexOf("where");
final int orderByIndex = (strQuery.indexOf("ORDERBY") != -1) ? strQuery.indexOf("ORDERBY") : strQuery.indexOf("orderby");
if (selectIndex == -1){
query.setSelect(false);
return query;
}
if (fromIndex == -1){
query.setParseMessage("Invalid SQL, missing the from clause");
return query;
}
String tableName = strQuery.substring(fromIndex, whereIndex).trim().substring(4);
query.setTableName(tableName);
String criteriaStr = null;
if(orderByIndex != -1){
String orderByField = strQuery.substring(orderByIndex).trim().substring(7).trim();
criteriaStr = strQuery.substring(whereIndex, orderByIndex).trim().substring(5);
query.setSortByFieldName(orderByField);
}else{
criteriaStr = strQuery.substring(whereIndex).trim();
}
query.setCriteriaList(parseCriteriaList(criteriaStr));
return query;
}
private String getOperator(String condition){
if(condition.contains("=")){
return "=";
}else if(condition.contains("<")){
return "<";
}else if(condition.contains(">")){
return ">";
}else if(condition.contains("<>")){
return "<>";
}
return "";
}
private Criteria getCriteria(String criteria){
Criteria criteriaObj = null;
if(criteria.contains("=") || criteria.contains("<") || criteria.contains(">") || criteria.contains("<>")){
String[] fields = criteria.split(getOperator(criteria));
if(fields.length == 2){
criteriaObj = new Criteria(getOperator(criteria), new Field(fields[0],fields[1]));
}
}
return criteriaObj;
}
public List<Criteria> parseCriteriaList(String criteriaStr){
List<Criteria> criteriaList = new ArrayList<Criteria>();
if(criteriaStr.indexOf("AND") != -1 || criteriaStr.indexOf("and") != -1){
int andIndex = (criteriaStr.indexOf("AND") != -1) ? criteriaStr.indexOf("AND") : criteriaStr.indexOf("and");
if(andIndex != -1){
query.setLogicalOperator("AND");
String[] criteriaArr = (criteriaStr.split("AND").length == 2)?criteriaStr.split("AND"):criteriaStr.split("and");
for(String criteria: criteriaArr){
criteriaList.add(getCriteria(criteria));
}
}
}else if(criteriaStr.indexOf("OR") != -1 || criteriaStr.indexOf("or") != -1){
int orIndex = (criteriaStr.indexOf("OR") != -1) ? criteriaStr.indexOf("OR") : criteriaStr.indexOf("or");
if(orIndex != -1){
query.setLogicalOperator("OR");
String[] criteriaArr = (criteriaStr.split("OR").length == 2)?criteriaStr.split("OR"):criteriaStr.split("or");
for(String criteria: criteriaArr){
criteriaList.add(getCriteria(criteria));
}
}
}
return criteriaList;
}
}
package com.innovation.service;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.innovation.model.Employee;
public class MappingService {
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException, SecurityException, InstantiationException {
Employee emp = Employee.class.newInstance();
Class aClass = Employee.class;
List<String> fieldList = Arrays.asList("firstName","lastName","empId","designation");
Map<String, Object> queryResult = new HashMap<String, Object>();
queryResult.put("firstName", "Amit");
queryResult.put("lastName", "Sharma");
queryResult.put("empId", 11380);
queryResult.put("designation", "Development Engineer");
for(String field:fieldList){
Field pvtField = aClass.getDeclaredField(field);
pvtField.setAccessible(true);
pvtField.set(emp,queryResult.get(field));
}
System.out.println("Employee::"+emp.toString());
}
}
package com.innovation.model;
import java.util.List;
public class Query {
private boolean isSelect;
private List<String> columns;
private String tableName;
private String sortByFieldName;
private String logicalOperator;
private List<Criteria> criteriaList;
private String parseMessage;
public List<Criteria> getCriteriaList() {
return criteriaList;
}
public void setCriteriaList(List<Criteria> criteriaList) {
this.criteriaList = criteriaList;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean isSelect) {
this.isSelect = isSelect;
}
public List<String> getColumns() {
return columns;
}
public void setColumns(List<String> columns) {
this.columns = columns;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getSortByFieldName() {
return sortByFieldName;
}
public void setSortByFieldName(String sortByFieldName) {
this.sortByFieldName = sortByFieldName;
}
public String getParseMessage() {
return parseMessage;
}
public void setParseMessage(String parseMessage) {
this.parseMessage = parseMessage;
}
public String getLogicalOperator() {
return logicalOperator;
}
public void setLogicalOperator(String logicalOperator) {
this.logicalOperator = logicalOperator;
}
}
package com.innovation.model;
public class Employee {
private String firstName;
private String lastName;
private int empId;
private String designation;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@Override
public String toString() {
return "FirstName:" + firstName + "\nLastName:" + lastName + "\nEmpId:"
+ empId + "\nDesignation:" + designation;
}
}
package com.innovation.model;
public class Field {
private String name;
private String value;
public Field(){}
public Field(String name, String value){
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package com.innovation.model;
public class Criteria {
private String operator;
private Field field;
public Criteria(){}
public Criteria(String operator, Field field){
this.operator = operator;
this.field = field;
}
public Field getField() {
return field;
}
public void setField(Field field) {
this.field = field;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
}
No comments:
Post a Comment