/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.CachedPage(pageNum); if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; session.UpdatePage(ref page); } pageInfoString = textReader.ReadLine(); } } var schemaInternalTypeFiles = new[] { "VelocityDb.TypeInfo.Schema65747.csv", "VelocityDb.TypeInfo.TypeVersion65749.csv", "VelocityDb.TypeInfo.VelocityDbType65753.csv", "VelocityDb.TypeInfo.DataMember65745.csv", "VelocityDb.Collection.BTree.BTreeSetOidShortVelocityDb.TypeInfo.VelocityDbType65623.csv", "VelocityDb.Collection.Comparer.VelocityDbTypeComparer65655.csv" }; var schemaInternalTypeFileInfo = new List <FileInfo>(); foreach (var fileName in schemaInternalTypeFiles) { schemaInternalTypeFileInfo.Add(di.GetFiles(fileName, SearchOption.TopDirectoryOnly).First()); } foreach (FileInfo info in schemaInternalTypeFileInfo) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } Database schemaDb = session.OpenDatabase(Schema.SchemaDB); Page schemaPage = schemaDb.CachedPage(1); schemaPage.FinishUpCsvImport(); var schemaTypes = new UInt32[] { 65747, 65749, 65753, 65745, 65623, 65655 }; for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if (((i == 0 && slotNumber < Schema.s_bootupTypeCountExpanded) || (i == 1 && slotNumber >= Schema.s_bootupTypeCountExpanded)) && !schemaTypes.Contains(typeShortId)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { var csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } } } }
/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.CachedPage(pageNum); if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; session.UpdatePage(ref page); } pageInfoString = textReader.ReadLine(); } } for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if ((i == 0 && slotNumber < Schema.s_bootupTypeCount) || (i == 1 && slotNumber >= Schema.s_bootupTypeCount)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } } } }