File: db-csv.cc

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (123 lines) | stat: -rw-r--r-- 2,554 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * Populate SQLite database table(s) and export to CSV
 */

#include "DB.h"
#include <string.h>

using namespace std;

static const char USAGE_MESSAGE[] =
"usage:\
\nabyss-db-csv SQLite_repository table_name(s)\
\nabyss-db-csv SQLite_repository --all\n";

static const char TABLE_LIST[] =
"select name from sqlite_master where type='table';";

typedef vector<string> vs;

static bool existFile(const char* f)
{
	return (bool)ifstream(f);
}

template <typename D>
static bool existTable(
		D& db, const string& t)
{
	dbVec v = db.readSqlToVec(TABLE_LIST);
	unsigned i = 0;

	while (i<v.size()) {
		if (t == v[i][0])
			return true;
		i++;
	}
	return false;
}

template  <typename D>
static string createCSV(
		D& db, const string& t, const string& csv)
{
	ofstream csvf(csv.c_str());
	stringstream ps, ss, msg;
	string pstr, sstr;
	dbVec head, val;

	ps << "pragma table_info (" << t << ");";
	pstr = ps.str();
	head = db.readSqlToVec(pstr.c_str());

	for (unsigned i=0; i<head.size(); i++)
		csvf << head[i][1] << ((i == (head.size()-1)) ? "" : ",");
	csvf << endl;

	ss << "select * from " << t << ";";
	sstr = ss.str();
	val = db.readSqlToVec(sstr.c_str());

	for (unsigned i=0; i<val.size(); i++) {
		for (unsigned j=0; j<val[i].size(); j++)
			csvf << val[i][j] << ((j == (val[i].size()-1)) ? "" : ",");
		csvf << endl;
	}
	msg << csv << " has been created.\n";
	return msg.str();
}

template <typename D>
static void populateOneTable(
		D& db, const string& repo, const string& t)
{
	string tt(db.getProperTableName(t));
	stringstream csv;
	csv << "db." << tt << ".csv";

	if (!existTable(db, tt)) {
		cout << "Table " << "'" <<
			tt << "' doesn't exist in " << repo << "." << endl;
		cout << USAGE_MESSAGE;
		exit(EXIT_FAILURE);
	}
	cout << createCSV(db, tt, csv.str());
}

template <typename D>
static void populateAll(
		D& db, const string& repo)
{
	dbVec v = db.readSqlToVec(TABLE_LIST);

	for (unsigned i=0; i<v.size(); i++)
		populateOneTable(db, repo, v[i][0]);
}

int main(int argc, char** argv)
{
	if (argc < 3) {
		cout << USAGE_MESSAGE;
		exit(EXIT_FAILURE);
	} else if (!existFile(argv[1])) {
		cout << "Database file " << "'" <<
			argv[1] << "' doesn't exist." << endl;
		cout << USAGE_MESSAGE;
		exit(EXIT_FAILURE);
	} else {
		DB db;
		init(db, argv[1]);

		if (argc == 3 && strcmp(argv[2], "--all") == 0) {
			populateAll(db, argv[1]);
			exit(EXIT_SUCCESS);
		}

		vs t(argc-2);
		char** last = argv + argc;
		copy(argv+2, last, t.begin());

		for (unsigned i=0; i<t.size(); i++)
			populateOneTable(db, argv[1], t[i]);
	}
}