// Delete the given table. public void deleteTable(ref FileDBTable table) { tables.Remove(table); deletedTables.Add(table.Name); table = null; }
private FileDBTable loadTable(String tableName) { if (File.Exists(dbPath + @"\" + tableName + tbExtention)) { String[] tableLines = File.ReadAllLines(dbPath + @"\" + tableName + tbExtention); FileDBTable table = new FileDBTable(this, tableName); // Split column types into array and column names into array. String[] types = tableLines[2].Split(','); String[] names = tableLines[3].Split(','); // Add columns to table. for (int i = 0; i < names.Length; i++) { table.addColumn(names[i], Type.GetType(types[i])); } // Add rows to table. for (int i = 4; i < tableLines.Length; i++) { String[] rowIDData = tableLines[i].Split(','); int rowID = Convert.ToInt32(rowIDData[0]); String[] rowData = new String[rowIDData.Length-1]; Array.Copy(rowIDData, rowData, rowData.Length); table.addRow(rowID, rowData); } // Return the table. return table; } else { // If table file not found. throw new FileNotFoundException("Table '" + tableName + "' not found!"); } }
// Create a new table. public FileDBTable createTable(String name) { FileDBTable table = new FileDBTable(this, name); tables.Add(table); return table; }
public void saveTable(FileDBTable table) { StringBuilder tableString = new StringBuilder(); tableString.AppendLine(table.Name); tableString.AppendLine(table.ColumnCount.ToString()); // Column types for (int i = 0; i < table.ColumnCount; i++) { tableString.Append(table.ColumnTypes[i]); if (i != table.ColumnCount - 1) tableString.Append(","); } tableString.AppendLine(); // Column names. for (int i = 0; i < table.ColumnCount; i++) { tableString.Append(table.ColumnNames[i]); if (i != table.ColumnCount - 1) tableString.Append(","); } tableString.AppendLine(); // Column rows. for (int i = 0; i < table.RowCount; i++) { FileDBRow row = table.Rows[i]; tableString.Append(row.getID() + ","); for (int j = 0; j < table.ColumnCount; j++) { tableString.Append(row[j]); if (j != table.ColumnCount - 1) tableString.Append(","); } tableString.AppendLine(); } /*****************/ // Save to a file if (!Directory.Exists(dbPath)) Directory.CreateDirectory(dbPath); File.WriteAllText(dbPath + @"\" + table.Name + tbExtention, tableString.ToString()); }