Vely logo Empower C
install  tutorials  examples
documentation  license  about

12.1.0 released on Sep 19, 2022

Example stock



PURPOSE:


Stock example.

SYNTAX:


See code below.

DESCRIPTION:


Stock ticker example:

This example is included in Vely installation. Here is how to compile and run it.

By default, Vely will automatically restart your application when a new executable is produced.

FILES:


The following are the source files for this project.

setup.sql:
create table if not exists stock (stock_name varchar(100) primary key, stock_price bigint);

stock.v:
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018 DaSoftver LLC.

#include "vely.h" // must always be here


// 
// Implementation of a stock ticker request handler. There is no firm rule
// about which functionality goes into which file (i.e. different sub-requests
// based on action could be further split into separate files etc.)
// Called from vely_handle_request().
//

void stock()
{

    out-header default

    @<html>
    @    <body>
        input-param action
        input-param stock_name
        input-param stock_price

        if (!strcmp (action, "add")) {

            // Add data to stock table, update if the stock exists

            get-app db-vendor db to define dbv
            char *qry = "";
            if (!strcmp (dbv, VV_SQLITE)) {
                qry = "insert or replace into stock (stock_name, stock_price) values ('%s', '%s')";
            } else if (!strcmp (dbv, VV_MARIADB)) {
                qry = "insert into stock (stock_name, stock_price) values ('%s', '%s') on duplicate key update stock_price='%s'";
            } else if (!strcmp (dbv, VV_POSTGRES)) {
                qry = "insert into stock (stock_name, stock_price) values ('%s', '%s') on conflict(stock_name) do update set stock_price='%s'";
            }

            char *err = "";
            if (!strcmp (dbv, VV_MARIADB) || !strcmp (dbv, VV_POSTGRES)) {
                run-prepared-query @db = qry  : stock_name, stock_price, stock_price no-loop error err
            } else if (!strcmp (dbv, VV_SQLITE)) {
                run-prepared-query @db = qry : stock_name, stock_price no-loop error err
            }
            if (strcmp (err, "0")) {
                report-error "Cannot update stock price, error [%s]", err
            }

    @       <div>
    @           Stock price updated!
    @       </div>
        } else if (!strcmp (action, "show")) {

            // Show stock names and values

    @       <table>
    @           <tr>
    @               <td>Stock name</td>
    @               <td>Stock price</td>
    @           </tr>
                run-prepared-query @db = "select stock_name, stock_price from stock" output stock_name, stock_price
    @               <tr>
    @                   <td>
                        query-result stock_name
    @                   </td>
    @                   <td>
                        query-result stock_price
    @                   </td>
    @               </tr>
                end-query
    @       </table>
        } else {
    @       <div>Unrecognized request!</div>
        }
    @   </body>
    @</html>

}


SEE ALSO:


Examples ( example_cookies   example_create_table   example_docker   example_form   example_hello_world   examples   example_sendmail   example_stock   example_upload_file   example_write_report  )  SEE ALL (documentation)



Copyright (c) 2022 DaSoftver LLC. Vely is a trademark of Dasoftver LLC. The software and information herein are provided "AS IS" and without any warranties or guarantees of any kind. Icons copyright PaweĊ‚ Kuna licensed under MIT. This web page is licensed under CC-BY-SA-4.0.