ç·´ç¿ç¨ã®ãã¼ãã«
create table articles( id integer primary key, title varchar(20) not null, author varchar(20), content text not null );
åå§çãªæ¹æ³
Javaã¨åãæ¹æ³ã§ã´ãªã´ãªæ¸ãã
import java.sql.DriverManager import java.sql.ResultSet import java.sql.Statement import java.sql.Connection fun main(args: Array<String>) { val articles = findAllArticles() println(articles) } fun findAllArticles(): List<Article> { var conn: Connection? = null var statement: Statement? = null var resultSet: ResultSet? = null val articleListBuilder = ImmutableArrayListBuilder<Article>() try { conn = DriverManager.getConnection("jdbc:postgresql://localhost/sample") statement = conn?.createStatement() resultSet = statement?.executeQuery("select * from articles") while(resultSet?.next() ?: false) { articleListBuilder.add(Article( resultSet!!.getLong(1), resultSet!!.getString(2)!!, resultSet!!.getString(3) ?: "", resultSet!!.getString(4)!! )) } } finally { resultSet?.close() statement?.close() conn?.close() } return articleListBuilder.build() } data class Article( val id: Long, val title: String, val author: String, val content: String )
Javaããã¯ç°¡åã«æ¸ããããã§ãã¯ä¾å¤ããªãã ãã§ã ãã¶ã¹ãããªãããªãã
Connection
ã¨ãã®close()
ã¯ã¡ãã£ã¨é¢åèããã©ä»æ¹ãªããKotlinæ¨æºã©ã¤ãã©ãªã«ã¯Closeable
ã®æ¡å¼µé¢æ°use
ããã£ã¦ãããã使ãã°èªåã§ã¯ãã¼ãºæ¸ããªãã¦ããããã ãã©ãConnection
ã¨ãã¯Closeable
ãããªãã
Kotlinæ¨æºã©ã¤ãã©ãªã使ãæ¹æ³
æ¨æºã©ã¤ãã©ãªãã©ããã¯ããããªããã©ãããã¸ã§ã¯ãKotlinã®ä¸é¨ã¨ãã¦éçºããã¦ãããã±ã¼ã¸kotlin.jdbc
ã使ãæ¹æ³ã
使ãã«ã¯ãªãã¸ããªããã¯ãã¼ã³ãã¦ãã¯ã©ã¹ãã¹ãéãå¿
è¦ãããã
import kotlin.jdbc.getConnection import kotlin.jdbc.use import kotlin.jdbc.useSql import kotlin.jdbc.iterator fun main(args: Array<String>) { val articles = findAllArticles() println(articles) } fun findAllArticles(): List<Article> { val articleListBuilder = ImmutableArrayListBuilder<Article>() getConnection("jdbc:postgresql://localhost/sample").use { // useSql -> use ã«ãªãã¼ã äºå® it.createStatement()?.useSql { it.executeQuery("select * from articles").use { it.iterator().forEach { articleListBuilder.add(Article( it.getLong(1), it.getString(2)!!, it.getString(3) ?: "", it.getString(4)!! )) } } } } return articleListBuilder.build() } data class Article( val id: Long, val title: String, val author: String, val content: String )
try-catchãã¯ãã¼ãºãvar
ãæ¶ãã¦ã¤ã¤æããã§ãããã ãã
ã¡ãªã¿ã«Statement
ã®æ¡å¼µé¢æ°useSql
ã¯use
ã«ãªãã¼ã äºå®ï¼ã¡ãã£ã¨æ®å¿µãªãã°ããããããï¼ã
ãµã¼ããã¼ãã£è£½ã©ã¤ãã©ãªã使ãæ¹æ³
Kotlinã§SQLãæèãããªãããã®ãµã¼ããã¼ãã£è£½ã©ã¤ãã©ãªãããã®ã§ä½¿ã£ã¦ã¿ãã Exposedã£ã¦ããã©ã¤ãã©ãªã§ãä¸å¿ãããã¿ã¤ãã¨ããä½ç½®ä»ãã 軽éãã売ãã«ãã¦ãã®ããªãããã¡ãã£ã¨ã«ãã³ããã§ããããªæãã¯ããã ãã¨ãããã¿ã¤ãã£ã¦ãã¨ãªã®ã§ãã¾ã ã¾ã æ©è½ãå å®ãã¦ãªãã§ãã
使ãããã«ã¯ãªãã¸ããªããå¼ã£å¼µã£ã¦æ¥ã¦ãã¯ã©ã¹ãã¹ã¨ããä¸æããã¨è¨å®ããã
import kotlin.sql.Database import kotlin.sql.Table import kotlin.sql.integer import kotlin.sql.varchar import kotlin.sql.template fun main(args: Array<String>) { val articles = findAllArticles() println(articles) } fun findAllArticles(): List<Article> { val articleListBuilder = ImmutableArrayListBuilder<Article>() Database("jdbc:postgresql://localhost/sample", "org.postgresql.Driver").withSession { Articles.all().forEach { val (id, title, author) = it articleListBuilder.add(Article( id.toLong(), title, author, "" )) } } return articleListBuilder.build() } object Articles: Table() { val id = integer("id").primaryKey() val title = varchar("title", length = 20) val author = varchar("author", length = 20) val content = varchar("content", length = 3000) val all = template(id, title, author) // 注 } data class Article( val id: Long, val title: String, val author: String, val content: String )
ã注ãã®ç®æãããã§ã¯select id, title, author
ç¸å½ã®SQLãæ§ç¯ããå
ãä½ã£ã¦ããã ãã©ããã®template
é¢æ°ã¯ç¾ç¶ã§ã¯æ大3ã¤ã®å¼æ°ããåããªãã£ã½ãããã®ããä»åã®ä¾ã§ã¯content
ãåãã®ã諦ããããã®ä»ã«ã¯ã«ã©ã ã®ãã¼ã¿åãæ´æ°å¤ã¨æååã®2種é¡ãããªããªã©ãä½ãå§ãã¦éããªãæããä¼ãã£ã¦ããã
Articles.all().forEach
ã®ãããã¯ã®ä¸ã§ãit
(ããã§ã¯Triple
ãªãã¸ã§ã¯ã)ã®åå¤ãå¤é宣è¨ã使ã£ã¦åååã«ãã¤ã³ããã¦ããã
ææ³
ã«ãã³ããã¦ä¾¿å©ãªã©ã¤ãã©ãªãä½ããã¨æãã¾ããã