Let’s write our own database!

I think it is a time to do something really crazy, write something which probably doesn’t give any value any of you instead of me, let’s write our own database from scratch! 😀 Until today I know quite good how databases work from a data management perspective. Especially Oracle allows you to study more of processes in data base, because of very good documentation and even direct access to raw data on hard drive, which I described before in this article. The main goal of this project is to learn me how databases technically works, and how algorithms can be implemented to perform all known for me operations like b-tree, transactions, etc.

Language selection

Okie, if you already decided to start this journey with me then we have to answer ourselves to one question: which language is the best to create this project? My choice was simple, for me language for this project should be typed and quite fast. Until today I was able to programming in ABAP, Java, Python, R, and … I just want to know something new… In this ranking, I saw that there is a lot of languages faster than Java. Python is dynamically typed, building this project in R or ABAP will be too crazy for me, so maybe Swift? Pretty fast, typed language and with a possibility to run on any OS like Windows, Linux and of course Mac OS. The main disadvantage in my case is that I don’t ever use him before, so please forgive me, because in this project we will have for sure a lot of refactors and changes.

Is someone do this before ?

We are living in 2019, and in most cases, if you search something on the internet, you will find that already somebody does this before you and this one is not different. After a short research I figure out that there are articles with pretty good explanations:

And of course, there is a lot of databases already on the market. Few of them are open source, and their code is available for reverse engineering on GitHub. I would use SQL Lite, which (as my database in future) will keep all data in the file.

Go ahead

Let’s start some coding, from articles how basic interface from databases should look different. When you connect or login to the database, in most cases you have a simple command prompt which not end without a command like quit (a similar solution is also in python). So let’s maybe create a function with infinitive loop and exit after the quit:

I don’t like really this if-else part, this will be probably changed in the future. My whole project basically contains 3 files:

Crud file is not much filled now, and contains only empty methods:

And if I good understand Swift, in main there is only execution of one method:

After execution, code should looks like below:

This is all in this part, next we try to improve some code and maybe insert some data! See you

All this development is available on my GitHub repo. Please go to: https://github.com/pawelwiejkut/my_swiftdb.git and check branch: part1

Sprawdź też

SQL ON conditions in left join

When you use SQL for your daily work, you probably can don’t know that when …