Results 1 to 4 of 4

Thread: Is it possible to store a struct instead of a class in database?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    4

    Default Is it possible to store a struct instead of a class in database?

    Is it possible to store a struct instead of a class in database?

  2. #2

    Default

    Yes, the database supports storing of structs, it will store all public read/write properties of the specified struct. See also supported classes and structs in FAQ.

    The difference between storing of classes and structs is, that by default structs are always stored in raw mode, without compression.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    4

    Default

    class Program
        {
            const string DbPath = "test.stsdb";
            //
            static void Main(string[] args)
            {
                if (!File.Exists(DbPath))
                    Write();
                else
                    Read();
                //
                Console.ReadKey();
            }
            //
            public static void Write()
            {
                StorageEngine storageEngine = StorageEngine.FromFile(DbPath);
                var table = storageEngine.Scheme.CreateOrOpenXTable<int, SomeType>(new Locator("SomeType"));
                storageEngine.Scheme.Commit();
                table[1] = new SomeType { Val1 = "Val1", Val2 = "Val2", Val3 = "Val3" };
                table.Commit();
            }
            //
            public static void Read()
            {
                StorageEngine storageEngine = StorageEngine.FromFile(DbPath);
                var table = storageEngine.Scheme.CreateOrOpenXTable<int, SomeType>(new Locator("SomeType"));
                storageEngine.Scheme.Commit();
                //
                try
                {
                    foreach (var row in table)
                        Console.WriteLine("{0},{1},{2}", row.Record.Val1, row.Record.Val2, row.Record.Val3);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e);
                }
            }
        }
        //
        public struct SomeType
        {
            public string Val1 { get; set; }
            public string Val2 { get; set; }
            public string Val3 { get; set; }
        }
    
    //
    First execution write to database, second and subsequent read. When SomeType is "class" alll is Ok, but when "struct" it is not work.

  4. #4

    Default

    This is a bug in the case that the struct is with a non-fixed length (i.e. it contains field with a non-fixed length as in your example above).

    The structs with a fixed length should not be a problem, but the non-fixed ones are problem indeed.

    We will discuss what would had to be the engine behaviour in that case. Currently, the structs are treated in a different way, than the primary one with classes and primitives. I suppose that we will eliminate the difference in the next release.

    Thank you for your report!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
2002 - 2014 STS Soft SC. All Rights reserved.
STSdb, Waterfall Tree and WTree are registered trademarks of STS Soft SC.