Ignore:
Timestamp:
01/13/12 11:49:05 (13 years ago)
Author:
jkraaijeveld
Message:

Updated documentation for updated Database design.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Doc/Backend/DBInterface.tex

    r196 r231  
    1919
    2020\pagebreak
     21\section{Changelog}
     22        \textit{13-01-2012}: Fixed the issue with Open-Closed, updated documentation accordingly.
    2123
    2224\section{Introduction}
     
    3335\section{How to use}\label{howtouse}
    3436        \subsection{Initialization}
    35                 Before the database can be used, you should initialize a \textit{DatabaseInterface} object. This is the only
    36                 database class you will use. This is done calling the DatabaseInterface constructor.
     37                Before the database can be used, you should remember to import master.php on every page:
    3738                \begin{verbatimtab}
    38                         $db = new DatabaseInterface();
     39                        require 'classes/master.php';
    3940                \end{verbatimtab}
    4041
    4142        \subsection{Basic queries}
    4243                \subsubsection{Retrieving data}
    43                         Getting data is done by calling the get method of the DatabaseInterface class. The specification for this
    44                         method is as follows. \\ \\
     44                        Getting data is done by calling the get method of the class for which you want to retrieve data. The
     45                        specification for this method is the same for every class and is as follows.\\ \\
    4546                        \begin{tabular}{| l || r |} \hline
    4647                                \multicolumn{2}{|c|}{Function get()} \\ \hline
    47                                 First argument: \$type  & String \\ \hline
    4848                                Second argument: \$arguments & Array \\ \hline
    4949                                Return type: & Array/Variable \\ \hline
    5050                        \end{tabular}  \\ \\
    5151
    52                         The return type is an Array of objects with the type specified as the first argument. For instance, if
    53                         the first argument is ``user", the function will return an array of User objects. The second argument can be
    54                         left out if you want to retrieve all entries of a specific type. Below are a few examples showing the usage
     52                        The return type is an Array of objects with the type corresponding to the class you caled it on. For
     53                        instance, if you call User::get() you will only get User objects. Below are a few examples showing the usage
    5554                        of get(). A description of what arguments are possible per type is given in section \ref{arguments}.
    5655
     
    5958                                \begin{framed}
    6059                                        \begin{verbatimtab}
    61 $questions = $db->get("question");
     60$questions = User::get(array());
    6261foreach ($questions as $question)
    6362{
     
    7574//To get all surveys created by the user Jos, we need his UID.
    7675//We first query the database for the User object belonging to Jos.
    77 $userResults = $db->get("user", array("name" => "Jos"));
     76$userResults = User::get(array("name" => "Jos"));
    7877//Assuming there is a result, the UID is:
    7978$josUID = $userResults[0]->uid;
    8079//Now to get the requested surveys:
    81 $surveys = $db->get("survey", array("questions" => array("q1", "q2"),
     80$surveys = Survey::get(array("questions" => array("q1", "q2"),
    8281                                    "creator" => $josUID));
    8382//And to print all the questions in these surveys:
     
    9594
    9695                \subsubsection{Storing data}
    97                         Storing data is easy, as long as you stick to using the given PHP Classes. Retrieve these classes by using
    98                         the get() function, and pass them as a parameter to the set() function of the DatabaseInterface. This set()
    99                         function will determine what type the object is and store it at the correct location. The method overview is
    100                         as follows: \\ \\
     96                        Storing data is easy, as long as you stick to using the given PHP Classes. Retrieve these objects by using
     97                        the get() function, and call the save() function of the objects.: \\ \\
    10198                        \begin{tabular}{| l || r |} \hline
    102                                 \multicolumn{2}{|c|}{Function set()} \\ \hline
    103                                 First argument: \$rToolObject  & Variable \\ \hline
     99                                \multicolumn{2}{|c|}{Function save()} \\ \hline
     100                                Returns: \$boolean  & succeeded \\ \hline
    104101                        \end{tabular}  \\ \\
    105                        
     102                        If the save function returns false, there is an invalid references in that object which needs to be resolved
     103                        before saving. This ensures the database is always valid.
    106104                        Another important thing to note is that currently it will overwrite a previous object with the same UID in
    107105                        the database. The following examples show how to create new objects and save them, as well as edit old
     
    113111                                        \begin{verbatimtab}
    114112//For this example, I choose a random question to answer                                       
    115 $questions = $db->get("question");
     113$questions = Question::get(array());
    116114$question = $questions[2];
    117115//Note two things:
     
    122120$answer = new Answer(null, $question, array("12345", "four"));
    123121//Save the answer in the database
    124 $db->set($answer);
     122if($answer->save())
     123        echo "Success";
    125124                                        \end{verbatimtab}
    126125                                \end{framed}   
     
    133132                                        \begin{verbatimtab}
    134133//Get the survey
    135 $surveyResults = $db->get("survey", array("uid" => "b91d39e8667372e220bb861b3f94b5bd"));                               
     134$surveyResults = Survey::get(array("uid" => "b91d39e8667372e220bb861b3f94b5bd"));                               
    136135$survey = surveyResults[0];
    137136//Remove the question
     
    141140$question->title = "New Title";
    142141//Save the survey and question
    143 $db->batchSet(array($survey, $question));
     142$question->save(); $survey->save();
    144143                                        \end{verbatimtab}
    145144                                \end{framed}
     
    209208                incorrect reference. By evaluating before saving an object, we ensure that there cannot exist invalid values in
    210209                the database.
    211         \subsection{Connectors}
    212                 Although the front end developer only uses one general DatabaseInterface, the different files for the different
    213                 datatypes are accessed by seperate connectors. A connector has to implement the IConnector interface, which
    214                 mainly enforces the get() and set() methods to ensure a connector can perform as expected. The rest of the
    215                 methods are implemented in the baseclass Connector, which should be extended. In every connector,
    216                 the get() and set() methods are different. A get() method builds the querystring based on the given arguments,
    217                 then retrieves the data and performs other necessary queries (like retrieving a set of fields of unspecified
    218                 length) to ensure the PHP DataModel is complete. A set() method has to store the data accordingly.
     210
     211        \subsection{Static functions, non-static functions}
     212                It is important to note that the get() function of each object is static, whilst the save() function is not.
     213                This is to provide a better saving mechanism in the future: tracking when an object gets changed and saving it
     214                automatically. The get() function is static because there is no reason for it to be linked to a specific Object,
     215                only to the type it describes as a whole.
     216
     217        \subsection{Adding to the system}
     218                Adding support for new datatypes is easy and can be done by extending the ResearchToolObject class and ensuring
     219                the get() and save() methods are implemented correctly. Simply look at the existing classes and stick to the
     220                same idea unless you want to radically change the infrastructure.
    219221
    220222        \subsection{PHPDoc}     
     
    223225
    224226\section{Future Work}\label{futurework}
    225         \subsection{Adhere to Open-Closed Principle}
    226                 At the moment, \textit{the DatabaseInterface.php} class violates the Open-Closed principle. Every new connector,
    227                 four lines need to be added in this class. This is unwanted if new RDF datatypes are added. The same holds for
    228                 the method createArguments in this class: extra cases have to be added if more types of arguments become possible.
    229                 Adhering to the Open-Closed principle will allow for easier extention and maintenance of the database-related classes.
    230 
    231227        \subsection{Deal with invalid or missing values}
    232228                This goes in two parts. Firstly, the database now assumes that given an entry, all the fields for its type are there and
     
    236232                does not exist.
    237233
    238         \subsection{Optimize queries}
    239                 Queries as they are at the moment can be a bottleneck in performance if the sets get really large. This is because of a
    240                 couple of things. For one, there are duplicate queries being executed: if a Session object is retrieved, it also retrieves
    241                 all the corresponding AnswerSets, which in turn retrieves the corresponding Questions. However, the Session also
    242                 retrieves the Surveys related to it, which afterwards also retrieve the same questions. There has been no benchmarking
    243                 of performance so I have no clue at what size of the .rdf files this will become an issue.
    244 
    245                 I have a gut feeling that the SPARQL queries can be optimized some as well. This is regarding entries with zero or more
    246                 elements of a specific type, like Survey. Now, there is a second query for getting all the IDs for all the questions, but
    247                 there must be a way to introduce this into the first query without getting the exponential growth of the RDF 'different
    248                 tree options'.
    249 
    250 
    251234\appendix
    252235\pagebreak
Note: See TracChangeset for help on using the changeset viewer.