Results 1 to 3 of 3

Thread: How to open any OpenXTable instance with differnt Schemes into one Variable?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Default How to open any OpenXTable instance with differnt Schemes into one Variable?

    I have some problem regarding opening XTable and Rows.

    srcTable = session.myDB.dbObject.Scheme.OpenXTable(Of Long, LibertyPIM_Vault)(New Locator("LibertyPIM_Vault"))

    I can't create duplicate functions in which I do actions for different tables. I want one and the same function in which I could do same actions for any XTable. I don't want to use Object data type. Is there a way by which I could open any XTable in a single XTable variable instance? For now, I have been creating different variables defined for any specific XTable(Of Long, Of "Class"). If there's a non-buggy code, please tell so that no bug or error arises. I want to have different XTables with different Schems in a single XTable Variable instance, so that I could create a single function to do same actions for any XTable of any Scheme.
    Last edited by aroratushar; 24.04.2011 at 15:15. Reason: word missing in title

  2. #2

    Default

    You can use the non-generic XTable class.

    The XTable is a lightweight wrapper of the generic XTable<TKey, TRecord>. The XTable class provides the opportunity to look at all heterogeneous generic XTables in homogeneous way - as a XTable<object,object>.

    For example, if we have two tables with different keys (or records)
        using (StorageEngine engine = StorageEngine.FromFile("test.stsdb"))
        {
            var table1 = engine.Scheme.CreateOrOpenXTable<int, string>(new Locator("table1"));
            var table2 = engine.Scheme.CreateOrOpenXTable<double, string>(new Locator("table2"));
            engine.Scheme.Commit();
    
            for (int i = 0; i < 100; i++)
                table1[i] = i.ToString();
            table1.Commit();
    
            for (int i = 0; i < 100; i++)
                table2[i / 10.0] = i.ToString();
            table2.Commit();
        }
    
    we can open them as a non-generic XTables and treat them the same way:
        using (StorageEngine engine = StorageEngine.FromFile("test.stsdb"))
        {
            XTable table1 = engine.Scheme.OpenXTable(new Locator("table1"));
            ShowInfo(table1);
    
            XTable table2 = engine.Scheme.OpenXTable(new Locator("table2"));
            ShowInfo(table2);
        }
    
    The ShowInfo() method is:
        public void ShowInfo(XTable table)
        {
            foreach (var row in table.Forward())
            {
                Console.WriteLine("{0} -> {1}", row.Key, row.Record);
            }
    
            Console.WriteLine("XTable<{0},{1}>", table.KeyType, table.RecordType);
            Console.WriteLine("{0} records", table.Count);
        }
    

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Default It is possible to get records for any Class specific XTable, by a single function.

    Yes I found the coding. To use the universal XTable Variable. Then I take out all the Records of the "Unknown" Class instances into an Arraylist. Later back in the calling function, it becomes possible to Cast the Record objects into their respective Types. Below is the function i just coded.

    
    #Region "Common Generic Database Functions"
    
        Public Shared Function GatherRecordsFromDB(ByRef inputDB as StorageEngine, ByVal inputLocaterName As String, ByRef outputArray As ArrayList) As Boolean
            Dim boolResult As Boolean = False
            If Not cMySTSDB.CheckDBSession(session) Then Return False
    
            Dim arrayRecords As New ArrayList()
    
            ' Open the XTable
            Try
                Dim srcTable = inputDB.Scheme.OpenXTable(New Locator(inputLocaterName))
                If IsNothing(srcTable) Then Return False
    
                Dim iIndex As Integer = 0
                For Each inputRow As Row(Of Object, Object) In srcTable
                    arrayRecords.Add(inputRow.Record)
                    iIndex += 1
                Next
                srcTable.Close()
                outputArray = arrayRecords ' Set the new array into the output
            Catch ex As Exception
                Return False
            End Try
    
            ' Success
            Return True
        End Function
    
    public sub FetchData(Byref inputDB as StorageEngine)
    
    dim arrayFound as ArrayList = nothing
    GatherRecordsFromDB(inputDB, "LocaterName",arrayFound)
    
    for each Record as Class1 in arrayFound
    
    ' todo
    
    for next
    
    end sub
    
    #End Region
    
    Quote Originally Posted by a.todorov View Post
    You can use the non-generic XTable class.

    The XTable is a lightweight wrapper of the generic XTable<TKey, TRecord>. The XTable class provides the opportunity to look at all heterogeneous generic XTables in homogeneous way - as a XTable<object,object>.

    For example, if we have two tables with different keys (or records)
        using (StorageEngine engine = StorageEngine.FromFile("test.stsdb"))
        {
            var table1 = engine.Scheme.CreateOrOpenXTable<int, string>(new Locator("table1"));
            var table2 = engine.Scheme.CreateOrOpenXTable<double, string>(new Locator("table2"));
            engine.Scheme.Commit();
    
            for (int i = 0; i < 100; i++)
                table1[i] = i.ToString();
            table1.Commit();
    
            for (int i = 0; i < 100; i++)
                table2[i / 10.0] = i.ToString();
            table2.Commit();
        }
    
    we can open them as a non-generic XTables and treat them the same way:
        using (StorageEngine engine = StorageEngine.FromFile("test.stsdb"))
        {
            XTable table1 = engine.Scheme.OpenXTable(new Locator("table1"));
            ShowInfo(table1);
    
            XTable table2 = engine.Scheme.OpenXTable(new Locator("table2"));
            ShowInfo(table2);
        }
    
    The ShowInfo() method is:
        public void ShowInfo(XTable table)
        {
            foreach (var row in table.Forward())
            {
                Console.WriteLine("{0} -> {1}", row.Key, row.Record);
            }
    
            Console.WriteLine("XTable<{0},{1}>", table.KeyType, table.RecordType);
            Console.WriteLine("{0} records", table.Count);
        }
    
    Last edited by aroratushar; 29.04.2011 at 00:38. Reason: coding was missing.

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.