GopherCon 2014

Desarrollando aplicaciones de bases de datos con el paquete database/sql

Baron Schwartz  · 
SQL

Presentación

PDF (pincha para descargar)

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

so I'm living proof that you can found a tech company without having dropped out of school so we founded Vivek cortex about a year and a half ago or venture-backed and we do database performance management so if you know what application performance management

tools like New Relic and F Dynamics do in the application we do that in the database and we do it for MySQL at the moment we have a bunch of code that we've written and it's been useful for us it's on our github repository hope some of that is

useful to some of you please contact me I love hearing from people also love meeting people it's one of the great things about conferences like this here's a couple of other I'm going to upload these slides but I just wanted to to mention a couple

of other resources that are good for learning about how to work with gos database sequel package of course the documentation is one we've written a tutorial there's kind of a lot of as with any source based documentation telling a story in source based

documentation is hard but a lot of times helping people to understand how to use something really needs to be kind of a narrative rather than here's something and here's what it does here's something else and here's what it does so our go database

sequel org tutorial it started out as just a readme file on github repository and it's turned into a website and a whole bunch of people contributing a lot of important and useful information there and in particular the two guys who write the MySQL driver

that I mentioned on this last link have contributed a lot there and a lot of it comes from things like repeated bug reports about the driver and so forth so there's a lot of there's a lot of sort of common gotchas that you can avoid if you read that

and it is a narrative and I'll cover a lot of that material today so there's things it's just a picture just because we I don't even know why this meme is funny to me it's just funny I still cannot explain this one there's a when I

first looked at database sequel my initial thought was oh my god just reinvented all the bad things about every other bad database interface library I've ever seen this is horrible I had written a couple of them myself and they were they frankly the the

ones that I was the most comfortable with and thought were the best designed work looked a lot like the ones that are in let's say in Microsoft's Microsoft class libraries and so forth I spent a few years in an all Microsoft company and I thought that

that was the best and the cleanest abstraction of a database and so there were a whole bunch of things here that just looked wrong to me and this is after coming from many years as a Perl programmer we had a DBH which was a database handling sths which are

a statement handle and that never felt right to me either as a programming abstraction or as an abstraction of what's you know an accurate representation and the model of the things that you're actually working with and so I looked at this and it was

like oh here they've reinvented it again all over wrong and I'm happy to report that I was wrong and that I think this was mostly grad Fitzpatrick's contribution to the standard library I think this is now I now believe that this is a cleaner and

simpler and more minimal abstraction of how to interact with the database than I was hoping for so you have this thing called a DB and a DB it's important to note is not a connection a DB represents your database whatever that may be it may be a database

server somewhere it may be something in memory or a library or something like that it could even be something that you've mocked but it is not a connection to a database and I guess this problem we took me a couple of weeks to figure out because I'm

a little bit dense that way and there's only a few things that you can do so one of the nice things about this is that there's not a lot of nouns and there's not a lot of verbs and in fact many of the verbs are consistent between a couple of the

different nouns so it feels like you're doing exactly the same thing even though the concrete types that you're operating on may be different so you open it a DB to create a DB not to connect to a DB and you can compose it when you're done with

it and you can do things like querying and query row and exec on it and another of the the major concrete types that you'll spend a lot of time working with his rows and a row is not as I been accustomed to thinking about it some kind of like a hash map

or an array or something in fact it is a an abstraction over a cursor and you can next to ask your cursor to go to and fetch a row of data from the database itself and you can scan that into variables so this my talk is done now welcome so there's a there's

a few the question is now with this with this kind of minimalistic interface how do you interact with it and who just show of hands who actually uses database sequel so like maybe 40% 50% of us use database sequel so I just want to cover some kind of patterns

that I have learned over the you know year and a half or so that I've been working with this so here's a complete little hello world program and it illustrates everything that you need to know to create a DB and then you'll be ready to start using

it this is actually compiled Abul none of everything else that I'll show you today is just snippets but this is kind of the framework that all of those snippets should fit into so the first thing that we import is going to be the driver the database sequel

package requires a driver which is database specific for how it actually interacts with the database and because I'm not actually going to use that driver in my program I have to underscore it so I don't get a an unused import error and then I mention

it in the sequel not open as the the the first parameter in quotes as a string so that's the name of the package that this database object this DB object should use to connect to its actual concrete implementation and in this case the and then there's

a second parameter which is the instructions about how the driver should connect to the database itself in this case it's a what's called a DSN data source name syntax it's user and password and host and so forth until one sequel dot open does

nothing except pass that on to the driver and you generally won't get any error so I have this commented outline what do we have here at the line where that comment is we do not have a connection to the database because you haven't done anything with

the database yet you have a DB object that is ready for you to issue queries against and DB handles connections for you creates and and destroys connections and so forth for you automatically it removes a ton of what's otherwise a lot of busy work and

then idiomatic database sequel would be idiomatic go so if you create something that's expensive and you expect to have to tear it down later you're gonna you're gonna defer so defer DB close in this case it's the last line in the function

[ ... ]

Nota: se han omitido las otras 3.558 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.