1
00:00:01,870 --> 00:00:07,780
So it's time to try hooking our bookings application up to our database, and we're going to do that

2
00:00:07,780 --> 00:00:09,680
using a driver package.

3
00:00:09,700 --> 00:00:14,560
So what I'm going to do is go to my internal packages, because this is a package that is specific to

4
00:00:14,560 --> 00:00:17,320
my application, not going to be of use to anybody else.

5
00:00:17,890 --> 00:00:20,590
And I'll create a new directory and I will call that driver,

6
00:00:24,700 --> 00:00:25,970
which I'd better name Driver.

7
00:00:26,410 --> 00:00:27,760
So let's refactor that.

8
00:00:27,910 --> 00:00:30,640
Rename it driver.

9
00:00:31,600 --> 00:00:38,980
OK, and inside of that, I will create a new go file, which I will call Driver Don't Go, and I will

10
00:00:38,980 --> 00:00:46,090
add that to my git repository and this is going to be the means by which we actually connect our application

11
00:00:46,180 --> 00:00:47,050
to a database.

12
00:00:47,050 --> 00:00:54,250
And the very first thing I'm going to do is create a type and that type will be called DB because it's

13
00:00:54,250 --> 00:00:57,250
going to hold a database connection and it's a structure.

14
00:00:57,550 --> 00:01:01,510
And inside of that it's going to have one member, which I'll call sequel, because it's where all of

15
00:01:01,510 --> 00:01:02,740
our school stuff is going to go.

16
00:01:03,040 --> 00:01:08,880
And it's a pointer to sequel DB, which should be important for me and it is good.

17
00:01:09,340 --> 00:01:10,870
And let's put a comment on this.

18
00:01:11,320 --> 00:01:19,810
DB holds the database connection pool and the reason I'm creating a struct is because right now it's

19
00:01:19,810 --> 00:01:22,570
going to hold a driver for postgrads.

20
00:01:22,960 --> 00:01:26,510
But I might want a driver for a different database at some point in the future.

21
00:01:26,530 --> 00:01:31,150
And by making it a struct, I can put whatever I want in the member here just by adding a new member

22
00:01:31,150 --> 00:01:38,860
or substituting the value for skilled DB to, say, Amaria DB connection, which also uses the DB appointer

23
00:01:38,860 --> 00:01:39,760
to Scooter Libby.

24
00:01:40,240 --> 00:01:41,340
So I have that type created.

25
00:01:42,460 --> 00:01:47,500
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

26
00:01:47,500 --> 00:01:56,500
to a reference to my DV type and I'm going to have to use the curly quotes or curly brackets because

27
00:01:56,500 --> 00:01:59,350
I'm initializing it and making it empty all in one step.

28
00:02:00,280 --> 00:02:02,640
Now I'm going to define a few constants, OK?

29
00:02:03,280 --> 00:02:07,930
We didn't use these when we created our simple database program, but we're going to use them in this

30
00:02:07,930 --> 00:02:08,140
one.

31
00:02:08,140 --> 00:02:14,290
And I'm going to define certain things like what's the maximum number of open database connections I

32
00:02:14,290 --> 00:02:14,710
can have?

33
00:02:14,710 --> 00:02:19,570
So I'll call it Max Open DB con and I'll set that to a reasonable number, which is 10.

34
00:02:19,690 --> 00:02:23,260
Never have more than 10 connections to the database open at any given time.

35
00:02:24,010 --> 00:02:28,690
And then I'll create another constant which I'll call Max Idol DB connections.

36
00:02:29,230 --> 00:02:35,170
How many connections can remain in the pool but remain idle and I'll set that to five, which is a nice

37
00:02:35,170 --> 00:02:35,890
reasonable number.

38
00:02:36,550 --> 00:02:40,560
And what's the maximum lifetime for a database connection?

39
00:02:40,690 --> 00:02:51,280
So I'll call this Max DB lifetime and that will be equal to five seconds, five times time, dot minute

40
00:02:51,280 --> 00:02:52,990
or five minutes, not five seconds.

41
00:02:54,130 --> 00:02:55,990
So that automatically imparts the time for me.

42
00:02:55,990 --> 00:03:01,240
Now, these are three constants I'm going to use to define the nature of my connection pool.

43
00:03:02,200 --> 00:03:09,580
So the very first thing I want to do is create a connection pool Soulis create a function func Connect

44
00:03:09,580 --> 00:03:10,090
sequel.

45
00:03:12,480 --> 00:03:17,040
And that's going to take an argument which will be a string, a database connection string, which I'll

46
00:03:17,040 --> 00:03:19,530
call DSN, that's what people typically call this.

47
00:03:19,540 --> 00:03:26,310
It's just a string and it's going to return to possible things, a pointer to my DBS type that I just

48
00:03:26,310 --> 00:03:29,700
defined a moment ago and potentially an error if something goes wrong.

49
00:03:30,030 --> 00:03:33,930
OK, so let's create a new database.

50
00:03:34,200 --> 00:03:38,310
And I'm going to do that by calling another function that I'm going to create right now.

51
00:03:38,790 --> 00:03:43,320
And that's going to be called new database func new database.

52
00:03:45,570 --> 00:03:53,280
And it's going to take a DSN, which is of type string and it's going to return a pointer to a sequel

53
00:03:53,280 --> 00:03:57,080
DB, which I want to get from postgrads and potentially an error.

54
00:03:57,090 --> 00:03:57,890
Something goes wrong.

55
00:03:58,650 --> 00:04:00,180
So let's create the connection.

56
00:04:00,180 --> 00:04:03,810
And we do that exactly as we did in the other application.

57
00:04:04,020 --> 00:04:12,420
We just say DB error is assign the value of school open and it's going to be of type in our case, PJI

58
00:04:12,420 --> 00:04:18,210
X and it's going to take the DSM, the data, the string to connect to the database, which doesn't

59
00:04:18,210 --> 00:04:24,060
exist yet, but it will by the time we actually call us and then we check for an error if error is not

60
00:04:24,060 --> 00:04:24,720
equal to nil.

61
00:04:28,440 --> 00:04:36,590
Then return nil because we don't have a database connection pool and the error otherwise keep going.

62
00:04:38,160 --> 00:04:41,850
So let's try testing it so we know how to do that.

63
00:04:42,120 --> 00:04:48,540
If error equals dbag ping, I'm going to do this all in one thing and the error is not equal to nil,

64
00:04:49,170 --> 00:04:53,910
then return nil and the error.

65
00:04:55,260 --> 00:04:59,460
So that will test our database connection just like we did in our other simple program.

66
00:04:59,820 --> 00:05:04,830
But I'm using a shorthand here to check for the error and to see if it's nil all in one step.

67
00:05:05,340 --> 00:05:09,810
And if everything works properly, then we'll return our DB and nil.

68
00:05:10,230 --> 00:05:11,250
So now we have our connection.

69
00:05:11,550 --> 00:05:14,420
So we're going to call that up here in Connect Sigma.

70
00:05:17,570 --> 00:05:22,430
So we'll get our connection, our database, cool, I'll just call that D for the sake of argument and

71
00:05:22,430 --> 00:05:28,430
error is new database and we're going to pass it, the DSM that this function receives as a parameter

72
00:05:29,270 --> 00:05:31,430
if error is not equal to nil.

73
00:05:31,460 --> 00:05:36,200
And what I'm going to do here is just die, because if I can't connect to the database, I can't go

74
00:05:36,200 --> 00:05:37,610
any further with our application.

75
00:05:37,730 --> 00:05:40,040
So I'll just say panic error.

76
00:05:40,970 --> 00:05:45,200
And it's OK to panic here, because if this doesn't work, I don't want anything else to start up.

77
00:05:45,200 --> 00:05:47,200
The program just doesn't work.

78
00:05:47,990 --> 00:05:54,290
Now, if it does work, then Deal will have some values and I can actually set these constants to certain

79
00:05:54,290 --> 00:05:56,480
parameters that are available for that connection pool.

80
00:05:56,600 --> 00:06:08,990
So I can say dot set, max open DB connections, so Max open and I'll set out to Max open DB comp in

81
00:06:08,990 --> 00:06:19,580
the same way I can say set Max vital cons to Max Idol cards and finally set Max DB.

82
00:06:20,090 --> 00:06:23,030
Sorry, set Max.

83
00:06:25,470 --> 00:06:29,430
Secon, Max, secon, max lifetime.

84
00:06:31,350 --> 00:06:34,500
Max, idle time, max lifetime, please.

85
00:06:35,880 --> 00:06:43,410
There we are to Max DBE Lifetime, so I've set some parameters on my database connection for sensible

86
00:06:43,410 --> 00:06:48,510
ones that will stop it from growing out of control, that will remove idle connections and return them

87
00:06:48,510 --> 00:06:53,400
to the database when they're not being used and used and will ensure that we have a certain lifetime

88
00:06:53,400 --> 00:06:54,870
for all of our database connections.

89
00:06:55,470 --> 00:07:00,660
Now let's take our Dickon Con that we divide up defined up here and assign a value to it.

90
00:07:00,780 --> 00:07:06,360
DB Con only has one one member and that is equal and that's going to be equal to.

91
00:07:07,920 --> 00:07:10,890
And just because I can, I'm going to test the database again.

92
00:07:10,890 --> 00:07:16,070
So I'll create another function here called func test DB.

93
00:07:16,350 --> 00:07:24,900
It has to take my connection pool which is a call and again it's a pointer to sequel DB and it returns

94
00:07:24,900 --> 00:07:25,680
potentially there.

95
00:07:27,270 --> 00:07:29,990
And let's test the database just like we did in our main program.

96
00:07:30,810 --> 00:07:39,600
So we'll say error equals dot pain and if error is not equal to nil, then what I want to do, I want

97
00:07:39,600 --> 00:07:41,340
to return my error.

98
00:07:46,930 --> 00:07:54,400
And this has to be a sign, OK, and otherwise return no, return no.

99
00:07:56,200 --> 00:08:04,360
So I will call Testbeds back up here in Connect sequel will just say error equals test Debe and we'll

100
00:08:04,360 --> 00:08:06,070
pass it our connection pool, which is deep.

101
00:08:06,400 --> 00:08:09,700
If error is not equal to now, what do I want to do?

102
00:08:09,880 --> 00:08:19,090
I want to return nil and error, otherwise return my connection pool, which is in this case the DB

103
00:08:19,090 --> 00:08:25,090
con variable that I that I have created and the signed my connection pool two and nil.

104
00:08:26,870 --> 00:08:29,100
All right, so that should work.

105
00:08:29,120 --> 00:08:30,470
Now, let's put some comments in here.

106
00:08:31,070 --> 00:08:32,270
What does Connect sequel do?

107
00:08:33,260 --> 00:08:35,390
Connect sequel creates.

108
00:08:37,290 --> 00:08:40,490
Database pool for postgrads.

109
00:08:41,820 --> 00:08:43,160
And what does testing do?

110
00:08:46,720 --> 00:08:50,650
Tries to ping the database.

111
00:08:53,590 --> 00:08:55,610
And finally need a new database.

112
00:08:55,630 --> 00:08:58,810
What does that do need?

113
00:08:58,840 --> 00:09:09,150
Base creates a new database for the application and that should be our connection pool.

114
00:09:09,670 --> 00:09:11,860
So that's enough for this time.

115
00:09:11,860 --> 00:09:14,350
In the next lecture, we'll start working with this.

116
00:09:16,300 --> 00:09:21,670
Actually, that wasn't quite enough, I forgot to do one thing and hopefully you noticed it, I do this

117
00:09:21,670 --> 00:09:22,330
sometimes.

118
00:09:22,330 --> 00:09:30,670
I neglected to actually tell our application, our bookings application, that it's using the package

119
00:09:30,670 --> 00:09:31,520
from Zhaxi.

120
00:09:31,540 --> 00:09:33,030
So let's import that.

121
00:09:33,280 --> 00:09:37,050
So I'm in the command prompt at the root level of my application.

122
00:09:37,480 --> 00:09:49,480
I just say, go get and it's at GitHub, dot com slash Zhaxi slash PJI X slash version four and I go

123
00:09:49,480 --> 00:09:55,210
get that so that I should have imported it into my Goman file and it looks like it did.

124
00:09:55,400 --> 00:09:55,810
Yep.

125
00:09:55,810 --> 00:09:56,410
There it is.

126
00:09:56,890 --> 00:09:58,440
And now I need to use it up here.

127
00:09:58,480 --> 00:10:09,880
And last time we just use one thing we used underscore GitHub dot com slash Zhaxi slash PGP version

128
00:10:09,880 --> 00:10:10,480
four.

129
00:10:10,480 --> 00:10:13,840
But I'm actually going to import two more things that we will be using as time goes on.

130
00:10:13,840 --> 00:10:15,130
We may as well import them right now.

131
00:10:15,790 --> 00:10:23,500
So I'm going to duplicate this line and change this part where it says PGP version for too long, which

132
00:10:23,500 --> 00:10:27,370
gives me access to the subroutines, will be using a little while.

133
00:10:27,670 --> 00:10:36,520
And I'm also going to import Zhaxi slash PGP version of Forgacs Steedle IP.

134
00:10:36,910 --> 00:10:38,380
So those are the three that I want.

135
00:10:39,310 --> 00:10:40,420
I think I got that wrong there.

136
00:10:46,090 --> 00:10:46,880
Got a pee in their.

137
00:10:53,450 --> 00:10:54,890
Jack see slash.

138
00:10:56,990 --> 00:11:04,580
PG version for there, that's it, and let's just format this so it sort of them correctly perfect.

139
00:11:05,060 --> 00:11:05,960
So sorry about that.

140
00:11:05,960 --> 00:11:10,730
I neglected to import those and nothing will work without those because database sequel needs to have

141
00:11:10,730 --> 00:11:11,770
an underlying driver.

142
00:11:11,780 --> 00:11:13,940
And in this case, we're just using pigs.

143
00:11:14,300 --> 00:11:19,340
And those three libraries will give us everything we need to work with the database as we continue in

144
00:11:19,340 --> 00:11:19,790
the course.

145
00:11:19,970 --> 00:11:23,300
So now we're ready to hook this up in the next lecture or two.
