Skip to content

Commit

Permalink
Merge pull request #334 from takapi327/refactor/2024-12-Change-use-sc…
Browse files Browse the repository at this point in the history
…hema

Refactor/2024 12 change use schema
  • Loading branch information
takapi327 authored Dec 13, 2024
2 parents 676c3f9 + 8717077 commit c347ac4
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 118 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
run: mkdir -p module/ldbc-query-builder/.js/target module/ldbc-codegen/native/target module/jdbc-connector/.jvm/target module/ldbc-query-builder/.native/target module/ldbc-codegen/jvm/target module/ldbc-query-builder/.jvm/target module/ldbc-dsl/.native/target module/ldbc-connector/js/target module/ldbc-codegen/js/target core/native/target module/ldbc-sql/.js/target module/ldbc-statement/.native/target core/js/target module/ldbc-schema/.js/target module/ldbc-sql/.native/target module/ldbc-schemaspy/target module/ldbc-schema/.native/target module/ldbc-statement/.jvm/target core/jvm/target module/ldbc-dsl/.js/target module/ldbc-hikari/target module/ldbc-sql/.jvm/target module/ldbc-statement/.js/target module/ldbc-connector/native/target module/ldbc-connector/jvm/target module/ldbc-schema/.jvm/target plugin/target module/ldbc-dsl/.jvm/target project/target
run: mkdir -p module/ldbc-query-builder/.js/target module/ldbc-codegen/native/target module/jdbc-connector/.jvm/target module/ldbc-query-builder/.native/target module/ldbc-codegen/jvm/target module/ldbc-query-builder/.jvm/target module/ldbc-dsl/.native/target module/ldbc-connector/js/target module/ldbc-codegen/js/target core/native/target module/ldbc-sql/.js/target module/ldbc-statement/.native/target core/js/target module/ldbc-renewal-schema/.jvm/target module/ldbc-schema/.js/target module/ldbc-sql/.native/target module/ldbc-schemaspy/target module/ldbc-schema/.native/target module/ldbc-statement/.jvm/target core/jvm/target module/ldbc-dsl/.js/target module/ldbc-hikari/target module/ldbc-sql/.jvm/target module/ldbc-statement/.js/target module/ldbc-connector/native/target module/ldbc-connector/jvm/target module/ldbc-schema/.jvm/target plugin/target module/ldbc-renewal-schema/.native/target module/ldbc-renewal-schema/.js/target module/ldbc-dsl/.jvm/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
run: tar cf targets.tar module/ldbc-query-builder/.js/target module/ldbc-codegen/native/target module/jdbc-connector/.jvm/target module/ldbc-query-builder/.native/target module/ldbc-codegen/jvm/target module/ldbc-query-builder/.jvm/target module/ldbc-dsl/.native/target module/ldbc-connector/js/target module/ldbc-codegen/js/target core/native/target module/ldbc-sql/.js/target module/ldbc-statement/.native/target core/js/target module/ldbc-schema/.js/target module/ldbc-sql/.native/target module/ldbc-schemaspy/target module/ldbc-schema/.native/target module/ldbc-statement/.jvm/target core/jvm/target module/ldbc-dsl/.js/target module/ldbc-hikari/target module/ldbc-sql/.jvm/target module/ldbc-statement/.js/target module/ldbc-connector/native/target module/ldbc-connector/jvm/target module/ldbc-schema/.jvm/target plugin/target module/ldbc-dsl/.jvm/target project/target
run: tar cf targets.tar module/ldbc-query-builder/.js/target module/ldbc-codegen/native/target module/jdbc-connector/.jvm/target module/ldbc-query-builder/.native/target module/ldbc-codegen/jvm/target module/ldbc-query-builder/.jvm/target module/ldbc-dsl/.native/target module/ldbc-connector/js/target module/ldbc-codegen/js/target core/native/target module/ldbc-sql/.js/target module/ldbc-statement/.native/target core/js/target module/ldbc-renewal-schema/.jvm/target module/ldbc-schema/.js/target module/ldbc-sql/.native/target module/ldbc-schemaspy/target module/ldbc-schema/.native/target module/ldbc-statement/.jvm/target core/jvm/target module/ldbc-dsl/.js/target module/ldbc-hikari/target module/ldbc-sql/.jvm/target module/ldbc-statement/.js/target module/ldbc-connector/native/target module/ldbc-connector/jvm/target module/ldbc-schema/.jvm/target plugin/target module/ldbc-renewal-schema/.native/target module/ldbc-renewal-schema/.js/target module/ldbc-dsl/.jvm/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
Expand Down Expand Up @@ -300,7 +300,7 @@ jobs:
- name: Submit Dependencies
uses: scalacenter/sbt-dependency-submission@v2
with:
modules-ignore: ldbcjs_3 ldbcjs_3 docs_3 docs_3 tests_3 ldbcnative_3 ldbcnative_3 ldbc-renewal-schema_3 ldbcjvm_3 ldbcjvm_3 ldbc-renewal-schema_native0.4_3 ldbc-renewal-schema_sjs1_3 benchmark_3 benchmark_3
modules-ignore: ldbcjs_3 ldbcjs_3 docs_3 docs_3 tests_3 ldbcnative_3 ldbcnative_3 ldbcjvm_3 ldbcjvm_3 benchmark_3 benchmark_3
configs-ignore: test scala-tool scala-doc-tool test-internal

validate-steward:
Expand Down
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ lazy val renewalSchema = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.settings(libraryDependencies += "org.typelevel" %% "munit-cats-effect" % "2.0.0" % Test)
.settings(Test / scalacOptions -= "-Werror")
.dependsOn(statement, connector)
.enablePlugins(NoPublishPlugin)

lazy val schemaSpy = LepusSbtProject("ldbc-schemaSpy", "module/ldbc-schemaspy")
.settings(
Expand Down Expand Up @@ -142,7 +141,7 @@ lazy val codegen = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.platformsSettings(JSPlatform, NativePlatform)(
libraryDependencies += "com.armanbilge" %%% "circe-scala-yaml" % "0.0.4"
)
.dependsOn(queryBuilder, schema)
.dependsOn(renewalSchema)

lazy val jdbcConnector = crossProject(JVMPlatform)
.crossType(CrossType.Pure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.nio.file.Files

import scala.io.Codec

import ldbc.query.builder.formatter.Naming
import ldbc.codegen.formatter.Naming
import ldbc.codegen.model.*

private[ldbc] object DatabaseModelGenerator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import java.io.File
import java.nio.file.Files
import java.nio.charset.Charset

import ldbc.query.builder.formatter.Naming
import ldbc.codegen.formatter.Naming
import ldbc.codegen.parser.Parser
import ldbc.codegen.parser.yml.Parser as YmlParser
import ldbc.codegen.model.{ Database, Table }

private[ldbc] object LdbcGenerator:

Expand Down Expand Up @@ -58,33 +57,17 @@ private[ldbc] object LdbcGenerator:
.groupBy(_._1)
.flatMap { (name, list) =>
val statements = list.flatMap(_._2).toSet
statements.map {
case statement: Table.CreateStatement =>
val customTables = custom.find(_.database.name == name).map(_.database.tables)
TableModelGenerator.generate(
name,
statement,
classNameFormatter,
propertyNameFormatter,
sourceManaged,
customTables,
packageName
)
case statement: Database.CreateStatement =>
val tableStatements = statements.flatMap {
case statement: Table.CreateStatement =>
Some(s"${ classNameFormatter.format(statement.tableName) }.table")
case _: Database.CreateStatement => None
}.toList

DatabaseModelGenerator.generate(
statement,
tableStatements,
classNameFormatter,
propertyNameFormatter,
sourceManaged,
packageName
)
statements.map { statement =>
val customTables = custom.find(_.database.name == name).map(_.database.tables)
TableModelGenerator.generate(
name,
statement,
classNameFormatter,
propertyNameFormatter,
sourceManaged,
customTables,
packageName
)
}
}
.toArray
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.nio.file.Files

import scala.io.Codec

import ldbc.query.builder.formatter.Naming
import ldbc.codegen.formatter.Naming
import ldbc.codegen.model.*
import ldbc.codegen.parser.yml.Parser
import ldbc.codegen.builder.ColumnCodeBuilder
Expand Down Expand Up @@ -85,14 +85,6 @@ private[ldbc] object TableModelGenerator:
outputFile.getParentFile.mkdirs()
outputFile.createNewFile()

val keyDefinitions = statement.keyDefinitions.map(key =>
s".keySet(table => ${ key.toCode("table", classNameFormatter, propertyNameFormatter) })"
)

val tableOptions = statement.options.fold("")(
_.map(option => s".setOption(${ Table.buildTableOptionCode(option) })").mkString("\n ")
)

val builder = ColumnCodeBuilder(classNameFormatter)

val columns =
Expand All @@ -102,6 +94,8 @@ private[ldbc] object TableModelGenerator:

val objectExtends = custom.flatMap(_.`object`.map(_.`extends`.mkString(", "))).fold("")(str => s" extends $str")

val allColumns = statement.columnDefinitions.map(column => Naming.toCamel(column.name))

val scalaSource =
s"""
|package ${ if database.nonEmpty then s"$packageName.$database" else packageName }
Expand All @@ -115,11 +109,12 @@ private[ldbc] object TableModelGenerator:
|object $className$objectExtends:
|
| ${ objects.mkString("\n ") }
| val table: Table[$className] = Table[$className]("${ statement.tableName }")(
| ${ columns.mkString(",\n ") }
| )
| ${ keyDefinitions.mkString("\n ") }
| $tableOptions
| val table = TableQuery[${ className }Table]
|
| class ${ className }Table extends Table[$className]("${ statement.tableName }"):
| ${ columns.mkString("\n ") }
|
| override def * : Column[$className] = (${ allColumns.mkString(" *: ") }).to[$className]
|""".stripMargin

Files.write(outputFile.toPath, scalaSource.getBytes(summon[Codec].name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package ldbc.codegen.builder

import ldbc.codegen.model.*
import ldbc.codegen.parser.yml.Parser
import ldbc.query.builder.formatter.Naming
import ldbc.codegen.formatter.Naming

/**
* Column model for constructing code strings.
Expand All @@ -24,7 +24,9 @@ case class ColumnCodeBuilder(formatter: Naming):
val builder = DataTypeCodeBuilder(scalaType, formatter)
val dataType = builder.build(column.dataType)

s"column(\"${ column.name }\", $dataType" + buildDefault(column).getOrElse("") + column._attributes + ")"
s"def ${ Naming.toCamel(column.name) }: Column[$scalaType] = column[$scalaType](\"${ column.name }\", $dataType" + buildDefault(
column
).getOrElse("") + column._attributes + ")"

private def buildScalaType(column: ColumnDefinition, customColumn: Option[Parser.Column]): String =
val code = customColumn match
Expand All @@ -34,7 +36,10 @@ case class ColumnCodeBuilder(formatter: Naming):
case ScalaType.Enum(_) => formatter.format(column.name)
case _ => column.dataType.scalaType.code

if column.isOptional then s"Option[$code]" else code
(column.isOptional, column.dataType) match
case (_, _: DataType.SERIAL) => code
case (true, _) => s"Option[$code]"
case (false, _) => code

private def buildDefault(column: ColumnDefinition): Option[String] =
column.attributes.fold(None)(attribute =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package ldbc.codegen.builder

import ldbc.codegen.model.DataType
import ldbc.query.builder.formatter.Naming
import ldbc.codegen.formatter.Naming

/**
* DataType model for constructing code strings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2023-2024 by Takahiko Tominaga
* This software is licensed under the MIT License (MIT).
* For more information see LICENSE or https://opensource.org/licenses/MIT
*/

package ldbc.codegen.formatter

import scala.annotation.tailrec

/**
* Enum of naming rules
*/
enum Naming:
case CAMEL, PASCAL, SNAKE

object Naming:

def fromString(str: String): Naming =
str match
case "CAMEL" => CAMEL
case "PASCAL" => PASCAL
case "SNAKE" => SNAKE
case unknown =>
throw new IllegalArgumentException(
s"$unknown does not match any of the Naming, it must be CAMEL, PASCAL, SNAKE, or KEBAB."
)

extension (`case`: Naming)
def format(name: String): String =
`case` match
case CAMEL => toCamel(name)
case PASCAL => toPascal(name)
case SNAKE => toSnake(name)

/**
* Converts to camelCase e.g.: PascalCase => pascalCase
*
* @param name
* name to be converted to camelCase
* @return
* camelCase version of the string passed
*/
def toCamel(name: String): String =
toSnake(name).split("_").toList match
case Nil => name
case head :: tail => head + tail.map(v => s"${ v.charAt(0).toUpper }${ v.drop(1) }").mkString

/**
* Converts to PascalCase e.g.: camelCase => CamelCase
*
* @param name
* name to be converted to PascalCase
* @return
* PascalCase version of the string passed
*/
def toPascal(name: String): String =
val list = toSnake(name).split("_").toList
if list.nonEmpty && !(list.size == 1 && list.head == "") then
list.map(v => s"${ v.charAt(0).toUpper }${ v.drop(1) }").mkString
else name

/**
* Converts to snake_case e.g.: camelCase => camel_case
*
* @param name
* name to be converted to snake_case
* @return
* snake_case version of the string passed
*/
def toSnake(name: String): String =
@tailrec def go(accDone: List[Char], acc: List[Char]): List[Char] = acc match
case Nil => accDone
case a :: b :: c :: tail if a.isUpper && b.isUpper && c.isLower => go(accDone ++ List(a, '_', b, c), tail)
case a :: b :: tail if a.isLower && b.isUpper => go(accDone ++ List(a, '_', b), tail)
case a :: tail => go(accDone :+ a, tail)
go(Nil, name.toList).mkString.toLowerCase.replaceAll("-", "_")
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package ldbc.codegen.model

import ldbc.query.builder.formatter.Naming
import ldbc.codegen.formatter.Naming

trait Key:
def toCode(tableName: String, classNameFormatter: Naming, propertyFormatter: Naming): String
Expand Down Expand Up @@ -70,10 +70,10 @@ object Key:
indexOption: Option[IndexOption]
) extends Key:
def toCode(tableName: String, classNameFormatter: Naming, propertyFormatter: Naming): String =
val columns = keyParts.map(v => s"$tableName.${ propertyFormatter.format(v) }")
val columns = keyParts.map(v => s"${ propertyFormatter.format(v) }")
s"INDEX_KEY(${ indexName.fold("None")(str => s"Some(\"$str\")") }, ${ indexType
.fold("None")(v => s"Some(${ v.toCode })") }, ${ indexOption
.fold("None")(option => s"Some(${ option.toCode })") }, ${ columns.mkString(", ") })"
.fold("None")(option => s"Some(${ option.toCode })") }, ${ columns.mkString(" *: ") })"

case class Primary(
constraint: Option[Constraint],
Expand All @@ -82,12 +82,12 @@ object Key:
indexOption: Option[IndexOption]
) extends Key:
def toCode(tableName: String, classNameFormatter: Naming, propertyFormatter: Naming): String =
val columns = keyParts.map(v => s"$tableName.${ propertyFormatter.format(v) }")
val key = indexType.fold(s"PRIMARY_KEY(${ columns.mkString(", ") })")(v =>
val columns = keyParts.map(v => s"${ propertyFormatter.format(v) }")
val key = indexType.fold(s"PRIMARY_KEY(${ columns.mkString(" *: ") })")(v =>
indexOption match
case None => s"PRIMARY_KEY(${ v.toCode }, ${ columns.mkString(", ") })"
case None => s"PRIMARY_KEY(${ v.toCode }, ${ columns.mkString(" *: ") })"
case Some(o) =>
s"PRIMARY_KEY(${ v.toCode }, ${ o.toCode }, ${ columns.mkString(", ") })"
s"PRIMARY_KEY(${ v.toCode }, ${ o.toCode }, ${ columns.mkString(" *: ") })"
)
constraint.fold(key)(_.name match
case Some(name) => s"CONSTRAINT(\"$name\", $key)"
Expand All @@ -102,11 +102,11 @@ object Key:
indexOption: Option[IndexOption]
) extends Key:
def toCode(tableName: String, classNameFormatter: Naming, propertyFormatter: Naming): String =
val columns = keyParts.map(v => s"$tableName.${ propertyFormatter.format(v) }")
val columns = keyParts.map(v => s"${ propertyFormatter.format(v) }")
val key =
s"UNIQUE_KEY(${ indexName.fold("None")(v => s"Some(\"$v\")") }, ${ indexType
.fold("None")(v => s"Some(${ v.toCode })") }, ${ indexOption
.fold("None")(v => s"Some(${ v.toCode })") }, ${ columns.mkString(", ") })"
.fold("None")(v => s"Some(${ v.toCode })") }, ${ columns.mkString(" *: ") })"
constraint.fold(key)(_.name match
case Some(name) => s"CONSTRAINT(\"$name\", $key)"
case None => s"CONSTRAINT($key)"
Expand All @@ -121,8 +121,8 @@ object Key:
def toCode(tableName: String, classNameFormatter: Naming, propertyFormatter: Naming): String =
val columns = keyParts.map(v => s"$tableName.${ propertyFormatter.format(v) }")
val key =
s"FOREIGN_KEY(${ indexName.fold("None")(v => s"Some(\"$v\")") }, (${ columns
.mkString(", ") }), ${ reference.toCode(classNameFormatter, propertyFormatter) })"
s"FOREIGN_KEY(${ indexName.fold("None")(v => s"Some(\"$v\")") }, ${ columns
.mkString(" *: ") }, ${ reference.toCode(classNameFormatter, propertyFormatter) })"
constraint.fold(key)(_.name match
case Some(name) => s"CONSTRAINT(\"$name\", $key)"
case None => s"CONSTRAINT($key)"
Expand All @@ -135,11 +135,11 @@ object Key:
on match
case Some(list) =>
(list.find(_.isInstanceOf[OnDelete]), list.find(_.isInstanceOf[OnUpdate])) match
case (None, None) => s"REFERENCE($className.table, (${ columns.mkString(",") }))"
case (None, None) => s"REFERENCE($className.table, ${ columns.mkString(" *: ") })"
case (Some(delete), None) =>
s"REFERENCE($className.table, (${ columns.mkString(", ") })).onDelete(${ delete.option })"
s"REFERENCE($className.table, ${ columns.mkString(" *: ") }).onDelete(${ delete.option })"
case (None, Some(update)) =>
s"REFERENCE($className.table, (${ columns.mkString(", ") })).onUpdate(${ update.option })"
s"REFERENCE($className.table, ${ columns.mkString(" *: ") }).onUpdate(${ update.option })"
case (Some(delete), Some(update)) =>
s"REFERENCE($className.table, (${ columns.mkString(", ") })).onDelete(${ delete.option }).onUpdate(${ update.option })"
case None => s"REFERENCE($className.table, (${ columns.mkString(",") }))"
s"REFERENCE($className.table, ${ columns.mkString(" *: ") }).onDelete(${ delete.option }).onUpdate(${ update.option })"
case None => s"REFERENCE($className.table, ${ columns.mkString(" *: ") })"
Loading

0 comments on commit c347ac4

Please sign in to comment.