nim-lang / db_connector

Unified db connector in Nim
MIT License
18 stars 5 forks source link

db_mysql testing getRow() for no results #11

Open jlp765 opened 8 years ago

jlp765 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

jlp765 commented 8 years ago

(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 commented 8 years ago
proc main() =
  let (isO,v)=ttest(44)
  # ..
jlp765 commented 8 years ago

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 commented 8 years ago

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[bot] commented 3 years ago

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.