📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 16

2022-10-06 •Youtube

Detailed Notes

Focus for this episode: This episode, we wrap up our look stored procedures and adding those to our target.

This tutorial walks through the creation of a database synchronization tool in Python. It continues our Python and SQL Tutorials with a useful application that provides a deeper look at SQL structures and commands.

Repository For Code: git clone [email protected]:rbroadhead/dbsync.git

Transcript Text
[Music]
well hello and welcome back we're
continuing our series of
basically the combo series we're looking
at python and sql we're putting a little
bit more into uh an application type of
you know point of view for this tutorial
and what we're doing is building a
database synchronization
app in python
focused on my sequel although it also as
we've mentioned in the past can work for
mariadb and
honestly probably some others we haven't
gotten into
if you get into like a sql server if we
get into oracle or db2 those are a
little bit different
but
this would give you at least a
foundation to do so this episode instead
of going down that digressing there
we're going to talk about actually
creating stored procedures
now last couple episodes we've gone out
and we've been able to figure out where
are the procedures that exist in our
source
basically we're using our information
schema routines and it's based off of a
database name
and
then we come in we get the the source
code for it
which actually happens to be um stored
in a different place from the
parameters
and now we've got the parameters if we
see here
uh last time around that's what we were
doing so we're looking at the parameters
so
what we're going to do now is we're
going to work on building out our sql
so to create it so
uh let's go ahead and leave that one
there
and we'll leave all the debug stuff in
there for now so what we want to do
though
is let's start here so this will be the
um
we're going to call this the proc sql
and we're going to start
with a creator replace procedure
and then we're going to add in the
schema name which is going to be source
name
uh where'd they go
and then we're gonna do a
dot
and then we're gonna come in here
and we're gonna need to add our
procedure name
and that goes right here
and so that gives us this first line
we're going to add in we'll go ahead and
open our parentheses
and we will do that for now
and so what we're going to do is we're
going to uh
so for each of these parameters
what we need to do is we're going to do
products equal
equals proc sql plus
so now we're going to add our parameters
and it needs to be
uh the direction the variable name and
the type
which happens to
be zero
because we can see it here
it lines up pretty well so it'll just be
zero one and two
but we do need a uh space
and then we're gonna do uh palm road
let's just do it this way
so we can probably do it like this
and then we're gonna do it like that
then we're gonna go one
and two
and
we're going to need to do we're going to
say first equals true
uh let's see
so uh if
first
let's just do yeah
if first equals false
then
first we gotta take care of our comma so
that's going to be
that
and we'll create our comma
and will be nice we'll put a space after
our comma so there if it's uh first
equals false
then we're gonna
first put a uh we're gonna put a comma
in there to separate these out and we'll
come through here and then we're going
to say first
equals false
and instead of palm row we're going to
do
um
we're going to actually pull the whole
sql out and this will be
uh proc sql
and so that gets us to here so when
we're done
we need to close off our parameters
so we close off our parameters
and then
we're going to take our
code
which is just a straight up
uh and let's do this we're going to do
we're going to actually do this a little
bit we're going to do a delimiter
[Music]
that
uh we'll wait on this let's do let's see
how this works first i think we have to
do a delimiter but i think we have to
call that beforehand so
uh let's see uh we may not we may be
okay with this
uh so we're gonna print the proxy oh we
need to do
we just need to add now we've got our
we've got our parameters set up we're
all set up to do
that plus
we've got a space so she should be good
to do the source which is here
and actually let's just do this let's do
uh
proc source equals
because what we can do is we'll just
sort of save that off that we don't have
to worry about cursor issues
and then uh we do want to do
oh we're gonna have to close some of
this stuff off
so what we want to do here is let's see
we're gonna do fetch all blah blah blah
we get that
so now we can do
uh which one is that
that's gonna be
uh proc cursor
so we can go ahead and close it
so now we've got the procedure cursor
and then when we're done here
we're going to want to do the same thing
let's see since this is going to be plus
proc source
and we want to close
uh this cursor of our cursor
make sure that gets closed off
and
do we have any others we've got proc
cursor
uh oh we have var cursor
and we have that's it okay so we've got
our two we've closed our
uh everything off
and we're gonna print this out so we're
not actually gonna call it at this point
so let's go ahead
and take a look at that
and we're going to see here
uh
i need to create procedure while loop so
here's our while loop we're going to
create that create a replace procedure
blah blah blah now
let's see
if we can go ahead and we'll do did we
create an update cursor i think we did
uh cursor so we'll just use cursor
destination at this point
so destination procs equals that so we
could actually
close
and close him afterwards because we
don't need him anymore
that means we're also going to need to
do cursor source close so we're sort of
cleaning up our properties here or our
uh resource a little bit here which is
always a good thing
one two let's do that whoop
and we need to
uh let's see so we're gonna do
uh let's not
ah do we need to we probably don't need
to close it yet so let's do this let's
do a uh cursor destination xq so let's
do this we'll just keep using that
we're going to come in here
and we need destination source we're
going to close both of those up
and we don't really need the cursor
anymore so we can come into here
[Music]
and for each of these
we can execute
proc sql
and so now let's go jump over somewhere
to here we're going to jump to our
database
and if we do
routine name
from here
these are our sources
but now
if we flip to our local
uh which i don't know where that is
there we go if we go to local
same database
select routine
we have none
so now what we're gonna do is we're
gonna go ahead and run this and see what
it does let's see if we
get errors or not
uh sort of running so let's see did any
of them run
yes some of them did so now we have to
look at this specific one is we've got a
case here
where
let's see
ah
we've got an issue here so this one
tutorial greeting
for some reason has got a procedure to
it i mean a parameter to it somehow
and let's go back here
let's go back and look at this
[Music]
and we want it to be greetings so if we
do
let's go from here this is going to be
out in our demo
is that it yeah i think it's demo
[Music]
tutorial
and our schema is going to be tutorial
and our procedure name is going to be
whatever that procedure name is
greeting
so if you look at that
we have name and flag
coming through so is that correct
it should be
we print it and then we try to execute
it
for some reason
the variables are not coming out right
so if we do tutorial
greeting
interesting that it's not
those for some reason are not coming
through
so we do of our results
cursor var cursor
var cursor
var cursor
interesting so for some reason we're not
getting somewhere along the way we're
not getting this to work
and it's because
why this is going to be a nice little an
irrational thing because here
we're getting those names right
when we go into var results
which is the var query
which is here let's print that that may
not work at this point because we may
have to
let's see what we got here
okay good so it did die again is it the
same one it's not dying on the same oh
yeah it is on the same one okay
excellent
so we can take that
and if we look
here
it does get stuff back so the parameter
name that comes back is name and flag
and
when we do
okay so it does variables
and we're not printing so let's go ahead
and print
the uh results
let's do print of our results
whoops
and let's see if that shows us something
useful
uh we come up here of our results in
name flag okay
for some reason it's not coming through
here so
for palm row in there
okay let's do if not first maybe it's
that's what it doesn't like
so it should come through for each of
those and
oh
we managed to do this that's our problem
so let's go in here
and let's do
because we probably just screwed
something up so let's go to our local
and we're going to do well we'll do a
credit replace so we'll be okay okay so
we can do that
that's right so it's going to try to
replace it each time anyway well let's
do this
prop procedure
and then we need these four
okay let's do this
and we're going to drop these procedures
and we'll put a little semicolon here
okay so we dropped our procedures now
i'll try it again because what we're
doing is we weren't building our
sql correctly
so if we went back we were probably we
would see stuff without parameters
so we've saved it let's take a look
we run through
cool
and it looks like we have everything
it's like if we look at
let's look at multiplier
let's make sure we've got those local
okay we've got those and i believe it
would be so so if we do call
multiplier
uh no that one probably isn't one that i
want to use because i have to set
something up let's do i think while loop
uh here we go
uh we do so let's just call simple
exception because all it has is a value
that comes in
so let's do call
how did we rent that that was
so we do a simple exception
test
value
there we go
so we get our message back so we're
we're getting our parameters in if we
look here
we see our 17 procedures if we go back
to our original
we're going to see
uh
let's go rv demo there we go
same thing whoops
we see 17 and here we see
17. so we have now been able to bring
across store procedures with their
source and their parameters
and pull that whole thing in
and give ourselves a
a more complete database now there are a
few things left if you look at what's
out there we also
can take a look at triggers and user
functions
and those will be some of the things we
go to next before we swing back to a
couple of more complicated
tricky kind of things that we're going
to run into when we start looking at
triggers
and maybe some workarounds we're going
to do there
we're also going to make this a little
more
i'll say generic
and be able to set it up we're going to
work on cleaning this up a little bit
with some parameters and such like that
so that we can actually point it at
at a server and bring multiple databases
across and how that will look so still
got plenty of topics to cover but we're
not going to cover them today we're just
going to wrap this one up so go out
there and have yourself a great day a
great week and we will talk to you
next time
you
Transcript Segments
0.43

[Music]

27.039

well hello and welcome back we're

28.64

continuing our series of

32.48

basically the combo series we're looking

34.079

at python and sql we're putting a little

36.719

bit more into uh an application type of

39.92

you know point of view for this tutorial

42.719

and what we're doing is building a

44.16

database synchronization

46.64

app in python

49.44

focused on my sequel although it also as

52.32

we've mentioned in the past can work for

53.84

mariadb and

55.6

honestly probably some others we haven't

57.92

gotten into

59.6

if you get into like a sql server if we

62

get into oracle or db2 those are a

64.879

little bit different

66.24

but

67.52

this would give you at least a

68.88

foundation to do so this episode instead

72.32

of going down that digressing there

75.2

we're going to talk about actually

76.88

creating stored procedures

79.6

now last couple episodes we've gone out

81.759

and we've been able to figure out where

84

are the procedures that exist in our

86

source

86.96

basically we're using our information

89.119

schema routines and it's based off of a

91.759

database name

93.6

and

94.64

then we come in we get the the source

96.799

code for it

98.96

which actually happens to be um stored

102.24

in a different place from the

104.72

parameters

106.56

and now we've got the parameters if we

108.32

see here

109.52

uh last time around that's what we were

110.88

doing so we're looking at the parameters

112.64

so

113.52

what we're going to do now is we're

115.04

going to work on building out our sql

118.32

so to create it so

121.2

uh let's go ahead and leave that one

122.64

there

124.159

and we'll leave all the debug stuff in

126

there for now so what we want to do

127.92

though

129.52

is let's start here so this will be the

132.879

um

134.239

we're going to call this the proc sql

137.84

and we're going to start

140.84

with a creator replace procedure

147.04

and then we're going to add in the

150.64

schema name which is going to be source

153.12

name

156.56

uh where'd they go

158.56

and then we're gonna do a

160.48

dot

164.4

and then we're gonna come in here

166.319

and we're gonna need to add our

168.319

procedure name

170.48

and that goes right here

175.04

and so that gives us this first line

176.959

we're going to add in we'll go ahead and

178.959

open our parentheses

182.4

and we will do that for now

185.12

and so what we're going to do is we're

186.879

going to uh

189.2

so for each of these parameters

192.56

what we need to do is we're going to do

194.319

products equal

196.84

equals proc sql plus

200.64

so now we're going to add our parameters

203.04

and it needs to be

204.48

uh the direction the variable name and

206.56

the type

207.92

which happens to

210.84

be zero

213.519

because we can see it here

216.319

it lines up pretty well so it'll just be

217.84

zero one and two

219.76

but we do need a uh space

224.159

and then we're gonna do uh palm road

226.319

let's just do it this way

231.28

so we can probably do it like this

233.2

and then we're gonna do it like that

235.36

then we're gonna go one

237.12

and two

241.12

and

243.439

we're going to need to do we're going to

244.4

say first equals true

248.72

uh let's see

253.36

so uh if

258.4

first

259.84

let's just do yeah

261.6

if first equals false

264.16

then

265.759

first we gotta take care of our comma so

268.24

that's going to be

271.6

that

277.52

and we'll create our comma

280.88

and will be nice we'll put a space after

283.04

our comma so there if it's uh first

285.919

equals false

287.6

then we're gonna

288.88

first put a uh we're gonna put a comma

291.52

in there to separate these out and we'll

294.56

come through here and then we're going

295.52

to say first

297.12

equals false

300.08

and instead of palm row we're going to

301.84

do

303.199

um

304.08

we're going to actually pull the whole

305.199

sql out and this will be

308.32

uh proc sql

313.919

and so that gets us to here so when

316.16

we're done

318.96

we need to close off our parameters

324

so we close off our parameters

326.4

and then

328.479

we're going to take our

330.88

code

332

which is just a straight up

334.96

uh and let's do this we're going to do

337.28

we're going to actually do this a little

338.24

bit we're going to do a delimiter

339.98

[Music]

347.44

that

351.039

uh we'll wait on this let's do let's see

353.36

how this works first i think we have to

354.56

do a delimiter but i think we have to

355.919

call that beforehand so

359.039

uh let's see uh we may not we may be

361.36

okay with this

362.8

uh so we're gonna print the proxy oh we

364.72

need to do

367.039

we just need to add now we've got our

372

we've got our parameters set up we're

374

all set up to do

375.52

that plus

377.68

we've got a space so she should be good

379.919

to do the source which is here

386.4

and actually let's just do this let's do

388.479

uh

389.28

proc source equals

395.52

because what we can do is we'll just

397.12

sort of save that off that we don't have

398.8

to worry about cursor issues

403.759

and then uh we do want to do

407.44

oh we're gonna have to close some of

408.479

this stuff off

412.96

so what we want to do here is let's see

414.8

we're gonna do fetch all blah blah blah

416.639

we get that

418.16

so now we can do

420.4

uh which one is that

422.479

that's gonna be

426

uh proc cursor

430.8

so we can go ahead and close it

434.639

so now we've got the procedure cursor

436.479

and then when we're done here

439.039

we're going to want to do the same thing

442.88

let's see since this is going to be plus

444.319

proc source

448.56

and we want to close

452.16

uh this cursor of our cursor

456.56

make sure that gets closed off

459.919

and

461.199

do we have any others we've got proc

462.96

cursor

466.16

uh oh we have var cursor

468.72

and we have that's it okay so we've got

471.68

our two we've closed our

473.36

uh everything off

475.12

and we're gonna print this out so we're

476.479

not actually gonna call it at this point

478.16

so let's go ahead

480.319

and take a look at that

484.16

and we're going to see here

486.479

uh

487.52

i need to create procedure while loop so

489.44

here's our while loop we're going to

490.72

create that create a replace procedure

492.96

blah blah blah now

494.879

let's see

496.879

if we can go ahead and we'll do did we

499.52

create an update cursor i think we did

503.36

uh cursor so we'll just use cursor

505.12

destination at this point

515.919

so destination procs equals that so we

518.88

could actually

520.959

close

525.92

and close him afterwards because we

528.08

don't need him anymore

532.959

that means we're also going to need to

534.16

do cursor source close so we're sort of

536.56

cleaning up our properties here or our

539.12

uh resource a little bit here which is

541.279

always a good thing

542.88

one two let's do that whoop

545.44

and we need to

548.399

uh let's see so we're gonna do

555.68

uh let's not

557.2

ah do we need to we probably don't need

558.72

to close it yet so let's do this let's

560.32

do a uh cursor destination xq so let's

562.959

do this we'll just keep using that

565.36

we're going to come in here

567.519

and we need destination source we're

569.279

going to close both of those up

574.24

and we don't really need the cursor

575.76

anymore so we can come into here

579.15

[Music]

580.88

and for each of these

583.92

we can execute

587.36

proc sql

591.12

and so now let's go jump over somewhere

593.519

to here we're going to jump to our

594.959

database

596.48

and if we do

598.32

routine name

600.959

from here

602.399

these are our sources

604.399

but now

605.519

if we flip to our local

608.72

uh which i don't know where that is

610.16

there we go if we go to local

612.64

same database

616.399

select routine

618.959

we have none

620.16

so now what we're gonna do is we're

621.04

gonna go ahead and run this and see what

622.32

it does let's see if we

623.92

get errors or not

626.24

uh sort of running so let's see did any

628.32

of them run

629.6

yes some of them did so now we have to

631.2

look at this specific one is we've got a

633.279

case here

635.12

where

636.24

let's see

638.079

ah

640.56

we've got an issue here so this one

642.72

tutorial greeting

645.519

for some reason has got a procedure to

648.8

it i mean a parameter to it somehow

652.32

and let's go back here

660.079

let's go back and look at this

665.63

[Music]

667.2

and we want it to be greetings so if we

669.92

do

672.56

let's go from here this is going to be

674.64

out in our demo

677.04

is that it yeah i think it's demo

678.59

[Music]

679.839

tutorial

683.44

and our schema is going to be tutorial

690.56

and our procedure name is going to be

693.839

whatever that procedure name is

696.88

greeting

701.36

so if you look at that

706.32

we have name and flag

709.04

coming through so is that correct

715.68

it should be

719.279

we print it and then we try to execute

721.839

it

726.72

for some reason

728.8

the variables are not coming out right

735.36

so if we do tutorial

738.8

greeting

742.72

interesting that it's not

747.36

those for some reason are not coming

748.88

through

753.839

so we do of our results

758.959

cursor var cursor

761.6

var cursor

765.76

var cursor

770.639

interesting so for some reason we're not

772.88

getting somewhere along the way we're

774.16

not getting this to work

777.6

and it's because

784.56

why this is going to be a nice little an

786.639

irrational thing because here

791.279

we're getting those names right

793.68

when we go into var results

797.04

which is the var query

800.32

which is here let's print that that may

802.48

not work at this point because we may

804.72

have to

809.6

let's see what we got here

814.639

okay good so it did die again is it the

816.399

same one it's not dying on the same oh

818.32

yeah it is on the same one okay

820.16

excellent

822.959

so we can take that

827.12

and if we look

828.639

here

831.6

it does get stuff back so the parameter

833.519

name that comes back is name and flag

837.839

and

841.6

when we do

843.36

okay so it does variables

845.839

and we're not printing so let's go ahead

848

and print

851.44

the uh results

855.279

let's do print of our results

860.399

whoops

861.519

and let's see if that shows us something

863.279

useful

866.8

uh we come up here of our results in

869.839

name flag okay

872.399

for some reason it's not coming through

874

here so

879.92

for palm row in there

889.04

okay let's do if not first maybe it's

891.92

that's what it doesn't like

899.6

so it should come through for each of

900.88

those and

908.24

oh

912

we managed to do this that's our problem

918.639

so let's go in here

921.279

and let's do

925.44

because we probably just screwed

926.639

something up so let's go to our local

935.199

and we're going to do well we'll do a

937.44

credit replace so we'll be okay okay so

939.519

we can do that

941.36

that's right so it's going to try to

942.399

replace it each time anyway well let's

944.32

do this

946.639

prop procedure

950.16

and then we need these four

957.519

okay let's do this

961.44

and we're going to drop these procedures

963.92

and we'll put a little semicolon here

968.8

okay so we dropped our procedures now

971.44

i'll try it again because what we're

972.639

doing is we weren't building our

974.48

sql correctly

977.04

so if we went back we were probably we

979.839

would see stuff without parameters

982.88

so we've saved it let's take a look

987.44

we run through

989.759

cool

990.72

and it looks like we have everything

992.16

it's like if we look at

995.04

let's look at multiplier

998

let's make sure we've got those local

1001.36

okay we've got those and i believe it

1003.44

would be so so if we do call

1006.6

multiplier

1008.32

uh no that one probably isn't one that i

1010.399

want to use because i have to set

1011.6

something up let's do i think while loop

1015.68

uh here we go

1021.6

uh we do so let's just call simple

1023.279

exception because all it has is a value

1024.959

that comes in

1026.48

so let's do call

1028.799

how did we rent that that was

1031.199

so we do a simple exception

1038.24

test

1039.6

value

1042.16

there we go

1044.4

so we get our message back so we're

1046.48

we're getting our parameters in if we

1048.079

look here

1050.24

we see our 17 procedures if we go back

1053.12

to our original

1056.08

we're going to see

1057.84

uh

1058.96

let's go rv demo there we go

1061.2

same thing whoops

1063.76

we see 17 and here we see

1066.4

17. so we have now been able to bring

1069.28

across store procedures with their

1072.4

source and their parameters

1075.52

and pull that whole thing in

1077.84

and give ourselves a

1080.96

a more complete database now there are a

1082.88

few things left if you look at what's

1084.32

out there we also

1085.84

can take a look at triggers and user

1088.24

functions

1089.28

and those will be some of the things we

1090.72

go to next before we swing back to a

1093.36

couple of more complicated

1095.679

tricky kind of things that we're going

1097.039

to run into when we start looking at

1098.559

triggers

1099.6

and maybe some workarounds we're going

1101.039

to do there

1102.08

we're also going to make this a little

1103.2

more

1105.36

i'll say generic

1107.039

and be able to set it up we're going to

1108.799

work on cleaning this up a little bit

1110.24

with some parameters and such like that

1111.76

so that we can actually point it at

1115.039

at a server and bring multiple databases

1117.6

across and how that will look so still

1120.32

got plenty of topics to cover but we're

1122.24

not going to cover them today we're just

1123.52

going to wrap this one up so go out

1125.039

there and have yourself a great day a

1126.799

great week and we will talk to you

1130

next time

1147.679

you