Skip to content

Commit

Permalink
String ops improvement and test progress, big skunk fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Katrix committed Jun 1, 2024
1 parent f97145f commit ddf55c0
Show file tree
Hide file tree
Showing 20 changed files with 536 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ trait SqlQueryPlatform
SqlStringOps,
SqlValueSources,
SqlQueries,
SqlOperations
SqlOperations {

//For better types in IntelliJ
type Api <: SqlQueryApi & SqlDbValueApi & SqlDbValueImplApi & SqlStringApi & SqlQueryApi & SqlOperationApi & QueryApi
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ trait MySqlPlatform extends DefaultCompleteSql, DefaultSqlOperations, SqlTrigFun
given SqlStringTrimLeadingCapability with {}
given SqlStringTrimTrailingCapability with {}

given SqlStringRegexMatchesCapability with {}

given SqlStringLeftCapability with {}
given SqlStringRightCapability with {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ trait PostgresPlatform extends DefaultCompleteSql, DefaultSqlOperations, SqlBitw
given SqlStringTrimLeadingCapability with {}
given SqlStringTrimTrailingCapability with {}

given SqlStringRegexMatchesCapability with {}

given SqlStringLeftCapability with {}
given SqlStringRightCapability with {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ trait SqlitePlatform extends DefaultCompleteSql, DefaultSqlOperations, SqlBitwis
given ACoshCapability with {}
given ATanhCapability with {}

given SqlStringHexCapability with {}

override protected def generateDeleteAlias: Boolean = false
override protected def generateUpdateAlias: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ trait SqlBitwiseOps extends SqlDbValuesBase { platform =>
type Api <: SqlBitwiseApi & SqlDbValueApi & QueryApi
trait SqlBitwiseApi {
export platform.SqlBitwise
export platform.SqlBitwise.given
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>
trait SqlStringTrimLeadingCapability
trait SqlStringTrimTrailingCapability

trait SqlStringRegexMatchesCapability

trait SqlStringLeftCapability
trait SqlStringRightCapability

Expand Down Expand Up @@ -60,7 +62,7 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>
def ltrim: DbValue[A]
def rtrim: DbValue[A]

def indexOf(a: DbValue[A]): DbValue[A]
def indexOf(a: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Int]]

def substr(from: DbValue[Int], forLength: DbValue[Int]): DbValue[A]

Expand All @@ -69,7 +71,7 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>
def trimBoth(rhs: DbValue[A]): DbValue[A]

def like(rhs: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]]
def matches(regex: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]]
def matches(regex: DbValue[A])(using n: Nullability[A], cap: SqlStringRegexMatchesCapability): DbValue[n.N[Boolean]]

def startsWith(rhs: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]]
def endsWith(rhs: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]]
Expand All @@ -86,13 +88,13 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>

// TODO: Postgres? Split functions

def hex(using SqlStringHexCapability): DbValue[A]

object SqlString:
def concat[A: SqlString](v1: DbValue[A], vars: DbValue[A]*): DbValue[A] =
Impl.function(SqlExpr.FunctionName.Concat, (v1 +: vars).map(_.unsafeAsAnyDbVal), v1.tpe)
def concatWs[A: SqlString](v1: DbValue[A], vars: DbValue[A]*): DbValue[A] =
Impl.function(SqlExpr.FunctionName.ConcatWs, (v1 +: vars).map(_.unsafeAsAnyDbVal), v1.tpe)
def concatWs[A: SqlString](sep: DbValue[A], v1: DbValue[A], vars: DbValue[A]*): DbValue[A] =
Impl.function(SqlExpr.FunctionName.ConcatWs, (sep +: v1 +: vars).map(_.unsafeAsAnyDbVal), v1.tpe)

def hex(i: DbValue[Long])(using SqlStringHexCapability): DbValue[String] = Impl.function(SqlExpr.FunctionName.Hex, Seq(i.unsafeAsAnyDbVal), AnsiTypes.defaultStringType)

def defaultInstance[A]: SqlString[A] = new SqlString[A]:
extension (lhs: DbValue[A])
Expand All @@ -110,15 +112,15 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>
Impl.function(SqlExpr.FunctionName.Upper, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)

override def lpad(length: DbValue[Int], content: DbValue[A])(using SqlStringLpadCapability): DbValue[A] =
Impl.function(SqlExpr.FunctionName.Lpad, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)
Impl.function(SqlExpr.FunctionName.Lpad, Seq(lhs.unsafeAsAnyDbVal, length.unsafeAsAnyDbVal, content.unsafeAsAnyDbVal), lhs.tpe)
override def rpad(length: DbValue[Int], content: DbValue[A])(using SqlStringRpadCapability): DbValue[A] =
Impl.function(SqlExpr.FunctionName.Rpad, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)
Impl.function(SqlExpr.FunctionName.Rpad, Seq(lhs.unsafeAsAnyDbVal, length.unsafeAsAnyDbVal, content.unsafeAsAnyDbVal), lhs.tpe)

override def ltrim: DbValue[A] = Impl.function(SqlExpr.FunctionName.Ltrim, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)
override def rtrim: DbValue[A] = Impl.function(SqlExpr.FunctionName.Rtrim, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)

override def indexOf(a: DbValue[A]): DbValue[A] =
Impl.function(SqlExpr.FunctionName.IndexOf, Seq(lhs.unsafeAsAnyDbVal, a.unsafeAsAnyDbVal), lhs.tpe)
override def indexOf(a: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Int]] =
Impl.function(SqlExpr.FunctionName.IndexOf, Seq(lhs.unsafeAsAnyDbVal, a.unsafeAsAnyDbVal), n.wrapType(AnsiTypes.integer))
override def substr(from: DbValue[Int], forLength: DbValue[Int]): DbValue[A] = Impl.function(
SqlExpr.FunctionName.Substring,
Seq(lhs.unsafeAsAnyDbVal, from.unsafeAsAnyDbVal, forLength.unsafeAsAnyDbVal),
Expand All @@ -134,7 +136,7 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>

override def like(rhs: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]] =
Impl.binaryOp(n.castDbVal(lhs), n.castDbVal(rhs), n.wrapBinOp(SqlStringLikeOp()))
override def matches(regex: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]] =
override def matches(regex: DbValue[A])(using n: Nullability[A], cap: SqlStringRegexMatchesCapability): DbValue[n.N[Boolean]] =
Impl.binaryOp(n.castDbVal(lhs), n.castDbVal(regex), n.wrapBinOp(SqlStringRegexMatchesOp()))

override def startsWith(rhs: DbValue[A])(using n: Nullability[A]): DbValue[n.N[Boolean]] =
Expand Down Expand Up @@ -168,9 +170,6 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>
override def reverse(using SqlStringReverseCapability): DbValue[A] =
Impl.function(SqlExpr.FunctionName.Reverse, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)

override def hex(using SqlStringHexCapability): DbValue[A] =
Impl.function(SqlExpr.FunctionName.Hex, Seq(lhs.unsafeAsAnyDbVal), lhs.tpe)

end defaultInstance

given SqlString[String] = defaultInstance
Expand All @@ -179,5 +178,6 @@ trait SqlStringOps extends SqlDbValuesBase { platform =>
type Api <: SqlStringApi & SqlDbValueApi & QueryApi
trait SqlStringApi {
export platform.SqlString
export platform.SqlString.given
}
}
4 changes: 2 additions & 2 deletions common/src/main/scala/dataprism/sharedast/H2AstRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class H2AstRenderer[Codec[_]](ansiTypes: AnsiTypes[Codec], getCodecTypeName: [A]
args: Seq[SqlExpr[Codec]],
tpe: String
): SqlStr[Codec] = call match
case SqlExpr.FunctionName.Md5 => sql"HASH('MD5' ${renderExpr(args.head)})"
case SqlExpr.FunctionName.Sha256 => sql"HASH('SHA-256' ${renderExpr(args.head)})"
case SqlExpr.FunctionName.Md5 => sql"HASH('MD5', ${renderExpr(args.head)})"
case SqlExpr.FunctionName.Sha256 => sql"HASH('SHA-256', ${renderExpr(args.head)})"
case _ => super.renderFunctionCall(call, args, tpe)

override protected def renderPreparedArgument(arg: SqlExpr.PreparedArgument[Codec]): SqlStr[Codec] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class SqliteAstRenderer[Codec[_]](ansiTypes: AnsiTypes[Codec], getCodecTypeName:

case SqlExpr.FunctionName.CharLength => normal("length")
case SqlExpr.FunctionName.IndexOf => normal("instr")
case SqlExpr.FunctionName.TrimBoth => sql"trim(${renderExpr(args.head)}, ${renderExpr(args(1))})"
case SqlExpr.FunctionName.Concat => subdivided("concat")
case SqlExpr.FunctionName.ConcatWs => subdivided("concat_ws")
case SqlExpr.FunctionName.TrimBoth => sql"trim(${renderExpr(args.head)}, ${renderExpr(args(1))})"

case _ => super.renderFunctionCall(call, args, tpe)

Expand Down
Loading

0 comments on commit ddf55c0

Please sign in to comment.