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

com.tsc9526.monalisa.tools.string.MelpSQL Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*******************************************************************************************
 *	Copyright (c) 2016, zzg.zhou([email protected])
 * 
 *  Monalisa is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.

 *	You should have received a copy of the GNU Lesser General Public License
 *	along with this program.  If not, see .
 *******************************************************************************************/
package com.tsc9526.monalisa.tools.string;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.tsc9526.monalisa.orm.dialect.Dialect;
 
/**
 * 
 * @author zzg.zhou([email protected])
 */
public class MelpSQL {	
	private MelpSQL(){}
	
	public static String getExecutableSQL(Dialect dialect,String original,List parameters) {		 
		if(parameters==null || parameters.isEmpty()){
			return original;
		}else{	
			 try{
				 return toSQL(dialect,original,parameters);
			 }catch(IOException e){
				 throw new RuntimeException(e);
			 }
		}
	}
	
	private static String toSQL(Dialect dialect,String original,List parameters)throws IOException{
		StringBuilder sb = new StringBuilder();
		int x=0;
		for(int i=0;i=parameters.size()){
					throw new RuntimeException("Parameter index out-of-bound: "+x+", total parameters: "+parameters.size());
				}
				
				Object p=parameters.get(x);
				if(p==null){
					sb.append("null");
				}else if(p instanceof Number || p instanceof Boolean){
					sb.append(p);
				}else if(p instanceof Date){
					String dateString="'"+MelpDate.toTime((Date)p)+"'";
					if(dialect!=null){
						dateString = dialect.getFieldDateValue(dateString);
					}
					sb.append(dateString);
				}else if(p.getClass() == byte[].class || p.getClass() == Byte[].class){
					appendBytes(sb,(byte[])p);
				}else if(p instanceof InputStream){
					appendStream(sb,(InputStream)p); 
				}else if(p instanceof Reader){
					appendReader(sb,(Reader)p); 
				}else{
					sb.append(escapeSqlValue(p.toString()));
				}	
				
				x++;
			}else{
				sb.append(c);
			}
		}
		
		return sb.toString();
	}
	
	public static void appendStream(StringBuilder sb,InputStream r)throws IOException{
		ByteArrayOutputStream tmp=new ByteArrayOutputStream();
		
		byte[] buf=new byte[16*1024];
		int len=r.read(buf);
		while(len>0){
			tmp.write(buf,0,len);
			
			len=r.read(buf);
		}
		r.close();
		
		appendBytes(sb,tmp.toByteArray());
	}
	
	private static void appendReader(StringBuilder sb,Reader r)throws IOException{
		StringBuilder tmp=new StringBuilder();
		
		char[] buf=new char[16*1024];
		int len=r.read(buf);
		while(len>0){
			tmp.append(new String(buf,0,len));
			len=r.read(buf);
		}
		r.close();
		
		sb.append(escapeSqlValue(tmp.toString()));
	}
	
	public static void appendBytes(StringBuilder sb,byte[] bytes){
		String s=MelpString.bytesToHexString(bytes,"\\x");
		
		sb.append("'");
		sb.append(s);
		sb.append("'");
	}
	
	
	public static String escapeSqlValue(String v){
		if(v==null){
			return null;
		}
		
		StringBuilder r=new StringBuilder();
		r.append("'");
		for(int i=0;i parameters)throws SQLException{
		if(parameters!=null && !parameters.isEmpty()){
			int parameterIndex=1;
			for(Object p:parameters){
				if(p==null){
					pst.setObject(parameterIndex, null);
				}else if(p instanceof Integer || p.getClass()==int.class){
					pst.setInt(parameterIndex, (Integer)p);
				}else if(p instanceof Long || p.getClass()==long.class){
					pst.setLong(parameterIndex, (Long)p);
				}else if(p instanceof Float || p.getClass()==float.class){
					pst.setFloat(parameterIndex, (Float)p);
				}else if(p instanceof Double || p.getClass()==double.class){
					pst.setDouble(parameterIndex, (Double)p);
				}else if(p instanceof Byte || p.getClass()==byte.class){
					pst.setByte(parameterIndex, (Byte)p);
				}else if(p instanceof Boolean || p.getClass()==boolean.class){
					pst.setBoolean(parameterIndex, (Boolean)p);
				}else if(p instanceof Date){
					pst.setTimestamp(parameterIndex, new java.sql.Timestamp(((Date)p).getTime()));
				}else if(p.getClass().isArray()){					
					pst.setBytes(parameterIndex, (byte[])p);
				}else{
					pst.setObject(parameterIndex, p);
				}				
				parameterIndex++;
			}
		}				
	}
	
	public static List splitKeyWords(String sql){
		List kws=new ArrayList();
		
		StringBuilder word=new StringBuilder();
		for(int i=0;i0){
					kws.add(word.toString().toUpperCase());
					
					word.delete(0, word.length());
				}
			}else if(c=='=' || c=='>' || c=='<' || c=='!' || c==','){
				if(word.length()>0){
					kws.add(word.toString().toUpperCase());
					
					word.delete(0, word.length());
				}
				kws.add(String.valueOf(c));				
			}else{
				if(c=='\''){
					int from=i;
					
					i++;
					for(;i0){
			kws.add(word.toString().toUpperCase());
			
			word.delete(0, word.length());
		}
		
		return kws;
	}
	
	public static boolean isStartByKeyWord(String s,String keyword){
		if(s==null || s.trim().length()==0){
			return false;
		}
		
		String x=s.trim().toUpperCase();
		int len=keyword.length();
		if(x.startsWith(keyword.toUpperCase()) && x.length()>len ){
			char c=x.charAt(len);
			
		 
			if( c==' ' || c=='\t' || c=='\r' || c=='\n'){
				return true;
			}
		}
		
		return false;
	}
}