API16

JDatabase/replacePrefix

< API16:JDatabase

The "API16" 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

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


<! 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

public 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 transcluded page call, red link never existed >

Examples

Code Examples


Advertisement