-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathColumn.scala
76 lines (67 loc) · 2.61 KB
/
Column.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (c) 2014-2016 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.iglu.schemaddl.redshift
import com.snowplowanalytics.iglu.schemaddl.sql.{ColumnAttribute, ColumnConstraint, DataType, Ddl}
/**
* Class holding all information about Redshift's column
*
* @param columnName column_name
* @param dataType data_type such as INTEGER, VARCHAR, etc
* @param columnAttributes set of column_attributes such as ENCODE
* @param columnConstraints set of column_constraints such as NOT NULL
*/
private[redshift] case class Column[T <: RedShiftDdl](
columnName: String,
dataType: DataType[Ddl],
columnAttributes: Set[ColumnAttribute[T]] = Set.empty[ColumnAttribute[T]],
columnConstraints: Set[ColumnConstraint[T]] = Set.empty[ColumnConstraint[T]]
) extends RedShiftDdl {
/**
* Formatted column's DDL
* Calling method must provide length for each tab via Tuple5
*
* @param tabs tuple of lengths (prepend, table_name, data_type, etc)
* @return formatted DDL
*/
def toFormattedDdl(tabs: (Int, Int, Int, Int, Int)): String =
withTabs(tabs._1, " ") +
withTabs(tabs._2, nameDdl) +
withTabs(tabs._3, dataTypeDdl) +
withTabs(tabs._4, attributesDdl) +
withTabs(tabs._5, constraintsDdl)
/**
* Compact way to output column
*
* @return string representing column without formatting
*/
override def toDdl = toFormattedDdl((1, 1, 1, 1, 1))
// Get warnings only from data types suggestions
override val warnings = dataType.warnings
/**
* column_name ready to output with surrounding quotes to prevent odd chars
* from breaking the table
*/
val nameDdl = "\"" + columnName + "\" "
/**
* data_type ready to output
*/
val dataTypeDdl = dataType.toDdl
/**
* column_attributes ready to output if exists
*/
val attributesDdl = columnAttributes.map(" " + _.toDdl).mkString(" ")
/**
* column_constraints ready to output if exists
*/
val constraintsDdl = columnConstraints.map(" " + _.toDdl).mkString(" ")
}