Package: rashell
rashell, arelationalalgebrashellrashell is aRelationalAlgebraShell. It provides a domain-specific language (DSL) to define and populate relational models and a read–eval–print loop (REPL) interface to query relational models using relational algebra operations. This tool is intended for educational use only, to illustrate in a more interactive way the underlying concepts of relational databasesQuick Screencast (Duration 02:27)Installationrashell can be installed viapip:$pipinstallrashellGetting startedrashell can be executed by running therashellcommand from a terminal. You will be taken to a REPL interface that allows you define, populate and query relational models:$rashell
Welcometorashell,aninteractiverelationalalgebrashell
Author:SalimKebir<s.kebir@esti-annaba.dz>
GitHub:https://github.com/skebir/rashell
>>>You can issue the.exitcommand to exit rashell.Defining a relational modelBy default multiline mode is enabled in order to be able to define several relationships at the same time. Multiline mode can be disabled/enabled by pressing theF3key. To execute an instruction in multiline mode, you must use thealt + entercombination. Otherwise, simply useenterif multiline mode is disabled.You can define a relational model as illustrated in the following example:>>>Movie(_Code,Title,Genre,Year,#DirectorID)DirectorIDreferencesDirector.IDDirector(_ID,Name,Nationality)>>>The syntax for defining a relational model is, as you can see, self-describing:A relation is defined by specifying its name followed by a parenthesis and then the list of attributes separated by a comma.The attributes that form the primary key must be preceded by the symbol_Foreign key attributes must be preceded by the#symbol and the attribute they reference must be specified just after the definition of the Relation.Inserting/Deleting tuplesIt may be better to disable the multiline mode (F3key) from here because each of the following instructions are single-lined.Once you have defined your relational model, you can populate it using theinsertcommand as follows:>>>Director.insert(1,'Robert Zemeckis','US')>>>Director.insert(2,'Stanley Kubrick','US')>>>Director.insert(3,'David Lynch','US')>>>Director.insert(4,'Luc besson','FR')>>>Movie.insert(0120663,'Eyes wide shut','Drame',1999,2)>>>Movie.insert(0116922,'Lost Highway','Thriller',1997,3)>>>Movie.insert(0110413,'Leon','Crime',1994,4)>>>Movie.insert(2872732,'Lucy','Action',2014,4)>>>Movie.insert(0119116,'The Fifth Element','Action',1997,4)>>>Movie.insert(0166924,'Mullholland Drive','Thriller',2001,3)>>>Movie.insert(0062622,"2001: A Space Odyssey",'Adventure',1968,2)>>>Movie.insert(0081505,'The shining','Horror',1980,2)>>>Movie.insert(0109830,'Forrest Gump','Drame',1994,1)>>>Movie.insert(0118884,'Contact','Drame',1997,1)>>>Movie.insert(0066921,'A Clockwork Orange','Crime',1971,2)>>>Movie.insert(0093058,'Full Metal Jacket','War',1987,2)>>>Movie.insert(0090756,'Blue Velvet','Mystery',1986,3)>>>Movie.insert(0088763,'Back to the future','Adventure',1985,1)>>>At this stage of development, rashell only supports three data types: integers, strings and floats. It is unlikely that other data types will be supported in the future as this tool is purely educational.Theinsertcommand can fail in four cases:If the relation does not exist ;If the number of issued values is different from the number of attributes of the relation ;If the primary key constraint fail ;If the foreign key constraint fail. In this case, we can force the insertion by using theforce_insertinstruction instead as illustrated in the following example:>>>Movie.insert(0111161,'The Shawshank Redemption','Drama',1994,5)Line1:ForeignKeyconstraintfailed5>>>Movie.force_insert(0111161,'The Shawshank Redemption','Drama',1994,5)>>>To delete tuples, you can use thedeletecommand with a condition as the only parameter as illustrated below:>>>Movie.delete(Year<2000)>>>Movie────────────────────────────────────────────────────────────CodeTitleGenreYearDirectorID────────────────────────────────────────────────────────────2872732LucyAction20144166924MullhollandDriveThriller20013────────────────────────────────────────────────────────────>>>Likeinsert, thedeletecommand can fail because of foreign key constraints. To force deletion, theforce_deletecommand can be used as shown below:>>>Director.delete(Nationality='FR')Line1:ForeignKeyconstraintfailed{4}>>>Director.force_delete(Nationality='FR')>>>Director────────────────────────────────────IdNameNationality────────────────────────────────────1RobertZemeckisUS3DavidLynchUS2StanleyKubrickUS────────────────────────────────────>>>Displaying relations and relational modelYou can check that the relations you have populated actually contain the tuples you have added by entering the name of the relation followed by theenterkey (oralt + enterif you have not disabled multiline mode) as illustrated below:>>>Director────────────────────────────────────IdNameNationality────────────────────────────────────4LucbessonFR3DavidLynchUS1RobertZemeckisUS2StanleyKubrickUS────────────────────────────────────>>>Movie─────────────────────────────────────────────────────────────────CodeTitleGenreYearDirectorID─────────────────────────────────────────────────────────────────118884ContactDrama1997190756BlueVelvetMystery1986381505TheshiningHorror19802110413LeonCrime19944116922LostHighwayThriller1997366921AClockworkOrangeCrime1971293058FullMetalJacketWar19872109830ForrestGumpDrama199412872732LucyAction20144120663EyeswideshutDrama19992166924MullhollandDriveThriller20013626222001:ASpaceOdysseyAdventure19682119116TheFifthElementAction19974─────────────────────────────────────────────────────────────────>>>You can also display the specification of your relational model to see what relations exist by running the command.model. Moreover, it is possible to display the relational model in its raw form (i.e. that can be copied and pasted as a command in rashell) by using the command.raw_modelas illustrated in the following example:>>>.model
╭──────────────────────────────────────────────╮
│Director(Id,Name,Nationality)│
│Movie(Code,Title,Genre,Year,#DirectorID) ││DirectorIDreferencesDirector.Id│
╰──────────────────────────────────────────────╯
>>>.raw_model
Director(_Id,Name,Nationality)Movie(_Code,Title,Genre,Year,#DirectorID)DirectorIDreferencesDirector.Id
>>>Querying the relational modelAt this stage, it is now possible to query your relational model using standard relational algebra operations (projection, restriction, join, union, intersection, difference and cartesian product). The table below shows the symbol, meaning and syntax of each of the relational algebra operations supported by rashell:OperationMeaningSyntax/ExampleπProjectionπ Name, Nationality (Director)σRestrictionσ Year > 2015 (Movie)⋈JoinMovie ⋈ Director | DirectorID = IDUUnionHorrorMovies U ComedyMovies∩IntersectionHorrorMovies ∩ ComedyMovies-DifferenceLynchMovies - ComedyMoviesXCartesian ProductMovie X DirectorIt may be difficult to write some symbols such as ⋈, σ, π, and ∩ using a combination of keys. To remedy this, rashell allows you to quickly insert an operation by pressing thetabkey.The result of an operation is displayed immediately after its execution as below:>>>πTitle,Year(Movie)──────────────────────────────TitleYear──────────────────────────────2001:ASpaceOdyssey1968Backtothefuture1985Eyeswideshut1999Theshining1980MullhollandDrive2001Contact1997Leon1994ForrestGump1994BlueVelvet1986AClockworkOrange1971LostHighway1997FullMetalJacket1987Lucy2014TheFifthElement1997──────────────────────────────>>>The result can also be assigned to a temporary relation as below:>>>R1=πTitle,Year(Movie)>>>This will create a new temporary relation calledR1which can in turn be displayed and/or queried using the previous relational algebra operations.Loading a relational model from a fileIt is also possible to open a pre-populated relational model by specifying at the beginning of the file the relational model followed by zero or more insert/relational algebra operations. The following is the content of a file calledmovies.ra(which can be found in theexamplesfolder of this repository) :Movie(_Code,Title,Genre,Year,#DirectorID)DirectorIDreferencesDirector.Id
Director(_Id,Name,Nationality)Director.insert(1,'Robert Zemeckis','US')Director.insert(2,'Stanley Kubrick','US')Director.insert(3,'David Lynch','US')Director.insert(4,'Luc besson','FR')Movie.insert(0120663,'Eyes wide shut','Drama',1999,2)Movie.insert(0116922,'Lost Highway','Thriller',1997,3)Movie.insert(0110413,'Leon','Crime',1994,4)Movie.insert(2872732,'Lucy','Action',2014,4)Movie.insert(0119116,'The Fifth Element','Action',1997,4)Movie.insert(0166924,'Mullholland Drive','Thriller',2001,3)Movie.insert(0062622,"2001: A Space Odyssey",'Adventure',1968,2)Movie.insert(0081505,'The shining','Horror',1980,2)Movie.insert(0109830,'Forrest Gump','Drama',1994,1)Movie.insert(0118884,'Contact','Drama',1997,1)Movie.insert(0066921,'A Clockwork Orange','Crime',1971,2)Movie.insert(0093058,'Full Metal Jacket','War',1987,2)Movie.insert(0090756,'Blue Velvet','Mystery',1986,3)Movie.insert(0088763,'Back to the future','Adventure',1985,1)To open a file, you just have to specify its name when running rashell. If the file contains no syntactic or semantic errors, the relational model will be displayed immediately as illustrated below:$rashellexamples/movies.raWelcometorashell,aninteractiverelationalalgebrashell
Author:SalimKebir<s.kebir@esti-annaba.dz>
GitHub:https://github.com/skebir/rashell
╭──────────────────────────────────────────────╮
│Movie(Code,Title,Genre,Year,#DirectorID) ││DirectorIDreferencesDirector.Id│
│Director(Id,Name,Nationality)│
╰──────────────────────────────────────────────╯
>>>Keyboard ShortcutsF3Toggle multiline modetabInsert operationalt + enterRun command when multiline mode is enabledenterRun command when multiline mode is disabledcontrol + lClear screencontrol + dExit rashellAcknowledgmentsWe would like to thank the contributors of the following packages without whom rashell would not have been possible:Python Prompt Toolkit:https://github.com/prompt-toolkit/python-prompt-toolkittextX:https://github.com/textX/textXRich:https://github.com/Textualize/rich