SlideShare a Scribd company logo
1 of 54
ScalaQuery
    flatMap(Oslo)
Trond Marius Øvstetun



        © Mesan AS
Trond Marius Øvstetun
• Sjefskonsulentog
 fagleder i Mesan
• Lidenskapelig
 utvikler, arkitekt,
 teamleder ++
• Java,    Scala, HTML/js
• Alltid
       på jakt etter
 bedre, enklere
 måter

                       © Mesan AS
Data
• Vi   har behov for å lagre data




                  © Mesan AS
Data
• Vi   har behov for å lagre data
• Ikke
     alle bruker eller kan bruke
 NoSQL




                  © Mesan AS
Data
• Vi   har behov for å lagre data
• Ikke
     alle bruker eller kan bruke
 NoSQL
 • konservativ   it-avdeling?
 • legacy   – eksisterende data
 • andre    behov?



                     © Mesan AS
Data
• Vi   har behov for å lagre data
• Ikke
     alle bruker eller kan bruke
 NoSQL
 • konservativ   it-avdeling?
 • legacy   – eksisterende data
 • andre    behov?
 • av   og til er relasjonsmodellen riktig

                     © Mesan AS
SQL
• Hvordan jobber vi med RDBMS på
 best mulig måte?




              © Mesan AS
SQL
• Hvordan jobber vi med RDBMS på
 best mulig måte?


• JDBC?




              © Mesan AS
SQL
• Hvordan jobber vi med RDBMS på
 best mulig måte?


• JDBC?

• ORM?




              © Mesan AS
SQL
• Hvordan jobber vi med RDBMS på
 best mulig måte?


• JDBC?

• ORM?

 • Hibernate?



                © Mesan AS
SQL
• Hvordan jobber vi med RDBMS på
 best mulig måte?


• JDBC?

• ORM?

 • Hibernate?

 • Anorm?   Mapper? Record?

                 © Mesan AS
ScalaQuery




   © Mesan AS
ScalaQuery
A type-safe database API for
           Scala



           © Mesan AS
ScalaQuery
A type-safe database API for
           Scala



           © Mesan AS
En enkel domenemodell

    «enumeration»
                           Artist
      Genre
                                                          Album
                    name : String
 Rock                                      1
                    biography : Text               name : String
 Pop                                           *
 Classic            mainGenre : Genre              release : Date
 Blues              founded : Date                 rating : Option[Int]
 Rap                split : Option[Date]
                                                                 1
 HipHop
 Alternative                     *



                                                             *


                             *                             Song
                                                   name : String
                          Person                   duration : Int
                    firstname : String              trackNumber : Int
                    lastname : String
                    biography : Text




                             © Mesan AS
Koble til en database
  • Med javax.sql.DataSource



def myDs : DataSource = {...}

val db = Database.forDataSource(myDs)




                 © Mesan AS
Koble til en database
• Alternativt




                © Mesan AS
Koble til en database
  • Alternativt



Database.forName("jdbc/my_ds") //JNDI

Database.forURL("jdbc:h2:test")




                  © Mesan AS
Gjøre noe med databasen

         val db = {...}
         db withSession {
           do_something()
         }

import Database.threadLocalSession



               © Mesan AS
StaticQuery
Håndkodet SQL mot databasen




           © Mesan AS
Spørringer


val q = StaticQuery[Int] +
        "select count(id) from Artists"
val count = q.first()




                 © Mesan AS
Spørringer

val q = StaticQuery[Int, (Int, String)] +
      "select id, name from Artists where id = ?"


val (id,name) = q(1001).first
val artist : (Int, String) = q(1001).first




                      © Mesan AS
Mapping av resultat

case class Artist(id:Int, name:String)
val q = StaticQuery.query[Int, (Int, String)](
  "select id, name from Artists where id = ?")


val qMapped = q.mapResult[Artist]({
  case (a, b) => Artist(a, b)
  })


var a:Artist = qMapped(1001).first()
                    © Mesan AS
Implisitt mapping av

case class Artist(id:Int, name:String)
implicit val artMapper = GetResult(
  (r: PositionedResult) => new Artist(r<<, r<<))


val q = StaticQuery.query[Int, Art](
  "select id, name from Artists where id = ?")
val a:Artist = q(1001).first



                     © Mesan AS
Insert / Update

import org.scalaquery.simple.StaticQuery._


val q = query[(String, String), Int](
  "insert into Artists(name, biography) "+
  "values (?, ?)")
val q2 = q("Seigmen", "")
q2.execute()



                   © Mesan AS
ScalaQuery




   © Mesan AS
ScalaQuery
På tide å få noe igjen?
      Hva om ...



        © Mesan AS
En enkel domenemodell

    «enumeration»
                           Artist
      Genre
                                                          Album
                    name : String
 Rock                                      1
                    biography : Text               name : String
 Pop                                           *
 Classic            mainGenre : Genre              release : Date
 Blues              founded : Date                 rating : Option[Int]
 Rap                split : Option[Date]
                                                                 1
 HipHop
 Alternative                     *



                                                             *


                             *                             Song
                                                   name : String
                          Person                   duration : Int
                    firstname : String              trackNumber : Int
                    lastname : String
                    biography : Text




                             © Mesan AS
Mapping til en tabell


object Artists extends Table[
  (Int, String, String,
    Genre.Genre, Date, Option[Date])
  ]("ARTISTS") {
  ...
}
                                     Artist
                              name : String
                              biography : Text
                              mainGenre : Genre
                              founded : Date
                © Mesan AS
                              split : Option[Date]
Mapping til en tabell

object Artists extends Table (...) {
    def id = column[Int]("ID", O PrimaryKey)
    def name = column[String]("NAME")
    def biography = column[String]("BIOGRAPHY")
    def maingenre = column[Genre.Genre]("MAINGENRE")
    def founded = column[Date]("FOUNDED")
    def split = column[Option[Date]]("SPLIT")
}                                                  Artist
                                            name : String
                                            biography : Text
                                            mainGenre : Genre
                                            founded : Date
                        © Mesan AS
                                            split : Option[Date]
Mapping til en tabell
object Artists extends Table (...) {
  ...
  def * = id ~ name ~ biography ~
          maingenre ~ founded ~ split
  def i = name ~ biography ~
          maingenre ~ founded ~ split
}
                                     Artist
                              name : String
                              biography : Text
                              mainGenre : Genre
                              founded : Date
                © Mesan AS
                              split : Option[Date]
Mapping med case-klasse

case class Artist(
    id:Int,name:String, bio:String,
    mainGenre:Genre.Genre, founded:Date,
    split:Option[Date])


object Artists extends Table[Artist]("ARTISTS") {
    def * = id ~ .. ~ split <> (Artist, Artist.unapply _)
}                                                    Artist
                                              name : String
                                              biography : Text
                                              mainGenre : Genre
                                              founded : Date
                           © Mesan AS
                                              split : Option[Date]
Bruke egne typer
  column[Genre.Genre]("MAINGENRE")

    object Genre extends Enumeration {
        type Genre = Value                  «enumeration»
                                              Genre

        val Rock = Value(1)              Rock
                                         Pop
                                         Classic

        ...
                                         Blues
                                         Rap
                                         HipHop
                                         Alternative
        val Alternative = Value(7)
    }
implicit val genreMapper =
  MappedTypeMapper.base[Genre.Genre, Int](
    _.id, Genre(_))
                    © Mesan AS
Bruke egne typer
    column[Duration]("DURATION")

case class Duration(mins:Int, secs:Int)

implicit object durationMapper extends
     MappedTypeMapper[Duration, Int]
     with BaseTypeMapper[Duration]
     with NumericTypeMapper {
  def map(dur: Duration) =
    dur.mins * 60 + dur.secs
  def comap(secs: Int) =
    Duration(secs / 60, secs % 60)
}                 © Mesan AS
Inserts
    val i: Int = Artists.i.insert(
      ("Seigmen", "", Genre.Rock,
        date("1989-12-27"),
        Some(date("2008-06-22"))))
    // i is number of rows affected


val artists : List[(...)] = ...
val i = Artists.i.insertAll(artists :_*)
// i is Option[Int] - num affected
                 © Mesan AS
Enkle spørringer
def findArtist(id:Int) : Option[(...)] = {
    val q = Artists.createFinderBy(_.id)
    q(id).firstOption
}




                        © Mesan AS
Enkle spørringer
def findArtist(id:Int) : Option[(...)] = {
    val q = Artists.createFinderBy(_.id)
    q(id).firstOption
}


def highRatedAlbums : List[...] = {
  val q = Albums.filter(_.rating === (Six:Rating))
  q.list
}
def unRatedAlbums : List[...] = {
  val q = Albums.filter(_.rating isNull)
  q.list
}                   © Mesan AS
Bruk av spørringer


val resList = q.list
val resOption = q.firstOption
val res = q.first




            © Mesan AS
Mer generelle spørringer

val q1 =
  Artists.filter(_.maingenre === Genre.Rock)




                   © Mesan AS
Mer generelle spørringer

val q1 =
  Artists.filter(_.maingenre === Genre.Rock)



val q = for {
  a <- Artists if a.maingenre === Genre.Rock
} yield a



                   © Mesan AS
Generell spørring

 val aQuery = for {
     x1 <- generator
     x2 <- generator
     ...
     xN <- generator
     guard
   } yield projection




        © Mesan AS
Parametrisert

val qArtist = for {
  n <- Parameters[String]
  a <- Artists if a.name === n
} yield a.id ~ a.name




            © Mesan AS
Parametrisert

val qArtist = for {
  n <- Parameters[String]
  a <- Artists if a.name === n
} yield a.id ~ a.name


val qTool = qArtist("Tool")
val (id, name) = qTool.first




            © Mesan AS
Spørringer
• Er   lazy
• Er   immutable
 • Dele   og gjenbruke
 • Byggeklosser




                   © Mesan AS
Spørringer
  • Er   lazy
  • Er   immutable
   • Dele   og gjenbruke
   • Byggeklosser


val q1 = for (a <- Artists) yield a
val q2 = for (a <- q1) yield a.id ~ a.name
val q3 = q2.take(10).drop(50)

                     © Mesan AS
Update


val q = for {
  a <- Artists if a.id === 1001
} yield a.name

q.update("updated") // num affected




               © Mesan AS
Delete


val q = for {
  a <- Albums if a.id === 1001
} yield a

q.delete




            © Mesan AS
Relasjoner /
object Albums extends Table[(...)]("ALBUMS") {
    def artist_id = column[Int]("ARTIST_ID")
    def artist =
      foreignKey("albums_artists_fk", artist_id, Artists)(_.id)


    def * = id ~ name ~ release ~ rating ~ artist_id
}


                          Artist
                                                              Album
                   name : String
                                           1
                   biography : Text                    name : String
                                                  *
                   mainGenre : Genre                   release : Date
                   founded : Date                      rating : Option[Int]
                   split : Option[Date]



                                          © Mesan AS
Joins
                                               Artist

            val q = for {                                                     Album
                                        name : String
                                                               1
                                        biography : Text               name : String
                                                                   *
                                        mainGenre : Genre              release : Date

              al <- Albums              founded : Date
                                        split : Option[Date]
                                                                       rating : Option[Int]



              a <- Artists
              if al.artist_id === a.id
            } yield a.name ~ al.name
            val q =    for {
              al <-    Albums
              a <-     al.artist
            } yield    a.name ~ al.name
val q = for {
  (al, a) <- Albums innerJoin Artists on (_.artist_id is _.id)
} yield a.name ~ al.name
                           © Mesan AS
Veien videre?
 ScalaQuery => SLICK

Scala Language Integrated



         © Mesan AS
SLICK
• Generiskrammeverk for
 spørringer mot data
• Ulike   backends
 • SQL,
      NoSQL, json, WebServices,
  XML ...
• Nærmere “vanlig” Scala-kode,
 backend som scala Collection


                 © Mesan AS
Q&A




© Mesan AS
Q&A




© Mesan AS
takk for meg
           tmo@mesan.no
             @ovstetun
github.com/ovstetun/scala-persistence




               © Mesan AS

More Related Content

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

ScalaQuery

  • 1. ScalaQuery flatMap(Oslo) Trond Marius Øvstetun © Mesan AS
  • 2. Trond Marius Øvstetun • Sjefskonsulentog fagleder i Mesan • Lidenskapelig utvikler, arkitekt, teamleder ++ • Java, Scala, HTML/js • Alltid på jakt etter bedre, enklere måter © Mesan AS
  • 3. Data • Vi har behov for å lagre data © Mesan AS
  • 4. Data • Vi har behov for å lagre data • Ikke alle bruker eller kan bruke NoSQL © Mesan AS
  • 5. Data • Vi har behov for å lagre data • Ikke alle bruker eller kan bruke NoSQL • konservativ it-avdeling? • legacy – eksisterende data • andre behov? © Mesan AS
  • 6. Data • Vi har behov for å lagre data • Ikke alle bruker eller kan bruke NoSQL • konservativ it-avdeling? • legacy – eksisterende data • andre behov? • av og til er relasjonsmodellen riktig © Mesan AS
  • 7. SQL • Hvordan jobber vi med RDBMS på best mulig måte? © Mesan AS
  • 8. SQL • Hvordan jobber vi med RDBMS på best mulig måte? • JDBC? © Mesan AS
  • 9. SQL • Hvordan jobber vi med RDBMS på best mulig måte? • JDBC? • ORM? © Mesan AS
  • 10. SQL • Hvordan jobber vi med RDBMS på best mulig måte? • JDBC? • ORM? • Hibernate? © Mesan AS
  • 11. SQL • Hvordan jobber vi med RDBMS på best mulig måte? • JDBC? • ORM? • Hibernate? • Anorm? Mapper? Record? © Mesan AS
  • 12. ScalaQuery © Mesan AS
  • 13. ScalaQuery A type-safe database API for Scala © Mesan AS
  • 14. ScalaQuery A type-safe database API for Scala © Mesan AS
  • 15. En enkel domenemodell «enumeration» Artist Genre Album name : String Rock 1 biography : Text name : String Pop * Classic mainGenre : Genre release : Date Blues founded : Date rating : Option[Int] Rap split : Option[Date] 1 HipHop Alternative * * * Song name : String Person duration : Int firstname : String trackNumber : Int lastname : String biography : Text © Mesan AS
  • 16. Koble til en database • Med javax.sql.DataSource def myDs : DataSource = {...} val db = Database.forDataSource(myDs) © Mesan AS
  • 17. Koble til en database • Alternativt © Mesan AS
  • 18. Koble til en database • Alternativt Database.forName("jdbc/my_ds") //JNDI Database.forURL("jdbc:h2:test") © Mesan AS
  • 19. Gjøre noe med databasen val db = {...} db withSession { do_something() } import Database.threadLocalSession © Mesan AS
  • 20. StaticQuery Håndkodet SQL mot databasen © Mesan AS
  • 21. Spørringer val q = StaticQuery[Int] + "select count(id) from Artists" val count = q.first() © Mesan AS
  • 22. Spørringer val q = StaticQuery[Int, (Int, String)] + "select id, name from Artists where id = ?" val (id,name) = q(1001).first val artist : (Int, String) = q(1001).first © Mesan AS
  • 23. Mapping av resultat case class Artist(id:Int, name:String) val q = StaticQuery.query[Int, (Int, String)]( "select id, name from Artists where id = ?") val qMapped = q.mapResult[Artist]({ case (a, b) => Artist(a, b) }) var a:Artist = qMapped(1001).first() © Mesan AS
  • 24. Implisitt mapping av case class Artist(id:Int, name:String) implicit val artMapper = GetResult( (r: PositionedResult) => new Artist(r<<, r<<)) val q = StaticQuery.query[Int, Art]( "select id, name from Artists where id = ?") val a:Artist = q(1001).first © Mesan AS
  • 25. Insert / Update import org.scalaquery.simple.StaticQuery._ val q = query[(String, String), Int]( "insert into Artists(name, biography) "+ "values (?, ?)") val q2 = q("Seigmen", "") q2.execute() © Mesan AS
  • 26. ScalaQuery © Mesan AS
  • 27. ScalaQuery På tide å få noe igjen? Hva om ... © Mesan AS
  • 28. En enkel domenemodell «enumeration» Artist Genre Album name : String Rock 1 biography : Text name : String Pop * Classic mainGenre : Genre release : Date Blues founded : Date rating : Option[Int] Rap split : Option[Date] 1 HipHop Alternative * * * Song name : String Person duration : Int firstname : String trackNumber : Int lastname : String biography : Text © Mesan AS
  • 29. Mapping til en tabell object Artists extends Table[ (Int, String, String, Genre.Genre, Date, Option[Date]) ]("ARTISTS") { ... } Artist name : String biography : Text mainGenre : Genre founded : Date © Mesan AS split : Option[Date]
  • 30. Mapping til en tabell object Artists extends Table (...) { def id = column[Int]("ID", O PrimaryKey) def name = column[String]("NAME") def biography = column[String]("BIOGRAPHY") def maingenre = column[Genre.Genre]("MAINGENRE") def founded = column[Date]("FOUNDED") def split = column[Option[Date]]("SPLIT") } Artist name : String biography : Text mainGenre : Genre founded : Date © Mesan AS split : Option[Date]
  • 31. Mapping til en tabell object Artists extends Table (...) { ... def * = id ~ name ~ biography ~ maingenre ~ founded ~ split def i = name ~ biography ~ maingenre ~ founded ~ split } Artist name : String biography : Text mainGenre : Genre founded : Date © Mesan AS split : Option[Date]
  • 32. Mapping med case-klasse case class Artist( id:Int,name:String, bio:String, mainGenre:Genre.Genre, founded:Date, split:Option[Date]) object Artists extends Table[Artist]("ARTISTS") { def * = id ~ .. ~ split <> (Artist, Artist.unapply _) } Artist name : String biography : Text mainGenre : Genre founded : Date © Mesan AS split : Option[Date]
  • 33. Bruke egne typer column[Genre.Genre]("MAINGENRE") object Genre extends Enumeration { type Genre = Value «enumeration» Genre val Rock = Value(1) Rock Pop Classic ... Blues Rap HipHop Alternative val Alternative = Value(7) } implicit val genreMapper = MappedTypeMapper.base[Genre.Genre, Int]( _.id, Genre(_)) © Mesan AS
  • 34. Bruke egne typer column[Duration]("DURATION") case class Duration(mins:Int, secs:Int) implicit object durationMapper extends MappedTypeMapper[Duration, Int] with BaseTypeMapper[Duration] with NumericTypeMapper { def map(dur: Duration) = dur.mins * 60 + dur.secs def comap(secs: Int) = Duration(secs / 60, secs % 60) } © Mesan AS
  • 35. Inserts val i: Int = Artists.i.insert( ("Seigmen", "", Genre.Rock, date("1989-12-27"), Some(date("2008-06-22")))) // i is number of rows affected val artists : List[(...)] = ... val i = Artists.i.insertAll(artists :_*) // i is Option[Int] - num affected © Mesan AS
  • 36. Enkle spørringer def findArtist(id:Int) : Option[(...)] = { val q = Artists.createFinderBy(_.id) q(id).firstOption } © Mesan AS
  • 37. Enkle spørringer def findArtist(id:Int) : Option[(...)] = { val q = Artists.createFinderBy(_.id) q(id).firstOption } def highRatedAlbums : List[...] = { val q = Albums.filter(_.rating === (Six:Rating)) q.list } def unRatedAlbums : List[...] = { val q = Albums.filter(_.rating isNull) q.list } © Mesan AS
  • 38. Bruk av spørringer val resList = q.list val resOption = q.firstOption val res = q.first © Mesan AS
  • 39. Mer generelle spørringer val q1 = Artists.filter(_.maingenre === Genre.Rock) © Mesan AS
  • 40. Mer generelle spørringer val q1 = Artists.filter(_.maingenre === Genre.Rock) val q = for { a <- Artists if a.maingenre === Genre.Rock } yield a © Mesan AS
  • 41. Generell spørring val aQuery = for { x1 <- generator x2 <- generator ... xN <- generator guard } yield projection © Mesan AS
  • 42. Parametrisert val qArtist = for { n <- Parameters[String] a <- Artists if a.name === n } yield a.id ~ a.name © Mesan AS
  • 43. Parametrisert val qArtist = for { n <- Parameters[String] a <- Artists if a.name === n } yield a.id ~ a.name val qTool = qArtist("Tool") val (id, name) = qTool.first © Mesan AS
  • 44. Spørringer • Er lazy • Er immutable • Dele og gjenbruke • Byggeklosser © Mesan AS
  • 45. Spørringer • Er lazy • Er immutable • Dele og gjenbruke • Byggeklosser val q1 = for (a <- Artists) yield a val q2 = for (a <- q1) yield a.id ~ a.name val q3 = q2.take(10).drop(50) © Mesan AS
  • 46. Update val q = for { a <- Artists if a.id === 1001 } yield a.name q.update("updated") // num affected © Mesan AS
  • 47. Delete val q = for { a <- Albums if a.id === 1001 } yield a q.delete © Mesan AS
  • 48. Relasjoner / object Albums extends Table[(...)]("ALBUMS") { def artist_id = column[Int]("ARTIST_ID") def artist = foreignKey("albums_artists_fk", artist_id, Artists)(_.id) def * = id ~ name ~ release ~ rating ~ artist_id } Artist Album name : String 1 biography : Text name : String * mainGenre : Genre release : Date founded : Date rating : Option[Int] split : Option[Date] © Mesan AS
  • 49. Joins Artist val q = for { Album name : String 1 biography : Text name : String * mainGenre : Genre release : Date al <- Albums founded : Date split : Option[Date] rating : Option[Int] a <- Artists if al.artist_id === a.id } yield a.name ~ al.name val q = for { al <- Albums a <- al.artist } yield a.name ~ al.name val q = for { (al, a) <- Albums innerJoin Artists on (_.artist_id is _.id) } yield a.name ~ al.name © Mesan AS
  • 50. Veien videre? ScalaQuery => SLICK Scala Language Integrated © Mesan AS
  • 51. SLICK • Generiskrammeverk for spørringer mot data • Ulike backends • SQL, NoSQL, json, WebServices, XML ... • Nærmere “vanlig” Scala-kode, backend som scala Collection © Mesan AS
  • 54. takk for meg tmo@mesan.no @ovstetun github.com/ovstetun/scala-persistence © Mesan AS

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. Herfra er det nesten bare kode - ROP ut om det er noe!!!\n
  16. Herfra er det nesten bare kode - ROP ut om det er noe!!!\n
  17. Herfra er det nesten bare kode - ROP ut om det er noe!!!\n
  18. &amp;#x201C;min faste&amp;#x201D; modell jeg bruker n&amp;#xE5;r jeg tester nye systemer/teknologier etc\nhar laget ogs&amp;#xE5; i mongo - fungerer str&amp;#xE5;lende der ogs&amp;#xE5;.\n
  19. \n
  20. \n
  21. \n
  22. settes typisk opp med cake/avhengigheter eller hvordan man &amp;#xF8;nsker\nLIFT: i boot\n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. hva om jeg forteller ScalaQuery noe om databasen min?\n
  30. \n
  31. starte med &amp;#xE5; definere strukturen p&amp;#xE5; tabellen\n
  32. legge til kolonnenne som finnes\n
  33. definere projeksjoner p&amp;#xE5; kolonnene som finnes\n* M&amp;#xC5; definere STJERNE - denne brukes til og fra mapping som default\n
  34. kan gj&amp;#xF8;res med caseklasser -&gt; her er resten som tidligere\n&lt;&gt; kaller eg til-og-fra - og er laget for akkurat case-klasser\n
  35. ENUMS i scala, kan gj&amp;#xF8;re tilsvarende med CASE OBJECTS\n
  36. mapping til/fra immutable verdiobjekter\n
  37. kaller insert p&amp;#xE5; en projeksjon - med samme struktur som projeksjonen selv!\ngir kompileringsfeil ved feil struktur\ninsert er eager - gj&amp;#xF8;res med en gang mot databasen\n
  38. \n
  39. vanligste operatorer -&gt; trigger databasen!\n
  40. kan ogs&amp;#xE5; skrives som...\n
  41. Generator == tabell (eller en fremmedn&amp;#xF8;kkel)\n
  42. === mappes til = i basen\nlike\nupper, st&amp;#xF8;rreEnn, mindreEnn etc - alt som finnes i databasen\n
  43. ingen av disse g&amp;#xE5;r mot basen f&amp;#xF8;r man faktisk kj&amp;#xF8;rer en list/first\n
  44. sp&amp;#xF8;rring representerer WHERE-delen\nyield-delen det som skal oppdateres - og inputstruktur til .update-kallet\n
  45. sp&amp;#xF8;rring representerer WHERE-delen\n
  46. ForeignKeyQuery\n
  47. \n
  48. \n
  49. kommer for scala 2.10, bruker makroer\n
  50. \n
  51. \n