MQdb::...edQuery(3pm)
π MQdb::...edQuery(3pm) π
okay so bestie, welcome to the User Contributed Perl Documentation glow-up of MQdb::...edQuery(3pm). yes the filename is literally doing that thing where it gets cut off mid-word, we don't talk about it π
the actual class inside is MQdb::MappedQuery and she is that girl. sit down, grab a strawberry, let's go πβ¨
πΈ NAME πΈ
MQdb::MappedQuery β DESCRIPTION of Object
(no cap, that's what it says. the name section really said "DESCRIPTION of Object" and left it there. iconic of her honestly π)
β¨ SYNOPSIS β¨
An Object_relational_mapping (ORM) design pattern based on mapping named_column results from any query into attributes of an object. As long as the column_names are parsable into attributes, any query is ok.
This is an evolution of several ideas I have either used or created over the last 15 years of coding. This is a variation on the ActiveRecord design pattern but it trades more flexibility, power and control for slightly less automation. It still provides a development speed/ease advange over many ORM patterns.
π¦ DESCRIPTION π¦
MappedQuery is an abstract superclass that is a variation on the ActiveRecord design pattern. Instead of actively mapping a table into an object, this will actively map the result of a query into an object. The query is standardized for a subclass of this object, and the columns returned by the query define the attributes of the object. This gives much more flexibility than the standard implementation of ActiveRecord. Since this design pattern is based around mapping a query (from potentially a multiple table join) to a single class object, this pattern is called MappedQuery.
In this particular implementation of this design pattern (mainly due to some limitations in perl) several aspects must be hand coded as part of the implementation of a subclass. Subclasses must handcode:
- all accessor methods
- override the
mapRowmethod - APIs for all explicit fetch methods (by using the superclass
fetch_singleandfetch_multiple) - the store methods are coded by general DBI code (no framework assistance)
Individual MQdb::Database handle objects are assigned at an instance level for each object. This is different from some ActiveRecord implementations which place database handles into a global context or at the Class level. By placing it with each instance, this allows creation of instances of the same class pulled from two different databases, but with similar schemas. This is very useful when building certain types of data analysis systems.
The only restriction is that the database handle must be able run the queries that the object requires for it to work.
Future implementations could do more automatic code generation but this version already speeds development time by 2x-3x without imposing any limitations and retains all the flexibility of handcoding with DBI.
π¬ CONTACT π¬
Jessica Severin <jessica.severin@gmail.com>
βοΈ LICENSE βοΈ
the legal bit, verbatim, do not touch, she's got receipts π
* Software License Agreement (BSD License)
* MappedQueryDB [MQdb] toolkit
* copyright (c) 2006-2009 Jessica Severin
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Jessica Severin nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
π APPENDIX π
The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
π§ mapRow
- Description: This method must be overridden by subclasses to do the mapping of columns from the query response into attributes of the object. This is part of the internal factory machinery. The instance of the class is created before this method is called and the default
init()method has already been called. The purpose of this method is to initialize the rest of the state of the instance based on the$row_hash - Arg (1):
$row_hashperl hash - Arg (2): optional
$dbcDBI connection (not generally used by most sublcasses) - Returntype:
$self - Exceptions: none
- Caller: only called by internal factory methods
- Example:
sub mapRow {
my $self = shift;
my $rowHash = shift;
$self->primary_id($rowHash->{'symbol_id'});
$self->type($rowHash->{'sym_type'});
$self->symbol($rowHash->{'sym_value'});
return $self;
}
π store
- Description: This method is just an empty template as part of the API definition. How it is defined, and how parameters are handled are completely up to each subclass. Each subclass should override and implement.
- Returntype:
$self - Exceptions: none
- Caller: general loader scripts
π fetch_single
- Description: General purpose template method for fetching a single instance of this class(subclass) using the
mapRowmethod to convert a row of data into an object. - Arg (1):
$database(MQdb::Database) - Arg (2):
$sql(string of SQL statement with place holders) - Arg (3...): optional parameters to map to the placehodlers within the SQL
- Returntype: instance of this Class (subclass)
- Exceptions: none
- Caller: subclasses (not public methods)
- Example:
sub fetch_by_id {
my $class = shift;
my $db = shift;
my $id = shift;
my $sql = "SELECT * FROM symbol WHERE symbol_id=?";
return $class->fetch_single($db, $sql, $id);
}
π fetch_multiple
- Description: General purpose template method for fetching an array of instance of this class(subclass) using the
mapRowmethod to convert a row of data into an object. - Arg (1):
$database(MQdb::Database) - Arg (2):
$sql(string of SQL statement with place holders) - Arg (3...): optional parameters to map to the placehodlers within the SQL
- Returntype: array of all instances of this Class (subclass) which match the query
- Exceptions: none
- Caller: subclasses (not public methods)
- Example:
sub fetch_all_by_value {
my $class = shift;
my $db = shift;
my $name = shift;
my $sql = "SELECT * FROM symbol WHERE sym_value=?";
return $class->fetch_multiple($db, $sql, $name);
}
π stream_multiple
- Description: General purpose template method for fetching multiple instance of this class(subclass) using the
mapRowmethod to convert a row of data into an object. Instead of instantiating all instance at once and returning as array, this method returns aDBStreaminstance which then creates each instance from an open handle on each$stream->next_in_stream()call. - Arg (1):
$database(MQdb::Database) - Arg (2):
$sql(string of SQL statement with place holders) - Arg (3...): optional parameters to map to the placehodlers within the SQL
- Returntype:
DBStreamobject - Exceptions: none
- Caller: subclasses use this internally when creating new API
stream_by....()methods - Example:
sub stream_by_value {
my $class = shift;
my $db = shift;
my $name = shift;
my $sql = "SELECT * FROM symbol WHERE sym_value=?";
return $class->stream_multiple($db, $sql, $name);
}
π fetch_col_value
- Description: General purpose function to allow fetching of a single column from a single row.
- Arg (1):
$sql(string of SQL statement with place holders) - Arg (2...): optional parameters to map to the placehodlers within the SQL
- Example:
$value = $self->fetch_col_value($db,
"select symbol_id from symbol where sym_type=? and sym_value=?",
$type,$value);
- Returntype: scalar value
- Exceptions: none
- Caller: within subclasses to easy development
π§ fetch_col_array
- Description: General purpose function to allow fetching of a single column from many rows into a simple array.
- Arg (1):
$sql(string of SQL statement with place holders) - Arg (2...): optional parameters to map to the placehodlers within the SQL
- Example:
$array_ref = $self->fetch_col_array($db, "select some_column from my_table where source_id=?", $id);
- Returntype: array reference of scalar values
- Exceptions: none
- Caller: within subclasses to easy development
π next_sequence_id
- Description: Convenience method for working with
SEQUENCESin ORACLE databases. - Arg (1):
$sequenceName - Returntype: scalar of the
nextvalin the sequence - Exceptions: none
π fr fr bestie, that's the whole page. go write your subclass, override mapRow, and handcode those accessors like the main character you are π«Ά
and that's a wrap on MQdb::...edQuery(3pm) β this version is perl v5.36.0, dated 2022-12-13, and it's been a whole vibe. mwah π
πβ¨
π¦ where this page comes from
- debian bookworm: libmqdb-perl 0.954-3 Β· Poetic
- debian trixie: libmqdb-perl 0.954-3 Β· Poetic
- ubuntu noble: libmqdb-perl 0.954-3 Β· Poetic
- ubuntu resolute: libmqdb-perl 0.954-3 Β· Poetic
- ubuntu stonking: libmqdb-perl 0.954-3 Β· Poetic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms