Changeset 231 for Doc/Backend
- Timestamp:
- 01/13/12 11:49:05 (13 years ago)
- Location:
- Doc/Backend
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Doc/Backend/DBInterface.tex
r196 r231 19 19 20 20 \pagebreak 21 \section{Changelog} 22 \textit{13-01-2012}: Fixed the issue with Open-Closed, updated documentation accordingly. 21 23 22 24 \section{Introduction} … … 33 35 \section{How to use}\label{howtouse} 34 36 \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: 37 38 \begin{verbatimtab} 38 $db = new DatabaseInterface();39 require 'classes/master.php'; 39 40 \end{verbatimtab} 40 41 41 42 \subsection{Basic queries} 42 43 \subsubsection{Retrieving data} 43 Getting data is done by calling the get method of the DatabaseInterface class. The specification for this44 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.\\ \\ 45 46 \begin{tabular}{| l || r |} \hline 46 47 \multicolumn{2}{|c|}{Function get()} \\ \hline 47 First argument: \$type & String \\ \hline48 48 Second argument: \$arguments & Array \\ \hline 49 49 Return type: & Array/Variable \\ \hline 50 50 \end{tabular} \\ \\ 51 51 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 55 54 of get(). A description of what arguments are possible per type is given in section \ref{arguments}. 56 55 … … 59 58 \begin{framed} 60 59 \begin{verbatimtab} 61 $questions = $db->get("question");60 $questions = User::get(array()); 62 61 foreach ($questions as $question) 63 62 { … … 75 74 //To get all surveys created by the user Jos, we need his UID. 76 75 //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")); 78 77 //Assuming there is a result, the UID is: 79 78 $josUID = $userResults[0]->uid; 80 79 //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"), 82 81 "creator" => $josUID)); 83 82 //And to print all the questions in these surveys: … … 95 94 96 95 \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.: \\ \\ 101 98 \begin{tabular}{| l || r |} \hline 102 \multicolumn{2}{|c|}{Function s et()} \\ \hline103 First argument: \$rToolObject & Variable\\ \hline99 \multicolumn{2}{|c|}{Function save()} \\ \hline 100 Returns: \$boolean & succeeded \\ \hline 104 101 \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. 106 104 Another important thing to note is that currently it will overwrite a previous object with the same UID in 107 105 the database. The following examples show how to create new objects and save them, as well as edit old … … 113 111 \begin{verbatimtab} 114 112 //For this example, I choose a random question to answer 115 $questions = $db->get("question");113 $questions = Question::get(array()); 116 114 $question = $questions[2]; 117 115 //Note two things: … … 122 120 $answer = new Answer(null, $question, array("12345", "four")); 123 121 //Save the answer in the database 124 $db->set($answer); 122 if($answer->save()) 123 echo "Success"; 125 124 \end{verbatimtab} 126 125 \end{framed} … … 133 132 \begin{verbatimtab} 134 133 //Get the survey 135 $surveyResults = $db->get("survey",array("uid" => "b91d39e8667372e220bb861b3f94b5bd"));134 $surveyResults = Survey::get(array("uid" => "b91d39e8667372e220bb861b3f94b5bd")); 136 135 $survey = surveyResults[0]; 137 136 //Remove the question … … 141 140 $question->title = "New Title"; 142 141 //Save the survey and question 143 $ db->batchSet(array($survey, $question));142 $question->save(); $survey->save(); 144 143 \end{verbatimtab} 145 144 \end{framed} … … 209 208 incorrect reference. By evaluating before saving an object, we ensure that there cannot exist invalid values in 210 209 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. 219 221 220 222 \subsection{PHPDoc} … … 223 225 224 226 \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 for228 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 231 227 \subsection{Deal with invalid or missing values} 232 228 This goes in two parts. Firstly, the database now assumes that given an entry, all the fields for its type are there and … … 236 232 does not exist. 237 233 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 a240 couple of things. For one, there are duplicate queries being executed: if a Session object is retrieved, it also retrieves241 all the corresponding AnswerSets, which in turn retrieves the corresponding Questions. However, the Session also242 retrieves the Surveys related to it, which afterwards also retrieve the same questions. There has been no benchmarking243 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 more246 elements of a specific type, like Survey. Now, there is a second query for getting all the IDs for all the questions, but247 there must be a way to introduce this into the first query without getting the exponential growth of the RDF 'different248 tree options'.249 250 251 234 \appendix 252 235 \pagebreak
Note: See TracChangeset
for help on using the changeset viewer.