<commit_before>/*
 * Copyright (c) 2016.
 *
 * This file is part of ProcessManager.
 *
 * This file is licenced to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You should have received a copy of the license with the source distribution.
 * Alternatively, you may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package uk.ac.bournemouth.kotlinsql

import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.*
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractCharColumnConfiguration.CharColumnConfiguration
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractCharColumnConfiguration.LengthCharColumnConfiguration
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractNumberColumnConfiguration.DecimalColumnConfiguration
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractNumberColumnConfiguration.NumberColumnConfiguration
import uk.ac.bournemouth.kotlinsql.ColumnConfiguration.ColumnFormat
import uk.ac.bournemouth.kotlinsql.ColumnConfiguration.StorageFormat
import uk.ac.bournemouth.kotlinsql.ColumnType.*
import uk.ac.bournemouth.util.kotlin.sql.DBConnection
import java.math.BigDecimal
import java.sql.SQLWarning
import java.util.*
import kotlin.reflect.KProperty


internal val LINE_SEPARATOR: String by lazy { System.getProperty("line.separator")!! }

/**
 * Implementation for the database API
 */

internal abstract class ColumnImpl<T:Any, S: ColumnType<T, S,C>, C:Column<T,S,C>> internal constructor (
      override val table: TableRef,
      override val type: S,
      override val name: String,
      override val notnull: Boolean?,
      override val unique: Boolean,
      override val autoincrement: Boolean,
      override val default: T?,
      override val comment: String?,
      override val columnFormat: ColumnFormat?,
      override val storageFormat: StorageFormat?,
      override val references: ColsetRef?,
      val unsigned:Boolean = false,
      val zerofill: Boolean = false,
      val displayLength: Int = -1,
      val precision: Int = -1,
      val scale:Int = -1,
      val charset:String? = null,
      val collation:String? = null,
      val binary:Boolean = false,
      val length:Int = -1
):Column<T,S,C> {


  @Suppress("UNCHECKED_CAST")
  override fun ref():C {
    return this as C
  }

  override fun toDDL(): CharSequence {
    val result = StringBuilder()
    result.apply {
      append('`').append(name).append("` ").append(type.typeName)
      if (this@ColumnImpl.length>0) append('(').append(this@ColumnImpl.length).append(')')
      else if (displayLength>0) append('(').append(displayLength).append(')')
      if (unsigned) append(" UNSIGNED")
      if (zerofill) append(" ZEROFILL")
      if (binary) append(" BINARY")
      charset?.let { append(" CHARACTER SET ").append(it)}
      collation?.let { append(" COLLATE ").append(it)}
      notnull?.let { append(if(it) " NOT NULL" else "NULL") }
      default?.let { append(" DEFAULT ")
        if (it is CharSequence)
          append('\'').append(it).append('\'')
        else append(it)
      }
      if (autoincrement) append(" AUTO_INCREMENT")
      if (unique) append(" UNIQUE")<commit_msg>Allow explicit null column.<commit_after>/*
 * Copyright (c) 2016.
 *
 * This file is part of ProcessManager.
 *
 * This file is licenced to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You should have received a copy of the license with the source distribution.
 * Alternatively, you may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package uk.ac.bournemouth.kotlinsql

import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.*
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractCharColumnConfiguration.CharColumnConfiguration
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractCharColumnConfiguration.LengthCharColumnConfiguration
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractNumberColumnConfiguration.DecimalColumnConfiguration
import uk.ac.bournemouth.kotlinsql.AbstractColumnConfiguration.AbstractNumberColumnConfiguration.NumberColumnConfiguration
import uk.ac.bournemouth.kotlinsql.ColumnConfiguration.ColumnFormat
import uk.ac.bournemouth.kotlinsql.ColumnConfiguration.StorageFormat
import uk.ac.bournemouth.kotlinsql.ColumnType.*
import uk.ac.bournemouth.util.kotlin.sql.DBConnection
import java.math.BigDecimal
import java.sql.SQLWarning
import java.util.*
import kotlin.reflect.KProperty


internal val LINE_SEPARATOR: String by lazy { System.getProperty("line.separator")!! }

/**
 * Implementation for the database API
 */

internal abstract class ColumnImpl<T:Any, S: ColumnType<T, S,C>, C:Column<T,S,C>> internal constructor (
      override val table: TableRef,
      override val type: S,
      override val name: String,
      override val notnull: Boolean?,
      override val unique: Boolean,
      override val autoincrement: Boolean,
      override val default: T?,
      override val comment: String?,
      override val columnFormat: ColumnFormat?,
      override val storageFormat: StorageFormat?,
      override val references: ColsetRef?,
      val unsigned:Boolean = false,
      val zerofill: Boolean = false,
      val displayLength: Int = -1,
      val precision: Int = -1,
      val scale:Int = -1,
      val charset:String? = null,
      val collation:String? = null,
      val binary:Boolean = false,
      val length:Int = -1
):Column<T,S,C> {


  @Suppress("UNCHECKED_CAST")
  override fun ref():C {
    return this as C
  }

  override fun toDDL(): CharSequence {
    val result = StringBuilder()
    result.apply {
      append('`').append(name).append("` ").append(type.typeName)
      if (this@ColumnImpl.length>0) append('(').append(this@ColumnImpl.length).append(')')
      else if (displayLength>0) append('(').append(displayLength).append(')')
      if (unsigned) append(" UNSIGNED")
      if (zerofill) append(" ZEROFILL")
      if (binary) append(" BINARY")
      charset?.let { append(" CHARACTER SET ").append(it)}
      collation?.let { append(" COLLATE ").append(it)}
      notnull?.let { append(if(it) " NOT NULL" else " NULL") }
      default?.let { append(" DEFAULT ")
        if (it is CharSequence)
          append('\'').append(it).append('\'')
        else append(it)
      }
      if (autoincrement) append(" AUTO_INCREMENT")
      if (unique) append(" UNIQUE")
{'fraction_non_alphanumeric': 0.10586296855691336, 'fraction_numerical': 0.0038445695455169575, 'mean_word_length': 3.9551020408163264, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7244081', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2029, 'n_words': 606}