2022-05-07 14:49:43 +00:00
|
|
|
#include "db_init.h"
|
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
Wrapper for single querying
|
|
|
|
*******************************************************************************/
|
2022-05-07 14:49:43 +00:00
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
int single_query(pqxx::connection& C, std::string query) {
|
2022-05-07 14:49:43 +00:00
|
|
|
try {
|
|
|
|
pqxx::work W(C);
|
|
|
|
W.exec( query );
|
|
|
|
W.commit();
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
namespace db {
|
|
|
|
namespace create {
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
Table creation queries
|
|
|
|
*******************************************************************************/
|
|
|
|
namespace table {
|
|
|
|
|
|
|
|
int history(pqxx::connection& C) {
|
|
|
|
// TODO: make timestamp the primary key
|
|
|
|
// make sizes macros
|
|
|
|
std::string query = "CREATE TABLE IF NOT EXISTS HISTORY(" \
|
|
|
|
"ID SERIAL PRIMARY KEY NOT NULL," \
|
|
|
|
"COIN CHAR(32) NOT NULL,"\
|
|
|
|
"TYPE INT,"\
|
|
|
|
"USERID CHAR(128) NOT NULL,"\
|
|
|
|
"QUANTITY INT,"\
|
|
|
|
"TIMESTAMP BIGINT NOT NULL,"\
|
|
|
|
"COMPLETED BOOLEAN,"\
|
|
|
|
"PRICE REAL);";
|
|
|
|
return single_query(C, query);
|
2022-05-07 14:49:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
int coin_price(pqxx::connection& C) {
|
|
|
|
std::string query = "CREATE TABLE IF NOT EXISTS COINPRICE(" \
|
|
|
|
"COIN CHAR(32) PRIMARY KEY,"\
|
|
|
|
"PRICE REAL," \
|
|
|
|
"SALEVALUE REAL," \
|
|
|
|
"INCIRCULATION INT, "\
|
|
|
|
"LASTUPDATE BIGINT);";
|
|
|
|
return single_query(C, query);
|
2022-05-07 14:49:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
int users(pqxx::connection& C) {
|
|
|
|
std::string query = "CREATE TABLE IF NOT EXISTS USERS(" \
|
|
|
|
"USERID CHAR(128) PRIMARY KEY,"\
|
|
|
|
"USERNAME TEXT NOT NULL,"\
|
|
|
|
"ICON CHAR(32) NOT NULL,"\
|
|
|
|
"NETWORTH REAL);";
|
|
|
|
return single_query(C, query);
|
2022-05-07 14:49:43 +00:00
|
|
|
|
|
|
|
}
|
2022-12-10 14:35:16 +00:00
|
|
|
} // db::create::table
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
Function creation queries
|
|
|
|
*******************************************************************************/
|
|
|
|
namespace function {
|
|
|
|
|
|
|
|
int last_n_anchor_prices_function(pqxx::connection& C) {
|
|
|
|
/* Create sql query for spaced prices function, delta is in seconds*/
|
|
|
|
std::string query = "CREATE OR REPLACE FUNCTION last_n_spaced_prices(var_coin CHAR(32), var_n INT, var_delta INT) RETURNS SETOF HISTORY AS $$" \
|
|
|
|
"DECLARE "\
|
|
|
|
" row HISTORY%ROWTYPE; "\
|
|
|
|
" cur_date BIGINT = 9223372036854775806; "\
|
|
|
|
" nb_dates INT = 0; "\
|
|
|
|
" nb_dates_max INT = var_n; "\
|
|
|
|
"BEGIN "\
|
|
|
|
" FOR row IN "\
|
|
|
|
" SELECT * "\
|
|
|
|
" FROM HISTORY "\
|
|
|
|
" WHERE HISTORY.COIN = var_coin "\
|
|
|
|
" ORDER BY TIMESTAMP DESC "\
|
|
|
|
" LOOP "\
|
|
|
|
" IF nb_dates = nb_dates_max "\
|
|
|
|
" THEN "\
|
|
|
|
" EXIT; "\
|
|
|
|
" END IF; "\
|
|
|
|
" "\
|
|
|
|
" IF row.TIMESTAMP <= cur_date - var_delta*1000 OR cur_date IS NULL "\
|
|
|
|
" THEN "\
|
|
|
|
" cur_date := row.TIMESTAMP; "\
|
|
|
|
" nb_dates := nb_dates + 1; "\
|
|
|
|
" RETURN NEXT row; "\
|
|
|
|
" END IF; "\
|
|
|
|
" END LOOP; "\
|
|
|
|
"END; "\
|
|
|
|
"$$ LANGUAGE plpgsql; ";
|
|
|
|
return single_query(C, query);
|
|
|
|
}
|
|
|
|
} // db::create::function
|
2022-05-07 14:49:43 +00:00
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
Database creation query
|
|
|
|
*******************************************************************************/
|
|
|
|
namespace database{
|
2022-05-07 14:49:43 +00:00
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
int all(void) {
|
2022-05-07 14:49:43 +00:00
|
|
|
try {
|
2022-12-10 14:35:16 +00:00
|
|
|
// TODO: GLOBAL INFO
|
|
|
|
pqxx::connection C("dbname = nasfaq user = steaky hostaddr = 127.0.0.1 port = 5432");
|
2022-05-07 14:49:43 +00:00
|
|
|
|
|
|
|
if (C.is_open()) {
|
|
|
|
std::cout << "Opened database successfully: " << C.dbname() << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "Can't open database" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
table::history(C);
|
|
|
|
table::coin_price(C);
|
|
|
|
table::users(C);
|
2022-05-07 14:49:43 +00:00
|
|
|
|
2022-12-10 14:35:16 +00:00
|
|
|
function::last_n_anchor_prices_function(C);
|
2022-05-07 14:49:43 +00:00
|
|
|
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-10 14:35:16 +00:00
|
|
|
} // db::create::database
|
|
|
|
} // db::create
|
2022-05-07 14:49:43 +00:00
|
|
|
}
|