Public Member Functions | Data Fields

ilDBx Class Reference

Inheritance diagram for ilDBx:
Collaboration diagram for ilDBx:

Public Member Functions

 ilDBx ($dsn)
 constructor
 _ilDBx ()
 destructor
 disconnect ()
 disconnect from database
 query ($sql)
 query
 getOne ($sql)
 getOne
 quote ($a_query, $null_as_empty_string=true)
 wrapper for quote method
 getRow ($sql, $mode=DB_FETCHMODE_OBJECT)
 getrow
 getLastInsertId ()
 get last insert id
 prepare ($query)
 Wrapper for Pear prepare.
 executeMultiple ($stmt, $data)
 Wrapper for Pear executeMultiple.
 execute ($stmt, $data)
 Wrapper for Pear executeMultiple.
 autoExecute ($a_tablename, $a_fields, $a_mode=DB_AUTOQUERY_INSERT, $a_where=false)
 Wrapper for Pear autoExecute.
 checkQuerySize ($a_query)
 setMaxAllowedPacket ()
 _lockTables ($a_table_params)
 Lock existing table.
 _unlockTables ()
 getMySQLVersion ()
 get mysql version
 isMysql4_1 ()
 check wether current MySQL server is version 4.1.x
 isMysql4_1OrHigher ()
 check wether current MySQL server is version 4.1.x or higher
 isMysql4_0OrHigher ()
 check wether current MySQL server is version 4.0.x or higher
 tableColumnExists ($a_table, $a_column_name)
 Checks for the existence of a table column.

Data Fields

 $error_class
 $db
 $result
 $max_allowed_packet_size

Detailed Description

Definition at line 42 of file class.ilDBx.php.


Member Function Documentation

ilDBx::_ilDBx (  ) 

destructor

Definition at line 120 of file class.ilDBx.php.

                         {
                //$this->db->disconnect();
        } //end destructor

ilDBx::_lockTables ( a_table_params  ) 

Lock existing table.

Parameters:
array (tablename => lock type READ, WRITE, READ LOCAL or LOW_PRIORITY) e.g array('tree' => 'WRITE')
Returns:
boolean

Definition at line 369 of file class.ilDBx.php.

References $counter, and $type.

Referenced by ilTree::deleteTree(), ilTree::insertNode(), ilTree::renumber(), and ilTree::saveSubTree().

        {
                global $ilDB;
                
                $lock_str = 'LOCK TABLES ';
                $counter = 0;
                foreach($a_table_params as $table_name => $type)
                {
                        $lock_str .= $counter++ ? ',' : '';
                        $lock_str .= $table_name.' '.$type;
                }

                $ilDB->query($lock_str);

                return true;
        }

Here is the caller graph for this function:

ilDBx::_unlockTables (  ) 

Definition at line 385 of file class.ilDBx.php.

Referenced by ilTree::deleteTree(), ilTree::insertNode(), and ilTree::renumber().

        {
                global $ilDB;
                
                $ilDB->query('UNLOCK TABLES');

                return true;
        }

Here is the caller graph for this function:

ilDBx::autoExecute ( a_tablename,
a_fields,
a_mode = DB_AUTOQUERY_INSERT,
a_where = false 
)

Wrapper for Pear autoExecute.

Parameters:
string tablename
array fields values
int DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
string where condition (e.g. "obj_id = '7' AND ref_id = '5'")
Returns:
mixed a new DB_result/DB_OK or a DB_Error, if fail

Definition at line 292 of file class.ilDBx.php.

References $data, and $res.

        {
                $res = $this->db->autoExecute($a_tablename,$a_fields,$a_mode,$a_where);

                if (DB::isError($res))
                {
                        $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
                }
                else
                {
                        return $res;
                }
        }

ilDBx::checkQuerySize ( a_query  ) 

Definition at line 305 of file class.ilDBx.php.

References $lang.

        {
                global $lang;

                if(strlen($a_query) >= $this->max_allowed_packet_size)
                {
                        return false;
                }
                else
                {
                        return true;
                }
        }

ilDBx::disconnect (  ) 

disconnect from database

Definition at line 127 of file class.ilDBx.php.

        {
//              $this->db->disconnect();
        }

ilDBx::execute ( stmt,
data 
)

Wrapper for Pear executeMultiple.

Parameters:
resource (statement from prepare)
array multidim array of data
Returns:
mixed a new DB_result/DB_OK or a DB_Error, if fail

Definition at line 270 of file class.ilDBx.php.

References $data, and $res.

        {
                $res = $this->db->execute($stmt,$data);

                if (DB::isError($res))
                {
                        $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
                }
                else
                {
                        return $res;
                }
        }

ilDBx::executeMultiple ( stmt,
data 
)

Wrapper for Pear executeMultiple.

Parameters:
resource (statement from prepare)
array multidim array of data
Returns:
mixed a new DB_result/DB_OK or a DB_Error, if fail

Definition at line 250 of file class.ilDBx.php.

References $data, and $res.

        {
                $res = $this->db->executeMultiple($stmt,$data);

                if (DB::isError($res))
                {
                        $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
                }
                else
                {
                        return $res;
                }
        }

ilDBx::getLastInsertId (  ) 

get last insert id

Definition at line 226 of file class.ilDBx.php.

References $row, and query().

        {
                $r = $this->query("SELECT LAST_INSERT_ID()");
                $row = $r->fetchRow();

                return $row[0] ? $row[0] : false;
        }

Here is the call graph for this function:

ilDBx::getMySQLVersion (  ) 

get mysql version

Definition at line 397 of file class.ilDBx.php.

        {
                return mysql_get_server_info();
        }

ilDBx::getOne ( sql  ) 

getOne

this is the wrapper itself. Runs a query and returns the first column of the first row or in case of an error, jump to errorpage

Parameters:
string 
Returns:
object DB

Definition at line 166 of file class.ilDBx.php.

        {
                $r = $this->db->getOne($sql);

                if (DB::isError($r))
                {
                        $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
                }
                else
                {
                        return $r;
                }
        } //end function

ilDBx::getRow ( sql,
mode = DB_FETCHMODE_OBJECT 
)

getrow

this is the wrapper itself. query a string, and return the resultobject, or in case of an error, jump to errorpage

Parameters:
string 
Returns:
object DB

Definition at line 208 of file class.ilDBx.php.

        {
                $r = $this->db->getrow($sql,$mode);

                if (DB::isError($r))
                {
                        $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
                }
                else
                {
                        return $r;
                }
        } //end function

ilDBx::ilDBx ( dsn  ) 

constructor

set up database conncetion and the errorhandling

Parameters:
string dsn database-connection-string for pear-db

Definition at line 77 of file class.ilDBx.php.

References isMysql4_1OrHigher(), query(), and setMaxAllowedPacket().

        {
                //call parent constructor
                $parent = get_parent_class($this);
                $this->$parent();

                //set up error handling
                $this->error_class = new ilErrorHandling();
                $this->setErrorHandling(PEAR_ERROR_CALLBACK, array($this->error_class,'errorHandler'));

                //check dsn
                if ($dsn=="")
                        $this->raiseError("no DSN given", $this->error_class->FATAL);

                $this->dsn = $dsn;

                //connect to database
                $this->db = DB::connect($this->dsn, true);

                //check error
                if (DB::isError($this->db)) {
                        $this->raiseError($this->db->getMessage(), $this->error_class->FATAL);
                }

                // SET 'max_allowed_packet' (only possible for mysql version 4)
                $this->setMaxAllowedPacket();
                
                // NOTE: Three sourcecodes use this or a similar handling:
                // - classes/class.ilDBx.php
                // - calendar/classes/class.ilCalInterface.php->setNames
                // - setup/classes/class.ilClient.php
                if ($this->isMysql4_1OrHigher())
                {
                        $this->query("SET NAMES utf8");
                        $this->query("SET SESSION SQL_MODE = ''");
                }

                return true;
        } //end constructor

Here is the call graph for this function:

ilDBx::isMysql4_0OrHigher (  ) 

check wether current MySQL server is version 4.0.x or higher

Definition at line 439 of file class.ilDBx.php.

        {
                $version = explode(".", $this->getMysqlVersion());
                if((int) $version[0] >= 4)
                {
                        return true;
                }
                return false;
        }               

ilDBx::isMysql4_1 (  ) 

check wether current MySQL server is version 4.1.x

Definition at line 405 of file class.ilDBx.php.

        {
                $version = explode(".", $this->getMysqlVersion());
                if ($version[0] == "4" && $version[1] == "1")
                {
                        return true;
                }
                
                return false;
        }

ilDBx::isMysql4_1OrHigher (  ) 

check wether current MySQL server is version 4.1.x or higher

NOTE: Three sourcecodes use this or a similar handling:

Definition at line 424 of file class.ilDBx.php.

Referenced by ilDBx().

        {
                $version = explode(".", $this->getMysqlVersion());
                if ((int)$version[0] >= 5 ||
                        ((int)$version[0] == 4 && (int)$version[1] >= 1))
                {
                        return true;
                }
                
                return false;
        }

Here is the caller graph for this function:

ilDBx::prepare ( query  ) 

Wrapper for Pear prepare.

Parameters:
String query
Returns:
resource

Definition at line 239 of file class.ilDBx.php.

References $query.

        {
                return $this->db->prepare($query);
        }

ilDBx::query ( sql  ) 

query

this is the wrapper itself. query a string, and return the resultobject, or in case of an error, jump to errorpage

Parameters:
string 
Returns:
object DB

Definition at line 141 of file class.ilDBx.php.

Referenced by getLastInsertId(), and ilDBx().

        {
                $r = $this->db->query($sql);

                if (DB::isError($r))
                {
                        $err = "<br>Details: ".mysql_error();
                        $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql.$err."</font>", $this->error_class->FATAL);
                }
                else
                {
                        return $r;
                }
        } //end function

Here is the caller graph for this function:

ilDBx::quote ( a_query,
null_as_empty_string = true 
)

wrapper for quote method

Definition at line 184 of file class.ilDBx.php.

        {
                if ($null_as_empty_string)
                {
                        if ($a_query == "")
                        {
                                $a_query = "";
                        }
                }

                // maybe quoteSmart should be used in the future
                return $this->db->quote($a_query);
        }

ilDBx::setMaxAllowedPacket (  ) 

Definition at line 323 of file class.ilDBx.php.

References $query, $res, and $row.

Referenced by ilDBx().

        {

                // GET MYSQL VERSION
                $query = "SHOW VARIABLES LIKE 'version'";
                $res = $this->db->query($query);
                if(DB::isError($res))
                {
                        $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
                }
                while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
                {
                        $version = $row->Value;
                }

                // CHANG VALUE IF MYSQL VERSION > 4.0
                if(substr($version,0,1) == "4")
                {
                        ini_get("post_max_size");
                        $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
                        $this->db->query($query);
                        if(DB::isError($res))
                        {
                                $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
                        }
                }
                // STORE NEW max_size in member variable
                $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
                if(DB::isError($res))
                {
                        $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
                }
                $res = $this->db->query($query);
                while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
                {
                        $this->max_allowed_packet_size = $row->Value;
                }
                #var_dump("<pre>",$this->max_allowed_packet_size,"<pre>");
                return true;
        }

Here is the caller graph for this function:

ilDBx::tableColumnExists ( a_table,
a_column_name 
)

Checks for the existence of a table column.

Parameters:
string $a_table The table name which should be examined
string $a_column_name The name of the column
Returns:
boolean TRUE if the table column exists, FALSE otherwise

Definition at line 456 of file class.ilDBx.php.

References $data, $query, and $res.

        {
                $column_visibility = FALSE;
                $query = "SHOW COLUMNS FROM `$a_table`";
                $res = $this->db->query($query);
                if ($res->numRows())
                {
                        while ($data = $res->fetchRow(DB_FETCHMODE_ASSOC))
                        {
                                if (strcmp($data["Field"], $a_column_name) == 0)
                                {
                                        $column_visibility = TRUE;
                                }
                        }
                }
                return $column_visibility;
        }


Field Documentation

ilDBx::$db

Definition at line 55 of file class.ilDBx.php.

ilDBx::$error_class

Definition at line 49 of file class.ilDBx.php.

ilDBx::$max_allowed_packet_size

Definition at line 67 of file class.ilDBx.php.

ilDBx::$result

Definition at line 61 of file class.ilDBx.php.


The documentation for this class was generated from the following file: