Skip to content

Commit

Permalink
Merge pull request #79 from kazumatsudo/feature/sort_sql_values
Browse files Browse the repository at this point in the history
modify: sort DDL/DML values by key name
  • Loading branch information
kazumatsudo authored Jan 18, 2024
2 parents 2a9aef6 + 6d1704a commit ca0d5ec
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/main/scala/domain/table/ddl/column/ColumnList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ case class ColumnList(private val value: Map[ColumnName, ColumnType])
}

def toSqlSentence: String =
value
value.toSeq
.sortBy { case (columnName, _) => columnName.toSqlSentence }
.map { case (columnName, columnType) =>
s"${columnName.toSqlSentence} ${columnType.toSqlSentence}"
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/domain/table/dml/RecordValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final case class RecordValue(private val value: Map[String, Any])
extends AnyVal {

def toSqlSentence: (String, String) = {
val (keys, values) = value.toSeq.unzip
val (keys, values) = value.toSeq.sortBy { case (key, _) => key }.unzip

val valuesForSql = values.map { value =>
ColumnType.apply(value) match {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/domain/table/ddl/TableListSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TableListSpec extends AnyFunSpec with Matchers {
accumulator.merge(currentValue)
}

vertexAnalyzedResult.toSqlSentence shouldBe "CREATE TABLE IF NOT EXISTS vertex (property_age INT(2), property_lang VARCHAR(4), property_name VARCHAR(6), label_software BOOLEAN, id INT(1), label_person BOOLEAN);"
vertexAnalyzedResult.toSqlSentence shouldBe "CREATE TABLE IF NOT EXISTS vertex (id INT(1), label_person BOOLEAN, label_software BOOLEAN, property_age INT(2), property_lang VARCHAR(4), property_name VARCHAR(6));"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ColumnListSpec extends AnyFunSpec with Matchers {
it("success") {
columnList1
.merge(columnList2)
.toSqlSentence shouldBe "name VARCHAR(50), id INT(11), address VARCHAR(255), created_at TEXT, updated_at TEXT"
.toSqlSentence shouldBe "address VARCHAR(255), created_at TEXT, id INT(11), name VARCHAR(50), updated_at TEXT"
}
}
}
51 changes: 21 additions & 30 deletions src/test/scala/domain/table/dml/RecordListSpec.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package domain.table.dml

import domain.table.ddl.TableName
import infrastructure.VertexQuery
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

import java.util.UUID

class RecordListSpec extends AnyFunSpec with Matchers {
describe("merge") {
describe("success") {
Expand Down Expand Up @@ -77,34 +77,25 @@ class RecordListSpec extends AnyFunSpec with Matchers {

describe("toSqlSentence") {
it("success") {
RecordValue(
Map(("boolean", false))
).toSqlSentence shouldBe ("boolean", "false")
RecordValue(Map(("byte", 1.toByte))).toSqlSentence shouldBe ("byte", "1")
RecordValue(
Map(("short", 1.toShort))
).toSqlSentence shouldBe ("short", "1")
RecordValue(Map(("int", 1))).toSqlSentence shouldBe ("int", "1")
RecordValue(Map(("long", 1.toLong))).toSqlSentence shouldBe ("long", "1")
RecordValue(
Map(("float", 1.toFloat))
).toSqlSentence shouldBe ("float", "1.0")
RecordValue(
Map(("double", 1.toDouble))
).toSqlSentence shouldBe ("double", "1.0")
val uuid = UUID.randomUUID()
RecordValue(
Map(("uuid", uuid))
).toSqlSentence shouldBe ("uuid", s"\"$uuid\"")
RecordValue(
Map(("char", 'a'))
).toSqlSentence shouldBe ("char", "\"a\"")
RecordValue(
Map(("string", 1.toString))
).toSqlSentence shouldBe ("string", "\"1\"")
RecordValue(
Map(("unknown", Seq(1)))
).toSqlSentence shouldBe ("unknown", "\"List(1)\"")
// TODO: not use Vertex
val graph = TinkerFactory.createModern().traversal()
val vertexQuery = VertexQuery(graph)
val vertex = vertexQuery.getList(0, vertexQuery.countAll.toInt)

val vertexAnalyzedResult = vertex
.map(_.toDml)
.reduce[RecordList] { case (accumulator, currentValue) =>
accumulator.merge(currentValue, checkUnique = false)
}

vertexAnalyzedResult.toSqlSentence shouldBe
"""INSERT INTO vertex (id, label_software, property_lang, property_name) VALUES (5, true, "java", "ripple");
|INSERT INTO vertex (id, label_person, property_age, property_name) VALUES (6, true, 35, "peter");
|INSERT INTO vertex (id, label_person, property_age, property_name) VALUES (4, true, 32, "josh");
|INSERT INTO vertex (id, label_person, property_age, property_name) VALUES (2, true, 27, "vadas");
|INSERT INTO vertex (id, label_person, property_age, property_name) VALUES (1, true, 29, "marko");
|INSERT INTO vertex (id, label_software, property_lang, property_name) VALUES (3, true, "java", "lop");""".stripMargin

}
}
}
16 changes: 16 additions & 0 deletions src/test/scala/domain/table/dml/RecordValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@ package domain.table.dml
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

import java.util.UUID

class RecordValueSpec extends AnyFunSpec with Matchers {
describe("toSqlSentence") {
it("success") {
RecordValue(
Map(("boolean", false))
).toSqlSentence shouldBe ("boolean", "false")
RecordValue(Map(("byte", 1.toByte))).toSqlSentence shouldBe ("byte", "1")
RecordValue(
Map(("short", 1.toShort))
).toSqlSentence shouldBe ("short", "1")
RecordValue(Map(("int", 1))).toSqlSentence shouldBe ("int", "1")
RecordValue(Map(("long", 1.toLong))).toSqlSentence shouldBe ("long", "1")
RecordValue(
Map(("float", 1.toFloat))
).toSqlSentence shouldBe ("float", "1.0")
RecordValue(
Map(("double", 1.toDouble))
).toSqlSentence shouldBe ("double", "1.0")
val uuid = UUID.randomUUID()
RecordValue(
Map(("uuid", uuid))
).toSqlSentence shouldBe ("uuid", s"\"$uuid\"")
RecordValue(
Map(("char", 'a'))
).toSqlSentence shouldBe ("char", "\"a\"")
RecordValue(
Map(("string", 1.toString))
).toSqlSentence shouldBe ("string", "\"1\"")
Expand Down

0 comments on commit ca0d5ec

Please sign in to comment.