I ported my application from mysql to postgresql. In doing so camel cased column names where changed to use an underscore as the word separator in most cases. There are a couple where the camel case was just removed.
I now need to insert to both of these databases. The data, the number of columns etc are the same just the column names are slightly different. I can use PDO to switch database drivers and insert, but id like to do this without writing two seperate sql statements for inserts. Is there a common way to map column names to array keys or populate some class and pass that to PDO.
$column_names[] = array('someName'=>'some_name');
$column_names[] = array('someOtherName'=>'some_other_name');
$column_names[] = array('someOtherNameAgain'=>'some_other_name_again');
foreach($column_names as $mysql=>$post_ogre)
{
//decide which one you want to use here
}
something similar to this should work
You can create column names correlation table automatically by looking at INFORMATION_SCHEMA.COLUMNS
table, which is supported by both databases.
First, run this against MySQL:
SELECT
column_name,
ordinal_position,
data_type
FROM information_schema.columns
WHERE table_schema = 'mysql_dbname'
AND table_name = 'mytable'
Then, run almost the same query against PostgreSQL:
SELECT
column_name,
ordinal_position,
data_type
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'mytable'
Now, looking at column_name
and ordinal_position
on both sides, you can easily create mapping table between new and old column names (assuming that Postgres names are primary for you). You can do it for every table separately, or you can do it all at once (just remove table_name constraint).
After mapping is known, anywhere when you need to use column name in any SQL statement against MySQL, you will have to substitute it via this map.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.