diff --git a/04-databases/Crud.m b/04-databases/Crud.m new file mode 100644 index 0000000..569c7ee --- /dev/null +++ b/04-databases/Crud.m @@ -0,0 +1,63 @@ + +; 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.",!,! + +; fetch a user + + s ok=$$fetch^UserRoutines(1,.data) + i ok=1 d + . w "User successfully fetched!",! + . w "Username: ",data("username"),! + . w "Email: ",data("email"),! + . w "Full name: ",data("fullname"),!,! + e d + . w "Whoops! The user could not be fetched.",!,! + +; update a user + + s ok=$$set^UserRoutines(1,"amelia~password~amelia-roberts@no-reply.com~Amelia Roberts") + i ok=1 d + . w !,"User successfully updated!",!,! + e d + . w !,"Whoops! The user could not be updated.",!,! + +; fetch a user + + s ok=$$fetch^UserRoutines(1,.data) + i ok=1 d + . w "User successfully fetched!",! + . w "Username: ",data("username"),! + . w "Email: ",data("email"),! + . w "Full name: ",data("fullname"),!,! + e d + . w "Whoops! The user could not be fetched.",!,! + +; delete a user + + s ok=$$remove^UserRoutines(2) + i ok=1 d + . w "User successfully deleted!",!,! + e d + . w "Whoops! The user could not be deleted.",!,! + +; fetch a user + + s ok=$$fetch^UserRoutines(2,.data) + i ok=1 d + . w "User successfully fetched!",! + . w "Username: ",data("username"),! + . w "Email: ",data("email"),! + . w "Full name: ",data("fullname"),! + e d + . w "Whoops! The user could not be fetched.",! diff --git a/04-databases/UserRoutines.m b/04-databases/UserRoutines.m new file mode 100644 index 0000000..82e71ef --- /dev/null +++ b/04-databases/UserRoutines.m @@ -0,0 +1,25 @@ + +fetch(n,data) + n record + k data + i n="" q 0 + s record=$get(^users(n)) + s data("username")=$piece(record,"~",1) + s data("password")=$piece(record,"~",2) + s data("email")=$piece(record,"~",3) + s data("fullname")=$piece(record,"~",4) + q 1 + +set(n,data) + i n="" q 0 + s username=$piece(data,"~",1) + s password=$piece(data,"~",2) + s email=$piece(data,"~",3) + s fullname=$piece(data,"~",4) + s ^users(n)=username_"~"_password_"~"_email_"~"_fullname + q 1 + +remove(n) + i n="" q 0 + k ^users(n) + q 1 diff --git a/README.md b/README.md index cc36ab6..6fceeae 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,35 @@ Copy the examples into your `~/.fis-gtm/V6.3-003A_x86_64/r` folder and run: $data(^gtree(0,1,1,1)) is 0 $data(^gtree(0,0,0,0,0)) is 0 $data(^gtree(0)) is 0 + +### [`Crud.m`](https://github.com/programarivm/mumps-examples/blob/master/04-databases/Crud.m) + + User successfully created! + + User successfully created! + + User successfully fetched! + Username: alice + Email: alice-jones@no-reply.com + Full name: Alice Jones + + + User successfully updated! + + User successfully fetched! + Username: amelia + Email: amelia-roberts@no-reply.com + Full name: Amelia Roberts + + User successfully deleted! + + User successfully fetched! + Username: + Email: + Full name: + + > For further details also visit [UserRoutines.m](https://github.com/programarivm/mumps-examples/blob/master/04-databases/UserRoutines.m) + --- ### Contributions