API15

JDatabase/replacePrefix

From Joomla! Documentation

< API15:JDatabase
Revision as of 00:27, 25 March 2017 by JoomlaWikiBot (talk | contribs) (preparing for archive only)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

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[edit]

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

Defined in[edit]

libraries/joomla/database/database.php

Importing[edit]

jimport( 'joomla.database.database' );

Source Body[edit]

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[edit]

Code Examples[edit]