inc/dal.inc.php
author rejo
Sat, 14 Apr 2007 11:37:27 +0000
changeset 2 9a485c41cfcf
parent 1 58094faf794d
child 4 55ed92aa7cf5
permissions -rwxr-xr-x
[feladat @ 3] First set of changes to provide translation framework.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     1
<?php
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     3
// +----------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     4
// | PHP Version 4                                                        |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     5
// +----------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     6
// | Copyright (c) 1997-2002 The PHP Group                                |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     7
// +----------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     8
// | This source file is subject to version 2.02 of the PHP license,      |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     9
// | that is bundled with this package in the file LICENSE, and is        |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    10
// | available at through the world-wide-web at                           |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    11
// | http://www.php.net/license/2_02.txt.                                 |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    12
// | If you did not receive a copy of the PHP license and are unable to   |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    13
// | obtain it through the world-wide-web, please send a note to          |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    14
// | license@php.net so we can mail you a copy immediately.               |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    15
// +----------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    16
// | Authors: Stig Bakken <ssb@fast.no>                                   |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    17
// |          Tomas V.V.Cox <cox@idecnet.com>                             |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    18
// +----------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    19
//
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    20
// $Id: dal.inc.php,v 1.1 2002/10/27 19:18:42 azurazu Exp $
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    21
//
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    22
// Database independent query interface.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    23
//
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    24
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    25
require_once "PEAR.php";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    26
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    27
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
 * The method mapErrorCode in each DB_dbtype implementation maps
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    29
 * native error codes to one of these.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    30
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    31
 * If you add an error code here, make sure you also add a textual
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    32
 * version of it in DB::errorMessage().
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    33
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    34
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
define("DB_OK",                         1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    36
define("DB_ERROR",                     -1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    37
define("DB_ERROR_SYNTAX",              -2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    38
define("DB_ERROR_CONSTRAINT",          -3);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    39
define("DB_ERROR_NOT_FOUND",           -4);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    40
define("DB_ERROR_ALREADY_EXISTS",      -5);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    41
define("DB_ERROR_UNSUPPORTED",         -6);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    42
define("DB_ERROR_MISMATCH",            -7);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    43
define("DB_ERROR_INVALID",             -8);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    44
define("DB_ERROR_NOT_CAPABLE",         -9);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    45
define("DB_ERROR_TRUNCATED",          -10);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    46
define("DB_ERROR_INVALID_NUMBER",     -11);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    47
define("DB_ERROR_INVALID_DATE",       -12);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    48
define("DB_ERROR_DIVZERO",            -13);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    49
define("DB_ERROR_NODBSELECTED",       -14);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    50
define("DB_ERROR_CANNOT_CREATE",      -15);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    51
define("DB_ERROR_CANNOT_DELETE",      -16);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    52
define("DB_ERROR_CANNOT_DROP",        -17);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    53
define("DB_ERROR_NOSUCHTABLE",        -18);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    54
define("DB_ERROR_NOSUCHFIELD",        -19);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    55
define("DB_ERROR_NEED_MORE_DATA",     -20);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    56
define("DB_ERROR_NOT_LOCKED",         -21);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    57
define("DB_ERROR_VALUE_COUNT_ON_ROW", -22);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    58
define("DB_ERROR_INVALID_DSN",        -23);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    59
define("DB_ERROR_CONNECT_FAILED",     -24);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    60
define("DB_ERROR_EXTENSION_NOT_FOUND",-25);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    61
define("DB_ERROR_ACCESS_VIOLATION",   -26);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    62
define("DB_ERROR_NOSUCHDB",           -27);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    63
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    64
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    65
 * Warnings are not detected as errors by DB::isError(), and are not
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    66
 * fatal.  You can detect whether an error is in fact a warning with
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    67
 * DB::isWarning().
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    68
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    69
 * @deprecated
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    70
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    71
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    72
define('DB_WARNING',           -1000);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    73
define('DB_WARNING_READ_ONLY', -1001);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    74
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    75
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    76
 * These constants are used when storing information about prepared
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    77
 * statements (using the "prepare" method in DB_dbtype).
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    78
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    79
 * The prepare/execute model in DB is mostly borrowed from the ODBC
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    80
 * extension, in a query the "?" character means a scalar parameter.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    81
 * There are two extensions though, a "&" character means an opaque
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    82
 * parameter.  An opaque parameter is simply a file name, the real
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    83
 * data are in that file (useful for putting uploaded files into your
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
 * database and such). The "!" char means a parameter that must be
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    85
 * left as it is.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    86
 * They modify the quote behavoir:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    87
 * DB_PARAM_SCALAR (?) => 'original string quoted'
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    88
 * DB_PARAM_OPAQUE (&) => 'string from file quoted'
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
 * DB_PARAM_MISC   (!) => original string
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    90
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    91
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    92
define('DB_PARAM_SCALAR', 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    93
define('DB_PARAM_OPAQUE', 2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    94
define('DB_PARAM_MISC',   3);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    95
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    96
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    97
 * These constants define different ways of returning binary data
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    98
 * from queries.  Again, this model has been borrowed from the ODBC
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    99
 * extension.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   100
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   101
 * DB_BINMODE_PASSTHRU sends the data directly through to the browser
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   102
 * when data is fetched from the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   103
 * DB_BINMODE_RETURN lets you return data as usual.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   104
 * DB_BINMODE_CONVERT returns data as well, only it is converted to
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   105
 * hex format, for example the string "123" would become "313233".
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   106
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   107
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   108
define('DB_BINMODE_PASSTHRU', 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   109
define('DB_BINMODE_RETURN',   2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   110
define('DB_BINMODE_CONVERT',  3);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   111
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   112
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   113
 * This is a special constant that tells DB the user hasn't specified
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   114
 * any particular get mode, so the default should be used.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   115
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   116
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   117
define('DB_FETCHMODE_DEFAULT', 0);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   118
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   119
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   120
 * Column data indexed by numbers, ordered from 0 and up
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   121
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   122
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   123
define('DB_FETCHMODE_ORDERED', 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   124
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   125
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   126
 * Column data indexed by column names
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   127
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   128
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   129
define('DB_FETCHMODE_ASSOC', 2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   130
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   131
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
 * Column data as object properties
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   134
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   135
define('DB_FETCHMODE_OBJECT', 3);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   136
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   137
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   138
 * For multi-dimensional results: normally the first level of arrays
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   139
 * is the row number, and the second level indexed by column number or name.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   140
 * DB_FETCHMODE_FLIPPED switches this order, so the first level of arrays
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   141
 * is the column name, and the second level the row number.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   142
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   143
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   144
define('DB_FETCHMODE_FLIPPED', 4);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   145
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   146
/* for compatibility */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   147
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   148
define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   149
define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   150
define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   151
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   152
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   153
 * these are constants for the tableInfo-function
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   154
 * they are bitwised or'ed. so if there are more constants to be defined
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   155
 * in the future, adjust DB_TABLEINFO_FULL accordingly
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   156
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   157
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   158
define('DB_TABLEINFO_ORDER', 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   159
define('DB_TABLEINFO_ORDERTABLE', 2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   160
define('DB_TABLEINFO_FULL', 3);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   161
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   162
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   163
 * Used by autoPrepare()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   164
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   165
define('DB_AUTOQUERY_INSERT', 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   166
define('DB_AUTOQUERY_UPDATE', 2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   167
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   168
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   169
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   170
 * The main "DB" class is simply a container class with some static
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   171
 * methods for creating DB objects as well as some utility functions
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   172
 * common to all parts of DB.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   173
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   174
 * The object model of DB is as follows (indentation means inheritance):
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   175
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   176
 * DB           The main DB class.  This is simply a utility class
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   177
 *              with some "static" methods for creating DB objects as
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   178
 *              well as common utility functions for other DB classes.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   179
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   180
 * DB_common    The base for each DB implementation.  Provides default
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   181
 * |            implementations (in OO lingo virtual methods) for
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   182
 * |            the actual DB implementations as well as a bunch of
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   183
 * |            query utility functions.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   184
 * |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   185
 * +-DB_mysql   The DB implementation for MySQL.  Inherits DB_common.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   186
 *              When calling DB::factory or DB::connect for MySQL
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   187
 *              connections, the object returned is an instance of this
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   188
 *              class.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   189
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   190
 * @package  DB
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   191
 * @author   Stig Bakken <ssb@fast.no>
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   192
 * @since    PHP 4.0
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   193
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   194
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   195
class DB
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   196
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   197
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   198
     * Create a new DB connection object for the specified database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   199
     * type
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   200
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   201
     * @param string $type database type, for example "mysql"
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   202
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   203
     * @return mixed a newly created DB object, or a DB error code on
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   204
     * error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   205
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   206
     * access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   207
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   208
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   209
    function &factory($type)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   210
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   211
        @include_once("DB/${type}.php");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   212
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   213
        $classname = "DB_${type}";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   214
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   215
        if (!class_exists($classname)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   216
            return PEAR::raiseError(null, DB_ERROR_NOT_FOUND,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   217
                                    null, null, null, 'DB_Error', true);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   218
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   219
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   220
        @$obj =& new $classname;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   221
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   222
        return $obj;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   223
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   224
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   225
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   226
     * Create a new DB connection object and connect to the specified
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   227
     * database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   228
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   229
     * @param mixed $dsn "data source name", see the DB::parseDSN
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   230
     * method for a description of the dsn format.  Can also be
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   231
     * specified as an array of the format returned by DB::parseDSN.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   232
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   233
     * @param mixed $options An associative array of option names and
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   234
     * their values.  For backwards compatibility, this parameter may
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   235
     * also be a boolean that tells whether the connection should be
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   236
     * persistent.  See DB_common::setOption for more information on
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   237
     * connection options.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   238
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   239
     * @return mixed a newly created DB connection object, or a DB
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   240
     * error object on error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   241
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   242
     * @see DB::parseDSN
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   243
     * @see DB::isError
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   244
     * @see DB_common::setOption
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   245
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   246
    function &connect($dsn, $options = false)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   247
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   248
        if (is_array($dsn)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   249
            $dsninfo = $dsn;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   250
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   251
            $dsninfo = DB::parseDSN($dsn);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   252
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   253
        $type = $dsninfo["phptype"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   254
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   255
        if (is_array($options) && isset($options["debug"]) &&
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   256
            $options["debug"] >= 2) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   257
            // expose php errors with sufficient debug level
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   258
            include_once "DB/${type}.php";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   259
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   260
            @include_once "DB/${type}.php";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   261
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   262
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   263
        $classname = "DB_${type}";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   264
        if (!class_exists($classname)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   265
            return PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   266
                                    "Unable to include the DB/{$type}.php file for `$dsn'",
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   267
                                    'DB_Error', true);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   268
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   269
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   270
        @$obj =& new $classname;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   271
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   272
        if (is_array($options)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   273
            foreach ($options as $option => $value) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   274
                $test = $obj->setOption($option, $value);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   275
                if (DB::isError($test)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   276
                    return $test;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   277
                }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   278
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   279
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   280
            $obj->setOption('persistent', $options);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   281
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   282
        $err = $obj->connect($dsninfo, $obj->getOption('persistent'));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   283
        if (DB::isError($err)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   284
            $err->addUserInfo($dsn);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   285
            return $err;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   286
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   287
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   288
        return $obj;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   289
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   290
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   291
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   292
     * Return the DB API version
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   293
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   294
     * @return int the DB API version number
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   295
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   296
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   297
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   298
    function apiVersion()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   299
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   300
        return 2;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   301
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   302
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   303
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   304
     * Tell whether a result code from a DB method is an error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   305
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   306
     * @param int $value result code
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   307
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   308
     * @return bool whether $value is an error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   309
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   310
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   311
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   312
    function isError($value)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   313
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   314
        return (is_object($value) &&
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   315
                (get_class($value) == 'db_error' ||
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   316
                 is_subclass_of($value, 'db_error')));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   317
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   318
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   319
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   320
     * Tell whether a value is a DB connection
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   321
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   322
     * @param mixed $value value to test
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   323
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   324
     * @return bool whether $value is a DB connection
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   325
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   326
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   327
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   328
    function isConnection($value)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   329
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   330
        return (is_object($value) &&
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   331
                is_subclass_of($value, 'db_common') &&
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   332
                method_exists($value, 'simpleQuery'));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   333
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   334
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   335
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   336
     * Tell whether a query is a data manipulation query (insert,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   337
     * update or delete) or a data definition query (create, drop,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   338
     * alter, grant, revoke).
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   339
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   340
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   341
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   342
     * @param string $query the query
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   343
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   344
     * @return boolean whether $query is a data manipulation query
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   345
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   346
    function isManip($query)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   347
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   348
        $manips = 'INSERT|UPDATE|DELETE|'.'REPLACE|CREATE|DROP|'.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   349
                  'ALTER|GRANT|REVOKE|'.'LOCK|UNLOCK';
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   350
        if (preg_match('/^\s*"?('.$manips.')\s+/i', $query)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   351
            return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   352
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   353
        return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   354
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   355
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   356
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   357
     * Return a textual error message for a DB error code
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   358
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   359
     * @param integer $value error code
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   360
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   361
     * @return string error message, or false if the error code was
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   362
     * not recognized
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   363
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   364
    function errorMessage($value)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   365
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   366
        static $errorMessages;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   367
        if (!isset($errorMessages)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   368
            $errorMessages = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   369
                DB_ERROR                    => 'unknown error',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   370
                DB_ERROR_ALREADY_EXISTS     => 'already exists',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   371
                DB_ERROR_CANNOT_CREATE      => 'can not create',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   372
                DB_ERROR_CANNOT_DELETE      => 'can not delete',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   373
                DB_ERROR_CANNOT_DROP        => 'can not drop',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   374
                DB_ERROR_CONSTRAINT         => 'constraint violation',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   375
                DB_ERROR_DIVZERO            => 'division by zero',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   376
                DB_ERROR_INVALID            => 'invalid',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   377
                DB_ERROR_INVALID_DATE       => 'invalid date or time',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   378
                DB_ERROR_INVALID_NUMBER     => 'invalid number',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   379
                DB_ERROR_MISMATCH           => 'mismatch',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   380
                DB_ERROR_NODBSELECTED       => 'no database selected',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   381
                DB_ERROR_NOSUCHFIELD        => 'no such field',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   382
                DB_ERROR_NOSUCHTABLE        => 'no such table',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   383
                DB_ERROR_NOT_CAPABLE        => 'DB backend not capable',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   384
                DB_ERROR_NOT_FOUND          => 'not found',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   385
                DB_ERROR_NOT_LOCKED         => 'not locked',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   386
                DB_ERROR_SYNTAX             => 'syntax error',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   387
                DB_ERROR_UNSUPPORTED        => 'not supported',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   388
                DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   389
                DB_ERROR_INVALID_DSN        => 'invalid DSN',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   390
                DB_ERROR_CONNECT_FAILED     => 'connect failed',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   391
                DB_OK                       => 'no error',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   392
                DB_WARNING                  => 'unknown warning',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   393
                DB_WARNING_READ_ONLY        => 'read only',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   394
                DB_ERROR_NEED_MORE_DATA     => 'insufficient data supplied',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   395
                DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   396
                DB_ERROR_NOSUCHDB           => 'no such database',
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   397
                DB_ERROR_ACCESS_VIOLATION   => 'insufficient permissions'
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   398
            );
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   399
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   400
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   401
        if (DB::isError($value)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   402
            $value = $value->getCode();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   403
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   404
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   405
        return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[DB_ERROR];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   406
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   407
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   408
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   409
     * Parse a data source name
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   410
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   411
     * A array with the following keys will be returned:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   412
     *  phptype: Database backend used in PHP (mysql, odbc etc.)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   413
     *  dbsyntax: Database used with regards to SQL syntax etc.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   414
     *  protocol: Communication protocol to use (tcp, unix etc.)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   415
     *  hostspec: Host specification (hostname[:port])
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   416
     *  database: Database to use on the DBMS server
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   417
     *  username: User name for login
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   418
     *  password: Password for login
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   419
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   420
     * The format of the supplied DSN is in its fullest form:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   421
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   422
     *  phptype(dbsyntax)://username:password@protocol+hostspec/database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   423
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   424
     * Most variations are allowed:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   425
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   426
     *  phptype://username:password@protocol+hostspec:110//usr/db_file.db
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   427
     *  phptype://username:password@hostspec/database_name
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   428
     *  phptype://username:password@hostspec
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   429
     *  phptype://username@hostspec
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   430
     *  phptype://hostspec/database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   431
     *  phptype://hostspec
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   432
     *  phptype(dbsyntax)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   433
     *  phptype
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   434
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   435
     * @param string $dsn Data Source Name to be parsed
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   436
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   437
     * @return array an associative array
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   438
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   439
     * @author Tomas V.V.Cox <cox@idecnet.com>
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   440
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   441
    function parseDSN($dsn)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   442
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   443
        if (is_array($dsn)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   444
            return $dsn;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   445
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   446
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   447
        $parsed = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   448
            'phptype'  => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   449
            'dbsyntax' => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   450
            'username' => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   451
            'password' => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   452
            'protocol' => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   453
            'hostspec' => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   454
            'port'     => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   455
            'socket'   => false,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   456
            'database' => false
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   457
        );
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   458
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   459
        // Find phptype and dbsyntax
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   460
        if (($pos = strpos($dsn, '://')) !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   461
            $str = substr($dsn, 0, $pos);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   462
            $dsn = substr($dsn, $pos + 3);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   463
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   464
            $str = $dsn;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   465
            $dsn = NULL;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   466
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   467
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   468
        // Get phptype and dbsyntax
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   469
        // $str => phptype(dbsyntax)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   470
        if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   471
            $parsed['phptype']  = $arr[1];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   472
            $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   473
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   474
            $parsed['phptype']  = $str;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   475
            $parsed['dbsyntax'] = $str;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   476
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   477
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   478
        if (empty($dsn)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   479
            return $parsed;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   480
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   481
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   482
        // Get (if found): username and password
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   483
        // $dsn => username:password@protocol+hostspec/database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   484
        if (($at = strrpos($dsn,'@')) !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   485
            $str = substr($dsn, 0, $at);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   486
            $dsn = substr($dsn, $at + 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   487
            if (($pos = strpos($str, ':')) !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   488
                $parsed['username'] = urldecode(substr($str, 0, $pos));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   489
                $parsed['password'] = urldecode(substr($str, $pos + 1));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   490
            } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   491
                $parsed['username'] = urldecode($str);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   492
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   493
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   494
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   495
        // Find protocol and hostspec
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   496
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   497
        // $dsn => proto(proto_opts)/database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   498
        if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   499
            $proto       = $match[1];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   500
            $proto_opts  = (!empty($match[2])) ? $match[2] : false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   501
            $dsn         = $match[3];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   502
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   503
        // $dsn => protocol+hostspec/database (old format)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   504
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   505
            if (strpos($dsn, '+') !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   506
                list($proto, $dsn) = explode('+', $dsn, 2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   507
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   508
            if (strpos($dsn, '/') !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   509
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   510
            } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   511
                $proto_opts = $dsn;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   512
                $dsn = null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   513
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   514
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   515
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   516
        // process the different protocol options
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   517
        $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   518
        $proto_opts = urldecode($proto_opts);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   519
        if ($parsed['protocol'] == 'tcp') {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   520
            if (strpos($proto_opts, ':') !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   521
                list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   522
            } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   523
                $parsed['hostspec'] = $proto_opts;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   524
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   525
        } elseif ($parsed['protocol'] == 'unix') {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   526
            $parsed['socket'] = $proto_opts;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   527
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   528
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   529
        // Get dabase if any
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   530
        // $dsn => database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   531
        if (!empty($dsn)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   532
            // /database
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   533
            if (($pos = strpos($dsn, '?')) === false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   534
                $parsed['database'] = $dsn;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   535
            // /database?param1=value1&param2=value2
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   536
            } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   537
                $parsed['database'] = substr($dsn, 0, $pos);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   538
                $dsn = substr($dsn, $pos + 1);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   539
                if (strpos($dsn, '&') !== false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   540
                    $opts = explode('&', $dsn);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   541
                } else { // database?param1=value1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   542
                    $opts = array($dsn);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   543
                }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   544
                foreach ($opts as $opt) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   545
                    list($key, $value) = explode('=', $opt);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   546
                    if (!isset($parsed[$key])) { // don't allow params overwrite
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   547
                        $parsed[$key] = urldecode($value);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   548
                    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   549
                }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   550
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   551
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   552
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   553
        return $parsed;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   554
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   555
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   556
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   557
     * Load a PHP database extension if it is not loaded already.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   558
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   559
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   560
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   561
     * @param string $name the base name of the extension (without the .so or
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   562
     *                     .dll suffix)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   563
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   564
     * @return boolean true if the extension was already or successfully
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   565
     *                 loaded, false if it could not be loaded
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   566
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   567
    function assertExtension($name)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   568
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   569
        if (!extension_loaded($name)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   570
            $dlext = OS_WINDOWS ? '.dll' : '.so';
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   571
            @dl($name . $dlext);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   572
            return extension_loaded($name);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   573
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   574
        return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   575
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   576
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   577
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   578
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   579
 * DB_Error implements a class for reporting portable database error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   580
 * messages.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   581
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   582
 * @package  DB
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   583
 * @author Stig Bakken <ssb@fast.no>
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   584
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   585
class DB_Error extends PEAR_Error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   586
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   587
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   588
     * DB_Error constructor.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   589
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   590
     * @param mixed   $code   DB error code, or string with error message.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   591
     * @param integer $mode   what "error mode" to operate in
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   592
     * @param integer $level  what error level to use for $mode & PEAR_ERROR_TRIGGER
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   593
     * @param mixed   $debuginfo  additional debug info, such as the last query
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   594
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   595
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   596
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   597
     * @see PEAR_Error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   598
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   599
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   600
    function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   601
              $level = E_USER_NOTICE, $debuginfo = null)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   602
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   603
        if (is_int($code)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   604
            $this->PEAR_Error('DB Error: ' . DB::errorMessage($code), $code, $mode, $level, $debuginfo);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   605
        } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   606
            $this->PEAR_Error("DB Error: $code", DB_ERROR, $mode, $level, $debuginfo);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   607
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   608
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   609
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   610
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   611
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   612
 * This class implements a wrapper for a DB result set.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   613
 * A new instance of this class will be returned by the DB implementation
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   614
 * after processing a query that returns data.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   615
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   616
 * @package  DB
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   617
 * @author Stig Bakken <ssb@fast.no>
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   618
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   619
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   620
class DB_result
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   621
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   622
    var $dbh;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   623
    var $result;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   624
    var $row_counter = null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   625
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   626
    * for limit queries, the row to start fetching
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   627
    * @var integer
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   628
    */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   629
    var $limit_from  = null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   630
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   631
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   632
    * for limit queries, the number of rows to fetch
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   633
    * @var integer
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   634
    */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   635
    var $limit_count = null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   636
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   637
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   638
     * DB_result constructor.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   639
     * @param resource &$dbh   DB object reference
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   640
     * @param resource $result result resource id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   641
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   642
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   643
    function DB_result(&$dbh, $result, $options = array())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   644
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   645
        $this->dbh = &$dbh;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   646
        $this->result = $result;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   647
        $this->limit_from  = isset($options['limit_from'])  ? $options['limit_from']  : null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   648
        $this->limit_count = isset($options['limit_count']) ? $options['limit_count'] : null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   649
        $this->limit_type  = $dbh->features['limit'];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   650
        $this->autofree    = $dbh->options['autofree'];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   651
        $this->fetchmode   = $dbh->fetchmode;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   652
        $this->fetchmode_object_class = $dbh->fetchmode_object_class;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   653
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   654
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   655
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   656
     * Fetch and return a row of data (it uses driver->fetchInto for that)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   657
     * @param int $fetchmode format of fetched row
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   658
     * @param int $rownum    the row number to fetch
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   659
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   660
     * @return  array a row of data, NULL on no more rows or PEAR_Error on error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   661
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   662
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   663
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   664
    function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   665
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   666
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   667
            $fetchmode = $this->fetchmode;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   668
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   669
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   670
            $fetchmode = DB_FETCHMODE_ASSOC;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   671
            $object_class = $this->fetchmode_object_class;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   672
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   673
        if ($this->limit_from !== null) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   674
            if ($this->row_counter === null) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   675
                $this->row_counter = $this->limit_from;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   676
                // Skip rows
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   677
                if ($this->limit_type == false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   678
                    $i = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   679
                    while ($i++ < $this->limit_from) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   680
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   681
                    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   682
                }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   683
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   684
            if ($this->row_counter >= (
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   685
                    $this->limit_from + $this->limit_count))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   686
            {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   687
                return null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   688
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   689
            if ($this->limit_type == 'emulate') {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   690
                $rownum = $this->row_counter;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   691
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   692
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   693
            $this->row_counter++;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   694
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   695
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   696
        if ($res !== DB_OK) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   697
            if ($res == null && $this->autofree) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   698
                $this->free();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   699
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   700
            return $res;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   701
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   702
        if (isset($object_class)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   703
            // default mode specified in DB_common::fetchmode_object_class property
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   704
            if ($object_class == 'stdClass') {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   705
                $ret = (object) $arr;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   706
            } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   707
                $ret =& new $object_class($arr);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   708
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   709
            return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   710
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   711
        return $arr;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   712
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   713
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   714
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   715
     * Fetch a row of data into an existing variable.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   716
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   717
     * @param  mixed   &$arr     reference to data containing the row
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   718
     * @param  integer $fetchmod format of fetched row
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   719
     * @param  integer $rownum   the row number to fetch
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   720
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   721
     * @return  mixed  DB_OK on success, NULL on no more rows or
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   722
     *                 a DB_Error object on error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   723
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   724
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   725
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   726
    function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   727
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   728
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   729
            $fetchmode = $this->fetchmode;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   730
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   731
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   732
            $fetchmode = DB_FETCHMODE_ASSOC;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   733
            $object_class = $this->fetchmode_object_class;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   734
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   735
        if ($this->limit_from !== null) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   736
            if ($this->row_counter === null) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   737
                $this->row_counter = $this->limit_from;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   738
                // Skip rows
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   739
                if ($this->limit_type == false) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   740
                    $i = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   741
                    while ($i++ < $this->limit_from) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   742
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   743
                    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   744
                }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   745
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   746
            if ($this->row_counter >= (
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   747
                    $this->limit_from + $this->limit_count))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   748
            {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   749
                return null;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   750
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   751
            if ($this->limit_type == 'emulate') {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   752
                $rownum = $this->row_counter;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   753
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   754
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   755
            $this->row_counter++;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   756
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   757
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   758
        if (($res === DB_OK) && isset($object_class)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   759
            // default mode specified in DB_common::fetchmode_object_class property
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   760
            if ($object_class == 'stdClass') {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   761
                $arr = (object) $arr;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   762
            } else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   763
                $arr = new $object_class($arr);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   764
            }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   765
        } elseif ($res == null && $this->autofree) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   766
            $this->free();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   767
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   768
        return $res;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   769
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   770
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   771
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   772
     * Get the the number of columns in a result set.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   773
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   774
     * @return int the number of columns, or a DB error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   775
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   776
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   777
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   778
    function numCols()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   779
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   780
        return $this->dbh->numCols($this->result);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   781
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   782
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   783
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   784
     * Get the number of rows in a result set.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   785
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   786
     * @return int the number of rows, or a DB error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   787
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   788
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   789
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   790
    function numRows()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   791
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   792
        return $this->dbh->numRows($this->result);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   793
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   794
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   795
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   796
     * Get the next result if a batch of queries was executed.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   797
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   798
     * @return bool true if a new result is available or false if not.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   799
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   800
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   801
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   802
    function nextResult()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   803
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   804
        return $this->dbh->nextResult($this->result);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   805
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   806
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   807
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   808
     * Frees the resources allocated for this result set.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   809
     * @return  int error code
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   810
     *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   811
     * @access public
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   812
     */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   813
    function free()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   814
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   815
        $err = $this->dbh->freeResult($this->result);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   816
        if(DB::isError($err)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   817
            return $err;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   818
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   819
        $this->result = false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   820
        return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   821
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   822
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   823
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   824
    * @deprecated
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   825
    */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   826
    function tableInfo($mode = null)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   827
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   828
        return $this->dbh->tableInfo($this->result, $mode);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   829
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   830
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   831
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   832
    * returns the actual row number
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   833
    * @return integer
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   834
    */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   835
    function getRowCounter()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   836
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   837
        return $this->row_counter;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   838
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   839
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   840
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   841
/**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   842
* Pear DB Row Object
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   843
* @see DB_common::setFetchMode()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   844
*/
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   845
class DB_row
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   846
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   847
    /**
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   848
    * constructor
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   849
    *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   850
    * @param resource row data as array
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   851
    */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   852
    function DB_row(&$arr)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   853
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   854
        for (reset($arr); $key = key($arr); next($arr)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   855
            $this->$key = &$arr[$key];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   856
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   857
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   858
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   859
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   860
?>