Skip to content

Commit 943d925

Browse files
committed
Add MSSQL dialect
1 parent ec4d13d commit 943d925

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package scalasql.dialects
2+
3+
import scalasql.core.{Aggregatable, DbApi, DialectTypeMappers, Expr, TypeMapper}
4+
import scalasql.operations
5+
import scalasql.core.SqlStr.SqlStringSyntax
6+
import scalasql.operations.{ConcatOps, MathOps, TrimOps}
7+
8+
trait MsSqlDialect extends Dialect {
9+
protected def dialectCastParams = false
10+
11+
override implicit def IntType: TypeMapper[Int] = new MsSqlIntType
12+
class MsSqlIntType extends IntType { override def castTypeString = "INT" }
13+
14+
override implicit def StringType: TypeMapper[String] = new MsSqlStringType
15+
class MsSqlStringType extends StringType { override def castTypeString = "VARCHAR" }
16+
17+
override implicit def ExprStringOpsConv(v: Expr[String]): MsSqlDialect.ExprStringOps[String] =
18+
new MsSqlDialect.ExprStringOps(v)
19+
20+
override implicit def ExprBlobOpsConv(
21+
v: Expr[geny.Bytes]
22+
): MsSqlDialect.ExprStringLikeOps[geny.Bytes] =
23+
new MsSqlDialect.ExprStringLikeOps(v)
24+
25+
implicit def ExprAggOpsConv[T](v: Aggregatable[Expr[T]]): operations.ExprAggOps[T] =
26+
new MsSqlDialect.ExprAggOps(v)
27+
28+
override implicit def DbApiOpsConv(db: => DbApi): MsSqlDialect.DbApiOps =
29+
new MsSqlDialect.DbApiOps(this)
30+
}
31+
32+
object MsSqlDialect extends MsSqlDialect {
33+
class DbApiOps(dialect: DialectTypeMappers)
34+
extends scalasql.operations.DbApiOps(dialect)
35+
with ConcatOps
36+
with MathOps
37+
38+
class ExprAggOps[T](v: Aggregatable[Expr[T]]) extends scalasql.operations.ExprAggOps[T](v) {
39+
def mkString(sep: Expr[String] = null)(implicit tm: TypeMapper[T]): Expr[String] = {
40+
val sepRender = Option(sep).getOrElse(sql"''")
41+
v.aggregateExpr(expr => implicit ctx => sql"STRING_AGG($expr + '', $sepRender)")
42+
}
43+
}
44+
45+
class ExprStringOps[T](v: Expr[T]) extends ExprStringLikeOps(v) with operations.ExprStringOps[T]
46+
class ExprStringLikeOps[T](protected val v: Expr[T])
47+
extends operations.ExprStringLikeOps(v)
48+
with TrimOps {
49+
50+
override def +(x: Expr[T]): Expr[T] = Expr { implicit ctx => sql"($v + $x)" }
51+
52+
override def startsWith(other: Expr[T]): Expr[Boolean] = Expr { implicit ctx =>
53+
sql"($v LIKE $other + '%')"
54+
}
55+
56+
override def endsWith(other: Expr[T]): Expr[Boolean] = Expr { implicit ctx =>
57+
sql"($v LIKE '%' + $other)"
58+
}
59+
60+
override def contains(other: Expr[T]): Expr[Boolean] = Expr { implicit ctx =>
61+
sql"($v LIKE '%' + $other + '%')"
62+
}
63+
64+
override def length: Expr[Int] = Expr { implicit ctx => sql"LEN($v)" }
65+
66+
override def octetLength: Expr[Int] = Expr { implicit ctx => sql"DATALENGTH($v)" }
67+
68+
def indexOf(x: Expr[T]): Expr[Int] = Expr { implicit ctx => sql"CHARINDEX($x, $v)" }
69+
def reverse: Expr[T] = Expr { implicit ctx => sql"REVERSE($v)" }
70+
}
71+
}

scalasql/src/package.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ package object scalasql {
5555

5656
val SqliteDialect = dialects.SqliteDialect
5757
type SqliteDialect = dialects.SqliteDialect
58+
59+
val MsSqlDialect = dialects.MsSqlDialect
60+
type MsSqlDialect = dialects.MsSqlDialect
5861
}

0 commit comments

Comments
 (0)