Skip to content

Commit

Permalink
SQL - improve CAST with CAST('field' AS CHAR(n)) (#12417)
Browse files Browse the repository at this point in the history
* SQL - improve CAST('field' AS CHAR(n))

SQL - improve CAST('field' AS CHAR(n))

* SQL - improve CAST('field' AS CHAR(n))

SQL - improve CAST('field' AS CHAR(n))

* moved castAsChar to the driver

moved castAsChar to the driver

* CAST with CAST('field' AS CHAR(n))

CAST with CAST('field' AS CHAR(n))

* tab

tab

* tabs again

tabs again

* re-added castAsChar()

re-added castAsChar() as per comment

* travis cs fix

travis cs fix

* the mssql castAsChar()

the mssql castAsChar()

* msdn

msdn
  • Loading branch information
alikon authored and rdeutz committed Mar 30, 2017
1 parent bcf9e61 commit 5d4539a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
29 changes: 29 additions & 0 deletions libraries/joomla/database/query/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,33 @@ public function selectRowNumber($orderBy, $orderColumnAlias)

return $this;
}

/**
* Casts a value to a char.
*
* Ensure that the value is properly quoted before passing to the method.
*
* Usage:
* $query->select($query->castAsChar('a'));
* $query->select($query->castAsChar('a', 40));
*
* @param string $value The value to cast as a char.
*
* @param string $len The lenght of the char.
*
* @return string Returns the cast value.
*
* @since 3.7.0
*/
public function castAsChar($value, $len = null)
{
if (!$len)
{
return $value;
}
else
{
return ' CAST(' . $value . ' AS CHAR(' . $len . '))';
}
}
}
28 changes: 28 additions & 0 deletions libraries/joomla/database/query/pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,32 @@
*/
class JDatabaseQueryPdo extends JDatabaseQuery
{
/**
* Casts a value to a char.
*
* Ensure that the value is properly quoted before passing to the method.
*
* Usage:
* $query->select($query->castAsChar('a'));
* $query->select($query->castAsChar('a', 40));
*
* @param string $value The value to cast as a char.
*
* @param string $len The lenght of the char.
*
* @return string Returns the cast value.
*
* @since 11.1
*/
public function castAsChar($value, $len = null)
{
if (!$len)
{
return $value;
}
else
{
return ' CAST(' . $value . ' AS CHAR(' . $len . '))';
}
}
}
14 changes: 12 additions & 2 deletions libraries/joomla/database/query/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,26 @@ public function clear($clause = null)
*
* Usage:
* $query->select($query->castAsChar('a'));
* $query->select($query->castAsChar('a', 40));
*
* @param string $value The value to cast as a char.
*
* @param string $len The lenght of the char.
*
* @return string Returns the cast value.
*
* @since 11.3
*/
public function castAsChar($value)
public function castAsChar($value, $len = null)
{
return $value . '::text';
if (!$len)
{
return $value . '::text';
}
else
{
return ' CAST(' . $value . ' AS CHAR(' . $len . '))';
}
}

/**
Expand Down
15 changes: 12 additions & 3 deletions libraries/joomla/database/query/sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,24 @@ public function __toString()
*
* Ensure that the value is properly quoted before passing to the method.
*
* @param string $value The value to cast as a char.
* @param string $value The value to cast as a char.
*
* @param string $len The lenght of the char.
*
* @return string Returns the cast value.
*
* @since 11.1
*/
public function castAsChar($value)
public function castAsChar($value, $len = null)
{
return 'CAST(' . $value . ' as NVARCHAR(10))';
if(!$len)
{
return 'CAST(' . $value . ' as NVARCHAR(30))';
}
else
{
return 'CAST(' . $value . ' as NVARCHAR(' . $len . '))';
}
}

/**
Expand Down

0 comments on commit 5d4539a

Please sign in to comment.