The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBQuery - Lib of DB Query

SYNOPSIS

The following lib are provided:

import

use DBQuery;

Struct Init

Init mysql struct example:

    my %DB = (
        'db_host'            => 'web10.search.cnb.yahoo.com',
        'db_user'            => 'yahoo',
        'db_pass'            => 'yahoo',
        'db_name'            => 'ADCode',
        'db_port'            => 3306,
        'db_pconnect'            => 1,
        'db_autocommit'            => 1,
        'db_enable_utf8'        => 0,
        );
    my $db = new DBQuery(\%DB);

or postgresql:

    my %PQ = (
        'driver_name'        => 'PgPP',
        'db_host'        => 'tool2.search.cnb.yahoo.com',
        'db_name'        => 'cnedb',
        'db_user'        => 'cnedb',
        'db_pass'        => 'cnedb',
        );
    my $db = new DBQuery(\%PQ);

or oracle:

    my %OC = (
        'driver_name'            => 'oracle',
        'db_host'            => 'ocndb',
        'db_user'            => 'alibaba',
        'db_pass'            => 'ocndb',
        'db_port'            => 1521,
        'db_name'            => 'ctutest', // the same as db_sid
        'db_longreadlen'        => 33554432,
        'db_longtruncok'        => 1,
        );
    my $db = new DBQuery(\%OC);

over this, you can use dsn for init structure.

    my %DB = (
        'dsn'        => 'dbi:mysql:database=testinter;host=localhost;mysql_socket=/var/lib/mysql/mysql.sock;mysql_use_result=1',
        'db_user'       => 'pca',
        'db_pass'       => 'pca',
        );
    my $db = new DBQuery(\%DB);

it yet run.

Connect

Connect resource from database.

    $db->connect();

You can unset the variable: %DB, %PQ or $OC, like this:

    undef %PQ;

or

    undef %DB;

or

    undef %OC;
Query

Simple query:

    $db->query("select url from edb.white_black_grey where spamtype=':demote2:' limit 10;");
    while (my @row = $db->fetch_array())
    {
        print Dumper @row, "\n";
    }

    $db->query("alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss'");

Common:

    my $query = $db->query("select url from edb.white_black_grey where spamtype=':demote2:' limit 10;");
    while (my @row = $db->fetch_array($query))
    {
        print Dumper @row, "\n";
    }
Disconnect

Release resource from database.

    $db->close();

OPTIONS

Nothing because of no script.

DESCRIPTION

DBQuery allows you to query some information from some different type databases, like mysql, postgresql and oracle, so our system need module which include DBD::mysql, DBD::PgPP and DBD::Oracle.

In future, it'll support more and more database types if you want. You can use DBQuery very expediently. so we use database easily.

This lib can use dsn which contains all connection information or use all single items, like db_host, db_pass etc.

$self->new()

Construct.

$self->connect()

Create a connect.

$self->close()

Close this connection.

$self->query()

Send a query.

$self->fetch_array()

Fetch and return array.

$self->fetch_arrayref()

Fetch and return reference of array.

$self->fetch_hash()

Fetch and return reference of hash.

$self->quote()

Quote some characters.

$self->insert_id()

Return last insert id.

TIPS

There's some extra tips found in our own's everyday use:

CUSTOM

Like php-mysql

   $db->connect();
   $db->query();
   $db->fetch_array();
ABSTRACT

One line description of the module. Will be included in PPD file.

ABSTRACT_FROM

Name of the file that contains the package description. MakeMaker looks for a line in the POD matching /^($package\s-\s)(.*)/. This is typically the first line in the "=head1 NAME" section. $2 becomes the abstract.

PREREQUISITES

This module uses DBI.

INSTALLATION

If you are not sudoer or root, you need contact administrator.

    perl Makefile.PL
    make
    make test
    make install

Win32 users should replace "make" with "nmake".

SOURCE CONTROL

You can always get the latest SSH::Batch source from its public Git repository:

    http://github.com/cnangel/DB/tree/master

If you have a branch for me to pull, please let me know ;)

TODO

To this:

  • Sqlite2 and sqlite3 will be supported.

  • New engine for DBQuery.

SEE ALSO

DBI, DBD::mysql, DBD::PgPP, DBD::Oracle

COPYRIGHT AND LICENSE

This module as well as its programs are licensed under the BSD License.

Copyright (c) 2007-2008, Yahoo! China Relevance Team, Alibaba Inc. All rights reserved.

Copyright (c) 2009, Alibaba Search Center, Alibaba Inc. All rights reserved.

Copyright (C) 2009, Cnangel Li (cnangel). 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 the Alibaba Search Center, Alibaba Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

AUTHOR

Cnangel (cnangel@gmail.com)

HISTORY

see ChangeLog.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.