Kinetica   C#   API  Version 7.2.3.1
Column.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace kinetica.Records;
6 
11  public sealed class Column
12  {
13  private readonly string _name;
14  private readonly ColumnType _columnType;
15  private readonly IReadOnlyList<string> _properties;
16  private readonly bool _isNullable;
17  private readonly bool _isPrimaryKey;
18  private readonly bool _isShardKey;
19 
25  public Column(string name, ColumnType columnType)
26  : this(name, columnType, Array.Empty<string>())
27  {
28  }
29 
36  public Column(string name, ColumnType columnType, IEnumerable<string> properties)
37  {
38  _name = name ?? throw new ArgumentNullException(nameof(name));
39  _columnType = columnType;
40  _properties = properties?.ToList().AsReadOnly() ?? (IReadOnlyList<string>)Array.Empty<string>();
41  _isNullable = _properties.Any(p => string.Equals(p, "nullable", StringComparison.OrdinalIgnoreCase));
42  _isPrimaryKey = _properties.Any(p => string.Equals(p, "primary_key", StringComparison.OrdinalIgnoreCase));
43  _isShardKey = _properties.Any(p => string.Equals(p, "shard_key", StringComparison.OrdinalIgnoreCase));
44  }
45 
50  public static Column WithProperties(string name, ColumnType columnType, IEnumerable<string> properties)
51  => new Column(name, columnType, properties);
52 
54  public string Name => _name;
55 
57  public ColumnType ColumnType => _columnType;
58 
60  public ColumnBaseType BaseType => _columnType.GetBaseType();
61 
63  public IReadOnlyList<string> Properties => _properties;
64 
66  public bool IsNullable => _isNullable;
67 
69  public bool IsPrimaryKey => _isPrimaryKey;
70 
72  public bool IsShardKey => _isShardKey;
73 
77  public string AvroTypeName => _columnType.GetAvroTypeName();
78 
82  public Column WithAddedProperties(params string[] additionalProperties)
83  {
84  var newProps = _properties.Concat(additionalProperties).Distinct().ToList();
85  return new Column(_name, _columnType, newProps);
86  }
87 
91  public Column AsNullable()
92  {
93  if (_isNullable) return this;
94  return WithAddedProperties("nullable");
95  }
96 
101  {
102  if (_isPrimaryKey) return this;
103  return WithAddedProperties("primary_key");
104  }
105 
110  {
111  if (_isShardKey) return this;
112  return WithAddedProperties("shard_key");
113  }
114 
115  public override string ToString()
116  {
117  var props = _properties.Count > 0 ? $" [{string.Join(", ", _properties)}]" : "";
118  return $"{_name}: {_columnType}{props}";
119  }
120 
121  public override bool Equals(object? obj)
122  {
123  if (obj is not Column other) return false;
124  return _name == other._name &&
125  _columnType == other._columnType &&
126  _properties.SequenceEqual(other._properties);
127  }
128 
129  public override int GetHashCode()
130  {
131  return HashCode.Combine(_name, _columnType);
132  }
133  }
override bool Equals(object? obj)
Definition: Column.cs:121
ColumnBaseType
Base column types that map to Avro primitive types.
Definition: ColumnType.cs:7
override string ToString()
Definition: Column.cs:115
Column AsNullable()
Creates a copy of this column marked as nullable.
Definition: Column.cs:91
override int GetHashCode()
Definition: Column.cs:129
bool IsShardKey
Returns true if this column is a shard key.
Definition: Column.cs:72
bool IsPrimaryKey
Returns true if this column is a primary key.
Definition: Column.cs:69
Immutable metadata about a column in a Kinetica type.
Definition: Column.cs:11
Column(string name, ColumnType columnType, IEnumerable< string > properties)
Creates a new column with the given name, type, and properties.
Definition: Column.cs:36
IReadOnlyList< string > Properties
Gets the column properties.
Definition: Column.cs:63
string AvroTypeName
Returns the Avro type name for this column.
Definition: Column.cs:77
Column AsPrimaryKey()
Creates a copy of this column marked as primary key.
Definition: Column.cs:100
Column WithAddedProperties(params string[] additionalProperties)
Creates a copy of this column with the specified properties added.
Definition: Column.cs:82
Column AsShardKey()
Creates a copy of this column marked as shard key.
Definition: Column.cs:109
bool IsNullable
Returns true if this column is nullable.
Definition: Column.cs:66
static Column WithProperties(string name, ColumnType columnType, IEnumerable< string > properties)
Creates a column with properties from a list.
ColumnBaseType BaseType
Gets the base type for this column.
Definition: Column.cs:60
string Name
Gets the column name.
Definition: Column.cs:54
Column(string name, ColumnType columnType)
Creates a new column with the given name and type.
Definition: Column.cs:25