10
10
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
11
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12
12
*/
13
- package com .snowplowanalytics .iglu .schemaddl .redshift
13
+ package com .snowplowanalytics .iglu .schemaddl
14
+ package redshift
15
+
16
+ import com .snowplowanalytics .iglu .schemaddl .sql .{Statement , TableAttribute , TableConstraint }
14
17
15
18
/**
16
19
* Class holding all information about Redshift's table
@@ -20,28 +23,28 @@ package com.snowplowanalytics.iglu.schemaddl.redshift
20
23
* @param tableConstraints set of table_constraints such as PRIMARY KEY
21
24
* @param tableAttributes set of table_attributes such as DISTSTYLE
22
25
*/
23
- case class CreateTable (
24
- tableName : String ,
25
- columns : List [Column ],
26
- tableConstraints : Set [TableConstraint ] = Set .empty[TableConstraint ],
27
- tableAttributes : Set [TableAttribute ] = Set .empty[TableAttribute ]
28
- ) extends Statement {
26
+ private [redshift] case class CreateTable [ T <: RedShiftDdl ] (
27
+ tableName : String ,
28
+ columns : List [Column [ T ] ],
29
+ tableConstraints : Set [TableConstraint ] = Set .empty[TableConstraint ],
30
+ tableAttributes : Set [TableAttribute [ T ]] = Set .empty[TableAttribute [ T ] ]
31
+ ) extends Statement {
29
32
30
33
def toDdl = {
31
34
val columnsDdl = columns.map(_.toFormattedDdl(tabulation)
32
35
.replaceAll(" \\ s+$" , " " ))
33
36
.mkString(" ,\n " )
34
37
s """ CREATE TABLE IF NOT EXISTS $tableName (
35
- | $columnsDdl$getConstraints
36
- |) $getAttributes""" .stripMargin
38
+ | $columnsDdl$getConstraints
39
+ |) $getAttributes""" .stripMargin
37
40
}
38
41
39
42
// Collect warnings from every column
40
43
override val warnings = columns.flatMap(_.warnings)
41
44
42
45
// Tuple with lengths of each column in formatted DDL file
43
46
private val tabulation = {
44
- def getLength (f : Column => Int ): Int =
47
+ def getLength (f : Column [ T ] => Int ): Int =
45
48
columns.foldLeft(0 )((acc, b) => if (acc > f(b)) acc else f(b))
46
49
47
50
val prepend = 4
@@ -54,26 +57,25 @@ case class CreateTable(
54
57
}
55
58
56
59
/**
57
- * Format constraints for table
58
- *
59
- * @return string with formatted table_constaints
60
- */
60
+ * Format constraints for table
61
+ *
62
+ * @return string with formatted table_constaints
63
+ */
61
64
private def getConstraints : String = {
62
65
if (tableConstraints.isEmpty) " "
63
66
else " ,\n " + tableConstraints.map(c => withTabs(tabulation._1, " " ) + c.toDdl).
64
67
65
68
mkString(" \n " )
66
69
}
67
70
/**
68
- * Format attributes for table
69
- *
70
- * @return string with formatted table_attributes
71
- */
71
+ * Format attributes for table
72
+ *
73
+ * @return string with formatted table_attributes
74
+ */
72
75
private def getAttributes : String = {
73
76
if (tableConstraints.isEmpty) " "
74
77
else " \n " + tableAttributes.map(_.toDdl).
75
78
76
79
mkString(" \n " )
77
80
}
78
- }
79
-
81
+ }
0 commit comments