NAME

DBIO::MySQL::DefaultValue - MySQL/MariaDB DEFAULT values for generated DDL, written and read

VERSION

version 0.900004

DESCRIPTION

Single source of truth for what stands behind DEFAULT in a statement this driver generates, and for what a column's default is once the catalog has reported it. The install DDL (DBIO::MySQL::DDL), the CREATE TABLE of a diff (DBIO::MySQL::Diff::Table) and its ADD / MODIFY COLUMN (DBIO::MySQL::Diff::Column) write it through "default_sql"; DBIO::MySQL::Introspect::Columns reads it through "catalog_default".

A default is a literal (a value, written as a string literal) or an expression (CURRENT_TIMESTAMP, uuid(), written as SQL). The model carries the difference as two keys of a column: default_value, the text, and default_is_expression, 0 or 1. A scalar reference as default_value (a schema's own \'CURRENT_TIMESTAMP') is an expression written as it is.

ON UPDATE CURRENT_TIMESTAMP is not a default but stands beside it, and the model carries it as a third key, on_update ("catalog_on_update", "on_update_sql"). A schema declares it with no key of its own, in the scalar reference: default_value => \'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'.

METHODS

default_sql

my $sql = default_sql($value, $is_expression);   # 'it''s', CURRENT_TIMESTAMP, (uuid())

The text after DEFAULT for a defined $value.

  • A scalar reference is written as it stands.

  • A literal becomes a string literal: the apostrophe is doubled, and so is the backslash, because it is an escape in the default sql_mode of both servers (measured on MySQL 8.0.30 and MariaDB 11.8.9: 'a\\b' is the value a\b). A NUL byte is written \0. Under NO_BACKSLASH_ESCAPES a doubled backslash would stay two; the driver does not set that mode.

  • An expression is written bare when it is CURRENT_TIMESTAMP or one of its spellings (NOW(), LOCALTIME, ... with or without a precision), the only expressions the servers take without parentheses; any other is put in parentheses (DEFAULT (uuid())), which both servers accept from MySQL 8.0.13 and MariaDB 10.2.1.

catalog_default

my ($value, $is_expression) = catalog_default(
  raw => $column_default, extra => $extra, data_type => $type, mariadb_expression => 1);

What the server's information_schema.columns.column_default means, as the pair the model carries. The two servers fill it in differently (measured):

  • MySQL reports a literal as the value itself (it's, a\b) and an expression as its text; the column's EXTRA says which: it contains DEFAULT_GENERATED for an expression (CURRENT_TIMESTAMP, uuid(), concat(_utf8mb4\'a\',...), with a quote it escaped in the report). Before 8.0.13 only CURRENT_TIMESTAMP of a timestamp / datetime column is an expression, and EXTRA does not say so.

  • MariaDB from 10.2.7 (mariadb_expression) reports an expression throughout: a string literal in quotes with the apostrophe doubled and the backslash and control characters escaped ('it''s', 'a\\b', 'a\nb'), a number bare (5, 1.50), anything else is an expression (current_timestamp(), concat('a','b')). The literal is returned unquoted and unescaped, so it is the value, as MySQL reports it.

undef stays undef.

catalog_on_update

my $on_update = catalog_on_update($extra);   # 'CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP(6)', undef

The ON UPDATE of a column, from its EXTRA. Both servers carry it there, in their own spelling (measured on MySQL 8.0.30 and MariaDB 11.8.9): MySQL DEFAULT_GENERATED on update CURRENT_TIMESTAMP(6), MariaDB on update current_timestamp(6), and current_timestamp() where MySQL has CURRENT_TIMESTAMP. The answer is one spelling for both: upper case, the precision kept, the () of a plain current_timestamp() taken off. undef for a column without ON UPDATE.

on_update_sql

my $sql = on_update_sql($on_update);   # ' ON UPDATE CURRENT_TIMESTAMP', ''

The clause a statement writes for the on_update of a column model, with its leading space, or the empty string for none. A MODIFY COLUMN that leaves it out takes the attribute off the column.

AUTHOR

DBIO & DBIx::Class Authors

COPYRIGHT AND LICENSE

Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.