php mb_detect_encoding equivalent in mysql - mysql

I have a process in php like...
if (mb_detect_encoding($msg) == 'ASCII') {
....
} else {
....
}
Now, the above functionality I have to enable in mysql procedure. Is there any function or work around to achieve in mysql.
In other simple word mb_detect_encoding() equivalent in mysql !
Please help. Thanks.

There is no such functionality in mysql and I'm not even sure that you should have such functionality in adatabase either. Columns storing text have their fixed character set in mysql. It is the responsibility of the application using the database to ensure that text with right encoding is entered into the database.
EDIT:
Obviously, nothing prevents you from implementing a similar functionality using UDFs if you really insist on having this functionality within mysql.

Related

Using "Point"-Datatype in Phinx-Migration (in CakePhp)

I'm creating an API for POIs and use the POINT-Type to store the coordinates.
As my company uses CakePHP I have to write a migration-script with Phinx.
But I don't have any Idea how to correctly create a column with the POINT-Type.
Sure, I just could make an "ALTER TABLE ..." in a handwritten Query, but maybe there is a better way?
Versions:
Cake: 3.4.7
Phinx: 0.6.5
MySQL: 5.7.18
Phinx Does not provide an adapter for POINT yet.
You should create your query manually.
See also Unable to seed data with POINT datatype #999
Just use "point" as you would use any other datatype as the second parameter of addColumn().
It's just not documented yet.
Credits for this solution are going to #ndm;
I just think it's worth putting this as answer instead as a comment.
Looks like Phinx supports point types for quite some time now (the docs are not up to date)... try to use \Phinx\Db\Adapter\AdapterInterface::PHINX_TYPE_POINT as the type

How to use MySQL AES_DECRYPT with aes-256-gcm

I'm using MySQL v5.7.17.
I'm trying to decrypt data I encrypted via Ruby using the aes-256-gcm algorithm.
So far I have this:
SELECT AES_DECRYPT(UNHEX(#encrypted_account_number), #key, #encrypted_account_number_iv);
Which would work, however is returning NULL because the encryption mode is does not match with what I used to encrypt.
I did some research and apparently MySQL does not support aes-256-gcm.
Is that true? If so, is there any way around it?
BTW - this is the command I'm using to switch between encryption modes:
SET ##session.block_encryption_mode = 'aes-256-ctr';
Thanks a lot!
mysql currently does not support ctr and ctr based modes like gcm, so I fear you will have to do this outside of your dbms.
http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_block_encryption_mode

Drupal : How can I know if the db is mysql or postgres

I have a complicated query and since I need that my module work on both mysql and postgres, I need to write two version of it.
Unfortunately, I don't know how I can check if the db I use is mysql or postgres, to know which query use. Do you know if a function can return this value?
As #kordirko says, one option is to query the server version: SELECT version(); will work on both MySQL and PostgreSQL, though not most other database engines.
Parsing version strings is always a bit fragile though, and MySQL returns just a version number like 5.5.32 wheras PostgreSQL returns something like PostgreSQL 9.4devel on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8), 64-bit. What do you do if you're connecting to a PostgreSQL-compatible database like EnterpriseDB Postgres Plus, or a MySQL-compatible database?
It's much safer to use the Drupal function for the purpose, DatabaseConnection::databaseType. This avoids a query round-trip to the DB, will work on databases that won't understand/accept SELECT version(), and will avoid the need to parse version strings.
You'll find this bug report useful; it suggests that the correct usage is Database::getConnection()->databaseType().
(I've never even used Drupal, I just searched for this).
As long as the abstract DatabaseConnection class extends PDO class, you can invoking pdo methods in order to know the current database driver.
For instance:
$conn = Database::getConnection();
print $conn->getAttribute($conn::ATTR_DRIVER_NAME); #returns mysql, pgsql...
There is a second way to do it using DatabaseConnection::driver():
print $conn->driver();
or DatabaseConnection::databaseType();
print $conn->databaseType();
Note that DatabaseConnection::driver() and DatabaseConnection::databaseType() are similar functions but not equals!
The return value from DatabaseConnection::driver() method depends on the implementation and other factors.
in the Drupal Database API page:
database.inc abstract public DatabaseConnection::driver()
This is not necessarily the same as the type of the database itself. For instance, there could be two MySQL drivers, mysql and mysql_mock. This function would return different values for each, but both would return "mysql" for databaseType().
In the most cases you just gonna want to use only
$conn->getAttribute($conn::ATTR_DRIVER_NAME)
or $conn->databaseType()
If you want get more specific properties, you should take advantage the PHP ReflectionClass features:
$conn = Database::getConnection();
$ref = new ReflectionClass($conn);
#ref->getProperties, ref->getConstants $ref->isAbstract...
Reference:
PDO::getAttribute
PDO::ATTR_DRIVER_NAME
Drupal Database API
Drupal Base Database API class

Access 2010 String Comparison Invalid procedure call

We've just upgraded from Access 2003 to Access 2010 and string comparisons are failing with an invalid procedure call error when default conditions are used. I’ve recreated two presumably related problems in a new Access 2007 format database containing only the default table, a query with the SQL below and a module containing only the code below, so I seriously doubt that this is a corruption issue.
First the following sub fails on the If Then line with Run-time error 5: Invalid procedure call or argument
Option Compare Database
Option Explicit
Sub checkStrCmp()
Dim str As String
str = "s"
If str = "s" Then
MsgBox "works"
End If
End Sub
If I change Option Compare Database to Option Compare Text the sub works as expected, but this seems like a bad idea as I may want to preform text as well as numeric comparisons inside a single sub.
I’m also getting “Invalid procedure call” errors in string comparison functions inside of SQL. The Replace function is requiring the supposedly optional compare parameter.
Select replace("foo-bar-baz", "-", "|", 1,-1);
Generates the “Invalid procedure call” error
Setting the compare parameter to any of the available values (0 -3) works as expected:
SELECT replace("foo-bar-baz", "-", "|", 1,-1, 0);
produces “foo|bar|baz”
Has anyone else seen this? Is there a setting that needs to be tweaked? Any other ideas outside of “Database corruption” which is all I’ve been able to find via Google.
TIA
apoligies for the sloppy code blocks I can't for the life of me get them to work right.
UPDATE: I should have mentioned that I'm running XP Pro sp3.
The problem seems limited to databases I create on my box. When I opened the test database I created on my box from other workstations on our network I saw the issue, but was then unable to recreate it when creating a new database as described above on those workstations. The databases I created on the two other workstations (same OS and MS Office versions installed) also worked correctly when opened on my machine. I was also unable to recreate the issue when I inserted new modules in those DBs from my machine.
In short the problem seems to only exist on databases created on my machine (and in old 2003 format databases I've converted to 2007 format on my machine). My best guess is that my install is hosed but I’d like to have some idea of how and why before I approach IT with a request to reinstall Office. I’d also like to rule out a conflict with other software on my box.
Your code modules do not all need to share the same Option Compare setting. So you could place those procedures which should use text comparisons in a module which has Option Compare Text in its Declarations section.
However, I don't understand your statement, "I may want to preform text as well as numeric comparisons inside a single sub." According to Access' help topic, the Option Compare Statement is "Used at module level to declare the default comparison method to use when string data is compared". In other words, Option Compare has no effect on the comparisons of numeric values.
Edit: Since the problem is limited to Option Compare Database for database files created on only one machine, I'll suggest you check Access' "New database sort order" setting on that machine. Change it to a choice which starts with "General" if it is set to anything else. Then create a new database and see whether you still have the problem.
The reason for this suggestion is that Option Compare Database tells Access to use the database's codepage setting for sorting. And "New database sort order" can set the codepage to the one which never gives me such troubles. However, my understanding of codepage details is pretty shallow; I never change it and don't know what the consequences of other settings would be.

NHibernate Full Text Search

What is the best way trying to get a text search function using nhibernate? I have read about NHibernate.Search, but cannot find the library anywhere.
I downloaded the latest NHibernate source code(2.1.2) and compiled it, but i still cannot find NHibernate.Search.
Does anyone have any suggestions? Or any other methods to do text search?
EDIT: Using MySQL database, incase it makes any difference.
NHiberante.Search a separate dll to make Nhiberante and Lucene work together. You have to download and reference it if you want to use it. You might like to read some introduction about Lucene to understand how Nhiberante.Search works.
One of the places where you can get the dll is hornget
If you want to the MySql specific full text search options, there won't be any help from Nhibernate for you to use them.
You can use Expression.Sql but i think it's good idea using mysql stored procedure
Your MySql Stored Procedure :
CREATE PROCEDURE `GetProductsByText`(IN `queryText` VARCHAR(100) CHARSET utf8)
SELECT *
FROM Products
WHERE MATCH(Title, Description) AGAINST (queryText)
Your nhibernate mapping xml file :
<sql-query name="GetProductsByText">
<return class="Product"/>
call `GetProductsByText`( :queryText )
</sql-query>
your c# nhibernate query:
public IList<Product> FindByText (string text)
{
var session = SessionFactory.GetCurrentSession ();
IQuery query = session.GetNamedQuery ("GetProductsByText");
return query.SetString ("queryText", text).List<Product> ();
}