Red — new-driver

How to create a new driver

A driver connects Red's AST and model types to a database engine. Keep a new backend in a separate distribution, Red::Driver::YourDB, with the module at lib/Red/Driver/YourDB.rakumod and an entry in META6.json's provides map. Then database 'YourDB', ... loads it without changes to Red's loader.

Start with Red::Driver, CommonSQL, Statement and the driver boundary. The bundled SQLite implementation is a complete example; the external Pg, MySQL and DuckDB drivers illustrate other clients.

Connection and statement contract

Compose Red::Driver::CommonSQL for shared SQL translation, or Red::Driver if your backend needs its own translation layer. Accept typed connection parameters and optionally a native :dbh. Define which component owns and closes each handle.

Implement these operations:

OperationContract
multi method prepare(Str $sql)Return a Red::Statement wrapping a native prepared statement.
translate(Red::AST $ast, $context?)Return SQL/bind pairs, or a sequence of pairs for multiple statements. Preserve bind order.
default-type-for(Red::Column $column --> Str:D)Select a physical type. CommonSQL delegates to default-type-for-type.
type-by-name, inflate, deflateMap explicit SQL types and convert database/Raku values, including nulls.
schema-readerSupply catalog inspection through Red::SchemaReader.
map-exceptionMap native errors to typed X::Red::Driver::Mapped::* exceptions, retaining the cause.

Red::Driver.prepare(Red::AST) optimizes/translates the AST, prepares SQL and attaches deflated values as .binds. Your statement's stt-exec($statement, *@bind) executes the native statement, using explicit binds when supplied and @!binds otherwise, and returns the native handle used for subsequent reads. stt-row($statement) returns a hash keyed by result column names and a false value at exhaustion. Reuse the public Red::Statement.execute and .row wrappers so exception mapping and transaction guards remain active. Implement cursor cleanup through finish-statement when the client requires it.

SQL lowering and types

Override CommonSQL's multi method translate candidates for syntax your engine does not support. Quote identifiers with the driver's quoting methods; pass user values as bind parameters. Test placeholder ordering in nested expressions, subqueries and multi-statement operations. Never interpolate values into SQL.

Implement generated primary keys and Red::AST::LastInsertedRow together. Red's serial means a non-null generated integer primary key, not a requirement that the database have a type named SERIAL. Supply the physical type and generation strategy. Test explicit IDs as well as generated IDs; implement advance-sequences when explicit inserts require sequence synchronization.

Backend-only AST nodes belong in your distribution and can use Red::AST::DriverExtension. Put backend schema helpers there too. Regex lowering belongs in Red::Driver::YourDB::Regex; reject unsupported operations with X::Red::Compiler::Unsupported rather than silently altering their meaning.

Transactions, catalogs and capabilities

Implement begin, commit, rollback and savepoint operations on the intended connection. The base begin uses new-connection; override it if your client must retain a session (SQLite does). Test that a nested rollback preserves outer writes, that closed scopes reject queries and that cursors are released before commit.

Catalog readers implement tables-names, indexes-of, table-definition and table-definition-from-create-table. Index support also needs catalog normalization and the relevant CommonSQL capability methods: for example, supports-partial-indexes, supports-index-method and supports-operator-classes. Do not advertise a capability merely because the server accepts related syntax.

Migration hooks are defined in Red::Driver::Migrations. Implement locking, session management, DDL transaction behavior, automatic columns, column alterations and temporary database provisioning as appropriate. The base role rejects unsupported operations. Implement database-client if the CLI can launch your native client. Global notifications have a separate optional events contract.

Validate and distribute

Add tests in your driver's distribution for loading by name, connection failures, CRUD, binds, nulls, types, generated IDs, relationships, indexes/catalogs, transactions/savepoints and supported migrations. Use temporary databases and explicit assertions for unsupported features. Exercise the actual native client; translation-only tests cannot establish database behavior.

During development, prepend both libraries when running integration tests:

raku -Ilib -I../Red-Driver-YourDB/lib ../Red-Driver-YourDB/t/driver-support.rakutest
raku -Ilib -c ../Red-Driver-YourDB/lib/Red/Driver/YourDB.rakumod
prove6 -lj8 t

The first command is an example path for a test you provide. Set RED_DATABASE when running Red tests against your backend, using a disposable database. Declare Red and client dependencies in META6.json, document native installation and supported server versions, and verify the compatible core/driver revisions before publishing. Keep public API documentation as Pod6 in source; regenerate it with raku -Ilib tools/make-docs.raku in the Red checkout.

Red v0.2.17

A Raku ORM

Authors

  • Fernando Correa de Oliveira

License

Artistic-2.0

Dependencies

ConfigurationDBIishUUIDJSON::Fast

Test Dependencies

Provides

  • CX::Red::Bool
  • MetamodelX::Red::Comparate
  • MetamodelX::Red::Describable
  • MetamodelX::Red::Dirtable
  • MetamodelX::Red::GlobalNotifiable
  • MetamodelX::Red::Id
  • MetamodelX::Red::Migration
  • MetamodelX::Red::Model
  • MetamodelX::Red::OnDB
  • MetamodelX::Red::Populatable
  • MetamodelX::Red::PreFetch
  • MetamodelX::Red::Refreshable
  • MetamodelX::Red::Relationship
  • MetamodelX::Red::Specialisable
  • MetamodelX::Red::SubModelHOW
  • MetamodelX::Red::Supply
  • MetamodelX::Red::View
  • MetamodelX::Red::VirtualView
  • Red
  • Red::AST
  • Red::AST::AddForeignKeyOnTable
  • Red::AST::BeginTransaction
  • Red::AST::Between
  • Red::AST::BoundFilter
  • Red::AST::Case
  • Red::AST::Chained
  • Red::AST::ChangeColumn
  • Red::AST::Comment
  • Red::AST::CommitTransaction
  • Red::AST::Constraints
  • Red::AST::CreateColumn
  • Red::AST::CreateIndex
  • Red::AST::CreateTable
  • Red::AST::CreateView
  • Red::AST::DateTimeFuncs
  • Red::AST::Delete
  • Red::AST::Divisible
  • Red::AST::DriverExtension
  • Red::AST::DropColumn
  • Red::AST::Empty
  • Red::AST::Function
  • Red::AST::Generic::Infix
  • Red::AST::Generic::Postfix
  • Red::AST::Generic::Prefix
  • Red::AST::Infix
  • Red::AST::Infixes
  • Red::AST::Insert
  • Red::AST::Intersect
  • Red::AST::IsDefined
  • Red::AST::JsonItem
  • Red::AST::JsonOperation
  • Red::AST::JsonRemoveItem
  • Red::AST::JsonSetItem
  • Red::AST::LastInsertedRow
  • Red::AST::Minus
  • Red::AST::MultiSelect
  • Red::AST::Native
  • Red::AST::Next
  • Red::AST::Operator
  • Red::AST::Optimizer::AND
  • Red::AST::Optimizer::Case
  • Red::AST::Optimizer::OR
  • Red::AST::Ordering
  • Red::AST::ProductSource
  • Red::AST::ProjectionField
  • Red::AST::Reference
  • Red::AST::RegexCapture
  • Red::AST::RegexChars
  • Red::AST::RegexMatch
  • Red::AST::ReleaseSavepoint
  • Red::AST::RollbackToSavepoint
  • Red::AST::RollbackTransaction
  • Red::AST::RowContext
  • Red::AST::RowParameter
  • Red::AST::Savepoint
  • Red::AST::Select
  • Red::AST::StringFuncs
  • Red::AST::TableComment
  • Red::AST::Unary
  • Red::AST::Union
  • Red::AST::Update
  • Red::AST::Value
  • Red::Attr::Column
  • Red::Attr::Index
  • Red::Attr::Query
  • Red::Attr::Relationship
  • Red::Class
  • Red::Cli
  • Red::Cli::Column
  • Red::Cli::Relationship
  • Red::Cli::Table
  • Red::Column
  • Red::ColumnMethods
  • Red::Compiler::Emitter
  • Red::Compiler::RakuAST
  • Red::Compiler::Regex
  • Red::Compiler::Runtime
  • Red::Compiler::Template
  • Red::Config
  • Red::DB
  • Red::Database
  • Red::DefaultResultSeq
  • Red::Do
  • Red::Driver
  • Red::Driver::Cache
  • Red::Driver::Cache::Memory
  • Red::Driver::CacheInvalidateOnWrite
  • Red::Driver::CacheWithStrKey
  • Red::Driver::CommonSQL
  • Red::Driver::Events
  • Red::Driver::Migrations
  • Red::Driver::Mock
  • Red::Driver::SQLite
  • Red::Driver::SQLite::Regex
  • Red::Driver::SQLite::SQLiteMaster
  • Red::Driver::SQLite::SchemaReader
  • Red::Event
  • Red::Fixture
  • Red::Formatter
  • Red::Formatter::ResultSeq
  • Red::Formatter::ResultSeq::Vertical
  • Red::FromRelationship
  • Red::GlobalEvents
  • Red::HiddenFromSQLCommenting
  • Red::Index
  • Red::LockType
  • Red::Migration::Applied
  • Red::Migration::Artifact
  • Red::Migration::Bundle
  • Red::Migration::Column
  • Red::Migration::Configuration
  • Red::Migration::Data
  • Red::Migration::Dependencies
  • Red::Migration::Driver
  • Red::Migration::Environment
  • Red::Migration::File
  • Red::Migration::Import
  • Red::Migration::Logical
  • Red::Migration::Manager
  • Red::Migration::Migration
  • Red::Migration::Planner
  • Red::Migration::Resolution
  • Red::Migration::SQL
  • Red::Migration::Scripts
  • Red::Migration::Snapshot
  • Red::Migration::Storage
  • Red::Migration::Table
  • Red::Migration::Test
  • Red::Model
  • Red::Operators
  • Red::Phaser
  • Red::PrepareCode
  • Red::Projection
  • Red::QueryPlan
  • Red::QueryPlan::Cache
  • Red::Regex
  • Red::Regex::Automaton
  • Red::Regex::Unicode
  • Red::ResultAssociative
  • Red::ResultSeq
  • Red::ResultSeq::Iterator
  • Red::ResultSeq::Supply
  • Red::ResultSeqMethods
  • Red::ResultSeqSeq
  • Red::RowSecurity
  • Red::RowSecurity::Context
  • Red::RowSecurity::DDL
  • Red::RowSecurity::PostgreSQL
  • Red::RowSecurity::Statement
  • Red::RowSecurity::Write
  • Red::Runtime::Configuration
  • Red::Schema
  • Red::SchemaReader
  • Red::Statement
  • Red::Traits
  • Red::Transaction::Guard
  • Red::Type
  • Red::Type::Json
  • Red::Type::Map
  • Red::Utils
  • X::Red::Exceptions
  • X::Red::Migration

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.