sending a db row to client

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

FireSmartFox
Posts: 33
Joined: 28 Sep 2018, 19:47

sending a db row to client

Postby FireSmartFox » 10 Dec 2018, 20:33

I want to put one db row result to a SFSObject to send it to the user. There are max. 1-2 rows at the same time and for some reasons I want to do separate submits, not the complete result.

Code: Select all

ISFSArray res = dbManager.executeQuery(sql, new Object[] {});
               if (res.size() > 0) {
                  for (int i = 0; i < res.size(); i++) {
                     ISFSObject temp = res.getSFSObject(i);
                     
                     SFSObject resObj = new SFSObject();
                     resObj.putSFSArray("dbzeile", res.getSFSArray(i));
                     // ...
                  }
               }

The last line with the putSFSArray causes a conversion error
com.smartfoxserver.v2.entities.data.SFSObject cannot be cast to com.smartfoxserver.v2.entities.data.ISFSArray

What is the easiest why to put one db row into the Sfsobject?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: sending a db row to client

Postby Lapo » 11 Dec 2018, 08:30

Hi,
I am not sure I understand. The query returns 1-2 rows max but you want to send them separately?
That doesn't sound too efficient. It's usually better to send data in one batch, unless it's very big or there's some other particular reason.

The simpler thing to do here is to just send the whole SFSArray returned by the executeQuery method.
Just wrap it into and SFSObject and send it:

Code: Select all

ISFSArray dbRes = dbManager.executeQuery(sql, new Object[] {});

if (dbRes.size() > 0)
{
   ISFSObject resp = new SFSObject();
   resp.putSFSArray("data", dbRes);
   
   send("command", resp, ... recipients...);
}


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
FireSmartFox
Posts: 33
Joined: 28 Sep 2018, 19:47

Re: sending a db row to client

Postby FireSmartFox » 11 Dec 2018, 10:37

Lapo wrote:Hi,
I am not sure I understand. The query returns 1-2 rows max but you want to send them separately?

Yes.

Lapo wrote:That doesn't sound too efficient. It's usually better to send data in one batch, unless it's very big or there's some other particular reason.

There are. The receiver for each row are different, so in the loop I assign each row to a different receiver depending to the row values.

Lapo wrote:The simpler thing to do here is to just send the whole SFSArray returned by the executeQuery method.
Just wrap it into and SFSObject and send it:

Yes, I know this possibility from the example code of the documentation. But it doesn't fit to my requirement to have different receiver which shouldn't get all rows or if I want to add some information to the single requests.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: sending a db row to client

Postby Lapo » 11 Dec 2018, 15:39

FireSmartFox wrote:There are. The receiver for each row are different, so in the loop I assign each row to a different receiver depending to the row values.

Okay, makes sense.

Lapo wrote:Yes, I know this possibility from the example code of the documentation. But it doesn't fit to my requirement to have different receiver which shouldn't get all rows or if I want to add some information to the single requests.


Every item in the result SFSArray is an SFSObject, containing the data for each column in your "SELECT" statement. You just need to send the SFSObject itself, extracted from the SFSArray.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Stevenor and 58 guests