Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db_mysql testing getRow() for no results #11

Open
jlp765 opened this issue Aug 15, 2015 · 8 comments
Open

db_mysql testing getRow() for no results #11

jlp765 opened this issue Aug 15, 2015 · 8 comments

Comments

@jlp765
Copy link
Contributor

jlp765 commented Aug 15, 2015

Is there a better way of identifying "query returned no result" other then testing that each field of returned seq[string] is an empty string?

Something like a tuple result, Tuple[hasData: bool, Row: seq[string]]
so that the user knows to process the row only if it hasData

@Araq
Copy link
Member

Araq commented Aug 25, 2015

meh but then you need to use tup.row[i] instead of tup[i] everywhere. Much better is a helper proc 'isNull'. And yes, I know nobody will agree with me.

@jlp765
Copy link
Contributor Author

jlp765 commented Aug 25, 2015

Ok, I was thinking the following was possible (a Python technique)

  (hasData, myRow) = myDb.getRow(....)
  (hasData, myRows) = myDb.getAllRows(...)
  (hasData, myValu) = myDb.getValue(...)

but have just learned that Nim can't do that.

It would be a useful feature that would eliminate the tup.row[i] you refered to :-)

@Araq
Copy link
Member

Araq commented Aug 25, 2015

Er ... what? Nim of course can do tuple unpacking.

@jlp765
Copy link
Contributor Author

jlp765 commented Aug 25, 2015

(I am probably missing the obvious, but ....)

So how do you set two independant variables from a proc returning a tuple?

type
  myTupl = tuple[b: bool, i: int]
var
  tup1  = (isOk: false, val: 42)

proc ttest(v: int): myTupl =
    result = (false, v)

proc main() =
  var
    isO: bool = true
    v = 43
  isO,v=ttest(44)      # Error: value of type 'bool' has to be discarded
  (isO,v)=ttest(44)    # Error: '(isO, v)' cannot be assigned to
  echo isO, " ",v

main()

In the current issue, getValue() would return a tuple of (bool, string), the user can then do

  hasData, myValu = getValue(.....)
  if hasData:
    # do something with myValu

and there is no issue with having to do tup.row[i] rather than just row[i] as you mentioned

@refi64
Copy link

refi64 commented Aug 25, 2015

proc main() =
  let (isO,v)=ttest(44)
  # ..

@jlp765
Copy link
Contributor Author

jlp765 commented Aug 26, 2015

Ok,

Then my proposal is to either
modify getValue(), getRow(), getAllRows() to return a tuple of (bool, string) or (bool, Row) as appropriate
OR
add new equivalent procs, so you can choose to use the existing procs as they are (backwards compatible), or choose to use the new ones that let you know if there is valid data returned

For my further education, why MUST it be via let. It means you can't assign to existing variables?
It seems inconsistent (to me) that a proc result of a single value can be assigned to a single variable, but if a proc returns a tuple it can't be assigned to a "tuple" of variables

@Araq
Copy link
Member

Araq commented Aug 26, 2015

For my further education, why MUST it be via let. It means you can't assign to existing variables?

In devel you can do just that for consistency. Note that IMO it's rarely required.

@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

@ringabout ringabout transferred this issue from nim-lang/Nim Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants