All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crobox.clickhouse.dsl.schemabuilder.CreateTable.scala Maven / Gradle / Ivy

There is a newer version: 0.7.5
Show newest version
package com.crobox.clickhouse.dsl.schemabuilder

import com.crobox.clickhouse.dsl.{ClickhouseStatement, Table}

/**
 * @author Sjoerd Mulder
 * @since 30-12-16
 */
case class CreateTable(table: Table,
                       engine: Engine,
                       ifNotExists: Boolean = false,
                       databaseName: String = ClickhouseStatement.DefaultDatabase,
                       clusterName : Option[String] = None)
    extends ClickhouseSchemaStatement with DistributedDdlSupport {

  require(ClickhouseStatement.isValidIdentifier(table.name), "Cannot create a table with invalid identifier")
  require(ClickhouseStatement.isValidIdentifier(databaseName), "Cannot create a table with invalid database identifier")
  requireValidCluster("Cannot create a table with invalid cluster identifier")
  require(table.columns.nonEmpty, "Cannot create a table without any columns")

  /**
   * Returns the query string for this statement.
   *
   * @return String containing the Clickhouse dialect SQL statement
   */
//  TODO migrate this to the tokenizer as well
  override def query: String =
    s"""CREATE TABLE${printIfNotExists(ifNotExists)} $databaseName.${table.name}${printOnCluster()} (
       |  ${table.columns.map(_.query()).mkString(",\n  ")}
       |) ENGINE = $engine""".stripMargin
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy