com.jfinal.template.activerecord.PageSqlKit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enjoy-sql Show documentation
Show all versions of enjoy-sql Show documentation
the jfinal enjoy plugin for normal java program to use sql management function.
/**
* Copyright (c) 2011-2017, James Zhan 詹波 ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jfinal.template.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;
}
}