How To Create A Flat File Database
A flatfile database is a kind of database that manages small pieces of information. Usually created in a very rustic way using simpletxt files to store the data. It can be used in a lot of situations when the complexity of an DBMS isn't necessary.
In this article I'll show you a Flatfile Database that I created as a hobby. It was written using the PHP language, but I think the concepts explaned here, can be used and applied to other languages like C/C++, C#, Pascal, Java, etc. I hope this article can be of some help to inspire you to create your own Flatfile Database system.
You can download a copy of theFlatdbase — The small database manager system I built. It is available from GITHub.
Note: The Readme file is currently written in English, but, maybe you find in the source code a lot of comments written in Portuguese. If you have trouble deciphering the language, consider contacting me and I will answer your questions.
FLATDbase - The FlatFile Database
The Flatbase, is a fully object-oriented system that consists of 7 classes. Where, 6 of these are main classes and 1 static class to hold the settings. These classes control all aspects of data management — Add, Query, Delete and of course Edit. Below you can check the Class Diagram of this system and a brief description of the purpose of each class. ( Note: I have maintained in this image the relationship of classes as aggregation in order to emphasize that one class exists only to provide resources to another).
Flatfile Database - Flatdbase Class Diagram
1. FDBTypes Class
When we are creating a table in a DBMS like MySQL, Oracle, etc, is mandatory that we define what type of data will be accepted in each column of the table. In theMySQL, for example, the data type can be INT, CHAR, VARCHAR, TEXT, BLOB, etc. In a Flatfile Database data type is not the kind of thing to be considered to implement, but, the Flatdbase does! In Flatdbase I decided to implement a data type system because this acts as a data standardizer, as will be explained in item 1.2.
1.1 - Common data types
In Flatdbase the FDBTypes class is responsible for defining the Data Type that is accepted in each column. In addition, this class validates the received data and prevents an invalid data from being inserted into the table.
Example:
class FDBTypes { public static function as_int ( $val ) { return self :: as_integer ( $val ) ; } public static function as_integer ( $val = null ) { $tmp = $val ; $val = FDBUtils:: numbersOnly ( $val ) ; if ( ! FDBUtils:: isInt ( $val ) ) FDBUtils:: show_error ( "Types->as_integer() : '$tmp' is not a integer value." ) ; return ( int ) $val ; } # ....... } |
In the example above, one of the data types supported by Flatdbase, INTEGER, is shown. If you create a table and set one of the columns with the Integer type, before entering any information in that column, Flatdbase sends the data to that method to do the validation, if approved, then it will be inserted into the table.
1.2 - Custom Types
With help ofFlatdbase — in particular the FDBTypes class — you can create custom data types. This is very useful for implementing data normalization and preventing data from being inserted into a table in a way that you don't want. For example, you can create a custom data type called PHONE and force the database system to accept only data who is normalized and masked like +55 (12) 3456-7890. Below you can see an example of a Custom Data Type, which is in the FDBTypes class. This is an E-mail data type:
public static function as_email ( $val = null ) { $email = FDBUtils:: textOnlyNoSpaces ( $val ) ; if ( ! filter_var ( $email , FILTER_VALIDATE_EMAIL ) === false ) : return $email ; else : FDBUtils:: show_error ( "'$val' is not a valid E-mail type." ) ; endif ; } |
In this example above, if you create a table and set the data type of a column as "email" then all information before being recorded in that column will go through that filter. If the information is not a valid email, it will be prevented from being recorded in the table.
2 - FDBTable Class
If you create a table named users in the database, then the FDBTable class will be as a driver for this table. By allowing you to manipulate the records, add, update, delete, and search.
Select all rows from Users table.
Every time the Flatdbase is initialized, an instance of the FDBTable class is created for each table that you have created in the database. And each instance becomes a property of$database object (FlatDbase). This mechanic allows us to access the tables using the command:
$database->table_name
3 - ContextSet Class
The ContextSet class allows the queries used for select data to become more flexible with the use of OOP. Currently in FlatDbase it's only used in the SELECT method of class FDBTable. The ContextSet class allows the addition of filters for the query with the PHP OOP features.
Select the register from table Users where ID is like 10
In the future, this same mechanism will be used in the Flatdbase DELETE, UPDATE and INSERT methods. Currently these methods are created in a rustic way. For example:
# # [CURRENTLY] # Example in the users table. # To delete a register from users table. # Today it's the only allowed way. $r = $database -> users -> delete ( 10 ) ; # [IN THE FUTURE] # The ideia is to implement the context mechanism. # And, by this way allow to delete a register like this: $r = $database -> users -> delete ( ) -> where ( 'id between 10 and 20' ) ; # |
4. FDBUtils Class
The FDBUtils Class, put together several methods that are useful throughout the system life cycle. Below is a list of methods and a brief description of their functionality:
- make_safe : Removes potentially dangerous characters and strings;
- replace_accents: Switches accented characters to non-accented characters;
- mres: MySQL Real Escape String;
- textOnly: Turns an input into text;
- textOnlyNoSpaces: Converts an input to text and removes all spaces;
- encode: Uses the FDBCripto Class to encrypt a message;
- decode: Decodes a message encoded by the previous method;
- show_errors: Kill the process and display an error message on the screen;
- isInt: Checks if an information is numeric;
- isNum: Checks if an input value is numeric;
- isTrue: Checks if a value is True;
- isNull: Checks if a value is NULL;
- ato: Receives an Array as parameter and turns it into an Object;
- deleteDirectory: Removes a Diretory;
5. Implementing a bit of security
Even if a flat database doesn't requires encryption or security mechanisms, this option has been implemented in the FlatDbase system. Data encryption prevents information from being viewed by someone without permission.
TheFDBCripto class encrypts the informations written to the database by using theAES-256-CBC method, and a mix of ofsha256 and a Secret Key that must be defined in the config.php file.
Below, you can see how the encrypted data is stored. this is the users table open in the Sublime text editor.
6. The main Class - Flatdbase
Flatdbase is our main class of database system. This class coordinates the use of other classes and is responsible for providing database functionality such as creating, deleting, and changing stored data. It also provides CRUD functionality when managing tables.
One particularity of the FlatDbase system is that errors are mostly silent. For example, when an information can not be excluded the delete method returns only a false Boolean result. Therefore, to know the real reason for the deletion does not execute, it's necessary to monitor the error stack of the Flatdbase class with:
var_dump( $database->getErrors() );
Conclusion
There are other features that aim to standardize the FlatDbase, but they will not be described here. These are particularities of logic or development environment.
Feel free to download and use the system. I hope he can inspire you to build a similar system. If you have any questions about the material described here, please contact me and explain your doubts and opinions.
the author:
Wanderlei Santana.
How To Create A Flat File Database
Source: http://sooho.com.br/2017/04/19/create-flatfile-database/
Posted by: thompsoncleggen.blogspot.com
0 Response to "How To Create A Flat File Database"
Post a Comment