class

Marten::DB::Connection::Base

Inherits Reference < Object

Abstract base class for a database connection.

A database connection provides the main interface allowing to interact with the underlying database. Subclasses must define a set of function allowing to define backend-specifics such as statements, operators, etc.

Constructors

new(config : Conf::GlobalSettings::Database)
Source

Instance methods

alias

Returns the DB alias of the considered connection.

Source
build_sql

Allows to conveniently build a SQL statement by yielding an array of nillable strings.

Source
bulk_batch_size(records_count : Int32, values_count : Int32) : Int32

Returns the batch size to use when inserting multiple rows in a specific table.

Source
bulk_insert(table_name : String, values : Array(Hash(String, ::DB::Any)), pk_column_to_fetch : String | Nil = nil) : Array(::DB::Any) | Nil

Allows to insert multiple rows in a specific table and returns the primary key values for the inserted rows.

This method allow inserting individual rows defined in values in the table_name table. When pk_column_to_fetch is specified, the primary key values for the inserted rows will be returned. Note that this method can return nil if the underlying database does not support returning primary key values for bulk inserts.

Source
distinct_clause_for(columns : Array(String)) : String

Returns a distinct clause to remove duplicates from a query's results.

If column names are specified, only these specific columns will be checked to identify duplicates.

Source
id

Returns the identifier of the connection.

Source
insert(table_name : String, values : Hash(String, ::DB::Any), pk_column_to_fetch : String | Nil = nil) : ::DB::Any

Allows to insert a new row in a specific table.

Source
left_operand_for_predicate(id : String, predicate) : String

Returns the left operand to use for a specific query predicate.

Most of the time the initial ID will be left intact but depending on the connection implementation and the considered predicate type (eg. "istartswith"), specific SQL functions could be applied on the column ID.

See also #left_operand_for_transformation for column lookup transformations applied before the predicate.

Source
left_operand_for_transformation(id : String, transformation) : String

Returns the left operand SQL after applying a column lookup transformation to id.

id is qualified column SQL (for example alias.col). transformation matches Marten::DB::Query::SQL::Transformation::Base.transformation_name (for example "year"). Implementations map transformation names to SQL fragments, the same way predicate-specific operand shaping is handled in #left_operand_for_predicate.

See also #left_operand_for_predicate, which shapes the column side for predicates.

Source
limit_value(value : Int | Nil) : Int32 | Int64 | Nil | UInt32 | UInt64

Returns a compatible value to use in the context of a LIMIT statement for the database at hand.

Source
local_datetime_expression(id : String) : String

Returns SQL that yields a datetime in Marten.settings.time_zone for date/time part extraction.

PostgreSQL EXTRACT on timestamp with time zone uses the session time zone, so datetime-part lookups would otherwise depend on the database TimeZone setting. Other backends store naive UTC datetimes and can use id unchanged.

Source
max_name_size

Returns the maximum size for table names, column names or index / constraint names.

Source
observe_transaction_commit(block : -> Nil)

Registers a proc to be called when the current transaction is committed to the database.

This method has no effect if it is called outside of a transaction block.

Source
observe_transaction_rollback(block : -> Nil)

Registers a proc to be called when the current transaction is rolled back.

This method has no effect if it is called outside of a transaction block.

Source
open

Provides a database entrypoint to the block.

If this method is called in an existing transaction, the connection associated with this transaction will be used instead.

Source
operator_for_predicate(predicate) : String

Returns the operator to use for a specific query predicate.

Source
parameter_id_for_ordered_argument(number : Int) : String

Returns the parameterized identifier for an ordered argument.

This method takes the number of the argument which is aimed to be part of an array of ordered SQL arguments.

Source
quote(name : String | Symbol) : String

Allows to quote a specific name (such as a table name or column ID) for the database at hand.

Source
quote_char

Returns the quote character to use to quote table names, columns, etc.

Source
sanitize_like_pattern(pattern : String) : String

Escapes special characters from a pattern aimed at being used in the context of a LIKE statement.

Source
scheme

Returns the scheme to consider for the underlying database backend.

Source
supports_logical_xor?

Returns a boolean indicating whether the database supports the logical XOR operator.

Source
test_database?

Returns true if the current database was explicitly configured for the test environment.

The only way this method can return true is when the database name was explicitly set in a configuration targeting the test environment.

Source
transaction

Open a transaction.

Atomicity will be ensured for the database operations performed inside the block. Note that any existing transaction will be used in case of nested calls to this method.

Source
update(table_name : String, values : Hash(String, ::DB::Any), pk_column_name : String, pk_value : ::DB::Any) : Nil

Allows to update an existing row in a specific table.

Source