Actions

API15

JDatabase/replacePrefix

From Joomla! Documentation

< API15:JDatabase
Revision as of 08:57, 12 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

This function replaces a string identifier $prefix with the string held is the _table_prefix class variable.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

replacePrefix($sql, $prefix='#__')
Parameter Name Default Value Description
$sql The SQL query
$prefix '#__' The common table prefix

Defined in

libraries/joomla/database/database.php

Importing

jimport( 'joomla.database.database' );

Source Body

function replacePrefix( $sql, $prefix='#__' )
{
        $sql = trim( $sql );
 
        $escaped = false;
        $quoteChar = '';
 
        $n = strlen( $sql );
 
        $startPos = 0;
        $literal = '';
        while ($startPos < $n) {
                $ip = strpos($sql, $prefix, $startPos);
                if ($ip === false) {
                        break;
                }
 
                $j = strpos( $sql, "'", $startPos );
                $k = strpos( $sql, '"', $startPos );
                if (($k !== FALSE) && (($k < $j) || ($j === FALSE))) {
                        $quoteChar      = '"';
                        $j                      = $k;
                } else {
                        $quoteChar      = "'";
                }
 
                if ($j === false) {
                        $j = $n;
                }
 
                $literal .= str_replace( $prefix, $this->_table_prefix,substr( $sql, $startPos, $j - $startPos ) );
                $startPos = $j;
 
                $j = $startPos + 1;
 
                if ($j >= $n) {
                        break;
                }
 
                // quote comes first, find end of quote
                while (TRUE) {
                        $k = strpos( $sql, $quoteChar, $j );
                        $escaped = false;
                        if ($k === false) {
                                break;
                        }
                        $l = $k - 1;
                        while ($l >= 0 && $sql{$l} == '\\') {
                                $l--;
                                $escaped = !$escaped;
                        }
                        if ($escaped) {
                                $j      = $k+1;
                                continue;
                        }
                        break;
                }
                if ($k === FALSE) {
                        // error in the query - no end quote; ignore it
                        break;
                }
                $literal .= substr( $sql, $startPos, $k - $startPos + 1 );
                $startPos = $k+1;
        }
        if ($startPos < $n) {
                $literal .= substr( $sql, $startPos, $n - $startPos );
        }
        return $literal;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />