cstorage/sdata.new.cpp

334 lines
7.3 KiB
C++

/*
_ * dtata.cpp
* Copyright (C) 2023 Leonardo de Araújo Lima <leonardo@deb-horus.local>
*
* dstorage is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dstorage is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sdata.h"
#include <iostream>
#include <vector>
#include <string>
#include <sqlite3.h>
using namespace std;
//
sdata::sdata(){
cout << "class constructor >>" << endl;
};
//
int sdata::add_data( const char *n , const char* d)
{
iname.push_back(n);
idata.push_back(d);
nitens++;
cout << "número de membros de dados: " << nitens << endl;
return 0;
};
//
int sdata::citens(){
cout << "número de dados armazenados: " << iname.size() << endl;
return 0;
};
//
int sdata::show() {
cout << "Informações." << endl;
for (int i=0; i < (nitens); i++)
{
if (debug == 1)
{
cout << "informações sobre o membros de dados: " << i << endl;
};
cout << iname.at(i) << " : " << idata.at(i) << endl;
};
cout << "### --- ###" << endl;
return 0;
};
//
void sdata::set_db(const char *name) {
dbn = name;
};
//
void sdata::set_tb(const char *name) {
tbn = name;
};
//
void sdata::set_dpath(const char *path) {
dpath = path;
};
//
int sdata::dbinfo() {
cout << "informações sobre o banco de dados. \n" << "#----#" << endl;
cout << "Path: " << dpath << endl;
cout << "Banco de dados: " << dbn << endl;
cout << "Tabela: " <<tbn << endl;
return 0;
};
//
int sdata::tsize()
{
int i=0;
dsize = 0;
for (i=0;i < (nitens); i++);
{
if (debug == 1);
{
cout << iname.at(i) << " : " << idata.at(i) << endl;
};
dsize += iname.at(i).size();
dsize += idata.at(i).size();
};
if (debug == 1)
{
cout << "data members size (keys, values : " << dsize << " bytes." << endl;
};
return dsize;
}
//
int sdata::nsize()
{
dsize = 0;
for (int i=0; i < (nitens ); i++)
{
if (debug == 1);
{
cout << iname.at(i) << " : " << iname.at(i) << endl;
};
dsize += iname.at(i).size();
};
if (debug == 1)
{
cout << "keys data members size : " << dsize << " bytes." << endl;
};
return dsize;
}
//
int sdata::vsize()
{
dsize = 0;
for (int i=0; i < (nitens ); i++)
{
if (debug == 1)
{
cout << iname.at(i) << " : " << idata.at(i) << endl;
};
dsize += idata.at(i).size();
};
if (debug == 1)
{
cout << "values data members size : " << dsize << " bytes." << endl;
};
return dsize;
}
// callback new...
int sdata::callback(void *data, int argc, char **argv, char **azColName) {
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i<argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
// callbackold ...
/*
int sdata::callback(void *unused, int count, char **data, char **columns)
{
int idx;
printf("There are %d column(s)\n", count);
for (idx = 0; idx < count; idx++) {
printf("The data in column \"%s\" is: %s\n", columns[idx], data[idx]);
}
printf("\n");
return 0;
}
*/
void sdata::dbtest()
{
// sqlite3* db;
int res = sqlite3_open(dbn, &db);
if(res)
//database failed to open
cout << "Database verification failed!" << endl;
else
{
cout << "database verified." << endl;
//your database code here
}
sqlite3_close(db);
}
sdata::~sdata(){
cout << "class destructor <<" << endl;
iname.clear();
idata.clear();
};
//
int sdata::cDBase()
{
string sql = "CREATE TABLE " + string(tbn) + "(";
sql.append("id int PRIMARY KEY");
for (int i=0; i < (nitens ); i++)
{
cout << iname.at(i) << " : " << idata.at(i) << endl;
sql.append(", " + iname[i] + " CHAR(254)");
};
sql.append(")");
cout <<"creating database and table!" << endl;
if (debug == 1){
cout << "sql statement: " << sql << endl;
}
int rc;
rc = sqlite3_open(dbn, &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return 0;
} else {
fprintf(stderr, "Opened database successfully\n");
}
rc = sqlite3_exec(db, sql.c_str(), callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Operation done successfully\n");
}
sqlite3_close(db);
return 0;
};
//
bool sdata::addreg() {
cnreg();
string sql = "INSERT INTO " + string(tbn) + " VALUES (" + to_string(nreg);
for (int i=0; i < (nitens ); i++)
{
cout << iname.at(i) << ": " << idata.at(i) << endl;
sql.append(", '" + idata[i] + "'");
};
sql.append(")");
cout <<"data to store on database!" << endl;
if (debug == 1){
cout << "sql statement used: " << sql << endl;
}
int rc;
rc = sqlite3_open(dbn, &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return 0;
} else {
fprintf(stderr, "\n");
}
rc = sqlite3_exec(db, sql.c_str(), callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "database peration done successfully\n");
return true;
}
sqlite3_close(db);
return true;
}
//
int sdata::cnreg(){
sqlite3_stmt *stmt;
int nid;
string sqldata = "SELECT id FROM " + string(tbn);
const char *sql = sqldata.c_str();
cout << "nreg sql :" << sql << endl;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
nid = sqlite3_column_int(stmt, 1);
// ...
}
if (rc != SQLITE_DONE) {
cout << nreg << "verifying nreg" <<endl;
cout << "nreg verified:" << nid << endl;
nreg = nid;
nreg += 1;
sqlite3_close(db);
return 0;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
};
//
bool sdata::load()
{
sqlite3_stmt *stmt;
string key;
string data;
int row = 0;
int bytes;
const unsigned char* text;
string sqldata = "SELECT * FROM " + string(tbn) + " WHERE id="+ to_string(objid);
const char* sql = sqldata.c_str();
cout << "\nload sql :" << sql << endl;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
/*
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
System.out.println(numberOfColumns);
*/
bool done = false;
while (!done) {
printf("In select while\n");
switch (sqlite3_step (stmt)) {
case SQLITE_ROW:
for (int z=0; z < nitens; z++)
{
bytes = sqlite3_column_bytes(stmt, z);
text = sqlite3_column_text(stmt, (z + 1));
};
printf ("count %d: %s (%d bytes)\n", row, text, bytes);
row++;
break;
case SQLITE_DONE:
done = true;
break;
default:
fprintf(stderr, "Failed.\n");
return false;
}
}
sqlite3_finalize(stmt);
return true;
};
/* ...
if (rc != SQLITE_DONE) {
cout << " object loaded." <<endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
return true;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return true;
*/
//