Importing anorm.SQL in IntelliJ IDEA 13 - scala

IntelliJ 13 Community Edition
Play Framework 2.2
Scala 2.10.2
I am importing anorm._ and using SQL in my object. The object starts off as follows:
package controllers
import play.api.mvc._
import play.api.db.DB
import play.api.Play.current
import anorm._
object Walks extends Controller {
val futureWalksSql = SQL("SELECT * FROM walks where evt_date > now()")
IntelliJ cannot resolve symbol SQL. If I ctrl+Enter, after anorm. there is no SQL option, although there is a .Sql trait, object and class.
When I run the play project, it all works just fine, with no compilation errors so this Scala is syntactically correct but IntelliJ isn't picking this up. I have created the idea files by calling idea from within the play console and I've also tried idea with-sources=yes.
How do I get IntelliJ Community Editon to pick up anorm.SQL? What is so special about this object? I'm still learning Scala and so this might be a Scala issue.

SQL is a method defined in a package object anorm. So when you import anorm._ you import the entire package with the package object as well. I have actually no clue why Idea does not pick this up. But if you look into the package object sources you can see that the SQL method is just a wrapper on anorm.Sql.sql(inSql: String).
As a workaround you may try to import anorm.Sql._ and use sql("select 1") instead of SQL("select 1")

Related

Scala/Spark/Databricks: How to import code from user-created JAR?

I have a JAR that I created with intellij and sbt, that defines a case class and Object. I've uploaded it to my databricks workspace, and attached it to a cluster as a library.
How do I actually import code from it/reference it in my notebook? So far all I can think of is to try
import NameOfJar._
which gives
java.lang.NoClassDefFoundError: DataFrameComparison$
Do I need to have built the jar differently somehow? (Package statement or something?)
you should import import packageName._, jar name is not used in import statement. It should work the same as in usual local java/scala code.
You can check this article for details - https://docs.databricks.com/libraries.html
Btw, does your notebook fail on import itself, or later, when you're trying to use class, that exists inside jar?

Upgrading Play to 2.4, Slick to 3.1.1, value withTransaction is not a member of play.api.db.slick.Database

I am trying to upgrade my application from using Play 2.3.x to Play 2.4.x (will end at 2.6, but going one step at a time) and Slick from 2.1.0 to 3.1.1.
I have done my best to follow Play's migration guide, the Play Slick migration guide, and the Slick upgrade guides.
One of the problems I'm having right now is with the following line:
val db: slick.Database = play.api.db.slick.DB
This no longer seems to be the correct way to do this b/c I get errors like:
value withTransaction is not a member of play.api.db.slick.Database
From the Play slick migration guide, it seems like I should modify this to something like
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
But idk if I just don't have the right imports or what, but I get errors like:
object driver is not a member of package play.api.db.slick
not found: value DatabaseConfigProvider
For more context, here is one of the files I'm working with that gives this error: https://github.com/ProjectSidewalk/SidewalkWebpage/blob/2c48dfa2e34c691e40568bfa9d50493aa3fe9971/app/models/attribute/GlobalAttributeTable.scala
Anyone know what I missed among these migration guides?
Thank you in advance!
Turns out that I was missing a few things:
I had not realized that I needed to use a more recent version of the play-slick library (I was using 0.8.0 still instead of 1.1.1).
I needed to add the import import play.api.Play instead of the import import play.api.Play.current that I already had.
I had an import import play.api.db.slick that was causing the "object driver is not a member of package play.api.db.slick" error at the line with this import: import slick.driver.JdbcProfile. I just removed the former import that was not needed.
As #Valerii said, withTransaction has been removed in Slick 3.1, and the replacement is documented in the various links in the comments above.

Reading Configuration in Play (2.6.5) with Scala without #Inject

I'm using Play 2.6.5 with Scala. Most of the times reading from the configuration works with injecting the configuration.
In some parts it would be too difficult to get a class into injection scope.
Is there a way to get the configuration object without injection?
You can use ConfigFactory ->
ConfigFactory.load().getString("db.url")
import for the same ->
import com.typesafe.config.ConfigFactory

Scala + Playframework 2.3.X importing dependencies

I am having problems importing two dependencies here is the import
import io.GooglePlayClient
import io.GooglePlayError
and I get this error
object GooglePlayError is not a member of package akka.io
[error] import io.GooglePlayError
object GooglePlayClient is not a member of package akka.io
[error] import io.GooglePlayClient
It seems it is prefixing the package where I am trying to import (akka) to this imports, and the is not able to import.
Thank you
You already have akka.io imported in scope. So, akka.io.GooglePlayError is tried. Instead use import _root_.io.GooglePlayError.
Another option is to import using the fully qualified name of the dependencies if available, that is
import com.yourcompany.io.GooglePlayClient
import com.yourcompany.io.GooglePlayError
If the io package is really a root package, you could consider refactoring your package structure to something like the above.

Error in scala + dispatch

I am trying to GET a response from an API, using scala and dispatch. However, I get this error, after building. I googled for a solution, and tried cleaning, and resarting eclipse, but the error wont go away. What seems to be the problem? I use eclipse Helios (ie 3.6) and Scala v2.8.1, with Scala IDE v1.0.0.201104170033, installed from the Eclipse market.
dispatch{dispatch.type}.Http{object dispatch.Http} of type object dispatch.Http does not take parameters
This is my code.
class getList {
def main(args: Array[String]){
Http("http://foo.com/" >>> System.out)
}
}
What am I doing wrong?
What libraries have you downloaded? Are you sure the dependencies are set correctly? I tried with dispatch_http, version 8.0 for Scala 2.8.1, and it worked.
What imports are you using? I used these imports to make it work:
import dispatch.Http
import dispatch.HandlerVerbs._
Finally... class getList??? I assume this is a result of cut&pasting from actual code, but you should strive to produce a compilable example of your problem. Scala doesn't run programs from class, only from object, and it follows Java style of having classes start with an uppercase letter.
Here's the minimal code I used with SBT to get a working example.
Initializing:
~/test$ sbt
Project does not exist, create new project? (y/N/s) y
Name: test
Organization: test
Version [1.0]:
Scala version [2.7.7]: 2.8.1
sbt version [0.7.4]:
~/test$ cat project/build/TestProject.scala
import sbt._
class TestProject(info: ProjectInfo) extends DefaultProject(info) {
val dvers = "0.8.0"
val http = "net.databinder" %% "dispatch-http" % dvers
}
~/test$ cat src/main/scala/GetList.scala
import dispatch.Http
import dispatch.HandlerVerbs._
object GetList {
def main(args: Array[String]){
Http("http://foo.com/" >>> System.out)
}
}
~/test# sbt update run