private static string GetTypeString(PocoColumn column, ColumnTypeCode typeCode, IColumnInfo typeInfo) { if (typeInfo == null) { //Is a single type return(typeCode.ToString().ToLower()); } string typeName = null; var frozenKey = column != null && column.HasFrozenKey; var frozenValue = column != null && column.HasFrozenValue; if (typeInfo is MapColumnInfo mapInfo) { typeName = "map<" + CqlGenerator.WrapFrozen(frozenKey, CqlGenerator.GetTypeString(null, mapInfo.KeyTypeCode, mapInfo.KeyTypeInfo)) + ", " + CqlGenerator.WrapFrozen(frozenValue, CqlGenerator.GetTypeString(null, mapInfo.ValueTypeCode, mapInfo.ValueTypeInfo)) + ">"; } else if (typeInfo is SetColumnInfo setInfo1) { typeName = "set<" + CqlGenerator.WrapFrozen(frozenKey, CqlGenerator.GetTypeString(null, setInfo1.KeyTypeCode, setInfo1.KeyTypeInfo)) + ">"; } else if (typeInfo is ListColumnInfo setInfo) { typeName = "list<" + CqlGenerator.WrapFrozen(frozenValue, CqlGenerator.GetTypeString(null, setInfo.ValueTypeCode, setInfo.ValueTypeInfo)) + ">"; } else if (typeInfo is TupleColumnInfo tupleInfo) { typeName = "tuple<" + string.Join(", ", tupleInfo.Elements.Select(e => CqlGenerator.GetTypeString(null, e.TypeCode, e.TypeInfo))) + ">"; } else if (typeInfo is UdtColumnInfo udtInfo) { // Escape keyspace and name from the UDT typeName = string.Join(".", udtInfo.Name.Split('.').Select(k => "\"" + k + "\"")); } if (typeName == null) { throw new NotSupportedException($"Type {typeCode} is not supported"); } return(CqlGenerator.WrapFrozen(column != null && column.IsFrozen, typeName)); }
private static string GetTypeString(Serializer serializer, PocoColumn column) { string typeName; if (!column.IsCounter) { var typeCode = serializer.GetCqlType(column.ColumnType, out var typeInfo); typeName = CqlGenerator.GetTypeString(column, typeCode, typeInfo); } else { typeName = "counter"; } if (column.IsStatic) { return(typeName + " static"); } return(typeName); }
/// <summary> /// Gets the CQL queries involved in a table creation (CREATE TABLE, CREATE INDEX) /// </summary> public static List <string> GetCreate(Serializer serializer, PocoData pocoData, string tableName, string keyspaceName, bool ifNotExists) { if (pocoData == null) { throw new ArgumentNullException(nameof(pocoData)); } if (pocoData.MissingPrimaryKeyColumns.Count > 0) { throw new InvalidOperationException(string.Format(CqlGenerator.MissingPkColumns, "CREATE", pocoData.PocoType.Name, pocoData.MissingPrimaryKeyColumns.ToCommaDelimitedString())); } var commands = new List <string>(); var secondaryIndexes = new List <string>(); var createTable = new StringBuilder("CREATE TABLE "); tableName = CqlGenerator.CqlIdentifierHelper.EscapeIdentifierIfNecessary(pocoData, tableName); if (keyspaceName != null) { //Use keyspace.tablename notation tableName = CqlGenerator.CqlIdentifierHelper.EscapeIdentifierIfNecessary(pocoData, keyspaceName) + "." + tableName; } createTable.Append(tableName); createTable.Append(" ("); foreach (var column in pocoData.Columns) { var columnName = CqlGenerator.CqlIdentifierHelper.EscapeIdentifierIfNecessary(pocoData, column.ColumnName); createTable .Append(columnName) .Append(" "); var columnType = CqlGenerator.GetTypeString(serializer, column); createTable .Append(columnType); createTable .Append(", "); if (column.SecondaryIndex) { secondaryIndexes.Add(columnName); } } createTable.Append("PRIMARY KEY ("); if (pocoData.PartitionKeys.Count == 0) { throw new InvalidOperationException("No partition key defined"); } if (pocoData.PartitionKeys.Count == 1) { createTable.Append(CqlGenerator.CqlIdentifierHelper.EscapeIdentifierIfNecessary(pocoData, pocoData.PartitionKeys[0].ColumnName)); } else { //tupled partition keys createTable .Append("(") .Append(string.Join(", ", pocoData.PartitionKeys.Select(CqlGenerator.EscapeFunc(pocoData)))) .Append(")"); } if (pocoData.ClusteringKeys.Count > 0) { createTable.Append(", "); createTable.Append(string.Join( ", ", pocoData.ClusteringKeys.Select( k => CqlGenerator.CqlIdentifierHelper.EscapeIdentifierIfNecessary(pocoData, k.Item1.ColumnName)))); } //close primary keys createTable.Append(")"); //close table column definition createTable.Append(")"); var clusteringOrder = string.Join(", ", pocoData.ClusteringKeys .Where(k => k.Item2 != SortOrder.Unspecified) .Select(k => CqlGenerator.CqlIdentifierHelper.EscapeIdentifierIfNecessary(pocoData, k.Item1.ColumnName) + " " + (k.Item2 == SortOrder.Ascending ? "ASC" : "DESC"))); var clusteringOrderIsDefined = !string.IsNullOrEmpty(clusteringOrder); if (clusteringOrderIsDefined) { createTable .Append(" WITH CLUSTERING ORDER BY (") .Append(clusteringOrder) .Append(")"); } if (pocoData.CompactStorage) { createTable.Append($" {(clusteringOrderIsDefined ? "AND" : "WITH")} COMPACT STORAGE"); } commands.Add(createTable.ToString()); //Secondary index definitions commands.AddRange(secondaryIndexes.Select(name => "CREATE INDEX ON " + tableName + " (" + name + ")")); return(commands); }