DBIx::Abstract(3pm)
β¨ DBIx::Abstract β¨ β the manual page, bestie edition π
okay so you found the DBIx::Abstract(3pm) page, a User Contributed Perl Documentation situation, and honestly? you're in the right place. sit down, hydrate π, and let's get into it.
πΈ NAME πΈ
DBIx::Abstract β DBI SQL abstraction. that's it, that's the whole vibe check. π
π VERSION π
version 1.04 β no cap, that's the number right there. β
β¨ SYNOPSIS β¨
here's the little taste test, copy it, run it, live your truth π¦:
use DBIx::Abstract;
my $db = DBIx::Abstract->connect({
driver=>'mydriver',
host=>'myhost.org',
dbname=>'mydb',
user=>'myuser',
password=>'mypassword',
});
if ($db->select('*','table')->rows) {
while (my $data = $db->fetchrow_hashref) {
# ...
}
}
my $id = 23;
my ($name) = $db->select('name','table',{id=>$id})->fetchrow_array;
###
$db = DBIx::Abstract->connect( { driver=>'csv', f_name=>'foo/' } );
###
$db = DBIx::Abstract->connect({
dsn=>'dbi:someotherdb:so_db_name=mydb',
user=>'myuser',
password=>'mypassword',
});
likeβ¦ the way it just connects and selects? main character energy fr fr π
π DESCRIPTION π
so basically, this module provides methods for doing manipulating database tables. (yes, that's what it says.) and then, also: this module provides methods retrieving and storing data in SQL databases. it gives you methods for all of the more important SQL commands (like SELECT, INSERT, REPLACE, UPDATE, DELETE). π₯
it endeavors to produce an interface that will be intuitive to those already familiar with SQL. lowkey thoughtful of it honestly π«Ά
notable features include:
- data_source generation for some DBD drivers.
- Can check to make sure the connection is not stale and reconnect if it is.
- Controls statement handles for you.
- Can delay writes.
- Generates complex where clauses from hashes and arrays.
- Shortcuts (convenience functions) for some common cases. (Like
select_all_to_hashref.)
it's giving "i handle the statement handles so you don't have to" π iconic behaviour. π
π DEPRICATED π
yes, that spelling is on purpose, i'm keeping it exactly as-is. π
We highly recommend that you use something like SQL::Abstract, which was inspired by this module. Or even DBIx::Class (which uses SQL::Abstract for it's query syntax). They're maintained and widely used.
so likeβ¦ it still works, but the girlies have moved on toSQL::AbstractandDBIx::Class. they're maintained and widely used.DBIx::Abstractis the origin story, not the current era. π
to be continuedβ¦ π
β¨ METHODS β¨
Okay bestie, here's the thing you actually came for π unless a method specifically says otherwise, every single one of these returns the database handle. Every. Single. One. No cap.
π connect
connect($connect_config | $dbihandle [,$options])
CONSTRUCTOR β¨
This is the glow-up moment where you open a connection to a database, configured by $connect_config. And $connect_config can be one of a couple things:
- If it's a scalar, it's just a DBI data source. Simple. Clean. Iconic.
- If it's a reference to a hash, it takes these keys:
dsn β the data source to connect to your database π«Ά
OR, DBIx::Abstract will try to generate the dsn for you if you hand it these instead:
driverβ the DBD driver to use (defaults tomysql)hostβ host of the database serverportβ port of the database serverdbnameβ name of the database
Username and password are always valid, always welcome π
userβ username to connect aspasswordβ password for that user
Alternatively, you can pass in a DBI handle directly. But heads up β doing that disables the reconnect and ensure_connection methods, because those two rely on connection info that a bare DBI handle just doesn't have. It's giving limited info, not the full lore π
Options is a hash reference, and every key/value pair in it gets passed on to the opt method.
𧬠clone
This clones the object. For those times when you need a second connection to the same DB. If you need a second connection to a different DB, make a whole new object with connect instead, okay?
This operation gets logged at level 5 with the message "Cloned."
π©Ί connected
Checks whether this object is connected to a database. Specifically it checks if it has a database handle and if that handle's "Active" attribute is true. Vibe check for your connection π¦
β»οΈ reconnect
If the object is not connected to a database, it reconnects using the exact same parameters connect was originally called with. Muscle memory, fr fr.
π ensure_connection
Makes sure the object is connected to a database. It confirms the connection is active (by sending a "SELECT 1"). If there's no connection, or the connection isn't active, it tries to reconnect. And if the reconnect fails? It dies. No soft launch here π
βοΈ opt
($key[,$value])
({key=>$key[,value=>$value])
Set option $key to $value. Here are the keys you can use:
loglevel(default0)0β Fatal errors only1β Modifications2β And selects3β And user created queries4β And results of queries5β And other misc commands6β Internals of commands
logfile(defaultundef) β log filedelaymods(defaultfalse) β delay making modifications to the database untilrun_delayedis runuseCachedβ if this is true, thenprepare_cachedis used instead ofprepare. Please actually check out the DBI documentation on this feature before you use it, bestie, I'm serious πsaveSQLβ if this is true, then with each query DBIx::Abstract will stuff the generated SQL into the'lastsql'key in the self payload
On top of all that, you may use any valid DBI attribute. So, for instance, you can pass AutoCommit or LongReadLen. Sky's the limit β¨
This operation is logged at level 5 with the message "Option Change", plus the key, the old value, and the new value.
π¨ query
($sql,@bind_params)
({sql=>$sql,bind_params=>[@bind_params]})
This sends $sql to the database object's query method. Use it for applications where the existing methods just aren't generating flexible enough SQL for you.
And listen β if you find yourself using this very often with things other than table manipulation (like 'create table', 'alter table', 'drop table'), then please let the author know so DBIx::Abstract can be extended to include the functionality you're using. They wanna know π«Ά
This operation is logged at level 3.
β³ run_delayed
Execute delayed update/insert/delete queries. The queue finally gets its moment π
This operation is logged at level 5 with the message "Run delayed".
ποΈ delete
($table[,$where])
({table=>$table[,where=>$where]})
Deletes records from $table. Also go read the docs on "DBIx::Abstract Where Clauses".
β insert
($table,$fields)
({table=>$table,fields=>$fields})
$table is the name of the table to insert into.
$fields is either a reference to a hash of field name/value, or a scalar containing the SQL to insert after the "SET" portion of the statement.
These all produce functionally equivalent SQL, bestie:
$db->insert('foo',{bar=>'baz'});
$db->insert('foo',q|bar='baz'|);
$db->insert({table=>'foo',fields=>{bar=>'baz'}});
$db->insert({table=>'foo',fields=>q|bar='baz'|});
We also support literals by making the value in the hash an arrayref:
$db->insert('foo',{name=>'bar',date=>['substring(now(),1,10)']});
Would generate something like this:
INSERT INTO foo (name,date) VALUES (?,substring(now(),1,10))
With "bar" bound to the first parameter. Slay π
π replace
($table,$fields)
({table=>$table,fields=>$fields})
$table is the name of the table to replace into.
$fields is either a reference to a hash of field name/value, or a scalar containing the SQL to insert after the "SET" portion of the statement.
replace works just like insert, except that if a record with the same primary key already exists, then the existing record gets replaced instead of throwing an error. It's giving "no drama, just overwrite" π
βοΈ update
($table,$fields[,$where])
({table=>$table,fields=>$fields[,where=>$where]})
$table is the table to update.
$fields is a reference to a hash keyed on field name / new value.
Also see the docs on "DBIx::Abstract Where Clauses".
π select
select
($fields,[$table,[$where[,$order]]])
({ fields=>$fields, table=>$table [,where=>$where] [,order=>$order]
[,join=>$join] [,group=>$group] })
The select method returns the DBIx::Abstract object it was invoked with. Which means... you can chain commands. Main character energy β¨
$fields can be either an array reference or a scalar. If it's an array reference, then it should be a list of fields to include. If it's a scalar, then it should be a literal to be inserted into the generated SQL after "SELECT".
$table can be either an array reference or a scalar. If it's an array reference, then it should be a list of tables to use. If it's a scalar, then it should be a literal to be inserted into the generated SQL after "FROM".
Also see the docs on "DBIx::Abstract Where Clauses".
$order is the output order. If it's a scalar, it gets inserted literally after "ORDER BY". If it's an arrayref, it gets join'd with a comma and inserted.
$join is there to make joining tables more convenient. It takes one or more (as an arrayref) sets of statements to use when joining. For instance:
$dbh->select({
fields=>'*',
table=>'foo,bar',
join=>'foo.id=bar.foo_id',
where=>{'foo.dollars',['>',30]}
});
Would produce:
SELECT * FROM foo,bar WHERE (foo.dollars > ?) and (foo.id=foo_id)
And put 30 into the bind_params list. Chef's kiss π₯
$group is/are the field(s) to group by. It may be a scalar or an arrayref. If it's a scalar, then it should be a literal to be inserted after "GROUP BY". If it's an arrayref, then it should be a list of fields to group on.
π― select_one_to_hashref
($fields,$table[,$where])
({fields=>$fields,table=>$table[,where=>$where]})
This returns a hashref to the first record returned by the select. Typically you'd use this for cases when your where clause already limits you to one record anyway.
$fields can be either an array reference or a scalar. If it's an array reference, then it should be a list of fields to include. If it's a scalar, then it should be a literal to be inserted into the generated SQL.
$table is the table to select from.
Also see the docs on "DBIx::Abstract Where Clauses".
π― select_one_to_arrayref
($fields,$table[,$where])
({fields=>$fields,table=>$table[,where=>$where]})
This returns an arrayref to the first record returned by the select. Typically you'd use this for cases when your where clause already limits you to one record anyway.
$fields can be either an array reference or a scalar. If it's an array reference, then it should be a list of fields to include. If it's a scalar, then it should be a literal to be inserted into the generated SQL.
$table is the table to select from.
Also see the docs on "DBIx::Abstract Where Clauses".
π― select_one_to_array
($fields,$table[,$where])
({fields=>$fields,table=>$table[,where=>$where]})
This returns an array to the first record returned by the select. Typically you'd use this for cases when your where clause already limits you to one record anyway.
$fields can be either an array reference or a scalar. If it's an array reference, then it should be a list of fields to include. If it's a scalar, then it should be a literal to be inserted into the generated SQL.
$table is the table to select from.
Also see the docs on "DBIx::Abstract Where Clauses".
π select_all_to_hashref
($fields,$table[,$where])
({fields=>$fields,table=>$table[,where=>$where]})
This returns a hashref to all of the results of the select. It's keyed on the first field. If there are only two fields, then the value is just the second field. If there are more than two fields, then the value is set to an arrayref that contains all of the fields.
$fields can be either an array reference or a scalar. If it's an array reference, then it should be a list of fields to include. If it's a scalar, then it should be a literal to be inserted into the generated SQL.
$table is the table to select from.
Also see the docs on "DBIx::Abstract Where Clauses".
π§² fetchrow_hashref
This is just a call to the DBI method. Passing it through, no notes π
π§² fetchrow_hash
This calls fetchrow_hashref and dereferences it for you. The little shortcut that could π«Ά
π§² fetchrow_array
This method calls the database handle's method of the same name.
π§² fetchall_arrayref
This method calls the database handle's method of the same name.
π’ rows
This method calls the database handle's method of the same name.
π¬ quote
This method is passed to the database handle via AUTOLOAD.
π disconnect
This method is passed to the database handle via AUTOLOAD.
πΎ commit
This method is passed to the database handle via AUTOLOAD.
βͺ rollback
This method is passed to the database handle via AUTOLOAD.
π΅οΈ trace
This method is passed to the database handle via AUTOLOAD.
π finish
This method is passed to the statement handle via AUTOLOAD.
π bind_col
This method is passed to the statement handle via AUTOLOAD.
π bind_columns
This method is passed to the statement handle via AUTOLOAD.
π Other things that need explanation π
Like bestie, some stuff just needs its own lil moment, okay? π«Ά
πΊοΈ DBIx::Abstract Where Clauses πΊοΈ
Where clauses in DBIx::Abstract can either be super simple, or extremely complex. They're literally designed to be easy if you're just typing in a hard coded statement, or if you have to build a complex query from data. Both vibes are valid π
Wheres are either a scalar, hash-ref or array-ref:
- If it's a scalar, then it's used as the literal where. Periodt.
- If it's a hash-ref then the key is the field to check, and the value is either a literal value to compare equality to, or an array-ref to an array of operator and value. Iykyk.
{
first=>'joe',
age=>['>',26],
last=>['like',q|b'%|]
}
Would produce:
WHERE first = ? AND last like ? AND age > ?
With joe, b'% and 26 passed as bind values. β¨
- If it's an array-ref then it's an array of hash-refs and connectors:
[
{
first=>'joe',
age=>['>',26]
},
'OR',
{
last=>['like',q|b'%|]
}
]
Would produce:
WHERE (first = ? AND age > ?) OR (last like ?)
With joe, 26 and b'% passed as bind values. π
[
{
first=>'joe',
last=>['like','%foo%'],
},
'AND',
[
{age=>['>',26]},
'OR',
{age=>['<',30]}
]
]
Would produce:
WHERE (first = ? AND last like ?) AND ((age > ?) OR (age < ?))
With joe, %foo%, 26 and 30 passed as bind values. nesting goes hard fr fr π₯
π SUPPORTED DBD DRIVERS π
These drivers have been reported to work:
mysql(development environment)Pg(development environment)OracleXBase
Any driver that uses ODBC syntax should work using the hash ref method. With other drivers you should pass the DBI data source instead β and lowkey this method will work with all drivers. π
π CHANGES SINCE LAST RELEASE π
- Updated source pointers to
github. - Fixed hash randomization related test failure.
Not much but she's honest π
π AUTHOR π
Rebecca Turner <me@re-becca.org>
π SOURCE π
The development version is on github at <http://https://github.com/iarna/DBIx-Abstract> and may be cloned from <git://https://github.com/iarna/DBIx-Abstract.git>
π«Ά SUPPORT π«Ά
π Websites π
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
- MetaCPAN
A modern, open-source CPAN search engine, useful to view POD in HTML format.
<http://metacpan.org/release/DBIx-Abstract>
π Bugs / Feature Requests π
Please report any bugs at <https://github.com/iarna/DBIx-Abstract/issues>.
Bestie if you find a bug, pls actually report it, don't just suffer in silence π
π AUTHOR π (yes, again, she said it twice and she's right) π
Rebecca Turner <me@re-becca.org>
π COPYRIGHT AND LICENSE π
Portions copyright 2001-2014 by Rebecca Turner
Portions copyright 2000-2001 by Adelphia Business Solutions
Portions copyright 1998-2000 by the Maine Internetworks (MINT)
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
And that's the whole page babes π
this manual page is DBIx::Abstract(3pm) β perl v5.36.0, dated 2022-12-12. Go forth and query, bestie, you're gonna slay πΈπ¦β¨
π¦ where this page comes from
- debian bookworm: libdbix-abstract-perl 1.040-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdbix-abstract-perl 1.040-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdbix-abstract-perl 1.040-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdbix-abstract-perl 1.040-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdbix-abstract-perl 1.040-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms