internal static List <DBColumnInfo> ParseColumns(JsonElement baseElement) { List <DBColumnInfo> rtnInfos = new List <DBColumnInfo>(); foreach (JsonElement columnElement in baseElement.EnumerateArray()) { DBColumnInfo cInfo = new DBColumnInfo(); cInfo.columnIdenfier = columnElement.GetProperty("name").GetString(); string typeStr = columnElement.GetProperty("typeStr").GetString(); if (typeStr.Contains(":")) { //extract and parse meta, process new typeStr as wells string[] splitStr = typeStr.Split(":"); typeStr = splitStr[0]; cInfo.columnMeta = splitStr[1].Split(','); } byte typeNum = byte.Parse(typeStr); if (!Enum.IsDefined(typeof(ColumnType), typeNum)) { Logger.Critical?.WriteLine("[RookDB] Attempted to parsed column with type " + typeStr + ". This value is not defined, ignoring column."); continue; } cInfo.columnType = (ColumnType)typeNum; rtnInfos.Add(cInfo); } return(rtnInfos); }
public static string PrettyPrintColumn(DBColumnInfo column) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("identifier="); sb.Append(column.columnIdenfier); sb.Append(", type="); sb.Append(column.columnType.ToString()); if (column.columnType == ColumnType.Enumeration || column.columnType == ColumnType.Flags) { //also display all available options sb.Append(", values="); for (int i = 0; i < column.columnMeta.Length; i++) { if (i > 0) { sb.Append("|"); } sb.Append((string)column.columnMeta[i]); } } else if (column.columnType == ColumnType.Reference) { sb.Append(", baseTable="); //this is the table that references will be directed to (cant know exact records, as those are stored on the records themselves) sb.Append((string)column.columnMeta[0]); } return(sb.ToString()); }
internal static object GetDefaultFieldValue(DBColumnInfo colInfo) { switch (colInfo.columnType) { case ColumnType.Boolean: return(false); case ColumnType.Color: return(new byte[] { 0, 0, 0 }); case ColumnType.Enumeration: return(colInfo.columnMeta[0]); case ColumnType.Flags: return(new string[0]); case ColumnType.Float: return(0f); case ColumnType.Integer: return(0); case ColumnType.List: return(null); default: return(""); } }
public static string PrettyPrintField(DBRecord record, int fieldIdx) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); DBColumnInfo columnInfo = record.ownerTable.columns[fieldIdx]; sb.Append(columnInfo.columnIdenfier); sb.Append(" ("); sb.Append(columnInfo.columnType); sb.Append(")="); sb.Append(PrettyPrintFieldValue(record, fieldIdx)); return(sb.ToString()); }
private static void WriteColumn(DBColumnInfo column, Utf8JsonWriter writer) { writer.WriteStartObject(); string typeString = ((byte)column.columnType).ToString(); if (column.columnMeta != null) { string typeMeta = StringHelper.SquishArray(column.columnMeta).Replace(" ", ""); typeString = typeString + ":" + typeMeta; } writer.WriteString("typeStr", typeString); writer.WriteString("name", column.columnIdenfier); //NOTE: RookDB supports reading columns marked optional from castleDB files, but it dosnt support them internally, hence it dosnt write out the value. writer.WriteEndObject(); }
public void AddColumn(string path, DBColumnInfo columnInfo) { string[] pathParts = path.Split('/', StringSplitOptions.RemoveEmptyEntries); if (pathParts.Length != 2 || columnInfo.columnIdenfier != pathParts[1]) { Logger.Info?.WriteLine("[RookDB] Cannot add column, column name/format incorrect."); return; } if (ColumnExists(path)) { Logger.Info?.WriteLine("[RookDB] Column already exists in this name by that name. Please check entered details."); return; } tables[pathParts[0]].columns.Add(columnInfo); foreach (DBRecord record in tables[pathParts[0]].records.Values) { object[] newVals = new object[record.values.Length + 1]; Array.Copy(record.values, 0, newVals, 0, record.values.Length); newVals[record.values.Length] = GetDefaultFieldValue(columnInfo); record.values = newVals; } }
private static void WriteRecord(DBRecord record, Utf8JsonWriter writer) { writer.WriteStartObject(); List <DBColumnInfo> columns = record.ownerTable.columns; for (int colIdx = 0; colIdx < columns.Count; colIdx++) { DBColumnInfo column = columns[colIdx]; switch (column.columnType) { case ColumnType.Boolean: writer.WriteBoolean(column.columnIdenfier, (bool)record.values[colIdx]); break; case ColumnType.Color: /* Probably my least favourite encoding of all time (I didnt design this! This is from castleDB spec) * Colours can only be RGB, alpha channel is not supported unfortunately. * Encoding works by taking each colour as 8bits (RGB24 format), converting each channel individually into a 2 digit hex number (0's needed for padding). * The 3 hex values are appended (RRGGBB) and converted to a single base10 integer. * This base10 int is what is written to disk. */ byte[] colArr = (byte[])record.values[colIdx]; string hexStr = Convert.ToString(colArr[0], 16); hexStr += Convert.ToString(colArr[1], 16); hexStr += Convert.ToString(colArr[2], 16); int compiledInt = Convert.ToInt32(hexStr, 16); writer.WriteNumber(column.columnIdenfier, compiledInt); break; case ColumnType.Enumeration: for (int i = 0; i < column.columnMeta.Length; i++) { if (column.columnMeta[i] == (string)record.values[colIdx]) { writer.WriteNumber(column.columnIdenfier, i); break; } } break; case ColumnType.Flags: int bitArray = 0; List <string> recordFlags = new List <string>((string[])record.values[colIdx]); for (int i = 0; i < column.columnMeta.Length && i < 32; i++) { if (recordFlags.Contains(column.columnMeta[i])) { bitArray = bitArray | (1 << i); } } writer.WriteNumber(column.columnIdenfier, bitArray); break; case ColumnType.Float: writer.WriteNumber(column.columnIdenfier, (float)record.values[colIdx]); break; case ColumnType.Integer: writer.WriteNumber(column.columnIdenfier, (int)record.values[colIdx]); break; case ColumnType.List: //TODO break; case ColumnType.Reference: string refId = (string)record.values[colIdx]; refId = refId.Remove(0, refId.IndexOf('/') + 1); writer.WriteString(column.columnIdenfier, refId); break; case ColumnType.Text: case ColumnType.UniqueIdentifier: writer.WriteString(column.columnIdenfier, (string)record.values[colIdx]); break; default: throw new Exception("*explosions* BOOOOM, this should not happen. (RookDB.WriteRecord tried to write a column with an invalid type.)"); } } writer.WriteEndObject(); }
public void UpdateColumn(string path, DBColumnInfo newColumnInfo) { throw new NotImplementedException(); }