Because I don't want excessive database use, I decided for FREE version of QetriX to use flat files as data storage.
Flat files are plain text files, where entries (rows) are delimited by Enter (Line Feed or \n in this case) and values on each rows are delimited by a special characters (CSV uses comma or semicolon, I prefer Tabs).
I noticed I was often quite lazy when writing data retrievals from database and instead of abstraction I used a lot of SQL code. I know this is a bad habit and I should use abstraction or ORM, but in this stage I prefer quickness over a nice'n'clean code (and I'm still balancing the most suitable object structure for datastore at the moment). I'm going to review the whole thing anyways. For this reason I use quite a lot inline CSS in HTML as well, it helps me to organize my CSS classes better afterwards.
Knowing this, I created a SQL parser for each of CRUD queries using regular expressions. Then I converted results into arrays and processed them over data. The most challenging one was a WHERE clause, which currently supports only AND operator (not OR). In all cases I consider the first column as an auto-increment primary key.
Aside I created routines for data retrieval and saving, I used multidimensional associative array for in-memory data storage. Because I wanted to keep the each table in a single file and for purpose of QetriX I don't care about data types, first line of each file will contain tab-separated column names. Later I found out that I'll be able to put some additional info about each column here too. Because column name mostly doesn't contain a space (in my case never), I can use space as a 2nd level separator. This way I can define primary key, default value (incl. auto increment, but with limitation I can't use spaces in strings) and even data types.
All data modifications I make on in-memory data and store changed structure into a file afterwards. If I want to modify anything, the use case is: load data > make changes > save data. There are some application logic for performance improvements, obviously.
At first I was somehow distrustful, because it worked weirdly good :) But later I discovered this was foul and gained a trust. It works really good, it's very simple and reliable.
As everything around QetriX, this solution didn't aim to completely replace database. It lacks most features of e.g. MySQL, it's just a workaround for purposes I don't want to use database and target data scope is rather small. Perfect for FREE account, where user can store 5 databases with 5 classifications and 5 attribute/relation types for each classification.
No comments:
Post a Comment