On Sunday, December 8, 2013 3:32:32 PM UTC+2, Marcus Holmes wrote:
Fair enough. I found I was endlessly writing the same code to set up
transactions, prepare statements, etc. and wanted a cleaner, DRY-er way to
do it.
I don't see the code style being different? apart from the yoda
comparisons, that is. What's the difference that's sticking out?
I only took a quick glance at the code and it seems to be doing things at
the wrong level. What I mean is that, the package is only there to shorten
the syntax, and doesn't do anything else. Well mostly it just puts the
syntax on one line.
To me the better way would seem to extract the database logic from the rest
of the code and then use it as a service to do the stuff. If needed most of
the SQL / Get / Update could be generated automatically.
e.g. this is a library that would make code simpler:
err := repo.Foo.GetById(123, &foo)
if err != nil {
// bad thing happened
}
as compared to:
sc := scf.NewCommand("SELECT bar, qux FROM foo WHERE id=?')
if
!sc.Begin().Prepare().QueryRow(idValue).Scan(&resultBar,&resultQux).CloseAll().IsValid(){
//bad thing happened
}
Also I cannot understand why your go example is using transaction when they
weren't required? Something like this would be more realistic:
func main() {
db, err := sql.Open(driver, datasource)
if err != nil {
panic("bad thing happened")
}
rows, err := db.Query(sqlStatement, value1, value2, value3)
if err != nil {
panic("query failed...")
}
results := make(map[int]Foo)
for rows.Next() {
foo := Foo{}
if err := rows.Scan(&foo.Id, &foo.Name); err != nil {
panic("wrong arguments while iterating...")
}
results[foo.Id] = foo
}
if err := rows.Err(); err != nil {
panic("err while iterating...")
}
}
I also would recommend against using Yoda comparisons, although you are
free to use whatever style you want, Go as a community has mostly accepted
one style that is defined by the "go fmt" and the std packages. To me, if
some package code doesn't use that style, I consider that to be of poor
quality... mainly because the maintainer doesn't take into account other
people trying to use and/or understand the library. (Of course, I can
understand that people come from different backgrounds, so their initial
style will vary.)
+ egon
On Sunday, December 8, 2013 6:24:17 PM UTC+8, Frank Schröder wrote:I don't see the benefit. Sure, you'll save a couple of lines of code but
at the expense of introducing a code style that's different than the rest.
Most of the SQL boilerplate I've had in my code was in the row mapper
function which does the Scan and constructs an object. Other than that the
plain database/sql commands are simple and readable enough IMHO.
On Friday, December 6, 2013 4:25:39 PM UTC+11, Marcus Holmes wrote:repo is here:
https://bitbucket.org/marcus_holmes/sqlcommandit allows database commands to be chained together and the error dealt
with at the end, reducing the amount of boilerplate that needs to be written
e.g.:
sc := scf.NewCommand("SELECT bar, qux FROM foo WHERE id=?')
if
!sc.Begin().Prepare().QueryRow(idValue).Scan(&resultBar,&resultQux).CloseAll().IsValid(){
//bad thing happened
}
any comments welcome, especially suggestions for improvements.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
[email protected].
For more options, visit
https://groups.google.com/groups/opt_out.