Last active
June 21, 2017 12:21
-
-
Save anaspk/c5469758784520f252db7e1913400949 to your computer and use it in GitHub Desktop.
Paste a list of column names copied from Oracle SQL Developer and get a string back to use in the SELECT clause of your SQL queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// If you select and copy the names of columns of a table from Oracle SQL Developer, | |
// you will get something like this. | |
$column_names = 'ACCOUNT_ID | |
COMMENTS | |
POST_DATE'; | |
// But in PHP, we normaly select columns with the aliases in lower case like so: | |
// column_name "column_name" | |
// Otherwise, names will be in all caps in the result set. | |
// Following code snippet performs this transformation and gives you a string to be | |
// directly inserted in your SQL query. | |
$table_alias = 'A'; | |
$column_names_processed = implode(', ', array_map(function ($column_name) { | |
global $table_alias; | |
$column_name = trim($column_name); | |
$column_name = "{$table_alias}.{$column_name} \"{$column_name}\""; | |
return $column_name; | |
}, explode("\n", strtolower($column_names)))); | |
echo $column_names_processed; | |
// Output: A.account_id "account_id", A.comments "comments", A.post_date "post_date" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment