arrays are traversed in 04-databases/basic-blog

This commit is contained in:
programarivm 2019-09-27 17:54:54 +01:00
parent 9bbf76e914
commit a24c24776b
3 changed files with 56 additions and 71 deletions

View File

@ -1,18 +1,22 @@
fetch(n,data)
fetchByComment(idComment,data)
n record
k data
i n="" q 0
s record=$get(^comments(n))
s data("description")=$piece(record,"~",1)
s data("id_post")=$piece(record,"~",2)
q 1
i idComment="" q 0
s lev1=""
s lev2=""
f s lev1=$o(^comments(lev1)) q:lev1="" d
. f s lev2=$o(^comments(lev1,lev2)) q:lev2="" d
. . i lev1=idComment d
. . . s record=$get(^comments(idComment,lev2))
. . . s data("description")=$piece(record,"~",1)
. . . q
q
set(n,data)
i n="" q 0
set(idComment,idPost,data)
i idComment=""!idPost="" q 0
s description=$piece(data,"~",1)
s idPost=$piece(data,"~",2)
s ^comments(n)=description_"~"_idPost
s ^comments(idComment,idPost)=description
q 1
remove(n)

View File

@ -2,71 +2,48 @@
; create users
s ok=$$set^UserRoutines(1,"alice~password~alice-jones@no-reply.com~Alice Jones")
i ok=1 d
. w "User successfully created!",!
e d
. w "Whoops! The user could not be created.",!
s ok=$$set^UserRoutines(2,"bob~password~bob-smith@no-reply.com~Bob Smith")
i ok=1 d
. w "User successfully created!",!
e d
. w "Whoops! The user could not be created.",!
; create posts
s ok=$$set^PostRoutines(1,"lorem-ipsum~Lorem ipsum~Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.~1")
i ok=1 d
. w "Post successfully created!",!
e d
. w "Whoops! The post could not be created.",!
s ok=$$set^PostRoutines(2,"excepteur-sint-occaecat~Excepteur sint occaecat~Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.~2")
i ok=1 d
. w "Post successfully created!",!
e d
. w "Whoops! The post could not be created.",!
s ok=$$set^PostRoutines(1,1,"lorem-ipsum~Lorem ipsum~Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
s ok=$$set^PostRoutines(2,1,"donec-pede-justo~Donec pede justo~Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.")
s ok=$$set^PostRoutines(3,2,"curabitur-ullamcorper-ultricies~Curabitur ullamcorper ultricies~ Curabitur ullamcorper ultricies nisi.")
s ok=$$set^PostRoutines(4,2,"donec-vitae-sapien~Donec vitae sapien~Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.")
; create comments
s ok=$$set^CommentRoutines(1,"This is awesome. Thank you.~1")
i ok=1 d
. w "Comment successfully created!",!
e d
. w "Whoops! The comment could not be created.",!
s ok=$$set^CommentRoutines(1,1,"This is awesome! Thank you.")
s ok=$$set^CommentRoutines(2,2,"Thank you so much for sharing this.")
s ok=$$set^CommentRoutines(2,"Thank you so much for sharing this.~2")
i ok=1 d
. w "Comment successfully created!",!
e d
. w "Whoops! The comment could not be created.",!
; fetch posts
; fetch a post
do fetchByPost^PostRoutines(1,.data)
w "Post fetched:",!
w "slug: ",data("slug"),!
w "title: ",data("title"),!
w "description: ",data("description"),!
s ok=$$fetch^PostRoutines(1,.data)
i ok=1 d
. w !,"Post successfully fetched!",!
. w "slug: ",data("slug"),!
. w "title: ",data("title"),!
. w "description: ",data("description"),!
. w "id_user: ",data("id_user"),!
e d
. w "Whoops! The post could not be fetched.",!
do fetchByPost^PostRoutines(2,.data)
w !,"Post fetched:",!
w "slug: ",data("slug"),!
w "title: ",data("title"),!
w "description: ",data("description"),!
; fetch a post
; fetch comments
s ok=$$fetch^CommentRoutines(2,.data)
i ok=1 d
. w !,"Comment successfully fetched!",!
. w "description: ",data("description"),!
. w "id_post: ",data("id_post"),!,!
e d
. w "Whoops! The post could not be fetched.",!
do fetchByComment^CommentRoutines(1,.data)
w !,"Comment fetched:",!
w "description: ",data("description"),!
do fetchByComment^CommentRoutines(2,.data)
w !,"Comment fetched:",!
w "description: ",data("description"),!
; drop database
s ok=$$drop^DatabaseRoutines()
i ok=1 d
. w "Database successfully dropped!",!
. w !,"Database successfully dropped!",!
e d
. w "Whoops! The database could not be dropped.",!

View File

@ -1,22 +1,26 @@
fetch(n,data)
fetchByPost(idPost,data)
n record
k data
i n="" q 0
s record=$get(^posts(n))
s data("slug")=$piece(record,"~",1)
s data("title")=$piece(record,"~",2)
s data("description")=$piece(record,"~",3)
s data("id_user")=$piece(record,"~",4)
q 1
i idPost="" q 0
s lev1=""
s lev2=""
f s lev1=$o(^posts(lev1)) q:lev1="" d
. f s lev2=$o(^posts(lev1,lev2)) q:lev2="" d
. . i lev1=idPost d
. . . s record=$get(^posts(idPost,lev2))
. . . s data("slug")=$piece(record,"~",1)
. . . s data("title")=$piece(record,"~",2)
. . . s data("description")=$piece(record,"~",3)
. . . q
q
set(n,data)
i n="" q 0
set(idPost,idUser,data)
i idPost=""!idUser="" q 0
s slug=$piece(data,"~",1)
s title=$piece(data,"~",2)
s description=$piece(data,"~",3)
s idUser=$piece(data,"~",4)
s ^posts(n)=slug_"~"_title_"~"_description_"~"_idUser
s ^posts(idPost,idUser)=slug_"~"_title_"~"_description
q 1
remove(n)