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

[manifold-sql] Code doesn't compile when using :: to cast a parameter or call function on parameter. #589

Closed
oldshensheep opened this issue May 11, 2024 · 8 comments

Comments

@oldshensheep
Copy link

Describe the bug
Code doesn't compile when using :: to cast a parameter.

error: method execute in interface Fragment_7963949495587867082 cannot be applied to given types;
              "[.sql/] insert into table_name(data) values(?::jsonb) ".execute(ctx,
                                                                                                  ^
  required: SqlChangeCtx
  found:    SqlChangeCtx,String
  reason: actual and formal argument lists differ in length

To Reproduce
Steps to reproduce the behavior:
table_name(data) is jsonb in postgresql

            DB.addSqlChange(ctx -> {
              "[.sql/] insert into table_name(data) values(?::jsonb) ".execute(ctx,"string");
            });
            DB.addSqlChange(ctx -> {
              "[.sql/] insert into table_name(data) values(to_hex(?)) ".execute(ctx,"string");
            });

Expected behavior
The code should work like the example below.

    Class.forName("org.postgresql.Driver");
    Connection conn = DriverManager.getConnection(
        "jdbc:postgresql://localhost:5432/postgres");
    String insertSQL = "INSERT INTO table_name(data) VALUES (?::jsonb)";
    try (PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
      pstmt.setString(1, "{}");
      pstmt.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    }

Screenshots
If applicable, add screenshots to help explain your problem (drag/drop them here).

Desktop (please complete the following information):

  • OS Type & Version:
  • Java/JDK version: 21
  • IDE version (IntelliJ IDEA or Android Studio): IntelliJ IDEA
  • Manifold version: 2024.1.14
  • Manifold IntelliJ plugin version: 2024.1.0

Additional context
Add any other context about the problem here.

Stack trace
Please include a stack trace if applicable

@rsmckinney
Copy link
Member

Hi @oldshensheep. Manifold uses named parameters prefixed with : where you would otherwise use JDBC ?.

 DB.addSqlChange(ctx -> {
              "[.sql/] insert into table_name(data) values(to_hex(:data)) ".execute(ctx,"string");
            });

DB.addSqlChange(ctx -> {
              "[.sql/] insert into table_name(data) values(:data::jsonb) ".execute(ctx,"string");
            });

See the Query docs for this.

@oldshensheep
Copy link
Author

I just tried, still the same error.
If I change the database schema, change the type of table_name(data) from jsonb to text and remove the ::jsonb from code, then no error occurred.

@oldshensheep
Copy link
Author

Or if I remove the ::jsonb from SQL, the code compiles, but fails at runtime.

Caused by: org.postgresql.util.PSQLException: ERROR: column "data" is of type jsonb but expression is of type character varying

Is this an expected error? JDBC standard can't map jsonb so it fallback to string.

@rsmckinney
Copy link
Member

Ah, yes there appears to be an issue with the json/jsonb postgresql data type. Will have a deeper look. Thanks.

rsmckinney added a commit that referenced this issue May 14, 2024
- support postgresql json & jsonb types
- fix parameter parser to account for postgresql '::' cast operator
@rsmckinney
Copy link
Member

Fix available with release 2024.1.15. Thanks for reporting this!

@oldshensheep
Copy link
Author

using :: to cast a parameter works now but call function on parameter failed with the same error

@rsmckinney
Copy link
Member

rsmckinney commented May 15, 2024

If your app defines more than one dbconfig, you probably need to qualify your sql command. Instead of “[.sql/]…” try ”[.sql:DB/]….

@oldshensheep
Copy link
Author

Ah, I see the manifold plugin error  ERROR: function to_hex(unknown) is not unique Hint: Could not choose a best candidate function. You might need to add explicit type casts. Position: 87
I change the to_hex(?) to to_hex(?::int) , the code can be compiled again.

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

No branches or pull requests

2 participants