diff --git a/04-databases/basic-sql-blog/Main.m b/04-databases/basic-sql-blog/Main.m index bcd0ed9..dc981c7 100644 --- a/04-databases/basic-sql-blog/Main.m +++ b/04-databases/basic-sql-blog/Main.m @@ -1,4 +1,5 @@ + zlink "routines/Category" zlink "routines/Comment" zlink "routines/Database" zlink "routines/Post" @@ -9,22 +10,49 @@ s ok=$$set^User(1,"alice~password~alice-jones@no-reply.com~Alice Jones") s ok=$$set^User(2,"bob~password~bob-smith@no-reply.com~Bob Smith") -; create posts +; create categories - s ok=$$set^Post(1,1,"lorem-ipsum~Lorem ipsum~Lorem ipsum dolor sit amet, consectetuer adipiscing elit.") - s ok=$$set^Post(2,1,"donec-pede-justo~Donec pede justo~Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.") - s ok=$$set^Post(3,2,"curabitur-ullamcorper-ultricies~Curabitur ullamcorper ultricies~ Curabitur ullamcorper ultricies nisi.") - s ok=$$set^Post(4,2,"donec-vitae-sapien~Donec vitae sapien~Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.") + s ok=$$set^Category(1,"foo~Foo") + s ok=$$set^Category(2,"bar~Bar") + s ok=$$set^Category(3,"foobar~Foobar") + +; create posts +; +; s ok=$$set^Post(1, ... +; ⮡ post id +; s ok=$$set^Post(1,1, ... +; ⮡ category id +; s ok=$$set^Post(1,1,1 ... +; ⮡ user id + + s ok=$$set^Post(1,1,1,"lorem-ipsum~Lorem ipsum~Lorem ipsum dolor sit amet, consectetuer adipiscing elit.") + s ok=$$set^Post(2,1,1,"donec-pede-justo~Donec pede justo~Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.") + s ok=$$set^Post(3,3,2,"curabitur-ullamcorper-ultricies~Curabitur ullamcorper ultricies~ Curabitur ullamcorper ultricies nisi.") + s ok=$$set^Post(4,3,2,"donec-vitae-sapien~Donec vitae sapien~Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.") ; create comments s ok=$$set^Comment(1,1,"This is awesome! Thank you.") s ok=$$set^Comment(2,2,"Thank you so much for sharing this.") +; fetch categories + + do fetch^Category(1,.data) + w "Category fetched:",! + w "description: ",data("description"),! + + do fetch^Category(2,.data) + w !,"Category fetched:",! + w "description: ",data("description"),! + + do fetch^Category(3,.data) + w !,"Category fetched:",! + w "description: ",data("description"),! + ; fetch posts by post do fetch^Post(1,.data) - w "Post fetched:",! + w !,"Post fetched:",! w "slug: ",data("slug"),! w "title: ",data("title"),! w "description: ",data("description"),! @@ -39,8 +67,15 @@ do fetchByUser^Post(1,.data) w !,"Posts fetched by user:",! - w data(1),! - w data(2),! + s lev1="" + f s lev1=$o(data(lev1)) q:lev1="" d w data(lev1),! + +; fetch posts by category + + do fetchByCategory^Post(3,.data) + w !,"Posts fetched by category:",! + s lev1="" + f s lev1=$o(data(lev1)) q:lev1="" d w data(lev1),! ; fetch comments diff --git a/04-databases/basic-sql-blog/routines/Category.m b/04-databases/basic-sql-blog/routines/Category.m new file mode 100644 index 0000000..fb36c5d --- /dev/null +++ b/04-databases/basic-sql-blog/routines/Category.m @@ -0,0 +1,21 @@ + +fetch(id,data) + n record + k data + i id="" q 0 + s record=$get(^categories(id)) + s data("slug")=$piece(record,"~",1) + s data("description")=$piece(record,"~",2) + q + +set(id,data) + i id="" q 0 + s slug=$piece(data,"~",1) + s description=$piece(data,"~",2) + s ^categories(id)=slug_"~"_description + q 1 + +remove(id) + i id="" q 0 + k ^categories(id) + q 1 diff --git a/04-databases/basic-sql-blog/routines/Database.m b/04-databases/basic-sql-blog/routines/Database.m index b10163c..cb7a45f 100644 --- a/04-databases/basic-sql-blog/routines/Database.m +++ b/04-databases/basic-sql-blog/routines/Database.m @@ -2,5 +2,6 @@ drop() k ^comments; k ^posts; + k ^categories; k ^users; q 1 diff --git a/04-databases/basic-sql-blog/routines/Post.m b/04-databases/basic-sql-blog/routines/Post.m index ee0efc9..1f645ed 100644 --- a/04-databases/basic-sql-blog/routines/Post.m +++ b/04-databases/basic-sql-blog/routines/Post.m @@ -5,14 +5,31 @@ fetch(id,data) i id="" q 0 s lev1="" s lev2="" + s lev3="" f s lev1=$o(^posts(lev1)) q:lev1="" d . f s lev2=$o(^posts(lev1,lev2)) q:lev2="" d - . . i lev1=id d - . . . s record=$get(^posts(id,lev2)) - . . . s data("slug")=$piece(record,"~",1) - . . . s data("title")=$piece(record,"~",2) - . . . s data("description")=$piece(record,"~",3) - . . . q + . . f s lev3=$o(^posts(lev1,lev2,lev3)) q:lev3="" d + . . . i lev1=id d + . . . . s record=$get(^posts(lev1,lev2,lev3)) + . . . . s data("slug")=$piece(record,"~",1) + . . . . s data("title")=$piece(record,"~",2) + . . . . s data("description")=$piece(record,"~",3) + . . . . q + q + +fetchByCategory(idCategory,data) + k data + i idCategory="" q 0 + s lev1="" + s lev2="" + s lev3="" + s count=1 + f s lev1=$o(^posts(lev1)) q:lev1="" d + . f s lev2=$o(^posts(lev1,lev2)) q:lev2="" d + . . f s lev3=$o(^posts(lev1,lev2,lev3)) q:lev3="" d + . . . i lev2=idCategory d + . . . . s data(count)=$get(^posts(lev1,lev2,lev3)) + . . . . s count=count+1 q fetchByUser(idUser,data) @@ -20,20 +37,22 @@ fetchByUser(idUser,data) i idUser="" q 0 s lev1="" s lev2="" + s lev3="" s count=1 f s lev1=$o(^posts(lev1)) q:lev1="" d . f s lev2=$o(^posts(lev1,lev2)) q:lev2="" d - . . i lev2=idUser d - . . . s data(count)=$get(^posts(lev1,idUser)) - . . . s count=count+1 + . . f s lev3=$o(^posts(lev1,lev2,lev3)) q:lev3="" d + . . . i lev3=idUser d + . . . . s data(count)=$get(^posts(lev1,lev2,lev3)) + . . . . s count=count+1 q -set(id,idUser,data) - i id=""!idUser="" q 0 +set(id,idCategory,idUser,data) + i id=""!idCategory=""!idUser="" q 0 s slug=$piece(data,"~",1) s title=$piece(data,"~",2) s description=$piece(data,"~",3) - s ^posts(id,idUser)=slug_"~"_title_"~"_description + s ^posts(id,idCategory,idUser)=slug_"~"_title_"~"_description q 1 remove(id)