Package ortus.boxlang.runtime.types
Class Query
java.lang.Object
ortus.boxlang.runtime.types.Query
- All Implemented Interfaces:
Serializable
,Iterable<IStruct>
,Collection<IStruct>
,IReferenceable
,IType
public class Query
extends Object
implements IType, IReferenceable, Collection<IStruct>, Serializable
This type represents a representation of a database query result set.
It provides language specific methods to access columnar data, both as value lists and within iterative loops
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends IStruct> rows) addColumn
(Key name, QueryColumnType type) Add a column to the query, populated with nullsaddColumn
(Key name, QueryColumnType type, Object[] columnData) Add a column to the query, populated with provided data.int
Helper method for queryNew() and queryAddRow() to handle the different scenarios for adding data to a queryint
Add an empty row to the queryint
Add a row to the queryint
Add a row to the queryint
Add a row to the queryint
addRows
(int rows) Add empty rows to the queryassign
(IBoxContext context, Key name, Object value) Assign a value to a key in this objectasString()
IType implementationvoid
clear()
boolean
boolean
containsAll
(Collection<?> c) void
deleteColumn
(Key name) Deletes a column from the query.deleteRow
(int index) Delete a row from the querydereference
(IBoxContext context, Key name, Boolean safe) IReferencable implementationdereferenceAndInvoke
(IBoxContext context, Key name, Object[] positionalArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method) using positional argumentsdereferenceAndInvoke
(IBoxContext context, Key name, Map<Key, Object> namedArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method) using named argumentsDuplicate the current query.duplicate
(boolean deep) Duplicate the current query.static Query
Create a new query with columns and datastatic Query
fromResultSet
(ResultSet resultSet) Create a new query and populate it from the given JDBC ResultSet.Get data for a single cell.Get the QueryColumn object for a column Throws an exception if the column doesn't existGet the list of column names as an arrayObject[]
getColumnData
(Key name) Get all data in a column as a Java Object[] Data is copied, so re-assignments into the array will not be reflected in the query.getColumnDataAsArray
(Key name) Get all data in a column as an BoxLang Array Data is copied, so re-assignments into the array will not be reflected in the query.int
getColumnIndex
(Key name) Get the index of a columnGet the list of column names as a comma-separated string TODO: Look into caching this and invalidating when columns are added/removedGet the list of column definitions for this querygetData()
Get the data for this query This method is really only for debugging and the underlying List you get will not be synchronized with the query.Retrieve query metadata as a struct.Object[]
getRow
(int index) Get data for a row as an array.getRowAsStruct
(int index) Get data for a row as a Struct.int
getRowFromContext
(IBoxContext context) Validate that a row index is within bounds Throw exception if notboolean
Does this query have a specific column?boolean
Does this query have columns?insertQueryAt
(int position, Query target) Insert a query into this query at a specific positionboolean
isEmpty()
iterator()
boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) Set data for a single cell.setMetadata
(IStruct meta) Override Query metadata - used for setting custom query meta on cached queries.int
size()
Collection implementationvoid
sort
(Comparator<IStruct> compareFunc) Sort the queryswapRow
(int sourceRow, int destinationRow) Swap a row with another row in the queryObject[]
toArray()
<T> T[]
toArray
(T[] a) Get the data as a Boxlang Array of Structs.void
validateRow
(int index) Validate that a row index is within bounds Throw exception if notMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
$bx
Metadata object
-
-
Constructor Details
-
Query
Create a new query with additional metadata- Parameters:
meta
- Struct of metadata, most likely JDBC metadata such as sql, cache parameters, etc.
-
Query
public Query()Create a new query with a default (empty) metadata struct
-
-
Method Details
-
fromResultSet
Create a new query and populate it from the given JDBC ResultSet.- Parameters:
resultSet
- JDBC result set.- Returns:
- Query object
-
fromArray
Create a new query with columns and data- Parameters:
columnNames
- List of column namescolumnTypes
- List of column typesrowData
- List of row data- Returns:
- Query object
-
setMetadata
Override Query metadata - used for setting custom query meta on cached queries. -
getColumns
Get the list of column definitions for this query- Returns:
- map of columns
-
hasColumns
public boolean hasColumns()Does this query have columns?- Returns:
- true if query has columns
-
hasColumn
Does this query have a specific column?- Returns:
- true if query has column
-
getData
Get the data for this query This method is really only for debugging and the underlying List you get will not be synchronized with the query.- Returns:
- list of arrays of data
-
addColumn
Add a column to the query, populated with nulls- Parameters:
name
- column nametype
- column type- Returns:
- this query
-
addColumn
Add a column to the query, populated with provided data. If the data array is shorter than the current number of rows, the remaining rows will be populated with nulls.- Parameters:
name
- column nametype
- column type- Returns:
- this query
-
getColumnData
Get all data in a column as a Java Object[] Data is copied, so re-assignments into the array will not be reflected in the query. Mutating a complex object in the array will be reflected in the query.- Parameters:
name
- column name- Returns:
- array of column data
-
getColumnDataAsArray
Get all data in a column as an BoxLang Array Data is copied, so re-assignments into the array will not be reflected in the query. Mutating a complex object in the array will be reflected in the query.- Parameters:
name
- column name- Returns:
- array of column data
-
getColumnIndex
Get the index of a column- Parameters:
name
- column name- Returns:
- index of column, or -1 if not found
-
getColumn
Get the QueryColumn object for a column Throws an exception if the column doesn't exist- Parameters:
name
- column name- Returns:
- QueryColumn object
-
getRow
Get data for a row as an array. 0-based index! Array is passed by reference and changes made to it will be reflected in the query.- Parameters:
index
- row index, starting at 0- Returns:
- array of row data
-
insertQueryAt
Insert a query into this query at a specific position- Parameters:
position
- position to insert attarget
- query to insert- Returns:
- this query
- Throws:
BoxRuntimeException
- if the query columns do not match
-
addRow
Add a row to the query- Parameters:
row
- row data as array of objects- Returns:
- this query
-
addRow
Add a row to the query- Parameters:
row
- row data as a BoxLang array- Returns:
- this query
-
swapRow
Swap a row with another row in the query- Parameters:
sourceRow
- The row to swap fromdestinationRow
- The row to swap to- Returns:
- this query
-
addEmptyRow
public int addEmptyRow()Add an empty row to the query- Returns:
- this query
-
addRow
Add a row to the query- Parameters:
row
- row data as Struct- Returns:
- this query
-
addRows
public int addRows(int rows) Add empty rows to the query- Parameters:
rows
- Number of rows to add- Returns:
- Last row added
-
deleteColumn
Deletes a column from the query.- Parameters:
name
- the name of the column to delete
-
deleteRow
Delete a row from the query- Parameters:
index
- row index, starting at 0- Returns:
- this query
-
addData
Helper method for queryNew() and queryAddRow() to handle the different scenarios for adding data to a query- Parameters:
rowData
- Data to populate the query. Can be a struct (with keys matching column names), an array of structs, or an array of arrays (in same order as columnList)- Returns:
- index of last row added
-
getRowAsStruct
Get data for a row as a Struct. 0-based index! Data is copied, so re-assignments into the struct will not be reflected in the query. Mutating a complex object in the array will be reflected in the query.- Parameters:
index
- row index, starting at 0- Returns:
- array of row data
-
getCell
Get data for a single cell. 0-based index!- Parameters:
columnName
- column namerowIndex
- row index, starting at 0- Returns:
- cell data
-
setCell
Set data for a single cell. 0-based index!- Parameters:
columnName
- column namerowIndex
- row index, starting at 0- Returns:
- this query
-
validateRow
public void validateRow(int index) Validate that a row index is within bounds Throw exception if not- Parameters:
index
- row index, 0-based
-
getRowFromContext
Validate that a row index is within bounds Throw exception if not- Parameters:
context
- context to get row from- Returns:
- row index, 0-based
-
getColumnList
Get the list of column names as a comma-separated string TODO: Look into caching this and invalidating when columns are added/removed- Returns:
- column names as string
-
getColumnArray
Get the list of column names as an array- Returns:
- column names as array
-
sort
Sort the query- Parameters:
compareFunc
- function to use for sorting
-
size
public int size()Collection implementation- Specified by:
size
in interfaceCollection<IStruct>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<IStruct>
-
contains
- Specified by:
contains
in interfaceCollection<IStruct>
-
iterator
-
toArray
- Specified by:
toArray
in interfaceCollection<IStruct>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArray
in interfaceCollection<IStruct>
-
toStructArray
Get the data as a Boxlang Array of Structs. Useful for queries with `returntype: "array"`. -
add
- Specified by:
add
in interfaceCollection<IStruct>
-
remove
- Specified by:
remove
in interfaceCollection<IStruct>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<IStruct>
-
addAll
- Specified by:
addAll
in interfaceCollection<IStruct>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<IStruct>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<IStruct>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<IStruct>
-
dereference
IReferencable implementation- Specified by:
dereference
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The key to dereferencesafe
- Whether to throw an exception if the key is not found- Returns:
- The requested object
-
dereferenceAndInvoke
public Object dereferenceAndInvoke(IBoxContext context, Key name, Object[] positionalArguments, Boolean safe) Description copied from interface:IReferenceable
Dereference this object by a key and invoke the result as an invokable (UDF, java method) using positional arguments- Specified by:
dereferenceAndInvoke
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The key to dereferencepositionalArguments
- The positional arguments to pass to the invokablesafe
- Whether to throw an exception if the key is not found- Returns:
- The requested object
-
dereferenceAndInvoke
public Object dereferenceAndInvoke(IBoxContext context, Key name, Map<Key, Object> namedArguments, Boolean safe) Description copied from interface:IReferenceable
Dereference this object by a key and invoke the result as an invokable (UDF, java method) using named arguments- Specified by:
dereferenceAndInvoke
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The key to dereferencenamedArguments
- The named arguments to pass to the invokablesafe
- Whether to throw an exception if the key is not found- Returns:
- The requested object
-
assign
Description copied from interface:IReferenceable
Assign a value to a key in this object- Specified by:
assign
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The name of the scope to getvalue
- The value to assign to the scope- Returns:
- The value that was assigned
-
asString
IType implementation -
getBoxMeta
- Specified by:
getBoxMeta
in interfaceIType
-
intStream
-
getMetaData
Retrieve query metadata as a struct. Used to populate QueryMeta. Will populate the following keys if they don't already exist: - recordCount: Number of rows in the query - columns: List of column names - _HASHCODE: Hashcode of the query- Returns:
- The metadata as a struct
-
duplicate
Duplicate the current query.- Returns:
- A copy of the current query.
-
duplicate
Duplicate the current query.- Parameters:
deep
- If true, nested objects will be duplicated as well.- Returns:
- A copy of the current query.
-