⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractselectsql.java

📁 羽量级数据持久层开发框架
💻 JAVA
字号:
package org.speedframework.sql;

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

import org.speedframework.exception.SpeedFrameworkException;

/**
 * Class AbstractSelectSQL
 * 
 * @author <a href="mailto:santafeng@gmail.com"> lizf </a>
 * @version $Revision:1.0.0, $Date: 2007-11-1 上午03:22:57 $
 */
public abstract class AbstractSelectSQL extends AbstractCommonSQL implements
		ISelectSQL {
	public String getCountSQL(String sql) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("select count(1) as count_ from ( " + sql
				+ " ) as  row_ ");
		return stringBuffer.toString();
	}

	public String getSQL(String autoIncrement) {
		return this.getSQL(new String[] { autoIncrement });
	}

	public String getSQL(String[] columns) {
		StringBuffer sel_sql = new StringBuffer();
		StringBuffer rel_sql = new StringBuffer();

		if (columns.length > 0) {
			for (int i = 0; i < columns.length; i++) {
				sel_sql.append(" and " + columns[i] + "=? ");
			}
		} else {
			throw new SpeedFrameworkException(
					"Could not define the identifier of the Database table by "
							+ this.getTableName() + " .");
		}

		rel_sql.append("select * from " + this.getTableName() + " where 1=1 ");

		return rel_sql.append(sel_sql.toString()).toString();
	}

	// public String getCountSQL(String sql) {
	// StringBuffer stringBuffer = new StringBuffer();
	// String condistion = "";
	// String from = "";
	//
	// sql = sql.toLowerCase();
	//
	// int column_end = sql.indexOf("from");
	// int condistion_where = sql.lastIndexOf("where");
	//
	// if (condistion_where < column_end) {
	// from = sql.substring(sql.lastIndexOf("from") + 4, sql.length())
	// .trim();
	//
	// if ((from.indexOf("order by") != -1)
	// || (from.indexOf("group by") != -1)) {
	// int order_index = from.indexOf("order by");
	// int group_index = from.indexOf("group by");
	//
	// if ((order_index != -1) && (group_index != -1)) {
	// if (order_index > group_index) {
	// from = from.substring(0, order_index);
	// } else {
	// from = from.substring(0, group_index);
	// }
	// } else {
	// if (order_index != -1) {
	// from = from.substring(0, order_index);
	// }
	//
	// if (group_index != -1) {
	// from = from.substring(0, group_index);
	// }
	// }
	// }
	// } else {
	// from = sql.substring(sql.lastIndexOf("from") + 4, condistion_where)
	// .trim();
	// condistion = sql.substring(condistion_where + 5, sql.length())
	// .trim();
	//
	// if ((condistion.indexOf("order by") != -1)
	// || (condistion.indexOf("group by") != -1)) {
	// int order_index = condistion.indexOf("order by");
	// int group_index = condistion.indexOf("group by");
	//
	// if ((order_index != -1) && (group_index != -1)) {
	// if (order_index > group_index) {
	// condistion = condistion.substring(0, order_index);
	// } else {
	// condistion = condistion.substring(0, group_index);
	// ;
	// }
	// } else {
	// if (order_index != -1) {
	// condistion = condistion.substring(0, order_index);
	// }
	//
	// if (group_index != -1) {
	// condistion = condistion.substring(0, group_index);
	// }
	// }
	// }
	// }
	//
	// if (condistion.length() > 0) {
	// stringBuffer.append("select count(1) as count_ from " + from
	// + " where 1=1 and " + condistion);
	// } else {
	// stringBuffer.append("select count(1) as count_ from " + from);
	// }
	//
	// // stringBuffer.append(sql.substring(sql.indexOf("from"),
	// // sql.length()));
	// return stringBuffer.toString();
	// }

	public String getSelectSQLByID(Class loadclass, String[] columns) {
		StringBuffer sel_sql = new StringBuffer();
		StringBuffer rel_sql = new StringBuffer();

		if (columns.length > 0) {
			for (int i = 0; i < columns.length; i++) {
				sel_sql.append(" and " + columns[i] + "=? ");
			}

			// if (columns != null) {
			// int key_count = columns.size();
			// if (key_count != 0) {
			// Iterator it = columns.iterator();
			// while (it.hasNext()) {
			// String colname = (String) it.next();
			// sel_sql.append(" and " + colname + "=? ");
			// }
			// }
		} else {
			throw new SpeedFrameworkException(
					"Could not define the identifier of the table ");
		}

		rel_sql.append("select * from " + this.getTableName() + " where 1=1 ");

		return rel_sql.append(sel_sql.toString()).toString();
	}

	public List getParam(String sql) {
		List parmList = new ArrayList();
		String[] splitListForWhere = sql.toLowerCase().split("where");

		for (int j = 0; j < splitListForWhere.length; j++) {
			String[] splitListForAnd = splitListForWhere[j].split("and");

			for (int i = 0; i < splitListForAnd.length; i++) {
				String[] splitList = null;

				if (splitListForAnd[i].indexOf("?") != -1) {
					if (splitListForAnd[i].indexOf("=") != -1) {
						splitList = splitListForAnd[i].split("=");
					} else if (splitListForAnd[i].indexOf("like") != -1) {
						splitList = splitListForAnd[i].split("like");
					} else if (splitListForAnd[i].indexOf("in") != -1) {
						splitList = splitListForAnd[i].split("in");
					}

					String strSplit = splitList[0].trim();

					strSplit = strSplit.substring(strSplit.indexOf(".") + 1,
							strSplit.length());
					parmList.add(strSplit);
				}
			}
		}

		return parmList;
	}

	public String getSQL() {
		// TODO 自动生成方法存根
		return null;
	}

	public Object[] getQueryLimitParam(Object[] param, int index, int max) {
		Object[] new_param = null;
		if (param != null) {
			int length = param.length;
			new_param = new String[length + 2];
			for (int i = 0; i < length; i++) {
				new_param[i] = param[i];
			}
			if (max != 0) {
				new_param[length]= Integer.toString(0);
				new_param[length + 1] = Integer.toString(max);
			}
		}
		return new_param;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -