QBResult

Overview

The QBResult class serves as a wrapper for managing query results in the QBSelect framework. It enables precise control over the structure and content of query results, including the addition of columns, aggregates, functions, case expressions, and subqueries. This flexibility allows developers to define custom outputs that align with specific SQL requirements.

Key features include support for distinct results, dynamic addition or removal of result components, and methods to include primary key columns automatically. Notable methods include add(column, alias) to add columns with aliases, addSubSelect(query, alias) for embedding subqueries, and remove(name) to remove a column by its name.

For further details, refer to the QBSelect documentation.

Properties Summarized

Type
Name
Summary

Get/set the distinct flag for the query.

Get query builder parent table clause, this may be a query or a join clause.

Get query builder parent.

Methods Summarized

Type
Name
Summary

Add an aggregate to the query result.

Add an aggregate with alias to the query result.

Add a column to the query result.

Add a column with alias to the query result.

Add all columns from a query or a join to the query result.

Add a function result to the query result.

Add a function with alias result to the query result.

Add a case searched expression to the query result.

Add a case searched expression with alias to the query result.

Add the tables' primary pk columns in alphabetical order to the query result.

Add a query to the query result.

Add a query with alias to the query result.

Add a custom subquery to the query result.

Add a custom subquery with alias to the query result.

Add a value to the query result.

Add a value with an alias to the query result.

Clear the columns in the query result.

returns an array with all the columns that will be in the select of this query.

remove a column from the query result.

Remove a column by name from the query result.

Properties Detailed

distinct

Get/set the distinct flag for the query.

Type Boolean The current state of the distinct flag for the query.

Sample

query.result.distinct = true

parent

Get query builder parent table clause, this may be a query or a join clause.

Type QBSelect

Sample

var query = datasources.db.example_data.person.createSelect();
	query.where.add(query.joins.person_to_parent.joins.person_to_parent.columns.name.eq('john'))
	foundset.loadRecords(query)

root

Get query builder parent.

Type QBSelect

Sample

var subquery = datasources.db.example_data.order_details.createSelect();

	var query = datasources.db.example_data.orders.createSelect();
	query.where.add(query
		.or
			.add(query.columns.order_id.not.isin([1, 2, 3]))

			.add(query.exists(
					subquery.where.add(subquery.columns.orderid.eq(query.columns.order_id)).root
			))
		)

	foundset.loadRecords(query)

Methods Detailed

add(aggregate)

Add an aggregate to the query result.

Parameters

  • QBColumn aggregate the aggregate to add to result

Returns: QBResult The query result object with the specified aggregate and alias added.

Sample

query.result.add(query.columns.label_text.max)

add(aggregate, alias)

Add an aggregate with alias to the query result.

Parameters

  • QBColumn aggregate the aggregate to add to result

  • String alias aggregate alias

Returns: QBResult The query result object with the specified function added.

Sample

query.result.add(query.columns.item_count.max, 'maximum_items')

add(column)

Add a column to the query result.

Parameters

Returns: QBResult The query result object with the specified column added.

Sample

query.result.add(query.columns.custname)

add(column, alias)

Add a column with alias to the query result.

Parameters

Returns: QBResult The query result object with the specified column and alias added.

Sample

query.result.add(query.columns.custname, 'customer_name')

add(columns)

Add all columns from a query or a join to the query result.

Parameters

Returns: QBResult The query result object with all columns from the specified query or join added.

Sample

query.result.add(query.columns)
query.result.add(query.joins.orders_to_orderdetail.columns)

add(func)

Add a function result to the query result.

Parameters

  • QBColumn func the function to add to the result

Returns: QBResult The query result object with the specified function and alias added.

Sample

query.result.add(query.columns.custname.upper())

add(func, alias)

Add a function with alias result to the query result.

Parameters

  • QBColumn func the function to add to the result

  • String alias function alias

Returns: QBResult The query result object with the specified searched case expression added.

Sample

query.result.add(query.columns.custname.upper(), 'customer_name')

add(qcase)

Add a case searched expression to the query result.

Parameters

  • QBColumn qcase The searched case expression.

Returns: QBResult The query result object with the specified searched case expression added.

Sample

var query = datasources.db.example_data.order_details.createSelect();

// case expressions can be added to the result of the query
	query.result.add(query.case.when(query.columns.quantity.ge(1000)).then('BIG').else('small'));

 // they can also be used in conditions
	query.where.add(query.case
		.when(query.columns.discount.gt(10)).then(50)
		.when(query.columns.quantity.le(20)).then(70)
		.else(100)
	.multiply(query.columns.unitprice).lt(10000));

add(qcase, alias)

Add a case searched expression with alias to the query result.

Parameters

  • QBColumn qcase The searched case expression.

  • String alias function alias

Returns: QBResult The query result object with the specified searched case expression and alias added.

Sample

var query = datasources.db.example_data.order_details.createSelect();

// case expressions can be added to the result of the query
	query.result.add(query.case.when(query.columns.quantity.ge(1000)).then('BIG').else('small'));

 // they can also be used in conditions
	query.where.add(query.case
		.when(query.columns.discount.gt(10)).then(50)
		.when(query.columns.quantity.le(20)).then(70)
		.else(100)
	.multiply(query.columns.unitprice).lt(10000));

addPk()

Add the tables' primary pk columns in alphabetical order to the query result.

Returns: QBResult The query result object with primary key columns added.

Sample

query.result.addPk()

addSubSelect(query)

Add a query to the query result.

Parameters

Returns: QBResult The query result object with the specified query added.

Sample

// make sure the query returns exactly 1 value.
query.result.addSubSelect(subquery);

addSubSelect(query, alias)

Add a query with alias to the query result.

Parameters

Returns: QBResult The query result object with the specified query and alias added.

Sample

// make sure the query returns exactly 1 value.
query.result.addSubSelect(subquery, "mx");

addSubSelect(customQuery, args)

Add a custom subquery to the query result.

Parameters

  • String customQuery query to add to result

  • Array args arguments to the query

Returns: QBResult The query result object with the specified custom subquery added.

Sample

// make sure the subquery returns exactly 1 value.
// select (select max from othertab where val = 'test') from tab
query.result.addSubSelect("select max(field) from othertab where val = ?", ["test"]);

addSubSelect(customQuery, args, alias)

Add a custom subquery with alias to the query result.

Parameters

  • String customQuery query to add to result

  • Array args arguments to the query

  • String alias result alias

Returns: QBResult The query result object with the specified custom subquery and alias added.

Sample

// make sure the subquery returns exactly 1 value.
// select (select max from othertab where val = 'test') as mx from tab
query.result.addSubSelect("select max from othertab where val = ?", ["test"], "mx");

addValue(value)

Add a value to the query result.

Parameters

  • Object value value add to result

Returns: QBResult The query result object with the specified value added.

Sample

query.result.addValue(100)

addValue(value, alias)

Add a value with an alias to the query result.

Parameters

Returns: QBResult The query result object with the specified value and alias added.

Sample

query.result.addValue(100, 'myvalue')

clear()

Clear the columns in the query result.

Returns: QBResult The query result object with all columns cleared.

Sample

query.result.clear()

getColumns()

returns an array with all the columns that will be in the select of this query. can return empty array. Then the system will auto append the pk when this query is used.

Returns: Array An array of QBColumn thats in the select of this query.

Sample

var columns = query.result.getColumns();

remove(column)

remove a column from the query result.

Parameters

  • QBColumn column column to remove from the result

Returns: QBResult The query result object with the specified column removed.

Sample

query.result.remove(query.columns.custname)

remove(name)

Remove a column by name from the query result.

Parameters

  • String name name or alias of column to remove from the result

Returns: QBResult The query result object with the specified column removed by name or alias.

Sample

query.result.remove("custname")

Last updated

Was this helpful?