Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql INET_ATON/INET_NTOA support #5072

Closed
mcxinyu opened this issue Mar 14, 2024 · 5 comments
Closed

mysql INET_ATON/INET_NTOA support #5072

mcxinyu opened this issue Mar 14, 2024 · 5 comments

Comments

@mcxinyu
Copy link

mcxinyu commented Mar 14, 2024

SQLDelight Version

2.0.1

SQLDelight Dialect

2.0.1

Describe the Bug

Execution failed for task ':service:service-open:generateMainDatabasesInterface'.
> A failure occurred while executing app.cash.sqldelight.gradle.SqlDelightTask$GenerateInterfaces
   > Failed to compile SqlDelightStmtClojureStmtListImpl(STMT_CLOJURE_STMT_LIST): [] :
     INSERT INTO open_whitelists (userId, ip)
         VALUES (?, INET_ATON(?));
         SELECT LAST_INSERT_ID();

Hello everyone, when I was writing a line of sql (mysql) statement, the above error warning appeared.
It seems that sqldelight does not support mysql's INET_ATON/INET_NTOA function.
What should I do to solve this problem?

Stacktrace

No response

@mcxinyu mcxinyu added the bug label Mar 14, 2024
@mcxinyu mcxinyu changed the title mysql INET_ATON/INET_NTOA 支持 mysql INET_ATON/INET_NTOA support Mar 14, 2024
@griffio
Copy link
Contributor

griffio commented Mar 14, 2024

Any functions that are not supported can be added here to the MySqlTypeResolver https://github.com/cashapp/sqldelight/blob/a38c8e72fea406989a9fa6b9f1dbd71384d6d5ec/dialects/mysql/src/main/kotlin/app/cash/sqldelight/dialects/mysql/MySqlTypeResolver.kt#L99

A work-around that may work for you, at this moment, could be to enclose the function in additional parentheses (). e.g

 INSERT INTO open_whitelists (userId, ip)
         VALUES (?, (INET_ATON(?))); 

This is valid in MySql and the SqlDelight compiler allows it as an expression without knowing the function.

@mcxinyu
Copy link
Author

mcxinyu commented Mar 14, 2024

Hello @griffio. It's really frustrating. I even referred to your sqldelight-custom-dialect project, but I did get an error prompt,

Compiling with dialect com.njf2016.sqldelight.MySqlCustomDialect

.../OpenWhitelists.sq: (18, 11): Unknown function LAST_INSERT_ID
18    SELECT LAST_INSERT_ID()
                 ^^^^^^^^^^^^^^^^

Here is the implementation of this class. Do you have any suggestions?

class MySqlCustomDialect : SqlDelightDialect by MySqlDialect() {
    override fun typeResolver(parentResolver: TypeResolver): TypeResolver {
        return MySqlTypeCustomResolver(parentResolver)
    }

    class MySqlTypeCustomResolver(private val parentResolver: TypeResolver) :
        TypeResolver by MySqlTypeResolver(parentResolver) {

        override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? {
            return when (functionExpr.functionName.text.lowercase()) {
                "inet_aton" -> IntermediateType(PrimitiveType.TEXT)
                "inet_ntoa" -> IntermediateType(PrimitiveType.TEXT)
                else -> parentResolver.functionType(functionExpr)
            }
        }
    }
}

@griffio
Copy link
Contributor

griffio commented Mar 14, 2024

@mcxinyu, LAST_INSERT_ID already exists, I had a go with a MySql version - see branch https://github.com/griffio/sqldelight-custom-dialect/tree/mysql
Seems to work ok - have a look and see if anything is different

@mcxinyu
Copy link
Author

mcxinyu commented Mar 14, 2024

@mcxinyu, LAST_INSERT_ID already exists, I had a go with a MySql version - see branch https://github.com/griffio/sqldelight-custom-dialect/tree/mysql Seems to work ok - have a look and see if anything is different

@griffio, Thank you so much, you gave me the correct answer.

image

@mcxinyu
Copy link
Author

mcxinyu commented Mar 14, 2024

At this point, the issue has been resolved, but I found that the sqldelight idea plugin seems to be abnormal.

20240314201928_rec_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants