Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Moving between datasessions
Message
From
11/01/2008 13:38:12
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
11/01/2008 13:00:26
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
Miscellaneous
Thread ID:
01281189
Message ID:
01281207
Views:
11
>We have several routines which run SQL SELECT statements against tables which may or may not already be open. In addition, they may open an unknown number of additional cursors (cur(1), cur(2),...cur(n) depending on the data.
>
>It might be better to write a cleanup routine but I was wondering if it is possible to open a new datasession, do the processing there, move the resulting cursor to the main datasession, and then close the new datasession.
>
>So............Is there a simple way to copy a cursor from one datasession to another?
>
>Thanks......Rich

If you're asking for the 'simple' way then instead of a cursor use a table and open it in the other session.
Otherwise if the data is not large you might use something like this:
* DatasessionPass.prg
***********************************************
* Sample code using DataSet class
***********************************************
Local loSession1, loSession2
* Create 1st private datasession
loSession1 = Createobject('Session1')
loSession1.SomeQueries() && run some queries

loSession2 = Createobject('Session2')
loSession2.GetTheDataAndBrowse(loSession1) && transfer data from session1


Define Class session1 As Session
  DataSession = 2 && Private

  Procedure SomeQueries

    Select * From (_samples+'data\customer') Where country = 'USA' Into Cursor crsUSA
    Select ord.* From (_samples+'data\orders') ord ;
      INNER Join crsUSA On crsUSA.cust_id == ord.cust_id ;
      ORDER By ord.cust_id, ord.order_date ;
      INTO Cursor crsUSAOrders ;
      nofilter

  Endproc

  Procedure GetDataCursors( tcAliasList, tcAsAliasList )

    If Empty(m.tcAsAliasList)
      tcAsAliasList = m.tcAliasList
    Endif

    Local laAlias[1], laAsAlias[1]
    Local oDataSetPacker As 'DataSet' Of Fullpath('DatasessionPass.prg')
    Local ix, lcAlias, lcAsAlias
    oDataSetPacker = Newobject('DataSet',Fullpath('DatasessionPass.prg'))


    Alines(laAsAlias, m.tcAsAliasList,1,',')
    For ix = 1 To Alines(laAlias, m.tcAliasList,1,',')
      lcAlias   = laAlias[m.ix]
      lcAsAlias = Iif(m.ix > Alen( laAsAlias ), laAlias[m.ix], laAsAlias[m.ix] )
      oDataSetPacker.AddTable(m.lcAlias, m.lcAsAlias)
    Endfor
    Return oDataSetPacker
  Endproc

Enddefine

Define Class session2 As Session
  DataSession = 2 && Private

  Procedure GetTheDataAndBrowse(toOtherSession)
    Local loDataSet
    loDataSet = toOtherSession.GetDataCursors( ;
      'crsUSA, crsUSAOrders', 'crsCustomer,crsOrders' )
    loDataSet.RestoreDataset(This.DataSessionId)
    Select crsCustomer
    Browse
    Select crsOrders
    Browse
  Endproc

Enddefine
***********************************************
* Sample code using DataSet class
***********************************************



**************************************************************************************
** Dataset class to pack given cursors into an object to pass between data sessions
**************************************************************************************
Define Class DataSet As Custom
  TableCount = 0
  DataSessionId = 0
  Dimension TableInfo[1]

  Procedure Init
    This.DataSessionId = Set("Datasession")
  Endproc

  Procedure AddTable(tcAlias, tcAsAlias)
    Local Array aStruc[1],aStripped[1]
    Afields(aStruc,m.tcAlias)
    Dimension aStripped[ALEN(aStruc,1),5]
    For ix = 1 To Alen(aStruc,1)
      aStripped[m.ix,1] = aStruc[m.ix,1]
      aStripped[m.ix,2] = aStruc[m.ix,2]
      aStripped[m.ix,3] = aStruc[m.ix,3]
      aStripped[m.ix,4] = aStruc[m.ix,4]
      aStripped[m.ix,5] = aStruc[m.ix,5]
    Endfor
    This.TableCount = This.TableCount + 1
    Dimension This.TableInfo[this.TableCount]
    This.TableInfo[this.TableCount] = Createobject('Custom')
    With This.TableInfo[this.TableCount]
      .AddProperty('aStructure[1]')
      .AddProperty('aRecords[1]')
      .AddProperty('Records')
      .AddProperty('Alias',m.tcAlias)
      .AddProperty('AsAlias', Iif(Empty(m.tcAsAlias),m.tcAlias,m.tcAsAlias) )

      Acopy(aStripped, .aStructure)
      Select * From (m.tcAlias) Where !Deleted() Into Cursor __crsTemp__ nofilter
      .RecordS = Reccount()
      If .RecordS > 0
        Dimension .aRecords[RECCOUNT()]
        Scan
          Scatter Name .aRecords[RECNO()] Memo
        Endscan
      Endif
      Use In '__crsTemp__'
    Endwith
  Endproc

  Procedure RestoreDataset(tnDataSessionID) && restore all with saved aliases
    For ix = 1 To This.TableCount
      This.Obj2Cursor( This.TableInfo[m.ix].Alias, '', m.tnDataSessionID )
    Endfor
  Endproc

  Procedure Obj2Cursor( tcStoredAlias, tcAlias, tnDataSessionID )
    Local ix,jx
    If !Empty(m.tnDataSessionID)
      Set DataSession To m.tnDataSessionID
    Endif
    For ix = 1 To This.TableCount
      With This.TableInfo[m.ix]
        If Upper(.Alias) == Upper(m.tcStoredAlias)
          tcAlias = Iif(Empty(m.tcAlias), .AsAlias, m.tcAlias )
          Create Cursor (m.tcAlias) From Array .aStructure
          For jx = 1 To .RecordS
            Append Blank
            Gather Name .aRecords[m.jx] Memo
          Endfor
          Exit
        Endif
      Endwith
    Endfor
    Set DataSession To This.DataSessionId
  Endproc
Enddefine
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform