19.0.0 released Nov 08, 2023
|
Using DDL and DML with database
Example of manipulating tables via SQL. Table is dropped, created, data inserted, then queried, and finally it is dropped.
In a nutshell: PostgreSQL; command line; web browser; Nginx; Unix sockets; 2 source files, 43 lines of code.
Screenshots of application
The web output from creating a table, inserting data, selecting and dropping it:
The same output, only when the program runs from the command line. Set VV_SILENT_HEADER to yes to skip the header output:
Install Vely - you can use standard packaging tools such as apt, dnf, pacman or zypper.
Because they are used in this example, you will need to install Nginx as a web server and PostgreSQL as a database.
After installing Vely, turn on syntax highlighting in vim if you're using it:
The source code is a part of Vely installation. It is a good idea to create a separate source code directory for each application (and you can name it whatever you like). In this case, unpacking the source code will do that for you:
tar xvf $(vv -o)/examples/create-table.tar.gz
cd create-table
The very first step is to create an application. The application will be named "create-table", but you can name it anything (if you do that, change it everywhere). It's simple to do with vf:
sudo vf -i -u $(whoami) create-table
This will create a new application home (which is "/var/lib/vv/create-table") and do the application setup for you. Mostly that means create various subdirectories in the home folder, and assign them privileges. In this case only current user (or the result of "whoami" Linux command) will own those directories with 0700 privileges; it means a secure setup.
Before any coding, you need some place to store the information used by the application. First, you will create PostgreSQL database "db_create_table". You can change the database name, but remember to change it everywhere here. And then, you will create database objects in the database.
Note the example here uses peer-authentication, which is the default on all modern PostgreSQL installations - this means the database user name is the same as the Operating System user name.
Execute the following in PostgreSQL database as root (using psql utility):
echo "create user $(whoami);
create database db_create_table with owner=$(whoami);
grant all on database db_create_table to $(whoami);
\q
" | sudo -u postgres psql
Next, login to database db_create_table and create the database objects for the application:
psql -d db_create_table -f setup.sql
Connect Vely to a database
In order to let Vely know where your database is and how to log into it, you will create database-config-file named "db_create_table". This name doesn't have to be "db_create_table", rather it can be anything - this is the name used in actual database statements in source code (like run-query), so if you change it, make sure you change it everywhere. Create it:
echo "user=$(whoami) dbname=db_create_table" > db_create_table
The above is a standard postgres connection string that describes the login to the database you created. Since Vely uses native PostgreSQL connectivity, you can specify any connection string that your database lets you.
Use vv utility to make the application:
vv -q --db=postgres:db_create_table
Note usage of --db option to specify PostgreSQL database and the database configuration file name.
Start your application server
To start the application server for your web application use vf FastCGI process manager. The application server will use a Unix socket to communicate with the web server (i.e. a reverse-proxy):
This will start 3 daemon processes to serve the incoming requests. You can also start an adaptive server that will increase the number of processes to serve more requests, and gradually reduce the number of processes when they're not needed:
See vf for more options to help you achieve best performance.
If you want to stop your application server:
This shows how to connect your application listening on a Unix socket (started with vf) to Nginx web server.
- Step 1:
You will need to edit the Nginx configuration file. For Ubuntu and similar:
sudo vi /etc/nginx/sites-enabled/default
while on Fedora and other systems it might be at:
sudo vi /etc/nginx/nginx.conf
Add the following in the "server {}" section ("/create-table" is the application path (see request-URL) and "create-table" is your application name):
location /create-table { include /etc/nginx/fastcgi_params; fastcgi_pass unix:///var/lib/vv/create-table/sock/sock; }
- Step 2:
Finally, restart Nginx:
sudo systemctl restart nginx
Note: you must not have any other URL resource that starts with "/create-table" (such as for example "/create-table.html" or "/create-table_something" etc.) as the web server will attempt to pass them as a reverse proxy request, and they will likely not work. If you need to, you can change the application path to be different from "/create-table", see request-URL.
Access application server from the browser
Use the following URL(s) to access your application server from a client like browser (see request-URL). Use actual IP or web address instead of 127.0.0.1 if different.
# Create, use and drop table, show output
http://127.0.0.1/create-table/create-table
Note: if your server is on the Internet and it has a firewall, you may need to allow HTTP traffic - see ufw, firewall-cmd etc.
Access application server from command line
To access your application server from command line (instead through web browser/web server), use this to see the application response:
export CONTENT_TYPE=
export CONTENT_LENGTH=
export REQUEST_METHOD=GET
export SCRIPT_NAME='/create-table'
export PATH_INFO='/create-table'
export QUERY_STRING=''
cgi-fcgi -connect /var/lib/vv/create-table/sock/sock /
Note: to suppress output of HTTP headers, add this before running the above:
export VV_SILENT_HEADER=yes
If you need to, you can also run your application as a CGI program.
Run program from command line
Execute the following to run your application from command line (as a command-line utility):
vv -r --app='/create-table' --req='/create-table?' --method=GET --exec
You can also omit "--exec" option to output the bash code that's executed; you can then copy that code to your own script. Note: to suppress output of HTTP headers, add "--silent-header" option to the above.
Note: if running your program as a command-line utility is all you want, you don't need to run an application server.
The following are the source files in this application:
- Setup database objects (setup.sql)
Create a table used in the example:
create table if not exists my_table (col1 bigint);
- Creating, using and dropping a table (create_table.vely)
DDL (Data Definition Language), DML (Data Definition Language) and SELECT SQL is used in this example. A table is dropped (if it exists), created, data inserted, queried, and then table is dropped again:
#include "vely.h"
%% /create-table
out-header default
run-query @db_create_table = "drop table if exists my_table" error define err error-text define err_text no-loop
if (strcmp (err, "0")) {
report-error "Trouble dropping table, error [%s], error text [%s]", err, err_text
}
@Table dropped!<br/>
run-query @db_create_table = "create table if not exists my_table (my_number bigint)" error err error-text err_text no-loop
if (strcmp (err, "0")) {
report-error "Trouble creating table, error [%s], error text [%s]", err, err_text
}
@Table created!<br/>
run-query @db_create_table="insert into my_table (my_number) values ('%s'), ('%s'), ('%s')" : "100", "200", "400" affected-rows define nrows
end-query
@Added <<pf-out "%lld", nrows>> rows!<br/>
@Getting data we inserted:<br/>
run-query @db_create_table="select my_number from my_table" output my_number
@I got number <<query-result my_number>>!<br/>
end-query
run-query @db_create_table = "drop table if exists my_table" error err error-text err_text no-loop
if (strcmp (err, "0")) {
report-error "Trouble creating table, error [%s], error text [%s]", err, err_text
}
@Table dropped!<br/>
%%
Examples
example-client-API
example-cookies
example-create-table
example-develop-web-applications-in-C-programming-language
example-distributed-servers
example-docker
example-encryption
example-file-manager
example-form
example-hash-server
example-hello-world
example-how-to-design-application
example-how-to-use-regex
example-json
example-multitenant-SaaS
example-postgres-transactions
examples
example-sendmail
example-shopping
example-stock
example-uploading-files
example-using-mariadb-mysql
example-using-trees-for-in-memory-queries
example-utility
example-write-report
See all
documentation
You are free to copy, redistribute and adapt this web page (even commercially), as long as you give credit and provide a dofollow link back to this page - see full license at
CC-BY-4.0. Copyright (c) 2019-2023 Dasoftver LLC. Vely and elephant logo are trademarks of Dasoftver LLC. The software and information on this web site are provided "AS IS" and without any warranties or guarantees of any kind. Icons from
table-icons.io copyright Paweł Kuna, licensed under
MIT license.