Scala Slick: cannot get parameters in query









up vote
0
down vote

favorite












I have just spent hours on this. I'm trying to make a pretty complicated query in PostgreSQL through Slick in Scala, but Slick will NEVER take any of my parameters into account. If I try something as simple as:



def get(location: String) = 
val query = sql"select * from cities_v where name = $location limit 10"
println(query.toString)



The output will be:



SQLActionBuilder(Vector(select * from cities_v where name = ? limit 10),<function2>)


If instead, I try the literal insert:



def get(location: String) = 
val query = sql"select * from cities_v where name = #$location limit 10"
println(query.toString)



The output will be:



SQLActionBuilder(Vector(select * from cities_v where name = , ville, limit 10),<function2>


Slick will ALWAYS add commas around any literal argument, no matter where it is placed, even if it's the only argument in the query, as in:



sql"#$q"


Now, what I'd like is make more complex queries, with calculations and function calls within Postgres. But I can't get anywhere given that Slick won't let me use any of my variables.



I tried setting all sorts of implicits, some that extend GetResult some that extend StatementParameters, Slick seems to ignore all them and either replaces my arguments with ? or surrounds them with commas, thereby rendering all of my queries invalid. I would like to add that the #$ is not great because it does not provide sanitization. I'd like to stick with Slick because it's asynchronous, but I'm not sure where to go from here.



These are my version numbers, according to build.sbt:



"postgresql" % "postgresql" % "9.1-901-1.jdbc4",
"com.typesafe.slick" % "slick_2.12" % "3.2.3",


What am I doing wrong?










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have just spent hours on this. I'm trying to make a pretty complicated query in PostgreSQL through Slick in Scala, but Slick will NEVER take any of my parameters into account. If I try something as simple as:



    def get(location: String) = 
    val query = sql"select * from cities_v where name = $location limit 10"
    println(query.toString)



    The output will be:



    SQLActionBuilder(Vector(select * from cities_v where name = ? limit 10),<function2>)


    If instead, I try the literal insert:



    def get(location: String) = 
    val query = sql"select * from cities_v where name = #$location limit 10"
    println(query.toString)



    The output will be:



    SQLActionBuilder(Vector(select * from cities_v where name = , ville, limit 10),<function2>


    Slick will ALWAYS add commas around any literal argument, no matter where it is placed, even if it's the only argument in the query, as in:



    sql"#$q"


    Now, what I'd like is make more complex queries, with calculations and function calls within Postgres. But I can't get anywhere given that Slick won't let me use any of my variables.



    I tried setting all sorts of implicits, some that extend GetResult some that extend StatementParameters, Slick seems to ignore all them and either replaces my arguments with ? or surrounds them with commas, thereby rendering all of my queries invalid. I would like to add that the #$ is not great because it does not provide sanitization. I'd like to stick with Slick because it's asynchronous, but I'm not sure where to go from here.



    These are my version numbers, according to build.sbt:



    "postgresql" % "postgresql" % "9.1-901-1.jdbc4",
    "com.typesafe.slick" % "slick_2.12" % "3.2.3",


    What am I doing wrong?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have just spent hours on this. I'm trying to make a pretty complicated query in PostgreSQL through Slick in Scala, but Slick will NEVER take any of my parameters into account. If I try something as simple as:



      def get(location: String) = 
      val query = sql"select * from cities_v where name = $location limit 10"
      println(query.toString)



      The output will be:



      SQLActionBuilder(Vector(select * from cities_v where name = ? limit 10),<function2>)


      If instead, I try the literal insert:



      def get(location: String) = 
      val query = sql"select * from cities_v where name = #$location limit 10"
      println(query.toString)



      The output will be:



      SQLActionBuilder(Vector(select * from cities_v where name = , ville, limit 10),<function2>


      Slick will ALWAYS add commas around any literal argument, no matter where it is placed, even if it's the only argument in the query, as in:



      sql"#$q"


      Now, what I'd like is make more complex queries, with calculations and function calls within Postgres. But I can't get anywhere given that Slick won't let me use any of my variables.



      I tried setting all sorts of implicits, some that extend GetResult some that extend StatementParameters, Slick seems to ignore all them and either replaces my arguments with ? or surrounds them with commas, thereby rendering all of my queries invalid. I would like to add that the #$ is not great because it does not provide sanitization. I'd like to stick with Slick because it's asynchronous, but I'm not sure where to go from here.



      These are my version numbers, according to build.sbt:



      "postgresql" % "postgresql" % "9.1-901-1.jdbc4",
      "com.typesafe.slick" % "slick_2.12" % "3.2.3",


      What am I doing wrong?










      share|improve this question















      I have just spent hours on this. I'm trying to make a pretty complicated query in PostgreSQL through Slick in Scala, but Slick will NEVER take any of my parameters into account. If I try something as simple as:



      def get(location: String) = 
      val query = sql"select * from cities_v where name = $location limit 10"
      println(query.toString)



      The output will be:



      SQLActionBuilder(Vector(select * from cities_v where name = ? limit 10),<function2>)


      If instead, I try the literal insert:



      def get(location: String) = 
      val query = sql"select * from cities_v where name = #$location limit 10"
      println(query.toString)



      The output will be:



      SQLActionBuilder(Vector(select * from cities_v where name = , ville, limit 10),<function2>


      Slick will ALWAYS add commas around any literal argument, no matter where it is placed, even if it's the only argument in the query, as in:



      sql"#$q"


      Now, what I'd like is make more complex queries, with calculations and function calls within Postgres. But I can't get anywhere given that Slick won't let me use any of my variables.



      I tried setting all sorts of implicits, some that extend GetResult some that extend StatementParameters, Slick seems to ignore all them and either replaces my arguments with ? or surrounds them with commas, thereby rendering all of my queries invalid. I would like to add that the #$ is not great because it does not provide sanitization. I'd like to stick with Slick because it's asynchronous, but I'm not sure where to go from here.



      These are my version numbers, according to build.sbt:



      "postgresql" % "postgresql" % "9.1-901-1.jdbc4",
      "com.typesafe.slick" % "slick_2.12" % "3.2.3",


      What am I doing wrong?







      scala jdbc slick jdbc-postgres






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 2:33

























      asked Nov 12 at 1:48









      eje211

      1,34131935




      1,34131935






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          There is nothing wrong with that ? appears in you result statements. It is just parametrized query, and each ? represents placeholder for future value.



          Just run you queries against any database and check results.






          share|improve this answer




















          • Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
            – eje211
            Nov 13 at 4:59










          • You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
            – eje211
            Nov 13 at 21:43










          • You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
            – Yevhenii Popadiuk
            Nov 14 at 10:21


















          up vote
          0
          down vote













          You need to modify your query like this.



          sql"""select * from cities_v where name = $location limit 10"""


          For more info refer






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255061%2fscala-slick-cannot-get-parameters-in-query%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            There is nothing wrong with that ? appears in you result statements. It is just parametrized query, and each ? represents placeholder for future value.



            Just run you queries against any database and check results.






            share|improve this answer




















            • Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
              – eje211
              Nov 13 at 4:59










            • You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
              – eje211
              Nov 13 at 21:43










            • You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
              – Yevhenii Popadiuk
              Nov 14 at 10:21















            up vote
            1
            down vote



            accepted










            There is nothing wrong with that ? appears in you result statements. It is just parametrized query, and each ? represents placeholder for future value.



            Just run you queries against any database and check results.






            share|improve this answer




















            • Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
              – eje211
              Nov 13 at 4:59










            • You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
              – eje211
              Nov 13 at 21:43










            • You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
              – Yevhenii Popadiuk
              Nov 14 at 10:21













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            There is nothing wrong with that ? appears in you result statements. It is just parametrized query, and each ? represents placeholder for future value.



            Just run you queries against any database and check results.






            share|improve this answer












            There is nothing wrong with that ? appears in you result statements. It is just parametrized query, and each ? represents placeholder for future value.



            Just run you queries against any database and check results.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 8:54









            Yevhenii Popadiuk

            567313




            567313











            • Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
              – eje211
              Nov 13 at 4:59










            • You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
              – eje211
              Nov 13 at 21:43










            • You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
              – Yevhenii Popadiuk
              Nov 14 at 10:21

















            • Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
              – eje211
              Nov 13 at 4:59










            • You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
              – eje211
              Nov 13 at 21:43










            • You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
              – Yevhenii Popadiuk
              Nov 14 at 10:21
















            Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
            – eje211
            Nov 13 at 4:59




            Let me try that. It seems that it had not, unlike what I thought, been run against a database. I went with another solution, but I need the raw SQL statements again, so I'll do this and let you know how it went.
            – eje211
            Nov 13 at 4:59












            You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
            – eje211
            Nov 13 at 21:43




            You were absolutely right. I was just not used to how Slick works. So far, I find Slick fantastic and their documentation abysmal. The docs are really (in my opinion at least) awful. But it's all sorted out now. Thanks for the help!
            – eje211
            Nov 13 at 21:43












            You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
            – Yevhenii Popadiuk
            Nov 14 at 10:21





            You are welcome. I suggest you read essential slick from underscore.io (i still use ctrl+f on this book to refresh on something).
            – Yevhenii Popadiuk
            Nov 14 at 10:21













            up vote
            0
            down vote













            You need to modify your query like this.



            sql"""select * from cities_v where name = $location limit 10"""


            For more info refer






            share|improve this answer


























              up vote
              0
              down vote













              You need to modify your query like this.



              sql"""select * from cities_v where name = $location limit 10"""


              For more info refer






              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                You need to modify your query like this.



                sql"""select * from cities_v where name = $location limit 10"""


                For more info refer






                share|improve this answer














                You need to modify your query like this.



                sql"""select * from cities_v where name = $location limit 10"""


                For more info refer







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 4:48

























                answered Nov 12 at 4:41









                Mahesh Chand Kandpal

                1,522618




                1,522618



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255061%2fscala-slick-cannot-get-parameters-in-query%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto