So it's time to try hooking our bookings application up to our database, and we're going to do that
using a driver package.
So what I'm going to do is go to my internal packages, because this is a package that is specific to
my application, not going to be of use to anybody else.
And I'll create a new directory and I will call that driver,
which I'd better name Driver.
So let's refactor that.
Rename it driver.
OK, and inside of that, I will create a new go file, which I will call Driver Don't Go, and I will
add that to my git repository and this is going to be the means by which we actually connect our application
to a database.
And the very first thing I'm going to do is create a type and that type will be called DB because it's
going to hold a database connection and it's a structure.
And inside of that it's going to have one member, which I'll call sequel, because it's where all of
our school stuff is going to go.
And it's a pointer to sequel DB, which should be important for me and it is good.
And let's put a comment on this.
DB holds the database connection pool and the reason I'm creating a struct is because right now it's
going to hold a driver for postgrads.
But I might want a driver for a different database at some point in the future.
And by making it a struct, I can put whatever I want in the member here just by adding a new member
or substituting the value for skilled DB to, say, Amaria DB connection, which also uses the DB appointer
to Scooter Libby.
So I have that type created.
Now I'm going to create a variable and the variable I'm going to call DB Con and it's going to be equal
to a reference to my DV type and I'm going to have to use the curly quotes or curly brackets because
I'm initializing it and making it empty all in one step.
Now I'm going to define a few constants, OK?
We didn't use these when we created our simple database program, but we're going to use them in this
one.
And I'm going to define certain things like what's the maximum number of open database connections I
can have?
So I'll call it Max Open DB con and I'll set that to a reasonable number, which is 10.
Never have more than 10 connections to the database open at any given time.
And then I'll create another constant which I'll call Max Idol DB connections.
How many connections can remain in the pool but remain idle and I'll set that to five, which is a nice
reasonable number.
And what's the maximum lifetime for a database connection?
So I'll call this Max DB lifetime and that will be equal to five seconds, five times time, dot minute
or five minutes, not five seconds.
So that automatically imparts the time for me.
Now, these are three constants I'm going to use to define the nature of my connection pool.
So the very first thing I want to do is create a connection pool Soulis create a function func Connect
sequel.
And that's going to take an argument which will be a string, a database connection string, which I'll
call DSN, that's what people typically call this.
It's just a string and it's going to return to possible things, a pointer to my DBS type that I just
defined a moment ago and potentially an error if something goes wrong.
OK, so let's create a new database.
And I'm going to do that by calling another function that I'm going to create right now.
And that's going to be called new database func new database.
And it's going to take a DSN, which is of type string and it's going to return a pointer to a sequel
DB, which I want to get from postgrads and potentially an error.
Something goes wrong.
So let's create the connection.
And we do that exactly as we did in the other application.
We just say DB error is assign the value of school open and it's going to be of type in our case, PJI
X and it's going to take the DSM, the data, the string to connect to the database, which doesn't
exist yet, but it will by the time we actually call us and then we check for an error if error is not
equal to nil.
Then return nil because we don't have a database connection pool and the error otherwise keep going.
So let's try testing it so we know how to do that.
If error equals dbag ping, I'm going to do this all in one thing and the error is not equal to nil,
then return nil and the error.
So that will test our database connection just like we did in our other simple program.
But I'm using a shorthand here to check for the error and to see if it's nil all in one step.
And if everything works properly, then we'll return our DB and nil.
So now we have our connection.
So we're going to call that up here in Connect Sigma.
So we'll get our connection, our database, cool, I'll just call that D for the sake of argument and
error is new database and we're going to pass it, the DSM that this function receives as a parameter
if error is not equal to nil.
And what I'm going to do here is just die, because if I can't connect to the database, I can't go
any further with our application.
So I'll just say panic error.
And it's OK to panic here, because if this doesn't work, I don't want anything else to start up.
The program just doesn't work.
Now, if it does work, then Deal will have some values and I can actually set these constants to certain
parameters that are available for that connection pool.
So I can say dot set, max open DB connections, so Max open and I'll set out to Max open DB comp in
the same way I can say set Max vital cons to Max Idol cards and finally set Max DB.
Sorry, set Max.
Secon, Max, secon, max lifetime.
Max, idle time, max lifetime, please.
There we are to Max DBE Lifetime, so I've set some parameters on my database connection for sensible
ones that will stop it from growing out of control, that will remove idle connections and return them
to the database when they're not being used and used and will ensure that we have a certain lifetime
for all of our database connections.
Now let's take our Dickon Con that we divide up defined up here and assign a value to it.
DB Con only has one one member and that is equal and that's going to be equal to.
And just because I can, I'm going to test the database again.
So I'll create another function here called func test DB.
It has to take my connection pool which is a call and again it's a pointer to sequel DB and it returns
potentially there.
And let's test the database just like we did in our main program.
So we'll say error equals dot pain and if error is not equal to nil, then what I want to do, I want
to return my error.
And this has to be a sign, OK, and otherwise return no, return no.
So I will call Testbeds back up here in Connect sequel will just say error equals test Debe and we'll
pass it our connection pool, which is deep.
If error is not equal to now, what do I want to do?
I want to return nil and error, otherwise return my connection pool, which is in this case the DB
con variable that I that I have created and the signed my connection pool two and nil.
All right, so that should work.
Now, let's put some comments in here.
What does Connect sequel do?
Connect sequel creates.
Database pool for postgrads.
And what does testing do?
Tries to ping the database.
And finally need a new database.
What does that do need?
Base creates a new database for the application and that should be our connection pool.
So that's enough for this time.
In the next lecture, we'll start working with this.
Actually, that wasn't quite enough, I forgot to do one thing and hopefully you noticed it, I do this
sometimes.
I neglected to actually tell our application, our bookings application, that it's using the package
from Zhaxi.
So let's import that.
So I'm in the command prompt at the root level of my application.
I just say, go get and it's at GitHub, dot com slash Zhaxi slash PJI X slash version four and I go
get that so that I should have imported it into my Goman file and it looks like it did.
Yep.
There it is.
And now I need to use it up here.
And last time we just use one thing we used underscore GitHub dot com slash Zhaxi slash PGP version
four.
But I'm actually going to import two more things that we will be using as time goes on.
We may as well import them right now.
So I'm going to duplicate this line and change this part where it says PGP version for too long, which
gives me access to the subroutines, will be using a little while.
And I'm also going to import Zhaxi slash PGP version of Forgacs Steedle IP.
So those are the three that I want.
I think I got that wrong there.
Got a pee in their.
Jack see slash.
PG version for there, that's it, and let's just format this so it sort of them correctly perfect.
So sorry about that.
I neglected to import those and nothing will work without those because database sequel needs to have
an underlying driver.
And in this case, we're just using pigs.
And those three libraries will give us everything we need to work with the database as we continue in
the course.
So now we're ready to hook this up in the next lecture or two.