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

src-main.org.awakefw.sql.util.FileNameFromBlobBuilder Maven / Gradle / Ivy

/*
 * This file is part of Awake SQL. 
 * Awake SQL: Remote JDBC access over HTTP.                                    
 * Copyright (C) 2013,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                    
 *                                                                         
 * Awake SQL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.         
 *              
 * Awake SQL 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 General Public License
 * along with this program; if not, see .
 *
 * If you develop commercial activities using Awake SQL, you must: 
 * a) disclose and distribute all source code of your own product,
 * b) license your own product under the GNU General Public License.
 * 
 * You can be released from the requirements of the license by
 * purchasing a commercial license. Buying such a license will allow you 
 * to ship Awake SQL with your closed source products without disclosing 
 * the source code.
 *
 * For more information, please contact KawanSoft SAS at this
 * address: [email protected]
 * 
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.awakefw.sql.util;

import org.apache.commons.lang3.StringUtils;
import org.awakefw.file.util.AwakeFileUtil;
import org.awakefw.file.util.Tag;

/**
 * @author Nicolas de Pomereu Build the raw filename that will contain the Blob
 *         or Clob contant
 */
public class FileNameFromBlobBuilder {
    
    // DML
    private final static String DELETE = "DELETE";
    private final static String INSERT = "INSERT";
    private final static String SELECT = "SELECT";
    private final static String UPDATE = "UPDATE";


    private static final String BLANK = " ";
    
    /** The statement sql string */
    private String sqlOrder = null;

    /** The column to dump */
    private String columnName = null;

    /** The parameter index */
    private int parameterIndex = 0;

    /** true if the Blob is a Clob */
    private boolean isClob = false;

    /**
     * Default constructor
     * 
     * @param sqlOrder
     *            The statement sql string
     * @param columnName
     *            The column to dump
     * @param isClob
     *            true if the Blob as a Clob
     */
    public FileNameFromBlobBuilder(String sqlOrder, String columnName,
	    boolean isClob) {
	if (sqlOrder == null) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "sqlOrder can not be null!");
	}

	if (columnName == null) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "columnName can not be null!");
	}

	this.sqlOrder = sqlOrder;
	this.columnName = columnName;
	this.isClob = isClob;
    }

    /**
     * Default constructor
     * 
     * @param sqlOrder
     *            The statement sql string
     * @param parameterIndex
     *            the index of the prepared statement
     * @param isClob
     *            true if the Blob as a Clob
     */
    public FileNameFromBlobBuilder(String sqlOrder, int parameterIndex,
	    boolean isClob) {
	if (sqlOrder == null) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "sqlOrder can not be null!");
	}

	if (parameterIndex < 1) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "Prepared statement parameterIndex must be >= 1");
	}

	this.sqlOrder = sqlOrder;
	this.parameterIndex = parameterIndex;
	this.isClob = isClob;
    }

    /**
     * @return the raw file name of the file to create from the blob/clob column
     *         with the content
     */
    public String getFileName() {

	String fileName = null;
	String unique = AwakeFileUtil.getUniqueId();

//	StatementAnalyser analyser = new StatementAnalyser(sqlOrder,
//	new Vector());	
	//String tableName = analyser.getTableNameFromDmlStatement();
	//String statementType = analyser.getStatementType();
	
	String statementType = StringUtils.substringBefore(sqlOrder, BLANK);	
	String tableName = getTableNameFromDmlStatement(statementType, sqlOrder);
	
	if (tableName == null) {
	    tableName = "unknown";
	}

	if (statementType == null) {
	    statementType = "unknown";
	}

	String blobHeader = "blob-";
	if (isClob) {
	    blobHeader = "clob-";
	}

	if (columnName != null) {
	    fileName = blobHeader + statementType.toLowerCase() + "-"
		    + tableName.toLowerCase() + "." + columnName + "-" + unique
		    + ".awake";
	} else {
	    fileName = blobHeader + statementType.toLowerCase() + "-"
		    + tableName.toLowerCase() + "-index-" + parameterIndex
		    + "-" + unique + ".awake";
	}

	if (isClob) {
	    fileName += ".txt";
	}

	return fileName;
    }
    
    /**
     * Returns the table name in use type from a DML SQL order.
     * 
     * @param statementType the statement type (INSERT, ...)
     * @param sql	the sql order
     * 
     * @return the table name in use (the first one in a SELECT
     *         statement) for a DML statement. Returns null if statement is not
     *         DML.
     */
    public String getTableNameFromDmlStatement(String statementType, String sql)
	    throws IllegalArgumentException {
	// Extract the first order
	String statementTypeUpper = statementType.toUpperCase();

	String sqlUpper = sql.toUpperCase();

	// Extract the table depending on the ordOer
	sqlUpper = StringUtils.substringAfter(sqlUpper, statementTypeUpper);
	sqlUpper = sqlUpper.trim();

	String table = null;

	if (statementTypeUpper.equals(INSERT)) {
	    sqlUpper = StringUtils.substringAfter(sqlUpper, "INTO ");
	    sqlUpper = sqlUpper.trim();
	    table = StringUtils.substringBefore(sqlUpper, " ");
	} else if (statementTypeUpper.equals(SELECT)
		|| statementTypeUpper.equals(DELETE)) {
	    sqlUpper = StringUtils.substringAfter(sqlUpper, "FROM ");
	    sqlUpper = sqlUpper.trim();
	    // Remove commas in the statement and replace with blanks in case we
	    // have
	    // a join: "TABLE," ==> "TABLE "
	    sqlUpper = sqlUpper.replaceAll(",", " ");
	    table = StringUtils.substringBefore(sqlUpper, BLANK);
	} else if (statementTypeUpper.equals(UPDATE)) {
	    //debug("sqlLocal :" + sqlUpper + ":");
	    table = StringUtils.substringBefore(sqlUpper, BLANK);
	} else {
	    return null; // No table
	}

	if (table != null) {
	    table = table.trim();
	}

	return table;
    }
    
    
    
}