DEV Community

Ingi
Ingi

Posted on

Databases in Plang

This article explores Plang, an intent-based programming language designed to interpret natural language. For more information, visit plang.is

Plang has a database built into the language. It uses an SQLite database. It's simple to use.

Setup Database

Define the table structure in the Setup.goal file.

Create Setup.goal in the root directory of your project. The project can be located anywhere on your computer.

Setup
- create table tasks, columns: 
    text(string, not null)
    due_date(datetime, default now)
Enter fullscreen mode Exit fullscreen mode

Here we are defining the table tasks, with two columns: text and due_date.

If you are not familiar with Plang, there is not really any syntax; you just need to follow simple rules that structure the goal file.

So you can write the create table in multiple ways. You can write it the way you would like, as long as your intent is clear.

Now build the code and run the setup to create the table in the database.

plang exec Setup
Enter fullscreen mode Exit fullscreen mode

plang exec both builds and runs the code.

Database Location

The database is located in .db/data.sqlite. This folder is hidden, so you might need to show hidden items in your operating system. You can open the database using any database tool that can read SQLite; I use DBeaver.

Insert, Update, and Select from Database

Let's insert and select some data from the database.

Create a new file Start.goal in your root folder:

Start
- insert into tasks, text='Buy milk', write into %id%
- insert into tasks, text='Fix car', due_date=%Now+2days%
- update tasks, set text='Buy milk now' where %id%
- select * from tasks, write to %tasks%
- delete from tasks
- write out %tasks%
Enter fullscreen mode Exit fullscreen mode

You can now build this.

plang build
Enter fullscreen mode Exit fullscreen mode

After the code has been built, you can run it:

plang
Enter fullscreen mode Exit fullscreen mode

The default entry point of Plang apps is Start.goal, so you do not need to define the Start.goal when you execute plang in your terminal/console.

SQL Statements

You don't really need to write valid SQL statements. The Plang builder will convert your intent into a valid statement.

You can do something like this:

Start
- insert data into the tasks table, put the %text%, and have the due_date=%Now+1day%
- select the 10 newest rows from tasks, write to %tasks%
- write out %tasks%
Enter fullscreen mode Exit fullscreen mode

Other Databases

Plang supports any other database as well. You can read more about it in the Services documentation.

More Information

If Plang is interesting to you, you should dig a bit deeper:

Top comments (0)