mysqlcoldfusiontransactionscoldfusion-8

ColdFusion: Multiple SQL statements in a query?


Apparently ColdFusion doesn't like multiple SQL statements within a single query... so what once was this:

SET @sender_user_id = 3, @recipient_user_id = 5;

INSERT INTO messages (message_type, sender_id, message_title, message_content) 
  VALUES(3, @sender_user_id, 'One more thing...', 'I am testing this message');

SET @saved_message_id = LAST_INSERT_ID();

INSERT INTO message_recipient (message_id, user_id) 
  VALUES(@saved_message_id, @recipient_user_id);

INSERT INTO message_status (message_id, user_id, is_read, read_datetime, is_deleted, deleted_datetime)
  VALUES (@saved_message_id, @recipient_user_id, 0, NULL, 0, NULL);

Get's turned into this:

<cftransaction>

    <cfquery name="insertMessage" dataSource="mySource">

        SET @sender_user_id = 3, @recipient_user_id = 5;

    </cfquery>
    <cfquery name="insertMessage2" dataSource="mySource">

        INSERT INTO messages (message_type, sender_id, message_title, message_content) 
        VALUES(3, @sender_user_id, '#params.message_title#', '#params.message_content#');

    </cfquery>
    <cfquery name="insertMessage3" dataSource="mySource">

        SET @saved_message_id = LAST_INSERT_ID();

    </cfquery>
    <cfquery name="insertMessage4" dataSource="mySource">   

        INSERT INTO message_recipient (message_id, user_id) 
        VALUES(@saved_message_id, @recipient_user_id);

    </cfquery>
    <cfquery name="insertMessage5" dataSource="mySource">   

        INSERT INTO message_status (message_id, user_id, is_read, read_datetime, is_deleted, deleted_datetime)
        VALUES (@saved_message_id, @recipient_user_id, 0, NULL, 0, NULL);

    </cfquery>

</cftransaction>

This works... but I'm wondering if there is some ColdFusion thing that I don't know about. Otherwise, this works.


Solution

  • In ColdFusion Admin, go to your Data Source definition form and add "allowMultiQueries=true" to the Connection String box. Once you do that, you can pass multiple semi-colon separated queries in a single CFQUERY tag. Just make sure you're using CFQUERYPARAM to screen for SQL Injection Attacks.