problem with convert like function in oracle / doctrine - oracle

In oracle there is a "convert" function to convert from one encoding to other. I wrote custom function for doctrine (converting column from ISO-8859-2 to UTF-8:
<?php
namespace AppBundle\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
class ConvertFunction extends FunctionNode
{
public $stringPrimary;
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return $sqlWalker->getConnection()->getDatabasePlatform()->getUpperExpression(
"CONVERT(".$sqlWalker->walkSimpleArithmeticExpression($this->stringPrimary).", 'EE8ISO8859P2', 'UTF8')"
);
}
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->stringPrimary = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
when I use it, it doesn't search anything, but when I run sql in DB it works.
Does symfony / doctrine change a variable encoding ?
edit:
(my custom function is declared as convertToISO8859): When I run (in symfony):
select * from tables where convertToISO8859(name) = 'Jesień'
It return nothing, but in symfony profiler it shows properly sql:
select * from tables where upper(convert(name), 'EE8ISO8859P2', 'UTF8') = 'Jesień'

Related

Which rule can I use to change variable name?

I need to replace all occurencies of $connection with $link?
I know I could do with a regexp replacement using my IDE, but I need to be able to re-run the sostitution automatically.
So I want to use rector.
Is there a way to replace a var name ? Which is the rule name?
There are a couple of potentially suitable rules & sets:
RenamePropertyRector
There is also the '\Rector\Set\ValueObject\SetList::NAMING' set that can be enabled in rector.php that will perform some similar rules, like renaming variables according to the type.
The complete set of basic rules (not including framework or library-specific) are at https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
I create a custom rule, very specific for my needs
<?php
namespace Rules;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use PhpParser\Node\Expr\Variable;
class ReplaceConnectionVarNameWithLink extends AbstractRector
{
public function getNodeTypes(): array
{
return [
Variable::class
];
}
public function getRuleDefinition(): \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new RuleDefinition(
'rename $connect into $link',
[]
);
}
public function refactor(\PhpParser\Node $node)
{
if (!$this->isName( $node, 'connection')) {
// return null to skip it
return null;
}
$node->name = "link";
return $node;
}
}

Doctrine is converting string to hex value

I am converting one database to another database through a script.
While running the script I am getting the following error:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xBFbangl...' for column 'country' at row 1
An exception is thrown where it shows that it tries to set country as "\xcf\xbb\xbf\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68".
When I var_dump the value, it just shows as "Bangladesh".
Is there something I have to adjust in my php code?
I have tried this, but phpmyadmin is throwing an #1064 error stating a syntax error in the query.
UPDATE
The scripts is a command in Symfony3:
<?php
namespace My\Bundle\Command;
use My\AccommodationBundle\Entity\Visit;
use My\NewAccommodationBundleV2\Entity\Visit as Visit2;
use My\OtherBundle\Entity\Person;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class VisitConvertToNewFormatCommand extends ContainerAwareCommand
{
private $em;
protected function configure()
{
$this
->setName('accommodation:visit:convert')
->setDescription("Converting old structure to new structure'")
;
}
protected function getTimeStamp() {
$currentTime = new \DateTime('now');
return '['.$currentTime->format('Y-m-d H:i:s').'] ';
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln($this->getTimeStamp().$this->getDescription());
$this->em = $this->getContainer()->get('doctrine')->getManager();
$visits = $this->em->getRepository('MyOldAccommodationBundle:Visit')->findAll();
foreach ($visits as $visit) {
$visit2 = new Visit2();
$visit2->setArrivalDate($visit->getArrivalDate());
$visit2->setArrivalStatus($visit->getArrivalStatus());
$visit2->setArrivalTime($visit->getArrivalTime());
$visit2->setBookingType($visit->getBookingType());
$visit2->setCountry($visit->getCountry()); // <----
...
$this->em->persist($visit2);
$user = $visit->getUser();
if ($user != null) {
$person = new Person();
...
$person->setFirstName(trim(ucfirst(strtolower($user->getFirstName()))));
$person->setLastName(trim(ucfirst(strtolower($user->getLastName()))));
$person->setEmail(preg_replace('/\s+/', '', $user->getEmail()));
...
$this->em->persist($person);
}
}
}
}
\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68 is Bangladesh; the \xcf\xbb\xbf before it makes no sense. In latin1 it is Ï»¿. It does not validly convert to utf8. CFBB is ϻ (GREEK SMALL LETTER SAN), but then bf is broken utf8.
I suggest it is coming from the source of the data.
var_dump probably showed nothing because of what device you were displaying it on.
Ok after days of trying this did the trick:
$country = iconv("UTF-8", "ISO-8859-1//IGNORE", $country);
I have no idea why it went wrong in the first place. if anybody knows, please share.

Codeigniter change database config at runtime

Can I change the database config per method in a controller?
$db['default']['db_debug'] = TRUE;
The default is TRUE, while I need to make it false in a certain method to catch the error and do something else (for example show 404 page).
When I tried $this->config->load('database') it fails.
Another question :
Can I check an incorrect query and catch it to some variables rather than displaying it to users other than setting the db_debug config to FALSE?
I checked the code of system/database/DB_Driver and found that:
$this->db->db_debug = FALSE;
will work in my controller to enable/disable the debug thing on the fly.
Expanding on the answer by comenk, you can extend the database class and implement various methods by which to achieve your goal.
First, you'll need to extend the core Loader class by creating a MY_Loader.php file
class MY_Loader extends CI_Loader
{
function __construct()
{
parent::__construct();
}
/**
* Load the Standard and/or Extended Database function & Driver class
*
* #access public
* #return string
*/
function database( $params = '', $return = FALSE, $active_record = NULL )
{
$ci =& get_instance();
if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($ci->db) AND is_object($ci->db))
{
return FALSE;
}
$my_db = config_item('subclass_prefix').'DB';
$my_db_file = APPPATH.'core/'.$my_db.EXT;
if(file_exists($my_db_file))
{
require_once($my_db_file);
}
else
{
require_once(BASEPATH.'database/DB'.EXT);
}
// Load the DB class
$db =& DB($params, $active_record);
$my_driver = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
$my_driver_file = APPPATH.'core/'.$my_driver.EXT;
if(file_exists($my_driver_file))
{
require_once($my_driver_file);
$db = new $my_driver(get_object_vars($db));
}
if ($return === TRUE)
{
return $db;
}
// Initialize the db variable. Needed to prevent
// reference errors with some configurations
$ci->db = '';
$ci->db = $db;
}
}
By implementing the above this will allow you to create a MY_DB_mysqli_driver.php whereby mysqli is replaced by whatever driver you're using in your CI database.php config.
At this point you'd add comenk's answer to MY_DB_mysqli_driver.php
function debug_on() {
return $this->db_debug = TRUE;
}
function debug_off() {
return $this->db_debug = FALSE;
}
function in_error() {
return (bool) $this->_error_number();
}
Then in your model/controller,
$this->db->debug_off();
$this->db->query('SELECT * FROM `table`');
if( $this->db->in_error() ) {
show_404();
}
$this->db->debug_on();
you must add function on system/database/DB_driver.php
function debug_on()
{
$this->db_debug = TRUE;
return TRUE;
}
function debug_off()
{
$this->db_debug = FALSE;
return FALSE;
}
after that you can simply do this command to changes at run-time
$this->db->debug_off();
$this->db->reconnect();
$this->db->db_debug = 0; // 0: off, 1: on
That worx for me...
You can look at the $GLOBALS variable to locate this generic setting.
To hide bad SQL (and other errors) from users, you need to set the php error reporting level. CodeIgniter ships in basically development mode.
Go to index.php and replace this
error_reporting(E_ALL);
with this
error_reporting(0);
This is the quick way to do it. You can also implement this using a hook, so you don't have to touch CI files. You can also add logic to that hook so that it only sets it on the production server.
For debugging SQL, you can create a class that inherits from CI_Model, then create all your model classes to extend that class. In that class, you can add code for running queries that writes the queries to the log so that you can debug them easier. This won't help if the query itself is bad, but you should be able to figure that out before you get to that point.

Entity framework LINQ query 2 tables

I am unable to use the Include("CITies") extension method on the db.Restaurants object.
When I use the include I get the following error:
DL.RESTAURANT does nto contain a definition for include or an extension method of include
namespace DL
{
public class DLgetRestaurants
{
DL.FVRGDataContext db = new FVRGDataContext();
public IEnumerable <RESTAURANT> getRestaurants(string cuisineName)
{
var restaurantList =
from RESTAURANT in db.RESTAURANTs.Include("CITies")
where RESTAURANT.CITies.Any(t => t.CITY_ID == 2)
orderby RESTAURANT.REST_NAME ascending
select RESTAURANT;
return restaurantList;
}
}
}
You can only use the Include on Entities that have a relation with another table. Other than that you shouldn't have an issue.

How to view LINQ Generated SQL statements?

How is it done using the ObjectQuery method?
You can always attach something to the .Log property of your DataContext. That will show all the SQL commands as they are sent.
I do this in my base for data access objects and output it to the Visual Studio debug console. As the objects create their DataContext I check it see if its debug and attach a TextWritter helper class like this:
dbDataContext _dB = new dbDataContext();
_dB.CommandTimeout = 5000;
#if DEBUG
_dB.Log = new DebugTextWriter();
#endif
Here is the helper object for output to the debug console:
//utility class for output of TextWriter for the Visual Sudio Debug window
class DebugTextWriter : System.IO.TextWriter
{
public override void Write(char[] buffer, int index, int count)
{
System.Diagnostics.Debug.Write(new String(buffer, index, count));
}
public override void Write(string value)
{
System.Diagnostics.Debug.Write(value);
}
public override Encoding Encoding
{
get { return System.Text.Encoding.Default; }
}
}
Here is what I found using ObjectQuery Method. Using console for testing, you can do the following:
Create an Extension Method as below, then call it. Say Product product, then SQL prints out as product.ToTraceString.
public static class MyExtensions
{
public static string ToTraceString<T>(this IQueryable<T> t)
{
string sql = "";
ObjectQuery<T> oqt = t as ObjectQuery<T>;
if (oqt != null)
sql = oqt.ToTraceString();
return sql;
}
}
You could have a look at the Linq-to-SQL Debug Visualizer, or just hover your mouse over your Linq-to-SQL query (tooltip should show generated SQL), or access:
context.GetCommand(query).CommandText
var q = from img in context.Images
...
select img;
string sql = q.ToString();
sql will contain the sql select query.
EDIT: disadvantage: parameters won't have any values at this time
You could run the SQL Server Profiler.
This is what I use when setting up the database context:
this.DbContext.Database.Log += s => Debug.WriteLine(s);
just a small update you can now use an Action to log the SQL:
// test SQL logger
Action<string> SQLLogger = (message) => System.Diagnostics.Debug.Write(message);
_dB.Context().Database.Log = SQLLogger;
If you are executing the linq query against a database, you can run the SQL Profiler to record the SQL query that is being executed. We do it quite often to identify any performance impact on conversion.

Resources