Joomla! Programmierung/Framework/JDatabase
JDatabase ist eine abstrakte Klasse, die von spezifischen Datenbanktreibern erweitert wird. JDatabase stellt Datenbankverbindungen für Joomla! zur Verfügung.
Inhaltsverzeichnis |
[Bearbeiten] Definiert in
[Bearbeiten] Status
[Bearbeiten] Erweitert
[Bearbeiten] Erweitert von
[Bearbeiten] Importieren
jimport( 'joomla.database.database' );
[Bearbeiten] Methoden
ToDo übersetzen Properties which do not have specific get or set methods listed here can be retrieved or set using the inherited JObject->get method.
| Name | Beschreibung |
|---|---|
| addQuoted | Adds a field or array of field names to the list that are to be quoted. |
| BeginTrans | ADODB compatibility function. |
| CommitTrans | ADODB compatibility function. |
| connected | Determines if the connection to the server is active. |
| debug | Sets the debug level on or off. |
| ErrorMsg | ADODB compatibility function. |
| ErrorNo | ADODB compatibility function. |
| Execute | ADODB compatibility function. |
| explain | Diagnostic function. |
| GenID | ADODB compatibility function. |
| getAffectedRows | Get the number of rows affected by the most recent query. |
| GetCol | ADODB compatibility function. |
| getCollation | Get the database collation. |
| getConnectors | Get the database connectors. |
| getErrorMsg | Get the error message. |
| getErrorNum | Get the error number. |
| getEscaped | Get a database escaped string. |
| getInstance | Returns a reference to the global database object, only creating it if it doesn't already exist. |
| getLog | Get a database error log. |
| getNullDate | Get the database null date. |
| getNumRows | Get the number of rows returned by the most recent query. |
| GetOne | ADODB compatibility function. |
| getPrefix | Get the database table prefix. |
| getQuery | Get the active query. |
| GetRow | ADODB compatibility function. |
| getTableCreate | Shows the CREATE TABLE statement that creates the given tables. |
| getTableFields | Retrieves information about the given tables. |
| getTableList | Lists tables in a database. |
| getTicker | Get the total number of queries made. |
| getUTFSupport | Get the database UTF-8 support. |
| getVersion | Get the version of the database connector. |
| hasUTF | Determines UTF support. |
| insertid | Gets the ID generated from the previous INSERT operation. |
| insertObject | Inserts a row into a table based on an objects properties. |
| isQuoted | Checks if field name needs to be quoted. |
| loadAssoc | Fetch a result row as an associative array. |
| loadAssocList | Loads an associative list of database rows. |
| loadObject | Loads the first row of a query into an object. |
| loadObjectList | Loads a list of database objects. |
| Joomla!_Programmierung/Framework/JDatabase/loadResult | Loads the first field of the first row returned by the query. |
| loadResultArray | Loads an array of single field results into an array. |
| loadRow | Loads the first row returned by the query. |
| loadRowList | Loads a list of database rows (numeric column indexing). |
| nameQuote | Quote an identifier name (field, table, etc.). |
| PageExecute | ADODB compatibility function. |
| query | Execute the query. |
| queryBatch | Execute a batch query. |
| Quote | Get a quoted database escaped string. |
| replacePrefix | Replaces a string identifier $prefix with the string held is the _table_prefix class variable. |
| RollbackTrans | ADODB compatibility function. |
| SelectLimit | ADODB compatibility function. |
| setQuery | Sets the SQL query string for later execution. |
| setUTF | Custom settings for UTF support. |
| splitSql | Splits a string of queries into an array of individual queries. |
| stderr | Prints out an error statement. |
| test | Tests to see if the connector is available. |
| updateObject | Updates an object in the database. |
[Bearbeiten] Beispiele
[Bearbeiten] Einen Dump der kompletten Datenbank erstellen
In diesem Beispiel kommen Methoden der JDatabase Klasse zum Einsatz, die hilfreich beim Erstellen von Datenbankdumps sind.
Zum Beispiel erzeugt die Methode getTableCreate() einen kompletten CREATE TABLE SQL String.
/** * Erstellt einen komletten Dump der Datenbank * @static */ class DbBackUp { /** * Erstellt einen komletten Dump der Datenbank * * @return string Dump der Datenbank */ public static function backupDB() { //-- New line character defined('NL') or define('NL', "\n"); //-- Referenz auf das JDatabase Objekt erzeugen $db = JFactory::getDBO(); //-- Eine Liste aller Tabellen der Datenbank erstellen $tabellen = $db->getTableList(); $s = ''; foreach($tabellen as $tablenName) { $s .= self::getTableDump($tablenName); }//foreach return $s; }//function /** * Erstellt einen Dump einer einzelnen Tabelle * * @param $tableName string Name der Tabelle * @return string Dump der Tabelle */ private static function getTableDump($tableName) { //-- Referenz auf das JDatabase Objekt erzeugen $db = JFactory::getDBO(); //-- Eine "CREATE TABLE" SQL Anweisung erzeugen $createString = $db->getTableCreate($tableName); //-- Einlesen der Feldnamen $tFields = $db->getTableColumns($tableName, false); //-- Einlesen der Daten $db->setQuery('SELECT * FROM '.$tableName); $rows = $db->loadAssocList(); $s = ''; $s .= '-- -----------------------------------------------'.NL; $s .= '-- Table structure for table '.$tableName.NL; $s .= '--'.NL; $s .= $createString[$tableName].';'.NL; //-- Wenn die Tabelle Daten enthält diese hizufügen if(count($rows)) { //-- "INSERT" Anweisungen generieren $s .= '--'.NL; $s .= '-- Data for table '.$tableName.NL; $s .= '--'.NL; $s .= 'INSERT INTO `'.$tableName.'`'.NL; $s .= ' (`'.implode('`, `', array_keys($rows[0])).'`)'.NL; $s .= 'VALUES'.NL; $r = array(); foreach ($rows as $row) { $l = array(); foreach ($row as $k => $v) { //-- Falls es kein integer ist, muss der Wert gequotet werden $l[] =(strpos($tFields[$k]->Type, 'int') !== false) ? $v : $db->quote($v); }//foreach $r[] = ' ('.implode(', ', $l).')'; }//foreach $s .= implode(','.NL, $r).';'.NL; } $s .= '-- -----------------------------------------------'.NL.NL; return $s; }//function }//class
Hier werden die Struktur und die Daten aller Tabellen der Datenbank eingelesen und SQL Anweisungen generiert, die zum Beispiel in Backup-Programmen verwendet werden können.
Der Aufruf erfolgt statisch mit
$dbDump = DbBackUp::backupDB();
Die Variable $dbDump enthält danach einen Standard Datenbank Dump in Form eines Strings.
zum Beispiel:
... ... ------------------------------------------------- -- Table structure for table jos_hello -- CREATE TABLE `jos_hello` ( `id` int(11) NOT NULL auto_increment, `greeting` varchar(25) NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=5; -- -- Data for table jos_hello -- INSERT INTO `jos_hello` (`id`, `greeting`) VALUES (1, 'Hello, World!'), (2, 'Bonjour, Monde!'), (3, 'Ciao, Mondo!'), (4, 'Hallo, Welt ;)'); ------------------------------------------------- ------------------------------------------------- -- Table structure for table jos_.... ...
Verwendete Methoden: getTableCreate(), getTableFields(), setQuery(), loadAssocList(), quote(), getTableList()