MySQL Shell API 8.4.3
Unified development interface for MySQL Products
Methods | List of all members
TableSelect Class Reference

Handler for record selection on a Table. More...

Methods

TableSelect select ()
 Defines the columns to be retrieved from the table. More...
 
TableSelect where (String expression)
 Sets the search condition to filter the records to be retrieved from the Table. More...
 
TableSelect groupBy (List columns)
 Sets a grouping criteria for the retrieved rows. More...
 
TableSelect having (String condition)
 Sets a condition for records to be considered in aggregate function operations. More...
 
TableSelect orderBy (List sortCriteria)
 Sets the order in which the records will be retrieved. More...
 
TableSelect limit (Integer numberOfRows)
 Sets the maximum number of rows to be returned on the select operation. More...
 
TableSelect offset (Integer numberOfRows)
 Sets number of rows to skip on the resultset when a limit has been defined. More...
 
TableSelect lockShared (String lockContention)
 Instructs the server to acquire shared row locks in documents matched by this find operation. More...
 
TableSelect lockExclusive (String lockContention)
 Instructs the server to acquire an exclusive lock on rows matched by this find operation. More...
 
TableSelect bind (String name, Value value)
 Binds a value to a specific placeholder used on this operation. More...
 
RowResult execute ()
 Executes the select operation with all the configured options. More...
 

Detailed Description

Handler for record selection on a Table.

This object provides the necessary functions to allow selecting record data from a table.

This object should only be created by calling the select function on the table object from which the record data will be retrieved.

See also
Table

Member Function Documentation

◆ select()

TableSelect select ( )

Defines the columns to be retrieved from the table.

Returns
This TableSelect object.

Calling select() will cause all the columns in the table to be retrieved.

To retrieve only certain columns use either select(List columns) and provide a list object containing the column definitions, or select(String column[, String column, ...]) and pass each column definition as a separate parameter.

Method Chaining

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ where()

TableSelect where ( String  expression)

Sets the search condition to filter the records to be retrieved from the Table.

Parameters
expressionA condition to filter the records to be retrieved.
Returns
This TableSelect object.

If used, only those rows satisfying the expression will be retrieved.

The expression supports Parameter Binding.

Method Chaining

This function can be invoked only once after:

See also
Usage examples at execute().

◆ groupBy()

TableSelect groupBy ( List  columns)

Sets a grouping criteria for the retrieved rows.

Returns
This TableSelect object.

Method Chaining

This function can be invoked only once after:

See also
Usage examples at execute().

◆ having()

TableSelect having ( String  condition)

Sets a condition for records to be considered in aggregate function operations.

Parameters
conditionA condition to be used with aggregate functions.
Returns
This TableSelect object.

If used the TableSelect operation will only consider the records matching the established criteria.

The condition supports Parameter Binding.

Method Chaining

This function can be invoked only once after:

See also
Usage examples at execute().

◆ orderBy()

TableSelect orderBy ( List  sortCriteria)

Sets the order in which the records will be retrieved.

Returns
This TableSelect object.

If used, the TableSelect operation will return the records sorted with the defined criteria.

Every defined sort criterion follows the format:

name [ ASC | DESC ]

ASC is used by default if the sort order is not specified.

Method Chaining

This function can be invoked only once after:

See also
Usage examples at execute().

◆ limit()

TableSelect limit ( Integer  numberOfRows)

Sets the maximum number of rows to be returned on the select operation.

Parameters
numberOfRowsThe maximum number of rows to be retrieved.
Returns
This TableSelect object.

If used, the operation will return at most numberOfRows rows.

Method Chaining

This function can be invoked only once after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ offset()

TableSelect offset ( Integer  numberOfRows)

Sets number of rows to skip on the resultset when a limit has been defined.

Parameters
numberOfRowsThe number of rows to skip before start including them on the RowResult.
Returns
This TableSelect object.

If used, the first numberOfRows records will not be included on the result.

Method Chaining

This function can be invoked only once after:

See also
Usage examples at execute().

◆ lockShared()

TableSelect lockShared ( String  lockContention)

Instructs the server to acquire shared row locks in documents matched by this find operation.

Parameters
lockContentionoptional parameter to indicate how to handle rows that are already locked.
Returns
This TableSelect object.

When this function is called, the selected rows will be locked for write operations, they may be retrieved on a different session, but no updates will be allowed.

The acquired locks will be released when the current transaction is committed or rolled back.

The lockContention parameter defines the behavior of the operation if another session contains an exclusive lock to matching rows.

The lockContention can be specified using the following constants:

  • mysqlx.LockContention.DEFAULT
  • mysqlx.LockContention.NOWAIT
  • mysqlx.LockContention.SKIP_LOCKED

The lockContention can also be specified using the following string literals (no case sensitive):

  • 'DEFAULT'
  • 'NOWAIT'
  • 'SKIP_LOCKED'

If no lockContention or the default is specified, the operation will block if another session already holds an exclusive lock on matching rows until the lock is released.

If lockContention is set to NOWAIT and another session already holds an exclusive lock on matching rows, the operation will not block and an error will be generated.

If lockContention is set to SKIP_LOCKED and another session already holds an exclusive lock on matching rows, the operation will not block and will return only those rows not having an exclusive lock.

This operation only makes sense within a transaction.

Method Chaining

This function can be invoked at any time before bind() or execute() are called.

After this function invocation, the following functions can be invoked:

If lockExclusive() is called, it will override the lock type to be used on the selected documents.

See also
Usage examples at execute().

◆ lockExclusive()

TableSelect lockExclusive ( String  lockContention)

Instructs the server to acquire an exclusive lock on rows matched by this find operation.

Parameters
lockContentionoptional parameter to indicate how to handle rows that are already locked.
Returns
This TableSelect object.

When this function is called, the selected rows will be locked for read operations, they will not be retrievable by other session.

The acquired locks will be released when the current transaction is committed or rolled back.

The lockContention parameter defines the behavior of the operation if another session contains a lock to matching rows.

The lockContention can be specified using the following constants:

  • mysqlx.LockContention.DEFAULT
  • mysqlx.LockContention.NOWAIT
  • mysqlx.LockContention.SKIP_LOCKED

The lockContention can also be specified using the following string literals (no case sensitive):

  • 'DEFAULT'
  • 'NOWAIT'
  • 'SKIP_LOCKED'

If no lockContention or the default is specified, the operation will block if another session already holds a lock on matching rows until the lock is released.

If lockContention is set to NOWAIT and another session already holds a lock on matching rows, the operation will not block and an error will be generated.

If lockContention is set to SKIP_LOCKED and another session already holds a lock on matching rows, the operation will not block and will return only those rows not having an exclusive lock.

This operation only makes sense within a transaction.

Method Chaining

This function can be invoked at any time before bind() or execute() are called.

After this function invocation, the following functions can be invoked:

If lockShared() is called, it will override the lock type to be used on the selected documents.

See also
Usage examples at execute().

◆ bind()

TableSelect bind ( String  name,
Value  value 
)

Binds a value to a specific placeholder used on this operation.

Parameters
nameThe name of the placeholder to which the value will be bound.
valueThe value to be bound on the placeholder.
Returns
This TableSelect object.

Binds the given value to the placeholder with the specified name.

An error will be raised if the placeholder indicated by name does not exist.

This function must be called once for each used placeholder or an error will be raised when the execute() method is called.

Method Chaining

This function can be invoked multiple times right before calling execute().

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ execute()

RowResult execute ( )

Executes the select operation with all the configured options.

Returns
A RowResult object that can be used to traverse the rows returned by this operation.

Method Chaining

This function can be invoked after any other function on this class.

Examples

Fetching all the records

var records = table.select().execute().fetchAll();
print("All:", records.length, "\n");

Fetching records matching specified criteria

var records = table.select().where('gender = "male"').execute().fetchAll();
print("Males:", records.length, "\n");
var records = table.select().where('gender = "female"').execute().fetchAll();
print("Females:", records.length, "\n");
var records = table.select().where('age = 13').execute().fetchAll();
print("13 Years:", records.length, "\n");
var records = table.select().where('age = 14').execute().fetchAll();
print("14 Years:", records.length, "\n");
var records = table.select().where('age < 17').execute().fetchAll();
print("Under 17:", records.length, "\n");
var records = table.select().where('name like "a%"').execute().fetchAll();
print("Names With A:", records.length, "\n");
var records = table.select().where('name LIKE "a%"').execute().fetchAll();
print("Names With A:", records.length, "\n");
var records = table.select().where('NOT (age = 14)').execute().fetchAll();
print("Not 14 Years:", records.length, "\n");

Selecting which columns to fetch

var result = table.select(['name', 'age']).execute();
var record = result.fetchOne();
var columns = dir(record)
print(columns)
print('1-Metadata Length:', columns.length, '\n');
print('1-Metadata Field:', columns[1], '\n');
print('1-Metadata Field:', columns[2], '\n');
var result = table.select(['age']).execute();
var record = result.fetchOne();
var columns = dir(record)
print('2-Metadata Length:', columns.length, '\n');
print('2-Metadata Field:', columns[1], '\n');

Sorting the results

var records = table.select().orderBy(['name']).execute().fetchAll();
for (index = 0; index < 7; index++) {
print('Select Asc', index, ':', records[index].name, '\n');
}
var records = table.select().orderBy(['name desc']).execute().fetchAll();
for (index = 0; index < 7; index++) {
print('Select Desc', index, ':', records[index].name, '\n');
}

Setting limit and offset

var records = table.select().limit(4).execute().fetchAll();
print('Limit-Offset 0 :', records.length, '\n');
for (index = 1; index < 8; index++) {
var records = table.select().limit(4).offset(index).execute().fetchAll();
print('Limit-Offset', index, ':', records.length, '\n');
}

Using parameter binding

var records = view.select().where('my_age = :years and my_gender = :heorshe').bind('years', 13).bind('heorshe', 'female').execute().fetchAll();
print('Select Binding Length:', records.length, '\n');
print('Select Binding Name:', records[0].my_name, '\n');