Playframework 2.1.x Database Error - database

I try to add a database to my play framework app but I have several problems. I used the template (from Typesafe Activator) called "hello-play-2_3-scala". I always get an error if i try to insert 'import play.api.db._'. I looked at this instruction for the play database, but the main problem is I haven't any Build.scala file in my project folder. Also I cannot create once, just Java Files (I use Intellj Idea). I hope someone can help me.
Thanks!
Greetings Chryb.

You should add jdbc dependency inside build.sbt file. It should look like this:
name := """hello-play-2_3-scala"""
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
PlayKeys.jdbc,
"org.webjars" %% "webjars-play" % "2.3-M1",
"org.webjars" % "bootstrap" % "2.3.1",
"org.webjars" % "requirejs" % "2.1.11-1"
)
lazy val root = (project in file(".")).addPlugins(PlayScala)

Related

ScalaTest HTML report is malformatted

I can generate the ScalaTest results in HTML but they seem malformatted like in the pic (there's no table to select the test classes)
report
I have the following versions of scalatest/pegdown:
"org.scalatest" %% "scalatest" % "3.0.0" % "test,it",
"org.pegdown" % "pegdown" % "1.0.2" % "test,it"
Have anyone experienced this before?
The problem was I was setting the scalatest version to 3.0.0 but another version, 3.0.8 was being transitively imported on the project so I added an exclusion rule to this dependency.

Julia - getting the version number of my module

I am writing a package for Julia and within the package I would like to be able to print the my package current version number. I can of course do that manually but I was looking for a way to read it directly from the module. the version number is written in the Project.toml file, but I have not been able to find a way to read it from the code. Any ideas?
You could use something like
julia> using Pkg
julia> loadedversion(m::Module) = VersionNumber(Pkg.TOML.parsefile(joinpath(string(first(methods(m.eval)).file), "..", "..", "Project.toml"))["version"])
loadedversion (generic function with 1 method)
julia> loadedversion(Atom)
v"0.12.8"
Some error handling would be prudent, of course, but that will give you the version of a package that's actually loaded (instead of the one specified in the current environment).
Possibly -
using Pkg
Pkg.TOML.parse(read("Project.toml", String))["version"]
-> "0.0.1"
using Pkg
function pkginfo(pkgname::AbstractString, key = "version")
# Check if Package Project
proj = Pkg.TOML.parsefile(Base.current_project())
proj["name"] == pkgname && return proj[key]
# If not in Project, pkgname must be in deps. Look in Manifest.toml
# An error will be thrown if Manifest.toml is missing
man = Pkg.TOML.parsefile(joinpath(Base.current_project() |> dirname, "Manifest.toml"));
for (pkg, info) in man
pkginfo = info |> first # Play with 'man' to see why
pkg == pkgname && return pkginfo[key]
end
end
This will give you the version (as string) of a package (or other data) in the current enviroment. But a package could be in a deeper enviroment in the stack, so...
Could it be that this is an answer?
using Pkg
Pkg.project().version
v"0.1.2"

Apache Flink: Cannot find compatible factory for specified execution.target (=local)

I've decided to experiment with apache flink a bit. I decided to use scala console (or more precisely http://ammonite.io/) to read some stuff from csv file and print it locally... just to debug end experiments.
import $ivy.`org.apache.flink:flink-csv:1.10.0`
import $ivy.`org.apache.flink::flink-scala:1.10.0`
import org.apache.flink.api.scala._
import org.apache.flink.api.scala.extensions._
val env = ExecutionEnvironment.createLocalEnvironment()
val lines = env.readCsvFile[(String, String, String)]("/home/slovic/Dokumenty/test.csv")
lines.collect()
//java.lang.NullPointerException: Cannot find compatible factory for specified execution.target (=local)
//org.apache.flink.util.Preconditions.checkNotNull(Preconditions.java:104)
//org.apache.flink.api.java.ExecutionEnvironment.executeAsync(ExecutionEnvironment.java:937)
//org.apache.flink.api.java.ExecutionEnvironment.execute(ExecutionEnvironment.java:860)
//org.apache.flink.api.java.ExecutionEnvironment.execute(ExecutionEnvironment.java:844)
//org.apache.flink.api.scala.ExecutionEnvironment.execute(ExecutionEnvironment.scala:495)
//org.apache.flink.api.scala.DataSet.collect(DataSet.scala:739)
//ammonite.$sess.cmd24$.<init>(cmd24.sc:1)
//ammonite.$sess.cmd24$.<clinit>(cmd24.sc)
What I need to do to run this code locally? (tested with scala 2.11 & 2.12)
EDIT: SOLLUTION BY Piyush_Rana
We need additional import:
import $ivy.`org.apache.flink::flink-streaming-scala:1.10.0` //Piyush_Rana's advice. !!!FIX!!!
I also got the same error and figured out that was missing one dependency -
val flinkVersion = "1.10.0"
"org.apache.flink" %% "flink-streaming-scala" % flinkVersion,
or in ammonite repl:
import $ivy.`org.apache.flink::flink-streaming-scala:1.10.0`
You didnt execute the flink program .
Try to add execute command at the end.
env.execute("unique name")

MultipleMatchesException while using react webjar

I was using react with play-framework from webjar. While running the code MultipleMatchesException exception is thrown. After looking into the issue, I find react.production.min.js file in two locations. One in cjs and other in umd.
build.sbt line: "org.webjars" % "react" % "16.3.2"
What is the difference between cjs and umd? Why are both being downloaded and how to specify only one to use???
Thanks in advance
CJS = Common JS
UMD = Universal Module Definition
You need to be more specific in your webjars-locator usage, so something like:
#webJarsUtil.locate("umd/react.production.min.js").script()

Play Slick config

I'm trying to get started with play and slick.
Strategy; take hello-slick-3.1 project from the activator tutorials.
If works fine with the H2 in memory database. I want to connect to a sql server. After a battle I have some configuration which appear to connect using jdts.
In application .conf
driver=net.sourceforge.jtds.jdbc.Driver
url="jdbc:jtds:sqlserver://%%%%:1433;databaseName=%%%%;user=%%%;password=%%%%%"
This is using the jtds driver instead of
com.typesafe.slick.driver.ms.SQLServerDriver
Which appears to have been made deliberately difficult to use. I have not found a sucessful config with it. JTDS manages to create the 'suppliers' table based on it's schema, but all subsequent requests fall over with a nebulous 'data truncation' message;
object HelloSlick extends App {
val db = Database.forConfig("sqlServerLocal")
try {
// The query interface for the Suppliers table
val suppliers: TableQuery[Suppliers] = TableQuery[Suppliers]
val setupAction: DBIO[Unit] = DBIO.seq(
// Create the schema by combining the DDLs for the Suppliers and Coffees
// tables using the query interfaces
//(suppliers.schema).create,
// Insert some suppliers
suppliers += (101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
suppliers += ( 49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460"),
suppliers += (150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966")
)
Telling me that
background log: info: 10:58:48.465 [sqlServerLocal-1] DEBUG slick.jdbc.JdbcBackend.statement - Preparing statement: insert into "SUPPLIERS" ("SUP_ID","SUP_NAME","STREET","CITY","STATE","ZIP") values (?,?,?,?,?,?)
background log: error: Exception in thread "main" java.sql.DataTruncation: Data truncation
Does anyone have any ideas? Is connecting to SQL server with slick - 3.1 even a sensible thing to attempt?
EDIT::
#szeiger makes the excellent point that I'm importing the wrong thing into the model classes. I had the old H2 driver import from the Hello Slick example, which should be replaced with this;
import com.typesafe.slick.driver.ms.SQLServerDriver.api._
In order to work as a SQL server language.
After making this change, the DB configuration which 'did something' no longer works, advertising this
background log: info: Running HelloSlick
background log: error: Exception in thread "main" java.lang.NoClassDefFoundError: slick/profile/BasicProfile$SimpleQL
I've tried altering the instantiation of the actual db variable to match the 'recommended' strategy suggested by szeiger.
val db = Database.forURL("jdbc:sqlserver://%%%:1433;user=%%%%;password=%%%%", driver="com.typesafe.slick.driver.ms.SQLServerDriver",
executor = AsyncExecutor("test1", numThreads=10, queueSize=1000))
Unfortunately, this fails like so;
background log: error: Exception in thread "main" java.lang.NoClassDefFoundError: slick/profile/BasicProfile$Implicits
with the jtds driver, it has the same error message with the slick MSSQL driver, and despite my best efforts, I cannot instantiate the MS JDBC driver. It's in a folder called 'lib' in the application , although I suspected that might be naive and the wrong place.
Managing this dependency through play-slick would be excellent. I had thought that I would be doing that via the inclusion of this line in SBT;
"com.typesafe.play" %% "play-slick" % "1.1.0",
but appears to have no effect in isolation, and I'm unsure how to configure this to reach the 'official' slick MS SQL driver.
EDIT 2:: Finally, this appears to build what I want.
name := """hello-slick-3.1"""
lazy val root = (project in file(".")).enablePlugins(PlayScala)
resolvers += "Typesafe Releases" at "http://repo.typesafe.com/typesafe/maven-releases/"
resolvers += "Scalaz Bintray Repo" at "https://dl.bintray.com/scalaz/releases"
resolvers += Resolver.url("Typesafe Ivy releases", url("https://repo.typesafe.com/typesafe/ivy-releases"))(Resolver.ivyStylePatterns)
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.1.0",
"com.typesafe.slick" %% "slick-extensions" % "3.1.0",
"com.typesafe.play" %% "play-slick" % "1.1.0",
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
)
I had lots of problems because I forgot to include the correct resolvers.
Don't forget to include the correct resolvers :-).
You're confusing Slick drivers and JDBC drivers. Which Slick driver are you using? The imports are missing from your snippet but there should be something like import com.typesafe.slick.driver.ms.SQLServerDriver.api._. Using a wrong driver here could explain String columns being created as VARCHAR(1).
The recommended way of configuring database connections in Slick 3.1 is via DatabaseConfig, which allows you to configure the Slick driver together with the actual connection parameters. When you're writing a Play app, use the play-slick plugin instead to handle database connections. It is also based on the DatabaseConfig syntax.
The alternative to net.sourceforge.jtds.jdbc.Driver would be Microsoft's own JDBC driver, com.microsoft.sqlserver.jdbc.SQLServerDriver which is available as a separate download from Microsoft. You have to manually add sqljdbc4.jar to your build to make this work.
BTW, Slick 3.2 will change terminology and use the word "driver" exclusively for JDBC drivers. Slick drivers will be called "profiles" in 3.2.
After bonus pain, it looks like slick creates columns in SQL Server as type 'varchar(1)'.
Which can't hold very much data.
Not using Slick to create the tables, and commenting out these lines then means that the intro works as advertised.

Resources