Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view the SQL generated by Anorm?

Tags:

scala

anorm

I am trying out anorm to execute a number of insert statements, and then return the value of LAST_INSERT_ID(). But I am getting an error saying my SQL syntax is invalid.

Can anyone tell me how to check and see what the final generated SQL that is sent ty MySQL looks like?

like image 240
skb Avatar asked Mar 09 '26 00:03

skb


1 Answers

Anorm doesn't really generate SQL, you do. But there is a way to log the exact queries that are sent over the wire to the console (after the statements are prepared, assuming you're using Anorm within Play).

Assuming you're using a single database called default (the default configuration), add the following to your application.conf:

 db.default.logStatements=true

Then you can save the following to conf/logger.xml:

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.jolbox.bonecp" level="NONE">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="play" level="INFO">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="application" level="NONE">
        <appender-ref ref="STDOUT" />
    </logger> 

</configuration>

The key line in the file is related to BoneCP logging, but we want to put in lines for the application and play loggers as well, so we don't mess up the default logging.

like image 157
Michael Zajac Avatar answered Mar 11 '26 13:03

Michael Zajac