# NAME

odb - object-relational mapping (ORM) compiler for C++

# SYNOPSIS

`odb [options] file [file...]`

# DESCRIPTION

Given a set of C++ classes in a header file, `odb` generates C++ code that allows you to persist, query, and update objects of these classes in a relational database (RDBMS). The relational database that the generated code should target is specified with the required `--database` option (see below).

For an input file in the form `name.hxx` (other file extensions can be used instead of `.hxx`), the following C++ files are generated: `name-odb.hxx` (header file), `name-odb.ixx` (inline file, generated unless the `--suppress-inline` option is specified), and `name-odb.cxx` (source file). Additionally, if the `--generate-schema` option is specified and the `sql` schema format is requested, the `name.sql` database schema file is generated. If the `separate` schema format is requested, the database creation code is generated into the separate `name-schema.cxx` file.

# OPTIONS

`--help`
  Print usage information and exit.
`--version`
  Print version and exit.
`-I` dir
  Add dir to the beginning of the list of directories to be searched for included header files.
`-D` name[=def]
  Define macro name with definition def. If definition is omitted, define name to be 1.
`-U` name
  Cancel any previous definitions of macro name, either built-in or provided with the `-D` option.
`--database`|`-d` db
  Generate code for the db database. Valid values are `mssql`, `mysql`, `oracle`, `pgsql`, and `sqlite`.
`--generate-query`|`-q`
  Generate query support code. Without this support you cannot use views and can only load objects via their ids.
`--generate-session`|`-e`
  Generate session support code. With this option session support will be enabled by default for all the persistent classes except those for which it was explicitly disabled using the `db session` pragma.
`--generate-schema`|`-s`
  Generate the database schema. The database schema contains SQL statements that create database tables necessary to store persistent classes defined in the file being compiled. Note that by applying this schema, all the existing information stored in such tables will be lost.

  Depending on the database being used (`--database` option), the schema is generated either as a standalone SQL file or embedded into the generated C++ code. By default the SQL file is generated for the MySQL, PostgreSQL, Oracle, and Microsoft SQL Server databases and the schema is embedded into the C++ code for the SQLite database. Use the `--schema-format` option to alter the default schema format.

`--schema-format` format
  Generate the database schema in the specified format. Pass `sql` as format to generate the database schema as a standalone SQL file or pass `embedded` to embed the schema into the generated C++ code. The `separate` value is similar to `embedded` except the schema creation code is generated into a separate C++ file (`name-schema.cxx` by default). This value is primarily useful if you want to place the schema creation functionality into a separate program or library. Repeat this option to generate the same database schema in multiple formats.
`--schema-name` name
  Use name as the database schema name. Schema names are primarily used to distinguish between multiple embedded schemas in the schema catalog. They are not to be confused with database schemas (database namespaces) which are specified with the `--schema` option. If this option is not specified, the empty name, which is the default schema name, is used.
`--default-pointer` ptr
  Use ptr as the default pointer for persistent objects and views. Objects and views that do not have a pointer assigned with the `db pointer` pragma will use this pointer by default. The value of this option can be `*` which denotes the raw pointer and is the default, or qualified name of a smart pointer class template, for example, `std::auto_ptr`. In the latter case, the ODB compiler constructs the object or view pointer by adding a single template argument of the object or view type to the qualified name, for example `std::auto_ptr<object>`. The ODB runtime uses object and view pointers to return, and, in case of objects, pass and cache dynamically allocated instances of object and view types.

  Except for the raw pointer and the standard smart pointers defined in the `<memory>` header file, you are expected to include the definition of the default pointer at the beginning of the generated header file. There are two common ways to achieve this: you can either include the necessary header in the file being compiled or you can use the `--hxx-prologue` option to add the necessary `#include` directive to the generated code.

`--profile`|`-p` name
  Specify a profile that should be used during compilation. A profile is an options file. The ODB compiler first looks for a database-specific version with the name constructed by appending the `-``database``.options` suffix to name, where `database` is the database name as specified with the `--database` option. If this file is not found, then the ODB compiler looks for a database-independant version with the name constructed by appending just the `.options` suffix.

  The profile options files are searched for in the same set of directories as C++ headers included with the `#include <...>` directive (built-in paths plus those specified with the `-I` options). The options file is first searched for in the directory itself and then in its `odb/` subdirectory.

  For the format of the options file refer to the `--options-file` option below. You can repeat this option to specify more than one profile.

`--schema` schema
  Specify a database schema (database namespace) that should be assigned to the persistent classes in the file being compiled. Database schemas are not to be confused with database schema names (schema catalog names) which are specified with the `--schema-name` option.
`--table-prefix` prefix
  Add prefix to table and index names. The prefix is added to both names that were specified with the `db table` pragma and those that were automatically derived from class names. If you require a separator, such as an underscore, between the prefix and the name, then you should include it into the prefix value.
`--std` version
  Specify the C++ standard that should be used during compilation. Valid values are `c++98` (default) and `c++11`.
`--output-dir`|`-o` dir
  Write the generated files to dir instead of the current directory.
`--odb-file-suffix` suffix
  Use suffix instead of the default `-odb` to construct the names of the generated C++ files.
`--schema-file-suffix` suffix
  Use suffix instead of the default `-schema` to construct the name of the generated schema C++ source file. See the `--schema-format` option for details.
`--hxx-suffix` suffix
  Use suffix instead of the default `.hxx` to construct the name of the generated C++ header file.
`--ixx-suffix` suffix
  Use suffix instead of the default `.ixx` to construct the name of the generated C++ inline file.
`--cxx-suffix` suffix
  Use suffix instead of the default `.cxx` to construct the name of the generated C++ source file.
`--sql-suffix` suffix
  Use suffix instead of the default `.sql` to construct the name of the generated database schema file.
`--hxx-prologue` text
  Insert text at the beginning of the generated C++ header file.
`--ixx-prologue` text
  Insert text at the beginning of the generated C++ inline file.
`--cxx-prologue` text
  Insert text at the beginning of the generated C++ source file.
`--schema-prologue` text
  Insert text at the beginning of the generated schema C++ source file.
`--sql-prologue` text
  Insert text at the beginning of the generated database schema file.
`--hxx-epilogue` text
  Insert text at the end of the generated C++ header file.
`--ixx-epilogue` text
  Insert text at the end of the generated C++ inline file.
`--cxx-epilogue` text
  Insert text at the end of the generated C++ source file.
`--schema-epilogue` text
  Insert text at the end of the generated schema C++ source file.
`--sql-epilogue` text
  Insert text at the end of the generated database schema file.
`--hxx-prologue-file` file
  Insert the content of file at the beginning of the generated C++ header file.
`--ixx-prologue-file` file
  Insert the content of file at the beginning of the generated C++ inline file.
`--cxx-prologue-file` file
  Insert the content of file at the beginning of the generated C++ source file.
`--schema-prologue-file` file
  Insert the content of file at the beginning of the generated schema C++ source file.
`--sql-prologue-file` file
  Insert the content of file at the beginning of the generated database schema file.
`--hxx-epilogue-file` file
  Insert the content of file at the end of the generated C++ header file.
`--ixx-epilogue-file` file
  Insert the content of file at the end of the generated C++ inline file.
`--cxx-epilogue-file` file
  Insert the content of file at the end of the generated C++ source file.
`--schema-epilogue-file` file
  Insert the content of file at the end of the generated schema C++ source file.
`--sql-epilogue-file` file
  Insert the content of file at the end of the generated database schema file.
`--odb-prologue` text
  Compile text before the input header file. This option allows you to add additional declarations, such as custom traits specializations, to the ODB compilation process.
`--odb-prologue-file` file
  Compile file contents before the input header file. Prologue files are compiled after all the prologue text fragments (`--odb-prologue` option).
`--odb-epilogue` text
  Compile text after the input header file. This option allows you to add additional declarations, such as custom traits specializations, to the ODB compilation process.
`--odb-epilogue-file` file
  Compile file contents after the input header file. Epilogue files are compiled after all the epilogue text fragments (`--odb-epilogue` option).
`--include-with-brackets`
  Use angle brackets (<>) instead of quotes ("") in the generated `#include` directives.
`--include-prefix` prefix
  Add prefix to the generated `#include` directive paths.
`--include-regex` regex
  Add regex to the list of regular expressions used to transform generated `#include` directive paths. The argument to this option is a Perl-like regular expression in the form `/pattern/replacement/`. Any character can be used as a delimiter instead of `/` and the delimiter can be escaped inside `pattern` and `replacement` with a backslash (`\`). You can specify multiple regular expressions by repeating this option. All the regular expressions are tried in the order specified and the first expression that matches is used.

  As an example, the following expression transforms include paths in the form `foo/bar-odb.h` to paths in the form `foo/generated/bar-odb.h`:

  `%foo/(.+)-odb.h%foo/generated/$1-odb.h%`

  See also the REGEX AND SHELL QUOTING section below.

`--include-regex-trace`
  Trace the process of applying regular expressions specified with the `--include-regex` option. Use this option to find out why your regular expressions don't do what you expected them to do.
`--guard-prefix` prefix
  Add prefix to the generated header inclusion guards. The prefix is transformed to upper case and characters that are illegal in a preprocessor macro name are replaced with underscores.
`--show-sloc`
  Print the number of generated physical source lines of code (SLOC).
`--sloc-limit` num
  Check that the number of generated physical source lines of code (SLOC) does not exceed num.
`--options-file` file
  Read additional options from file with each option appearing on a separate line optionally followed by space and an option value. Empty lines and lines starting with `#` are ignored. Option values can be enclosed in double (`"`) or single (`'`) quotes to preserve leading and trailing whitespaces as well as to specify empty values. If the value itself contains trailing or leading quotes, enclose it with an extra pair of quotes, for example `'"x"'`. Non-leading and non-trailing quotes are interpreted as being part of the option value.

  The semantics of providing options in a file is equivalent to providing the same set of options in the same order on the command line at the point where the `--options-file` option is specified except that the shell escaping and quoting is not required. You can repeat this option to specify more than one options file.

`-x` option
  Pass option to the underlying C++ compiler (`g++`). The option value that doesn't start with `-` is considered the `g++` executable name.
`-v`
  Print the commands executed to run the stages of compilation.
`--trace`
  Trace the compilation process.
`--mssql-server-version` ver
  Specify the minimum SQL Server server version with which the generated C++ code will be used. This information is used to enable version-specific optimizations and workarounds in the generated C++ code. The version must be in the `major.minor` form, for example, `9.0` (SQL Server 2005), `10.5` (2008R2), or `11.0` (2012). If this option is not specified, then `10.0` (SQL Server 2008) or later is assumed.
`--mssql-short-limit` size
  Specify the short data size limit. If a character, national character, or binary data type has a maximum length (in bytes) less than or equal to this limit, then it is treated as short data, otherwise it is long data. For short data ODB pre-allocates an intermediate buffer of the maximum size and binds it directly to a parameter or result column. This way the underlying API (ODBC) can read/write directly from/to this buffer. In the case of long data, the data is read/written in chunks using the `SQLGetData()`/`SQLPutData()` ODBC functions. While the long data approach reduces the amount of memory used by the application, it may require greater CPU resources. The default short data limit is 1024 bytes. When setting a custom short data limit, make sure that it is sufficiently large so that no object id in the application is treated as long data.
`--mysql-engine` engine
  Use engine instead of the default `InnoDB` in the generated database schema file. For more information on the storage engine options see the MySQL documentation. If you would like to use the database-default engine, pass `default` as the value for this option.
`--oracle-client-version` ver
  Specify the minimum Oracle client library (OCI) version with which the generated C++ code will be linked. This information is used to enable version-specific optimizations and workarounds in the generated C++ code. The version must be in the `major.minor` form, for example, `11.2`. If this option is not specified, then `10.1` or later is assumed.
`--sqlite-lax-auto-id`
  Do not force monotonically increasing automatically-assigned object ids. In this mode the generated database schema omits the `AUTOINCREMENT` keyword which results in faster object persistence but may lead to automatically-assigned ids not being in a strictly ascending order. Refer to the SQLite documentation for details.

# REGEX AND SHELL QUOTING

When entering a regular expression argument in the shell command line it is often necessary to use quoting (enclosing the argument in `" "` or `' '`) in order to prevent the shell from interpreting certain characters, for example, spaces as argument separators and `$` as variable expansions.

Unfortunately it is hard to achieve this in a manner that is portable across POSIX shells, such as those found on GNU/Linux and UNIX, and Windows shell. For example, if you use `" "` for quoting you will get a wrong result with POSIX shells if your expression contains `$`. The standard way of dealing with this on POSIX systems is to use `' '` instead. Unfortunately, Windows shell does not remove `' '` from arguments when they are passed to applications. As a result you may have to use `' '` for POSIX and `" "` for Windows (`$` is not treated as a special character on Windows).

Alternatively, you can save regular expression options into a file, one option per line, and use this file with the `--options-file` option. With this approach you don't need to worry about shell quoting.

# DIAGNOSTICS

If the input file is not valid C++, `odb` will issue diagnostic messages to STDERR and exit with non-zero exit code.

# BUGS

Send bug reports to the odb-users@codesynthesis.com mailing list.