pingbu.search.SqlIndex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pingbu-search Show documentation
Show all versions of pingbu-search Show documentation
A multiple condition search library
The newest version!
package pingbu.search;
import java.util.ArrayList;
import java.util.List;
class SqlIndex implements SearchIndex {
private static void __appendValue(final StringBuilder s, String value,
String format) {
if (format == null || format.equalsIgnoreCase("String")) {
s.append('\"');
s.append(value.replace("\"", "\"\""));
s.append('\"');
} else if (format.equalsIgnoreCase("Bool") || format.equalsIgnoreCase("Boolean")) {
s.append(Boolean.parseBoolean(value) ? "1" : "0");
} else {
s.append(value);
}
}
private static String __formatSQL(String fmt, String value) {
final String[] values = value.split(",");
final StringBuilder sql = new StringBuilder();
for (int p = 0; ; ) {
final int a = fmt.indexOf('{', p) + 1;
if (a <= 0) {
sql.append(fmt.substring(p));
break;
}
final int b = fmt.indexOf('}', a + 1);
if (b < 0)
throw new RuntimeException();
sql.append(fmt.substring(p, a - 1));
final String slot = fmt.substring(a, b);
final int q = slot.indexOf(':');
if (q < 0)
__appendValue(sql, values[Integer.parseInt(slot)], null);
else
__appendValue(sql, values[Integer.parseInt(slot.substring(0, q))], slot.substring(q + 1));
p = b + 1;
}
return sql.toString();
}
private final String mConnStr, mSqlFmt;
public SqlIndex(final String connStr, final String sqlFmt) {
mConnStr = connStr;
mSqlFmt = sqlFmt;
}
@Override
public void addItem(final int id, final String value) {
throw new RuntimeException();
}
@Override
public Iterator iterate(final String value) {
final String[] values = value.split("\\|");
if (values.length == 1)
return new SqlIterator(mConnStr, __formatSQL(mSqlFmt, value));
final List iterators = new ArrayList();
for (String v : values)
iterators.add(new SqlIterator(mConnStr, __formatSQL(mSqlFmt, v)));
return new MultiIterator(iterators);
}
}