Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/2023 12 enhanced data type default value #96

Merged
merged 5 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions core/src/main/scala/ldbc/core/DataType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ object DataType:
* @param value
* Value set as the default value for DataType
*/
inline def DEFAULT(value: T | 0 | String): Date[T] =
inline def DEFAULT(value: T | String | 0 | Some[Int]): Date[T] =
inline erasedValue[value.type] match
case _: Option[?] =>
this.copy(default = Some(value.asInstanceOf[Option[?]].fold(Default.Null)(Default.Value(_))))
Expand All @@ -1239,6 +1239,7 @@ object DataType:
"The DATE type must be passed a string in the format YYYY-MM-DD, ranging from '1000-01-01' to '9999-12-31'."
)
case _: 0 => this.copy(default = Some(Default.Value(value)))
case _: Some[Int] => this.copy(default = Some(Default.Value(value.asInstanceOf[Some[Int]].get)))
case _: LocalDate => this.copy(default = Some(Default.Value(value)))

/** Methods for setting default values for dates.
Expand Down Expand Up @@ -1276,7 +1277,7 @@ object DataType:
* @param value
* Value set as the default value for DataType
*/
inline def DEFAULT(value: T | 0 | String): DateTime[T] =
inline def DEFAULT(value: T | String | 0 | Some[Int]): DateTime[T] =
inline erasedValue[value.type] match
case _: Option[?] =>
this.copy(default = Some(value.asInstanceOf[Option[?]].fold(Default.Null)(Default.Value(_))))
Expand All @@ -1292,7 +1293,8 @@ object DataType:
error(
"The DATETIME type must be passed a string in the format YYYY-MM-DD hh:mm:ss, ranging from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'."
)
case _: 0 => this.copy(default = Some(Default.Value(value)))
case _: 0 => this.copy(default = Some(Default.Value(value)))
case _: Some[Int] => this.copy(default = Some(Default.Value(value.asInstanceOf[Some[Int]].get)))
case _: (Instant | LocalDateTime | OffsetTime) => this.copy(default = Some(Default.Value(value)))

/** Methods for setting default values for dates.
Expand Down Expand Up @@ -1335,7 +1337,7 @@ object DataType:
* @param value
* Value set as the default value for DataType
*/
inline def DEFAULT(value: T | 0 | String): TimeStamp[T] =
inline def DEFAULT(value: T | String | 0 | Some[Int]): TimeStamp[T] =
inline erasedValue[value.type] match
case _: Option[?] =>
this.copy(default = Some(value.asInstanceOf[Option[?]].fold(Default.Null)(Default.Value(_))))
Expand All @@ -1351,7 +1353,8 @@ object DataType:
error(
"The TIMESTAMP type must be passed a string in the format YYYY-MM-DD hh:mm:ss, ranging from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'."
)
case _: 0 => this.copy(default = Some(Default.Value(value)))
case _: 0 => this.copy(default = Some(Default.Value(value)))
case _: Some[Int] => this.copy(default = value.asInstanceOf[Some[Int]].map(Default.Value(_)))
case _: (Instant | LocalDateTime | OffsetDateTime | ZonedDateTime) =>
this.copy(default = Some(Default.Value(value)))

Expand Down Expand Up @@ -1392,7 +1395,7 @@ object DataType:
* @param value
* Value set as the default value for DataType
*/
inline def DEFAULT(value: T | 0 | String): Time[T] =
inline def DEFAULT(value: T | String | 0 | Some[Int]): Time[T] =
inline erasedValue[value.type] match
case _: Option[?] =>
this.copy(default = Some(value.asInstanceOf[Option[?]].fold(Default.Null)(Default.Value(_))))
Expand All @@ -1406,6 +1409,7 @@ object DataType:
"A string in hh:mm:ss or hhh:mm:ss format and in the range from '-838:59:59' to '838:59:59' must be passed to the TIME type."
)
case _: 0 => this.copy(default = Some(Default.Value(value)))
case _: Some[Int] => this.copy(default = value.asInstanceOf[Some[Int]].map(Default.Value(_)))
case _: LocalTime => this.copy(default = Some(Default.Value(value)))

/** This model is used to represent SQL DataType Year data.
Expand Down Expand Up @@ -1516,6 +1520,6 @@ object DataType:
* @param value
* Value set as the default value for DataType
*/
def DEFAULT(value: T): Bool[T] = value match
def DEFAULT(value: T | Int | Option[Int]): Bool[T] = value match
case v: Option[?] => this.copy(default = Some(v.fold(Default.Null)(Default.Value(_))))
case v => this.copy(default = Some(Default.Value(v)))
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ case class DataTypeCodeBuilder(scalaType: String, formatter: Naming):
case data: DataType.TIME => data.fsp.fold(s"${ data.name }[$scalaType]")(n => s"${ data.name }[$scalaType]($n)")
case data: DataType.YEAR => data.digit.fold(s"${ data.name }[$scalaType]")(n => s"${ data.name }[$scalaType]($n)")
case data: DataType.SERIAL => s"${ data.name }[${ data.scalaType.code }]"
case data: DataType.BOOLEAN => s"${ data.name }[${ data.scalaType.code }]"
case data: DataType.BOOLEAN => s"${ data.name }[$scalaType]"

private def buildNumberDataType(unsigned: Boolean, zerofill: Boolean): String =
(unsigned, zerofill) match
Expand Down
9 changes: 8 additions & 1 deletion plugin/src/sbt-test/plugin/example/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ DROP TABLE IF EXISTS `alias`;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `alias` (
`id` SERIAL,
`bool` BOOLEAN NOT NULL DEFAULT true
`bool` BOOLEAN NOT NULL DEFAULT true,
`optBool` BOOLEAN DEFAULT false,
`intBool` BOOLEAN NOT NULL DEFAULT 0,
`optIntBool` BOOLEAN DEFAULT 1
);

SET GLOBAL max_connections = 1000, sort_buffer_size = 1000000;
Expand All @@ -111,9 +114,13 @@ DROP TABLE IF EXISTS `java_time`;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `java_time` (
`date` DATE NOT NULL DEFAULT '2023-10-24',
`zero_date` DATE DEFAULT 0,
`date_time` DATETIME NOT NULL DEFAULT '2023-10-24 09:28:55',
`zero_date_time` DATETIME NULL DEFAULT 0,
`timestamp` TIMESTAMP NOT NULL DEFAULT '2023-10-24 09:28:55',
`zero_timestamp` TIMESTAMP NULL DEFAULT 0,
`time` TIME NOT NULL DEFAULT '09:28:55',
`zero_time` TIME NULL DEFAULT 0,
`year` YEAR NOT NULL DEFAULT 2023
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand Down