|
Muldis::DB::Interface - Common public API for Muldis DB Engines |
Muldis::DB::Interface - Common public API for Muldis DB Engines
This document describes Muldis::DB::Interface version 0.4.0 for Perl 5.
It also describes the same-number versions for Perl 5 of Muldis::DB::Interface::DBMS (``DBMS''), Muldis::DB::Interface::Var (``Var''), Muldis::DB::Interface::FuncBinding (``FuncBinding''), and Muldis::DB::Interface::ProcBinding (``ProcBinding'').
This simple example declares two Perl variables containing relation data, then does a (N-ary) relational join (natural inner join) on them, producing a third Perl variable holding the relation data of the result.
use Muldis::DB::Interface;
my $dbms = Muldis::DB::Interface::new_dbms({
'engine_name' => 'Muldis::DB::Engine::Example',
'dbms_config' => {},
});
my $r1 = $dbms->new_var({
'decl_type' => 'sys.Core.Relation.Relation' });
my $r2 = $dbms->new_var({
'decl_type' => 'sys.Core.Relation.Relation' });
$r1->store_ast({ 'ast' => [ 'Relation', 'sys.Core.Relation.Relation', [
{
'x' => [ 'PInt', 'perl_pint', 4 ],
'y' => [ 'PInt', 'perl_pint', 7 ],
},
{
'x' => [ 'PInt', 'perl_pint', 3 ],
'y' => [ 'PInt', 'perl_pint', 2 ],
},
] ] });
$r2->store_ast({ 'ast' => [ 'Relation', 'sys.Core.Relation.Relation', [
{
'y' => [ 'PInt', 'perl_pint', 5 ],
'z' => [ 'PInt', 'perl_pint', 6 ],
},
{
'y' => [ 'PInt', 'perl_pint', 2 ],
'z' => [ 'PInt', 'perl_pint', 1 ],
},
{
'y' => [ 'PInt', 'perl_pint', 2 ],
'z' => [ 'PInt', 'perl_pint', 4 ],
},
] ] });
my $r3 = $dbms->call_func(
'func_name' => 'sys.Core.Relation.join',
'args' => {
'topic' => [ 'QuasiSet', 'sys.Core.Spec.QuasiSetOfRelation', [
$r1,
$r2,
],
}
);
my $r3_ast = $r3->fetch_ast();
# Then $r3_ast contains:
# [ 'Relation', 'sys.Core.Relation.Relation', [
# {
# 'x' => [ 'PInt', 'perl_pint', 3 ],
# 'y' => [ 'PInt', 'perl_pint', 2 ],
# 'z' => [ 'PInt', 'perl_pint', 1 ],
# },
# {
# 'x' => [ 'PInt', 'perl_pint', 3 ],
# 'y' => [ 'PInt', 'perl_pint', 2 ],
# 'z' => [ 'PInt', 'perl_pint', 4 ],
# },
# ] ]
For most examples of using Muldis DB, and tutorials, please see the separate the Muldis::DB::Cookbook manpage distribution (when that comes to exist).
Muldis::DB::Interface, aka Interface, comprises the minimal core of the Muldis DB framework, the one component that probably every program would use. Together with the Muldis D language (see the Language::MuldisD manpage), it defines the common API for Muldis DB implementations to do and which applications invoke.
This documentation is pending.
The interface of Muldis::DB::Interface is fundamentally object-oriented; you use it by creating objects from its member classes (or more specifically, of implementing subclasses of its member roles) and then invoking methods on those objects. All of their attributes are private, so you must use accessor methods.
To aid portability of your applications over multiple implementing Engines,
the normal way to create Interface objects is by invoking a
constructor-wrapping method of some other object that would provide context
for it; since you generally don't have to directly invoke any package
names, you don't need to change your code when the package names change due
to switching the Engine. You only refer to some Engine's root package name
once, as a Muldis::DB::Interface::new_dbms argument, and even that can
be read from a config file rather than being hard-coded in your
application.
The usual way that Muldis::DB::Interface indicates a failure is to throw an exception; most often this is due to invalid input. If an invoked routine simply returns, you can assume that it has succeeded, even if the return value is undefined.
The Muldis::DB::Interface module is the stateless root package by way of
which you access the whole Muldis DB API. That is, you use it to load
engines and instantiate virtual machines, which provide the rest of the
Muldis DB API.
new_dbms of Muldis::DB::Interface::DBMS (Str :$engine_name!, Any
:$dbms_config!)DBMS object that is
implemented by the Muldis DB Engine named by its named argument
$engine_name; that object is initialized using the $dbms_config
argument. The named argument $engine_name is the name of a Perl module
that is expected to be the root package of a Muldis DB Engine, and which is
expected to declare a new_dbms subroutine with a single named argument
$dbms_config; invoking this subroutine is expected to return an object
of some class of the same Engine which does the Muldis::DB::Interface::DBMS
role. This function will start by testing if the root package is already
loaded (it may be declared by some already-loaded file of another name),
and only if not, will it do a Perl 'require' of the $engine_name.
A DBMS object represents a single active Muldis DB virtual machine /
Muldis D environment, which is the widest scope stateful context in which
any other database activities happen. Other activities meaning the
compilation and execution of Muldis D code, mounting or unmounting depots,
performing queries, data manipulation, data definition, and transactions.
If a DBMS object is ever garbage collected by Perl while it has any
active transactions, then those will all be rolled back, and then an
exception thrown.
new_var of Muldis::DB::Interface::Var (Str :$decl_type!)Var object that is associated with
the invocant DBMS, and whose declared Muldis D type is named by the
$decl_type argument, and whose default Muldis D value is the default
value of its declared type.
assoc_vars of Array ()Var objects that are associated with the invocant
DBMS.
new_func_binding of Muldis::DB::Interface::FuncBinding ()FuncBinding object that is
associated with the invocant DBMS.
assoc_func_bindings of Array ()FuncBinding objects that are associated with the
invocant DBMS.
new_proc_binding of Muldis::DB::Interface::ProcBinding ()ProcBinding object that is
associated with the invocant DBMS.
assoc_proc_bindings of Array ()ProcBinding objects that are associated with the
invocant DBMS.
call_func of Muldis::DB::Interface::Var (Str :$func_name!, Hash
:$args!)$func_name
argument, giving it arguments from $args, and then returning the result
as a new Var object. This method is conceptually a wrapper over the
creation of a FuncBinding object, setting up its bindings, and invoking
its call method.
call_proc (Str :$proc_name!, Hash :$upd_args!, Hash :$ro_args!)$proc_name
argument, giving it subject-to-update arguments from $upd_args and
read-only arguments from $ro_args; the Var objects in $upd_args
are possibly updated as a side-effect of the procedure's execution. This
method is conceptually a wrapper over the creation of a ProcBinding
object, setting up its bindings, and invoking its call method.
trans_nest_level of Int ()start_trans will increase the nesting level by one, and each
commit_trans or rollback_trans will decrease it by one (it can't be
decreased below zero). Note that all transactions started or ended within
Muldis D code are attached to a particular lexical scope in the Muldis D
code (specifically a ``try/catch'' context), and so they will never have any
effect on the nest level that Perl sees (assuming that a Muldis D host
language will never be invoked by Muldis D), regardless of whether the
Muldis D code successfully returns or throws an exception.
start_trans ()commit_trans ()rollback_trans ()
A Var object is a Muldis D variable that is lexically scoped to the Perl
environment (like an ordinary Perl variable). It is associated with a
specific DBMS object, the one whose new_var method created it, but it
is considered anonymous and non-invokable within the virtual machine. The
only way for Muldis D code to work with these variables is if they bound to
Perl invocations of Muldis D routines being call(|\w+) by Perl; a Muldis
D routine parameter one is bound to is the name it is referenced by in the
virtual machine. Var objects are the normal way to directly share or
move data between the Muldis D and Perl environments. A Var is strongly
typed, and the declared Muldis D type of the variable (which affects what
values it is allowed to hold) is set when the Var object is created, and
this declared type can't be changed afterwards.
assoc_dbms of Muldis::DB::Interface::DBMS ()DBMS object that the invocant Var is
associated with.
decl_type of Str ()Var.
fetch_ast of Array ()Var as a
Perl Hosted Abstract Muldis D data structure (whose root node is a Perl
Array).
store_ast (Array :$ast!)Var, which is
supplied in the $ast argument; the argument is expected to be a valid
Perl Hosted Abstract Muldis D data structure (whose root node is a Perl
Array).
A FuncBinding represents a single Muldis D function that may be directly
invoked by Perl code. It is associated with a specific DBMS object, the
one whose new_func_binding method created it, and the function it
represents lives in and has a global-public scoped name in the
corresponding virtual machine. This is specifically a lazy binding, so no
validity checking of the object happens except while the FuncBinding's
call method is being executed, and a then-valid object can then become
invalid afterwards. A FuncBinding is conceptually used behind the
scenes to implement a DBMS object's call_func method, but you can use
it directly instead, for possibly better performance.
assoc_dbms of Muldis::DB::Interface::DBMS ()DBMS object that the invocant FuncBinding is
associated with.
bind_func (Str :$func_name!)FuncBinding to be associated with the
Muldis D function named by the $func_name argument.
bound_func of Str ()FuncBinding is currently associated with, or undef if that wasn't set.
bind_result (Muldis::DB::Interface::Var :$var!)Var object in $var to the result of the Muldis
D function associated with the invocant FuncBinding; when the function
is executed via the FuncBinding, its result will end up in $var.
bound_result of Muldis::DB::Interface::Var ()Var object currently bound to the function
result.
bind_params (Hash :$args!)Var objects that are the Hash values in $args
to the parameters of the Muldis D function such that they correspond by
Hash key names matching parameter names; when the function is executed via
the FuncBinding, its arguments are pulled from the $args. Note that the
same Var object may be bound to multiple parameters and/or the result at
once. This method alternately allows a Perl Array which is Perl Hosted
Muldis D to be supplied instead of any given Var object, in which case a
new Var object will be non-lazily created with that value, and be used
there.
bound_params of Hash ()Var objects currently
bound to the function's parameters, with the corresponding Hash keys being
the names of the parameters they are bound to.
call ()FuncBinding,
and with no failure, it then invokes the Muldis D function. It is at this
time that the current values of any bound Var objects are taken.
A ProcBinding represents a single Muldis D procedure that may be
directly invoked by Perl code. It is associated with a specific DBMS
object, the one whose new_proc_binding method created it, and the
procedure it represents lives in and has a global-public scoped name in the
corresponding virtual machine. This is specifically a lazy binding, so no
validity checking of the object happens except while the ProcBinding's
call method is being executed, and a then-valid object can then become
invalid afterwards. A ProcBinding is conceptually used behind the
scenes to implement a DBMS object's call_proc method, but you can use
it directly instead, for possibly better performance.
assoc_dbms of Muldis::DB::Interface::DBMS ()DBMS object that the invocant ProcBinding is
associated with.
bind_proc (Str :$proc_name!)ProcBinding to be associated with the
Muldis D procedure named by the $proc_name argument.
bound_proc of Str ()ProcBinding is currently associated with, or undef if that wasn't set.
bind_upd_params (Hash :$args!)Var objects that are the Hash values in $args
to the subject-to-update parameters of the Muldis D procedure such that
they correspond by Hash key names matching parameter names; when the
procedure is executed via the ProcBinding, its subject-to-update arguments
(if they would be used) are pulled from the $args, and resulting values
are written to them (if applicable).
bound_upd_params of Hash ()Var objects currently
bound to the procedure's subject-to-update parameters, with the
corresponding Hash keys being the names of the parameters they are bound
to.
bind_ro_params (Hash :$args!)Var objects that are the Hash values in $args
to the read-only parameters of the Muldis D procedure such that they
correspond by Hash key names matching parameter names; when the procedure
is executed via the ProcBinding, its read-only arguments are pulled from
the $args. Note that the same Var object may be bound to multiple
parameters and/or the result at once. This method alternately allows a
Perl Array which is Perl Hosted Muldis D to be supplied instead of any
given Var object, in which case a new Var object will be non-lazily
created with that value, and be used there.
bound_ro_params of Hash ()Var objects currently
bound to the procedure's read-only parameters, with the corresponding Hash
keys being the names of the parameters they are bound to.
call ()ProcBinding,
and with no failure, it then invokes the Muldis D procedure. It is at this
time that the current values of any bound Var objects are taken.
This documentation is pending.
This documentation is pending.
This file requires any version of Perl 5.x.y that is at least 5.8.1.
None reported.
Go to the Muldis::DB manpage for the majority of distribution-internal references, and the Muldis::DB::SeeAlso manpage for the majority of distribution-external references.
The Muldis DB framework for Perl 5 is built according to certain old-school or traditional Perl-5-land design principles, including that there are no explicit attempts in code to enforce privacy of the framework's internals, besides not documenting them as part of the public API. (The Muldis DB framework for Perl 6 is different.) That said, you should still respect that privacy and just use the public API that Muldis DB provides. If you bypass the public API anyway, as Perl 5 allows, you do so at your own peril.
This documentation is pending.
Darren Duncan (perl@DarrenDuncan.net)
This file is part of the Muldis DB framework.
Muldis DB is Copyright © 2002-2007, Darren Duncan.
See the LICENSE AND COPYRIGHT of the Muldis::DB manpage for details.
The ACKNOWLEDGEMENTS in the Muldis::DB manpage apply to this file too.
|
Muldis::DB::Interface - Common public API for Muldis DB Engines |