I would like to know an elegant way to process data out of SQL database.
a simple query could be:
val rs = statement.executeQuery("SELECT * FROM MyTable LIMIT 5")
thanks for your help.
aliunix
* scalaz/Iteratee.scala
* case class OrSQLException[A](o: Either[java.sql.SQLException, A])
* case class Connect[A](k: java.sql.Connection =>
WriterT[OrSQLException, IndSeq[L], A]
--
Tony Morris
http://tmorris.net/
If you don't plan on using one of the suggested libraries to assist
you, do this in Scala is much like doing it in Java.
--
Derek
An agreeable way to talk to your database.
I mention these points because I think, regardless of how you choose to
tackle the problem, it's important to own up. I am currently attempting
to solve this exact problem using Scala in my day job. Among those who
are also attempting it, there is not yet a generally agreed approach.
Hope that helps.
On 12/03/11 09:49, aliunix wrote:
> OK!
>
> Thanks for your replies.
> Well, I'm quite new to scala but coming from java world.
> Your hints look fine but it seems to be a lot more complicated than processing SQL response from java.
>
> what I need is quite "simple" to explain:
> I managed to execute SQL statement from my scala code:
> val rs = stmt.executeQuery("SELECT * FROM MyTable LIMIT 5")
>
> I just need a way to process "rs" without explicitly knowing the content of "MyTable"
> a king to pretty printing the content on standard output
>
> any idea?
>
> Many thanks,
> aliunix
>
> Le 12 mars 2011 � 00:21, Tony Morris a �crit :
thank you all for your replies and pointers.
That's very exciting following this forum! :-)
Indeed, "simple" and "elegant" is not that easy to have regarding programming languages.
Anyway the pointers that have been provided are helping me to get closer to my goal: to do it in a purely functional style
Thanks a lot (and still checking out whether there will be additional posts on the topic ;-) )
Bests,
aliunix
Le 12 mars 2011 à 04:12, Tony Morris a écrit :
> Unfortunately, there is a contention between what you are calling
> "simple" and "elegant." It's not a simple problem and to solve it
> effectively and elegantly requires a lot of "complexity." I use quotes
> here because I don't necessarily agree that is in fact complex or not
> simple, but I expect this is how you are using these terms.
>
> I mention these points because I think, regardless of how you choose to
> tackle the problem, it's important to own up. I am currently attempting
> to solve this exact problem using Scala in my day job. Among those who
> are also attempting it, there is not yet a generally agreed approach.
>
> Hope that helps.
>
> On 12/03/11 09:49, aliunix wrote:
>> OK!
>>
>> Thanks for your replies.
>> Well, I'm quite new to scala but coming from java world.
>> Your hints look fine but it seems to be a lot more complicated than processing SQL response from java.
>>
>> what I need is quite "simple" to explain:
>> I managed to execute SQL statement from my scala code:
>> val rs = stmt.executeQuery("SELECT * FROM MyTable LIMIT 5")
>>
>> I just need a way to process "rs" without explicitly knowing the content of "MyTable"
>> a king to pretty printing the content on standard output
>>
>> any idea?
>>
>> Many thanks,
>> aliunix
>>
That's a one-liner with spring-jdbc:
-jason
Hi,
I would like to know an elegant way to process data out of SQL database.
a simple query could be:
val rs = statement.executeQuery("SELECT * FROM MyTable LIMIT 5")
You're not going to get a simple explanation from Tony :)
OK!Thanks for your replies.Well, I'm quite new to scala but coming from java world.Your hints look fine but it seems to be a lot more complicated than processing SQL response from java.what I need is quite "simple" to explain:I managed to execute SQL statement from my scala code:val rs = stmt.executeQuery("SELECT * FROM MyTable LIMIT 5")I just need a way to process "rs" without explicitly knowing the content of "MyTable"a king to pretty printing the content on standard output
Yeah!That's somehow what I tried to do by the following pattern matching:metadata.getColumnType(columnNumber) match {case (Types.VARCHAR | Types.CHAR | Types.LONGVARCHAR) => rs.getString(column)case (Types.INTEGER | Types.BIGINT | Types.SMALLINT | Types.NUMERIC) => rs.getLong(column).toStringcase (Types.DECIMAL | Types.DOUBLE | Types.FLOAT | Types.REAL) => rs.getDouble(column).toStringcase (Types.DATE | Types.TIME | Types.TIMESTAMP) => rs.getTimestamp(column).toString}
i predict an NPE
(not sure: are you supposed to write "an" or "a"? i know N is not a vocal, but is pronounced "en" like in "end")
Am 12.03.2011 22:38, schrieb aliunix:You're right!rs.getObject(column).toStringis enough!
ThanksaliunixLe 12 mars 2011 à 22:10, Nils Kilden-Pedersen a écrit :
On Sat, Mar 12, 2011 at 2:34 PM, aliunix <aliu...@gmail.com> wrote:
Yeah!
That's somehow what I tried to do by the following pattern matching:
metadata.getColumnType(columnNumber) match {case (Types.VARCHAR | Types.CHAR | Types.LONGVARCHAR) => rs.getString(column)case (Types.INTEGER | Types.BIGINT | Types.SMALLINT | Types.NUMERIC) => rs.getLong(column).toStringcase (Types.DECIMAL | Types.DOUBLE | Types.FLOAT | Types.REAL) => rs.getDouble(column).toStringcase (Types.DATE | Types.TIME | Types.TIMESTAMP) => rs.getTimestamp(column).toString}
Actually, if you're using a half-decent JDBC implementation, calling rs.getObject already give you the right data type back (or in O/R Broker: row.any)
--
Tony Morris
http://tmorris.net/
On 12 Mar 2011 22:39, "Tony Morris" <tonym...@gmail.com> wrote:
>
> Stop using the word simple to mean "I do not understand" and complex to
> mean, "I do not understand."
> By request from Viktor.
>
I agree, to a certain limit. If you'd said that the use of `Either` and monadic comprehension is an elegant solution to the problem, then you'd be right, and would have some valid basis for the inevitable follow-up claims of anti-intellectualism.
But you didn't. Instead you just posted 3 lines of code that show little more than the names chosen by scalaz to represent certain concepts. That isn't simple, it's accidental complexity.
Worse still, it's only loosely relevant.
You have no idea what I posted. No fucking clue whatsoever. Nada. I know
this because what you just wrote is completely wrong on more than one
level. It's a dead give-away.
Stop trying to claim you do understand, or come to understand what I
posted, so that you can pass comment. Why is this unreasonable to you?
Do you think you are less transparent than you are? You're not doing
anyone any favours by making stuff up -- I am of the understanding that
helping people is the intention. Why is "please try" such an
unreasonable request?
> Worse still, it's only loosely relevant.
You have demonstrated that you are severely under-qualified to make this call. Worse still, you're probably going to insist otherwise.
i started to think about a dsl in scala that secretly executes sql calls in the background.
you need to explain to scala which tables exist:
class TableDescPerson(val name:ColDef[String], val age:ColDef[Int])
put all of these into a big object and then access the tables like this:
AllTables.person select (TableDescPerson.name, TableDescPerson.age) where
and so on, using lots of implicits. the resulting row would be a typed tuple.
-- Jim
It looks like IndSeq[L] is a wrapper around Finger Trees. Daniel
Spiewak gave a talk at the NE Scala Symposium
(http://www.nescala.org/2011/) on functional data structures and
covered Finger Trees at some length. The claim Daniel made is that
Finger Trees have very desirable on-paper (time/space complexity)
properties but the actual run-time behavior isn't in agreement with
"the theory". The video can be watched here:
http://vimeo.com/20262239
Thoughts? Comments? Open to all of course.
--
Jim Powers
I'm either misunderstanding something important or Daniel is. There is a
lot of nonsense out there, so my optimising algorithm for nonsense
detection compels me to dismiss it and carry on, but I'm also curious
what on earth it could possibly mean to have such a dichotomy in the
first place. If you can clear it up any more that would be great.
--
Tony Morris
http://tmorris.net/
Well, as best as I can tell there are 4 possibilities
~ == Flips good to not good
T == Test procedure good (for some appropriate definition of "good")
I == Implementation of algorithm good (for some appropriate definition
of "good")
(T,I) => Daniel's claims are valid
(~T,I) => Daniel's claims are not to be relied upon because the
testing procedure is flawed
(T,~I) => Daniel's implementation exhibits the undesirable behavior
claimed (reliably) but the claim is likely not generalizable because
the implementation is not good
(~T,~I) => No useful information can be extracted from Daniel's claims
I take it that you consider the case (T,I) to be unlikely,
understandable. One would expect that the only meaningful definition
of "a good implementation" that could be applied in this case is one
where the theoretical properties of the algorithm are exhibited. It
is certainly possible to verify his claims.
--
Jim Powers
On 13 Mar 2011 13:25, "Tony Morris" <tmo...@tmorris.net> wrote:
>
> You may continue to protest all you like but your constant demands to respond such that "as long as Kevin understands it" will continue to be met with resistance. If you continue to make these demands in such a poorly thought-out manner, I will continue to think to myself, "fuck you", dismiss your babble and carry on.
>
> Of course I want to teach, but even the most dedicated will take prognostic factors into account. If you think you represent a majority in your whining, you're horribly mistaken, again.
>
I believe I represent the majority in having limited time in which to study entire new branches of mathematics, yes.
If you want to teach, do so.
I challenge you to stand by your claim and take all the creative energy that you clearly possess for the sake of personal criticism, using it to explain iteratees instead.
Judge for yourself whether or not it's a useful exercise based on the responses to this list, don't just take my word for it.
> I hope you work it out, I really do. I don't enjoy this one bit.
>
> On 13/03/2011 11:06 PM, "Kevin Wright" <kev.lee...@gmail.com> wrote:
Well, as a side note on iteratees one of the links I provided on them said asking the original author to explain them required a "save vs. confusion" :-).
--
Jim Powers
I side with Kevin about the direct usefulness of such replies but, as is almost always the case, Tony gives plenty information enough for the truly curious.
I personally tend to think of Iteratees simply as resumable folds. In this case the db can say its out of data or the row printer can say its had enough (either for now or in total).
The main trick is that the next operation kind of acts as a continuation for the one after it. This then allows "state" to be kept together with the computation itself and lovely composition to boot.
My personal experience with them is via xml comprehension and processing - think composable stream handling made easy (Scales Xml for those interested). And yes I do leverage Scalaz for that :-)
As a side-note, the use of IndSeq here is already an unfortunate
compromise because a more suitable structure such as Endo[List[A]] has
not been shown to be viable in Scala (is there a way?). If there is a
way of improving the situation, I'd be very keen to see how.
https://github.com/djspiewak/extreme-cleverness
His slides:
PDF: http://bit.ly/fZKfTc
Keynote: http://bit.ly/ge1VhF
There are a lot of animations so if you're on a Mac (I'm under Fedora
so no love) the Keynote version is the way to go because of embedded
animations.
Sadly, he's using buildr (http://buildr.apache.org/ - I've had not the
greatest success using it) to build the project, but should not be
hard to move to SBT or Maven as necessary, seems all self-contained.
Not to fan the flames of recent fires involving you (and myself on
another thread), but I'm trying to catch up on all the papers,
presentations, videos, etc. behind the various things in Scalaz, I
haven't even cracked open Endomorphisms yet - that's about 50 papers
or so down the list. Your posts have that effect on people ;-).
Hope the link is what you want.
--
Jim Powers
// endomorphism
// Wikipedia: endomorphism is a morphism (or homomorphism) from a
mathematical object to itself.
case class Endo[A](k: A => A)
Pretty easy eh?
Scalaz used to have:
case class DList[A](z: Endo[List[A]])
However, Paul Chiusano and friends tried to use it in production only to
find that Scala didn't have sufficient optimisation to make it viable.
We tried to save it to no avail. The reason it would be useful is
because it has O(1) cons and snoc (append), which would be great for
logging. It has a O(n) toList method that runs along the functions. I've
not found a way to save the DList -- would be great if we were wrong
about its viability.
Thanks for the pointers -- I'll check it out when I get a moment.
HA! I saw that on Wikipedia as well, here's the deal (no doubt you
encounter this *all the time*): from the perspective of someone not
coming from a (modern - Lisps not counting) functional programming
background the natural question is "what's the point?", or "what does
that buy me?", or more commonly "huh?"
I've been programming for a long time and have read a lot, apparently
I've been reading mostly the wrong stuff ;-).
I take it somewhat on faith that I *must* learn this stuff because
people I recognize as objectively vastly smarter than I are able to
make very good use of the concepts (merely in how one thinks about
problems) as well as the various "implementations" of said concepts.
Ah well, back to work.
--
Jim Powers
def collapse[A](a: List[A]): A = a.foldRight(Endo(z => z)) { case
(Endo(a), Endo(b)) => Endo(a compose b) }
Let's be clear here. This is one operation, specialised to one reducible
container (List) on one specific monoid (Endo). What about all the other
containers and monoids? You might get a glimpse here at potential
usefulness.
The fact of the matter is that you (any programmer) will use these
concepts knowingly or not. The useful question is whether or not it is
used knowingly, not whether it is used at all (it is, it just is). Then
there is the next question of whether or not it matters if it is
knowingly or not (who cares?). I think it is beneficial to use it
knowingly -- in particular because it does a few things:
a) it prevents you from taking extraneous excursions because you can
draw on others' attempts and see those results
b) it gives you more information with which to make decisions when you
are on your own i.e. cannot draw on the efforts of others
c) it's fun and interesting
Worse, too often I see "fumbling in the dark" when someone is just
reinventing a well-documented concept that the fumbler has simply
refused to acknowledge or written off as irrelevant. This is often done
with errors, redundancy and incompleteness. As you might imagine, this
is painful to watch. So painful that I am compelled to step in
sometimes. I'm sure it is agreed that we don't particularly want to be
in either position of fumbling or having to point it out. That's a huge
benefit alone in my opinion.
Hope this helps.
Explicit vs implicit: Agreed. Even if your *using* the concepts
implicitly one is still better off knowing *what* one is implicitly
using.
For context: I'm generally viewed as the "vanguard" (occasionally
crazy) at work. For illustration - getting people to use type
constructors (higher-kinds) and functional closures (correctly, most
of the time) was a major triumph. Getting people to STOP using
Java-idioms is very hard when they cannot see why they should stop
(and look upon the new-fangled stuff with suspicion).
Much appreciate the time. You should take that last post and throw it
up on your blog. Better than Wikipedia.
--
Jim Powers
that type signature should be:
def collapse[A](a: List[Endo[A]]): Endo[A]
in order to satisfy the stated specialisation to Endo.
"It's not as hard as you think, please trust me on this." Too often this
is construed as an under-estimation of how difficult a concept is,
because the person stating it already understands that concept. The best
way I have found to deal with this is to work through for one instance
and then introspect on that initial over-estimation of difficulty so
that the next time I say, "it's not as hard as you think", then some
amount of trust has been gained because there is agreement that I was
right the first time.
> Explicit vs implicit: Agreed. Even if your *using* the concepts
> implicitly one is still better off knowing *what* one is implicitly
> using.
>
> For context: I'm generally viewed as the "vanguard" (occasionally
> crazy) at work.
Ha, I'm happy to share the crazy title with you. Actually, I am lucky to
have a colleague who is very good at learning, so we feed off each other
a lot.
> For illustration - getting people to use type
> constructors (higher-kinds) and functional closures (correctly, most
> of the time) was a major triumph. Getting people to STOP using
> Java-idioms is very hard when they cannot see why they should stop
> (and look upon the new-fangled stuff with suspicion).
>
> Much appreciate the time. You should take that last post and throw it
> up on your blog. Better than Wikipedia.
I'll have to think about that one.
scala> case class Endo[A](k: A => A)defined class Endoscala> def collapse[A](a: List[A]): A = a.foldRight(Endo(z => z)) { case(Endo(a), Endo(b)) => Endo(a compose b) }<console>:7: error: missing parameter typedef collapse[A](a: List[A]): A = a.foldRight(Endo(z => z)) { case(Endo(a), Endo(b)) => Endo(a compose b) }^
scala> def collapse[A](a: List[A]): A = a.foldRight(Endo[A]((z : A) => z)) { case(Endo(a), Endo(b)) => Endo(a compose b) }<console>:7: error: type mismatch;found : (A) => Anyrequired: (A) => Adef collapse[A](a: List[A]): A = a.foldRight(Endo[A]((z : A) => z)) { case(Endo(a), Endo(b)) => Endo(a compose b) }^<console>:7: error: type mismatch;found : Endo[A]required: Adef collapse[A](a: List[A]): A = a.foldRight(Endo[A]((z : A) => z)) { case(Endo(a), Endo(b)) => Endo(a compose b) }^
scala> def collapse[A](a: List[Endo[A]]): Endo[A] = a.foldRight(Endo[A]((z : A) => z)) { case(Endo(a), Endo(b)) => Endo(a compose b) }collapse: [A](a: List[Endo[A]])Endo[A]
Same here, I tried this with JRuby and failed to get all Ruby dependencies. :-(
> to build the project, but should not be
> hard to move to SBT or Maven as necessary, seems all self-contained.
You can add Gradle to the list, it is a good tool to compile Scala. (Even if it uses the
evil twin/concurrent of Scala, ie. Groovy, as scripting language.)
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
An invertible endomorphism of X is called an automorphism.