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

com.litongjava.db.activerecord.PageSqlKit Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
package com.litongjava.db.activerecord;


/**
 * PageSqlKit
 */
public class PageSqlKit {
	
	private static final int start = "select ".length();
	
	private static final char NULL = 0;
	private static final char SIZE = 128;
	private static char[] charTable = buildCharTable();
	
	private static char[] buildCharTable() {
		char[] ret = new char[SIZE];
		for (char i=0; i= SIZE) {
				continue ;
			}
			
			c = charTable[c];
			if (c == NULL) {
				continue ;
			}
			
			if (c == '(') {
				parenDepth++;
				continue ;
			}
			
			if (c == ')') {
				if (parenDepth == 0) {
					throw new RuntimeException("Can not match left paren '(' for right paren ')': " + sql);
				}
				parenDepth--;
				continue ;
			}
			if (parenDepth > 0) {
				continue ;
			}
			
			if (c == 'f'
				&& charTable[sql.charAt(i + 1)] == 'r'
				&& charTable[sql.charAt(i + 2)] == 'o'
				&& charTable[sql.charAt(i + 3)] == 'm') {
				c = sql.charAt(i + 4);
				// 测试用例: "select count(*)from(select * from account limit 3) as t"
				if (charTable[c] == ' ' || c == '(') {		// 判断 from 后方字符
					c = sql.charAt(i - 1);
					if (charTable[c] == ' ' || c == ')') {	// 判断 from 前方字符
						return i;
					}
				}
			}
		}
		return -1;
	}
	
	public static String[] parsePageSql(String sql) {
		int index = getIndexOfFrom(sql);
		if (index == -1) {
			return null;
		}
		
		String[] ret = new String[2];
		ret[0] = sql.substring(0, index);
		ret[1] = sql.substring(index);
		return ret;
	}
}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy