If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Infinite Arguments for UDF

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-11, 09:04
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
Infinite Arguments for UDF

Hi all,

A while ago, I've begun to wonder in a thread if I can write up a user defined function in MySQL for printing table codes.

I've come up with the following two-column code but have no idea how to scale it to allow indefinite number of arguments.
Code:
mysql> SELECT
    -> CONCAT('<tr>'
    ->      , CONCAT('<td>'
    ->             , CONCAT_WS('</td><td>'
    ->                       , IFNULL(id,'NULL')
    ->                       , IFNULL(amount,'NULL')
    ->                       , IFNULL(NULL,'NULL')
    ->               )
    ->             , '</td>'
    ->        )
    ->      , '</tr>') asdf
    ->   FROM product;
+----------------------------------------------+
| asdf                                         |
+----------------------------------------------+
| <tr><td>1</td><td>100</td><td>NULL</td></tr> | 
| <tr><td>2</td><td>200</td><td>NULL</td></tr> | 
| <tr><td>3</td><td>300</td><td>NULL</td></tr> | 
| <tr><td>4</td><td>400</td><td>NULL</td></tr> | 
+----------------------------------------------+
4 rows in set (0.08 sec)
Can I create a UDF like CONCAT? Or do I have to write it in, shudder, C++ and compile it?
Reply With Quote
  #2 (permalink)  
Old 07-19-11, 02:09
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
I am not sure why you do not just pull the information from the database and from your application generate the HTML. If your client application is on a separate server to your database server you are transferring a lot more data by having the database generate the HTML than generating it yourself in your application. This will be more scalable too.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 07-19-11, 11:32
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
It's a strange request, so there is no wonder you ask why. There is no application in this case that deals with the representation layer because I am going to use this table-generating UDF for documents. It would be a very convenient tool when I copy result sets from the terminal into HTML or LaTeX documents.

Surely, I can write tables in docs using "export as HTML" command. I won't die for it but it'd be a lot nicer if I don't have to save "resultset.html" file every time I have a table to copy.

Maybe it is more a life-hack than a DB question. I should feel very lucky if someone ever holds my hand while I write an all-query solution. Still, I am wondering how, if at all, infinite arguments are possible in SQL functions as they are in programming languages.
Reply With Quote
  #4 (permalink)  
Old 07-19-11, 11:55
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
alternatively you can dynamically create SQL statements in stored procedures which can have its result set returned. I had created a mini example MySQL – stored procedures and dynamic SQL « IT Integrated Business Solutions

So what you do is generate the SQL dynamically within the stored procedure and then return this resultset. Would that help?
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 07-27-11, 12:01
Ikviens Ikviens is offline
Registered User
 
Join Date: Mar 2006
Posts: 55
Ronan, thanks for the reference.

It took me a long time to create something very simplistic. At one time I tried to concatenate columns with a CASE expression and the got a collation error. So here is my code, albeit very unsatisfactory:
Code:
DROP PROCEDURE IF EXISTS html_table;
DELIMITER $$
CREATE PROCEDURE html_table(IN listOfColumns VARCHAR(100), tableName VARCHAR(100))
BEGIN
  SET @query := CONCAT("SELECT CONCAT('<tr>', CONCAT('<td>', CONCAT_WS('</td><td>',", listOfColumns, "), '</td>'), '</tr>') htmlTableTags FROM ", tableName);
  PREPARE stmt FROM @query;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END;
$$
It takes column names and the table name and executes an unconditional SELECT statement (no JOIN or WHERE). Test results are as follows:
Code:
mysql> delimiter ;
mysql> call html_table( "bookid, title", "books" );
+-----------------------------------------+
| htmlTableTags                           |
+-----------------------------------------+
| <tr><td>1</td><td>abc</td></tr>         | 
| <tr><td>2</td><td>photography</td></tr> | 
| <tr><td>3</td><td>video</td></tr>       | 
| <tr><td>4</td><td>soccer</td></tr>      | 
| <tr><td>5</td><td>football</td></tr>    | 
| <tr><td>6</td><td>tennis</td></tr>      | 
| <tr><td>7</td><td>golf</td></tr>        | 
| <tr><td>8</td><td>fishing</td></tr>     | 
| <tr><td>9</td><td>cycling</td></tr>     | 
+-----------------------------------------+
9 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
Thanks again.

Last edited by Ikviens; 07-27-11 at 12:07.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On