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

Fixes insert command #72

Merged
merged 3 commits into from Oct 21, 2019
Merged

Fixes insert command #72

merged 3 commits into from Oct 21, 2019

Conversation

gvolpe
Copy link
Member

@gvolpe gvolpe commented Oct 19, 2019

Insertions like the one below are now working:

val insertCity: Command[Int ~ String ~ String ~ String ~ Long] =
    sql"""
         INSERT INTO city
         VALUES ($int4, $varchar, $varchar, $varchar, $int8)
       """.command

I decided to ignore the parameters in the Completion pattern match but I'm not sure if that's correct, I'm not very familiar with the protocol.

@tpolecat
Copy link
Member

I think the argument is probably an int like the others, reporting the number of modified rows. But I don't know, this part is undocumented.

@gvolpe
Copy link
Member Author

gvolpe commented Oct 19, 2019

No, it's not 😄 ! The arguments indicate the number of arguments we pass to values.

What we get is INSERT 0, or INSERT 0 1, or INSERT 0 1 2, etc. That didn't seem relevant to me and I've no idea of how to parse that using regex hehe

@gvolpe
Copy link
Member Author

gvolpe commented Oct 19, 2019

[error] Error: Total 26, Failed 0, Errors 1, Passed 25
[error] Error during tests:
[error] 	tests.CommandTest
[error] (tests / Test / test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 54 s, completed Oct 19, 2019 7:47:41 PM

This is strange, tests are passing for me locally. Maybe restart?

@gvolpe
Copy link
Member Author

gvolpe commented Oct 19, 2019

I think the argument is probably an int like the others, reporting the number of modified rows

Okay, I'm back. I forgot to mention that I also thought that was the case and this was the first thing I tried:

val Insert: Regex = """INSERT (\d+)""".r

Then I discovered that it was failing when I was inserting two values:

val insertCmd: Command[String ~ String] =
    sql"""
         INSERT INTO brands
         VALUES ($varchar, $varchar)
       """.command

This was the error I was getting:

scala.MatchError: INSERT 0 1 (of class java.lang.String)
	at skunk.net.message.CommandComplete$.$anonfun$decoder$1(CommandComplete.scala:46)
	at scodec.DecodeResult.map(DecodeResult.scala:17)
	at scodec.Decoder$$anon$1.$anonfun$decode$1(Decoder.scala:48)
	at scodec.Attempt$Successful.map(Attempt.scala:111)
	at scodec.Decoder$$anon$1.decode(Decoder.scala:48)
	at scodec.GenCodec$$anon$2.decode(GenCodec.scala:76)
	at scodec.Decoder.decodeValue(Decoder.scala:41)
	at scodec.Decoder.decodeValue$(Decoder.scala:41)
	at scodec.GenCodec$$anon$2.decodeValue(GenCodec.scala:73)
	at skunk.net.MessageSocket$$anon$1.$anonfun$receiveImpl$2(MessageSocket.scala:54)

Then I realized that it returns the number of arguments in the same way so I added other patterns:

val Insert1: Regex = """INSERT (\d+)""".r
val Insert2: Regex = """INSERT (\d+ \d+)""".r
val Insert2: Regex = """INSERT (\d+ \d+ \d+)""".r

This does work but as you can see it gets ugly quickly, and I don't know how to write such a regex for an arbitrary number of arguments. After some Googleing I couldn't figure it out so I wrote a match-all after the INSERT clause since the result doesn't seem relevant anyway.

Hope that clarifies things a bit! :)

@gvolpe
Copy link
Member Author

gvolpe commented Oct 21, 2019

Hey @tpolecat ,

It turns out I was wrong. The pattern is always something like INSERT 0 1 where the second number indicates the number of records inserted and the first one related to some OIDS I have no idea about.

https://www.postgresql.org/message-id/4AD5F063.8050708@iol.ie

I fixed the pattern now and can confirm it works well.

@tpolecat
Copy link
Member

This isn't going to work if oids are turned on but I'll open an issue and we can follow up. Thanks!

@tpolecat tpolecat merged commit 5631141 into typelevel:master Oct 21, 2019
@gvolpe gvolpe deleted the feature/insert branch October 21, 2019 16:54
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

Successfully merging this pull request may close these issues.

None yet

2 participants