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 ResultSet.
TODO This will be changed in the future (It's proved starting at 0 is more bug free).
Author:
odys-z@github.com
  • Constructor Details

  • Method Details

    • currentRow

      public int currentRow()
      current row index, start from 1. If used for getRowAt(int), must - 1.
      Returns:
      current row index
      Since:
      1.4.25
    • colnames

      public HashMap<String,Object[]> colnames()
    • indices0

      public HashMap<String,Integer> indices0()
    • rowIndex0

      public int rowIndex0(String k)
      Get row index, starting at 0.
      Parameters:
      k -
      Returns:
      index starting at 0
    • ICRconstructor

      public void ICRconstructor(ResultSet rs) throws SQLException
      Throws:
      SQLException
    • appendDeeply

      public AnResultset appendDeeply(ArrayList<Object> row)
      Append a new row - deep copy, set current row as the appended row.
      Parameters:
      row -
      Returns:
      this
    • append

      public AnResultset append(ArrayList<Object> includingRow)
    • results

      public AnResultset results(ArrayList<ArrayList<Object>> rows)
    • getColnames

      public HashMap<String,Object[]> getColnames()
      Returns:
      column names
    • hasCol

      public boolean hasCol(String c)
    • getRows

      public ArrayList<ArrayList<Object>> getRows()
    • next

      public boolean next() throws SQLException
      Throws:
      SQLException
    • nextUntill

      public boolean nextUntill(int last) throws SQLException
      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

      public int append(AnResultset more) throws SQLException
      Throws:
      SQLException
    • stringFormat

      public AnResultset stringFormat(Class<?> clz, String format)
      Add a formatter to type of clz when converting to String.
      Parameters:
      clz -
      format -
      Returns:
      this
    • getString

      public String getString(int colIndex) throws SQLException
      Throws:
      SQLException
    • getString

      public String getString(String colName) throws SQLException
      Throws:
      SQLException
    • getString

      public String getString(String colName, SimpleDateFormat sdf) throws SQLException
      If field is a date value, return string formatted by sdf.
      Parameters:
      colName -
      sdf -
      Returns:
      string value
      Throws:
      SQLException
    • getString

      public String getString(int colIndex, SimpleDateFormat sdf) throws SQLException
      If field is a date value, return string formatted by sdf.
      Parameters:
      colIndex -
      sdf -
      Returns:
      string value
      Throws:
      SQLException
    • getStringNonull

      public String getStringNonull(String colName) throws SQLException
      if null, change to ""
      Parameters:
      colName -
      Returns:
      string value
      Throws:
      SQLException
    • getString

      public String getString(String colName, String deflt) throws SQLException
      Get string. If not exists, return deflt.
      Parameters:
      colName -
      deflt -
      Returns:
      string value or deflt
      Throws:
      SQLException
    • getStringAtRow

      public String getStringAtRow(String colName, int row) throws NumberFormatException, SQLException
      Get row's field value
      Parameters:
      colName - field name
      row - row index, start at 1. (If get from rowIndex0(String), add 1.)
      Returns:
      string value
      Throws:
      NumberFormatException
      SQLException
    • getStringAtRow

      public String getStringAtRow(int col, int row) throws NumberFormatException, SQLException
      Throws:
      NumberFormatException
      SQLException
    • getStringByIndex

      public String getStringByIndex(String colName, String entityId) throws SQLException
      Throws:
      SQLException
    • getBoolean

      public boolean getBoolean(int colIndex) throws SQLException
      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

      public boolean getBoolean(String colName) throws SQLException
      Throws:
      SQLException
    • getDouble

      public double getDouble(int colIndex) throws SQLException
      Throws:
      SQLException
    • getDouble

      public double getDouble(String colName) throws SQLException
      Throws:
      SQLException
    • getDate

      public Date getDate(int index) throws SQLException
      Throws:
      SQLException
    • getDate

      public Date getDate(String colName) throws SQLException
      Throws:
      SQLException
    • getDateTime

      public Date getDateTime(int index) throws SQLException
      Parameters:
      index -
      Returns:
      datetime
      Throws:
      SQLException
    • getDateTime

      public Date getDateTime(String colName) throws SQLException
      Throws:
      SQLException
    • getInt

      public int getInt(int colIndex) throws SQLException
      Throws:
      SQLException
    • getInt

      public int getInt(String col, int deflt)
    • getLong

      public long getLong(int colIndex) throws SQLException
      Throws:
      SQLException
    • getLong

      public long getLong(String size, long empty)
    • getLong

      public long getLong(String colName) throws SQLException
      Throws:
      SQLException
    • getLongAt

      public long getLongAt(String colName, int row0) throws NumberFormatException, SQLException
      Parameters:
      colName -
      row0 - index start at 0
      Returns:
      v
      Throws:
      NumberFormatException
      SQLException
    • getLongAtRow

      public long getLongAtRow(int col, int row0) throws NumberFormatException, SQLException
      Parameters:
      col - column index start at 0
      row0 - index start at 0
      Returns:
      v
      Throws:
      NumberFormatException
      SQLException
    • getInt

      public int getInt(String colName) throws SQLException
      Throws:
      SQLException
    • getBlob

      public Blob getBlob(int colIndex) throws SQLException
      Throws:
      SQLException
    • getBlob

      public Blob getBlob(String colName) throws SQLException
      Throws:
      SQLException
    • getObject

      public Object getObject(String colName) throws SQLException
      Throws:
      SQLException
    • getObject

      public Object getObject(int colIndex) throws SQLException
      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.AnsonException
      SQLException
      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.AnsonException
      SQLException
      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

      public void first() throws SQLException
      Throws:
      SQLException
    • beforeFirst

      public AnResultset beforeFirst() throws SQLException
      Throws:
      SQLException
    • before

      public AnResultset before(int idx) throws SQLException
      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

      public void close() throws SQLException
      Throws:
      SQLException
    • previous

      public boolean previous() throws SQLException
      Throws:
      SQLException
    • getColumnName

      public String getColumnName(int i)
      Get col name in raw case.
      Parameters:
      i - start at 1
      Returns:
      column name or null
    • getColumex

      public int getColumex(String colName)
      Get col index
      Parameters:
      colName -
      Returns:
      col index
    • setColumnName

      public void setColumnName(int i, String n)
      Set column raw name.
      Parameters:
      i - index
      n - name
    • getRowCount

      public int getRowCount()
    • size

      public int size()
    • getColCount

      public int getColCount()
    • getRowAt

      public ArrayList<Object> getRowAt(int idx0) throws SQLException
      Parameters:
      idx0 - start at 0
      Returns:
      row
      Throws:
      SQLException
    • set

      public AnResultset set(int colIndex, String v) throws SQLException
      Set value to current row
      Parameters:
      colIndex -
      v -
      Returns:
      this
      Throws:
      SQLException
    • set

      public AnResultset set(String colName, String v) throws SQLException
      Set value to current row
      Parameters:
      colName -
      v -
      Returns:
      this
      Throws:
      SQLException
    • findFirst

      public int findFirst(String col, String regex) throws SQLException
      Find the first row that contain a matched value in field col. The matching is done with regex.
      Parameters:
      col -
      regex -
      Returns:
      row index or 0
      Throws:
      SQLException
    • getRowCells

      public ArrayList<Object> getRowCells()
    • printSomeData

      public int printSomeData(boolean err, int max, String... includeCols)
      Print ResutSet in System.out or System.err.
      Parameters:
      err - weather output in "out" or "err"
      max - max rows to print
      includeCols - include column of names.
      Returns:
      size
    • printSomeData

      public int printSomeData(PrintStream out, int max, String... includeCols)
    • print

      public AnResultset print(String... cols)
      Print all data with in cols.
      Parameters:
      cols -
      Returns:
      this;
    • print

      public AnResultset print(PrintStream out)
    • collectFields

      public static String collectFields(AnResultset rs, String... fields) throws SQLException
      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

      public String getString(int rowix, String field) throws SQLException
      Throws:
      SQLException
    • total

      public int total()
    • total

      public AnResultset total(int total)
    • getRowsInt

      public ArrayList<?> getRowsInt()
      Try in-place convert all values to integer elements - expensive, especially with many non-integers.
      Returns:
      list
    • getStrArray

      public String[] getStrArray(String udpcols) throws SQLException
      Throws:
      SQLException
    • toArr

      public List<String> toArr(String col) throws SQLException
      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 to select().
      Parameters:
      keyField - value of the field name used for map's key
      objCreator - 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

      public HashSet<String> set(String f) throws SQLException
      Construct a hash set using all value of field f.
      Parameters:
      f -
      Returns:
      set
      Throws:
      SQLException
      Since:
      1.4.12
    • index0

      public AnResultset index0(String pk)
      Generate row indices, start at 0. FIXME move this method to Query and be called before rs construction.
      Parameters:
      pk -
      Returns:
      this
    • nxt

      public AnResultset nxt() throws SQLException
      A mutation of next(). 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

      public String nextString(String col) throws SQLException
      Get next row's string value. Call hasnext() before calling this.
      Parameters:
      col -
      Returns:
      string value
      Throws:
      SQLException
      Since:
      1.4.40
    • prevString

      public String prevString(String col) throws SQLException
      Get previous row's string value. Call hasprev() 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

      public ArrayList<Object> getRowAt() throws SQLException
      Get current row
      Returns:
      current row
      Throws:
      SQLException
      Since:
      1.4.40
    • getFlatColumns0

      public String[] 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

      public ArrayList<Object> getRowById(String id) throws SQLException
      Throws:
      SQLException
    • getFieldArray

      public Object[] getFieldArray(String... fields) throws SQLException
      Get an object array of fields in the current row.
      Parameters:
      fields -
      Returns:
      [rs.field[0], rs.field[1], ...]
      Throws:
      SQLException