Coverage Report

Created: 2023-06-07 06:06

/src/sql-parser/src/sql/CreateStatement.h
Line
Count
Source
1
#ifndef SQLPARSER_CREATE_STATEMENT_H
2
#define SQLPARSER_CREATE_STATEMENT_H
3
4
#include "ColumnType.h"
5
#include "SQLStatement.h"
6
7
#include <ostream>
8
#include <unordered_set>
9
10
// Note: Implementations of constructors and destructors can be found in statements.cpp.
11
namespace hsql {
12
struct SelectStatement;
13
14
enum struct ConstraintType { None, NotNull, Null, PrimaryKey, Unique };
15
16
// Superclass for both TableConstraint and Column Definition
17
struct TableElement {
18
358k
  virtual ~TableElement() {}
19
};
20
21
// Represents definition of a table constraint
22
struct TableConstraint : TableElement {
23
  TableConstraint(ConstraintType keyType, std::vector<char*>* columnNames);
24
25
  ~TableConstraint() override;
26
27
  ConstraintType type;
28
  std::vector<char*>* columnNames;
29
};
30
31
// Represents definition of a table column
32
struct ColumnDefinition : TableElement {
33
  ColumnDefinition(char* name, ColumnType type, std::unordered_set<ConstraintType>* column_constraints);
34
35
  ~ColumnDefinition() override;
36
37
  // By default, columns are nullable. However, we track if a column is explicitly requested to be nullable to
38
  // notice conflicts with PRIMARY KEY table constraints.
39
214k
  bool trySetNullableExplicit() {
40
214k
    if (column_constraints->count(ConstraintType::NotNull) || column_constraints->count(ConstraintType::PrimaryKey)) {
41
2.77k
      if (column_constraints->count(ConstraintType::Null)) {
42
1.25k
        return false;
43
1.25k
      }
44
1.51k
      nullable = false;
45
1.51k
    }
46
47
213k
    return true;
48
214k
  }
49
50
  std::unordered_set<ConstraintType>* column_constraints;
51
  char* name;
52
  ColumnType type;
53
  bool nullable;
54
};
55
56
enum CreateType {
57
  kCreateTable,
58
  kCreateTableFromTbl,  // Hyrise file format
59
  kCreateView,
60
  kCreateIndex
61
};
62
63
// Represents SQL Create statements.
64
// Example: "CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)"
65
struct CreateStatement : SQLStatement {
66
  CreateStatement(CreateType type);
67
  ~CreateStatement() override;
68
69
  void setColumnDefsAndConstraints(std::vector<TableElement*>* tableElements);
70
71
  CreateType type;
72
  bool ifNotExists;                                 // default: false
73
  char* filePath;                                   // default: nullptr
74
  char* schema;                                     // default: nullptr
75
  char* tableName;                                  // default: nullptr
76
  char* indexName;                                  // default: nullptr
77
  std::vector<char*>* indexColumns;                 // default: nullptr
78
  std::vector<ColumnDefinition*>* columns;          // default: nullptr
79
  std::vector<TableConstraint*>* tableConstraints;  // default: nullptr
80
  std::vector<char*>* viewColumns;
81
  SelectStatement* select;
82
};
83
84
}  // namespace hsql
85
86
#endif