Package io.odysz.module.rs
Class AnResultset
java.lang.Object
io.odysz.anson.Anson
io.odysz.module.rs.AnResultset
- All Implemented Interfaces:
io.odysz.anson.IJsonable
public class AnResultset
extends io.odysz.anson.Anson
This Resultset is used for non-connected manipulation.
Rows and Cols are start at 1, the same as
TODO This will be changed in the future (It's proved starting at 0 is more bug free).
ResultSet.TODO This will be changed in the future (It's proved starting at 0 is more bug free).
- Author:
- odys-z@github.com
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceObject creator for converting an entity record to a user type instance.Nested classes/interfaces inherited from interface io.odysz.anson.IJsonable
io.odysz.anson.IJsonable.JsonableFactory -
Field Summary
Fields inherited from class io.odysz.anson.Anson
verbose -
Constructor Summary
ConstructorsConstructorDescriptionfor deserializingAnResultset(int rows, int cols) AnResultset(int rows, int cols, String colPrefix) For debug, generate a fake rs.AnResultset(int rows, String[] colNames, boolean generateData) AnResultset(AnResultset icrs) AnResultset(String[] colNames) AnResultset(ResultSet rs) AnResultset(ResultSet rs, Connection connection, Statement statement) AnResultset(ArrayList<String> colNames) Construct an empty set, used for appending rows.AnResultset(HashMap<String, Object[]> columns) AnResultset(HashMap<String, Object[]> colnames, boolean toUpperCase) Construct an empty set, used for appending rows. -
Method Summary
Modifier and TypeMethodDescriptionintappend(AnResultset more) appendDeeply(ArrayList<Object> row) Append a new row - deep copy, set current row as the appended row.before(int idx) idx start from 1.voidclose()static StringcollectFields(AnResultset rs, String... fields) Collect fields value that can be used in "IN" condition, e.g.colnames()intcurrent row index, start from 1.intFind the first row that contain a matched value in field col.voidfirst()<T extends io.odysz.transact.sql.parts.AnDbField>
TgetAnson(int colIndex) <T extends io.odysz.transact.sql.parts.AnDbField>
TgetBlob(int colIndex) booleangetBoolean(int colIndex) if value is equals case insensitive to 1,true, yes, y, t, decimal > 0.001 return true, else return false;booleangetBoolean(String colName) intintgetColumex(String colName) Get col indexintgetColumnName(int i) Get col name in raw case.getDate(int index) getDateTime(int index) getDateTime(String colName) doublegetDouble(int colIndex) doubleObject[]getFieldArray(String... fields) Get an object array of fields in the current row.String[]Get the cached flat column names in the same sequence with rows.intgetInt(int colIndex) intintlonggetLong(int colIndex) longlonglonglonggetLongAtRow(int col, int row0) getObject(int colIndex) intgetRow()Get current row index.
Row index start from 1.
The last row indix == getRowCount()getRowAt()Get current rowgetRowAt(int idx0) getRowById(String id) intgetRows()ArrayList<?>Try in-place convert all values to integer elements - expensive, especially with many non-integers.String[]getStrArray(String udpcols) getString(int colIndex) getString(int colIndex, SimpleDateFormat sdf) If field is a date value, return string formatted by sdf.Get string.getString(String colName, SimpleDateFormat sdf) If field is a date value, return string formatted by sdf.getStringAtRow(int col, int row) getStringAtRow(String colName, int row) Get row's field valuegetStringByIndex(String colName, String entityId) getStringNonull(String colName) if null, change to ""booleanbooleanhasnext()Are there rows following current row index?booleanhasprev()Are there rows before current row index?voidGenerate row indices, start at 0.indices0()map(String[] keyFields, AnResultset.ObjCreator<T> objCreator) map(String keyField, AnResultset.ObjCreator<T> objCreator) Iterating through the results and convert to hash map, like this:booleannext()nextString(String col) Get next row's string value.booleannextUntill(int last) last start at 1, included in nexting range.
If current index = 4, nextUntill(5) return true;
If current index = 5, nextUntill(5) return false;nxt()A mutation ofnext().booleanprevious()prevString(String col) Get previous row's string value.print(PrintStream out) Print all data with in cols.intprintSomeData(boolean err, int max, String... includeCols) Print ResutSet in System.out or System.err.intprintSomeData(PrintStream out, int max, String... includeCols) intGet row index, starting at 0.Set value to current rowConstruct a hash set using all value of field f.Set value to current rowvoidsetColumnName(int i, String n) Set column raw name.intsize()stringFormat(Class<?> clz, String format) Add a formatter to type of clz when converting to String.Convert results to an 1D array with elements from colinttotal()total(int total) booleanvalidx()Is current row index valid?Methods inherited from class io.odysz.anson.Anson
appendArr, appendObjStr, appendPair, escape, fromBlock, fromJson, fromJson, toBlock, toEnvelope, toJson, toJson, toMapBlock, toString, unescapeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.odysz.anson.IJsonable
toBlock
-
Constructor Details
-
AnResultset
public AnResultset()for deserializing -
AnResultset
- Throws:
SQLException
-
AnResultset
- Throws:
SQLException
-
AnResultset
- Throws:
SQLException
-
AnResultset
@SafeVarargs public AnResultset(HashMap<String, Integer> colnames, ArrayList<ArrayList<Object>>... rows) Construct an empty set, used for appending rows.- Parameters:
colnames-rows-
-
AnResultset
Construct an empty set, used for appending rows. Cols are deep copied.- Parameters:
colnames-
-
AnResultset
For debug, generate a fake rs.- Parameters:
rows-cols-
-
AnResultset
- Throws:
Exception
-
AnResultset
-
AnResultset
-
AnResultset
-
AnResultset
-
-
Method Details
-
currentRow
public int currentRow()current row index, start from 1. If used forgetRowAt(int), must - 1.- Returns:
- current row index
- Since:
- 1.4.25
-
colnames
-
indices0
-
rowIndex0
Get row index, starting at 0.- Parameters:
k-- Returns:
- index starting at 0
-
ICRconstructor
- Throws:
SQLException
-
appendDeeply
Append a new row - deep copy, set current row as the appended row.- Parameters:
row-- Returns:
- this
-
append
-
results
-
getColnames
- Returns:
- column names
-
hasCol
-
getRows
-
next
- Throws:
SQLException
-
nextUntill
last start at 1, included in nexting range.
If current index = 4, nextUntill(5) return true;
If current index = 5, nextUntill(5) return false;- Parameters:
last-- Returns:
- true: ok
- Throws:
SQLException
-
append
- Throws:
SQLException
-
stringFormat
Add a formatter to type of clz when converting to String.- Parameters:
clz-format-- Returns:
- this
-
getString
- Throws:
SQLException
-
getString
- Throws:
SQLException
-
getString
If field is a date value, return string formatted by sdf.- Parameters:
colName-sdf-- Returns:
- string value
- Throws:
SQLException
-
getString
If field is a date value, return string formatted by sdf.- Parameters:
colIndex-sdf-- Returns:
- string value
- Throws:
SQLException
-
getStringNonull
if null, change to ""- Parameters:
colName-- Returns:
- string value
- Throws:
SQLException
-
getString
Get string. If not exists, returndeflt.- Parameters:
colName-deflt-- Returns:
- string value or
deflt - Throws:
SQLException
-
getStringAtRow
Get row's field value- Parameters:
colName- field namerow- row index, start at 1. (If get fromrowIndex0(String), add 1.)- Returns:
- string value
- Throws:
NumberFormatExceptionSQLException
-
getStringAtRow
- Throws:
NumberFormatExceptionSQLException
-
getStringByIndex
- Throws:
SQLException
-
getBoolean
if value is equals case insensitive to 1,true, yes, y, t, decimal > 0.001 return true, else return false;- Parameters:
colIndex-- Returns:
- string value
- Throws:
SQLException
-
getBoolean
- Throws:
SQLException
-
getDouble
- Throws:
SQLException
-
getDouble
- Throws:
SQLException
-
getDate
- Throws:
SQLException
-
getDate
- Throws:
SQLException
-
getDateTime
- Parameters:
index-- Returns:
- datetime
- Throws:
SQLException
-
getDateTime
- Throws:
SQLException
-
getInt
- Throws:
SQLException
-
getInt
-
getLong
- Throws:
SQLException
-
getLong
-
getLong
- Throws:
SQLException
-
getLongAt
- Parameters:
colName-row0- index start at 0- Returns:
- v
- Throws:
NumberFormatExceptionSQLException
-
getLongAtRow
- Parameters:
col- column index start at 0row0- index start at 0- Returns:
- v
- Throws:
NumberFormatExceptionSQLException
-
getInt
- Throws:
SQLException
-
getBlob
- Throws:
SQLException
-
getBlob
- Throws:
SQLException
-
getObject
- Throws:
SQLException
-
getObject
- Throws:
SQLException
-
getAnson
public <T extends io.odysz.transact.sql.parts.AnDbField> T getAnson(int colIndex) throws io.odysz.anson.x.AnsonException, SQLException - Type Parameters:
T-- Parameters:
colIndex-- Returns:
- Anson instance (value unescaped)
- Throws:
io.odysz.anson.x.AnsonExceptionSQLException- Since:
- 1.4.25, 1.4.27
-
getAnson
public <T extends io.odysz.transact.sql.parts.AnDbField> T getAnson(String col) throws io.odysz.anson.x.AnsonException, SQLException - Parameters:
col-- Returns:
- Anson instance (value unescaped)
- Throws:
io.odysz.anson.x.AnsonExceptionSQLException- Since:
- 1.4.27
-
getRow
public int getRow()Get current row index.
Row index start from 1.
The last row indix == getRowCount()- Returns:
- row index
-
getColumnCount
public int getColumnCount() -
first
- Throws:
SQLException
-
beforeFirst
- Throws:
SQLException
-
before
idx start from 1. before(1) == beforeFirst().
As java.sql.Resultset don't support this method, calling this will throw an exception if this object are created from a java.sql.Resultset.- Parameters:
idx-- Returns:
- this
- Throws:
SQLException
-
close
- Throws:
SQLException
-
previous
- Throws:
SQLException
-
getColumnName
Get col name in raw case.- Parameters:
i- start at 1- Returns:
- column name or null
-
getColumex
Get col index- Parameters:
colName-- Returns:
- col index
-
setColumnName
Set column raw name.- Parameters:
i- indexn- name
-
getRowCount
public int getRowCount() -
size
public int size() -
getColCount
public int getColCount() -
getRowAt
- Parameters:
idx0- start at 0- Returns:
- row
- Throws:
SQLException
-
set
Set value to current row- Parameters:
colIndex-v-- Returns:
- this
- Throws:
SQLException
-
set
Set value to current row- Parameters:
colName-v-- Returns:
- this
- Throws:
SQLException
-
findFirst
Find the first row that contain a matched value in field col. The matching is done withregex.- Parameters:
col-regex-- Returns:
- row index or 0
- Throws:
SQLException
-
getRowCells
-
printSomeData
Print ResutSet in System.out or System.err.- Parameters:
err- weather output in "out" or "err"max- max rows to printincludeCols- include column of names.- Returns:
- size
-
printSomeData
-
print
Print all data with in cols.- Parameters:
cols-- Returns:
- this;
-
print
-
collectFields
Collect fields value that can be used in "IN" condition, e.g. 'v1', 'v2', ...- Parameters:
rs-fields-- Returns:
- ['row0 field-val', 'row1 field-val', ...]
- Throws:
SQLException
-
getString
- Throws:
SQLException
-
total
public int total() -
total
-
getRowsInt
Try in-place convert all values to integer elements - expensive, especially with many non-integers.- Returns:
- list
-
getStrArray
- Throws:
SQLException
-
toArr
Convert results to an 1D array with elements from col- Parameters:
col- column name- Returns:
- list of string
- Throws:
SQLException
-
map
public <T> HashMap<String,T> map(String keyField, AnResultset.ObjCreator<T> objCreator) throws SQLException Iterating through the results and convert to hash map, like this:HashMap <String, SynState> res = st .select(met.tbl, "l") .rs(st.instancontxt(conn, usr)) .rs(0) .<UserType>map((currow) -> { // create instance according current row return new UserType(currow.getString("id")); });TODO: This is a temporary way. Which will be moved toselect().- Parameters:
keyField- value of the field name used for map's keyobjCreator- object creator (mapper)- Returns:
- objects' map
- Throws:
SQLException- Since:
- 1.4.12
-
map
public <T> HashMap<String,T> map(String[] keyFields, AnResultset.ObjCreator<T> objCreator) throws SQLException - Throws:
SQLException
-
set
Construct a hash set using all value of field f.- Parameters:
f-- Returns:
- set
- Throws:
SQLException- Since:
- 1.4.12
-
index0
Generate row indices, start at 0. FIXME move this method to Query and be called before rs construction.- Parameters:
pk-- Returns:
- this
-
nxt
A mutation ofnext(). If has a next row, move index and return this, otherwise null.For convenience if only needs to check the first row.
E.g. to check the updating records' existence:return ((AnResultset) transbuilder .select(targetable) .col(Funcall.count(pk), "c") .where(updt.where()) .rs(syb.instancontxt(stx.connId(), usr)) .rs(0)) .nxt() .getInt("c") > 0;- Returns:
- this or null
- Throws:
SQLException- Since:
- 1.4.25
-
hasnext
public boolean hasnext()Are there rows following current row index?- Returns:
- true if there are
-
hasprev
public boolean hasprev()Are there rows before current row index?- Returns:
- true if there are
-
nextString
Get next row's string value. Callhasnext()before calling this.- Parameters:
col-- Returns:
- string value
- Throws:
SQLException- Since:
- 1.4.40
-
prevString
Get previous row's string value. Callhasprev()before calling this.- Parameters:
col-- Returns:
- value
- Throws:
SQLException- Since:
- 1.4.40
-
validx
public boolean validx()Is current row index valid?- Returns:
- true if accessing currrent row is valid.
- Since:
- 1.4.40
-
getRowAt
Get current row- Returns:
- current row
- Throws:
SQLException- Since:
- 1.4.40
-
getFlatColumns0
Get the cached flat column names in the same sequence with rows. (cached for performance)- Returns:
- column names, index start at 0
- Since:
- 2.0.0 ISSUE This will throw the out-of-bound exception if the select statament who constructed this resultset has ignored same name fields. It's planned to not ignore columns in the future.
-
getRowById
- Throws:
SQLException
-
getFieldArray
Get an object array of fields in the current row.- Parameters:
fields-- Returns:
- [rs.field[0], rs.field[1], ...]
- Throws:
SQLException
-