slightly better print, column alias supported
This commit is contained in:
parent
5201f32023
commit
fb5098cf1d
|
|
@ -1,7 +1,5 @@
|
|||
|
||||
### TODO
|
||||
- default double shoud have 2 decimal points (use set)
|
||||
- support for select col as ALIAS
|
||||
- date functions - now, add_date...
|
||||
- string functions rtrim, ltrim, rpad, lpad
|
||||
- round function
|
||||
|
|
|
|||
|
|
@ -69,7 +69,9 @@ int CsvReader::parseCSV(const std::string &filename, std::vector<ColDefNode> &co
|
|||
|
||||
field.clear();
|
||||
line.clear();
|
||||
// DEBUG if (row_cnt > 1000) break;
|
||||
// DEBUG
|
||||
// if (row_cnt > 1000) break;
|
||||
//
|
||||
}
|
||||
|
||||
newfile.close();
|
||||
|
|
|
|||
19
main.cpp
19
main.cpp
|
|
@ -132,18 +132,18 @@ void debug() {
|
|||
// "select * from ticker where ticker = 'WFC' and tablee = 'SF1'",
|
||||
// "set 'DATE_FORMAT' = '%Y-%m-%d'",
|
||||
// "show 'DATE_FORMAT'",
|
||||
// "create table sf1 ( ticker varchar(8), dimension varchar(3), calendar_date date, date_key date, report_period date, last_updated date, accoci float, assets float, assetsavg float, assetsc float, assetsnc float, assetturnover float, bvps float, capex float, cashneq float, cashnequsd float, cor float, consolinc float, currentratio float, de float, debt float, debtc float, debtnc float, debtusd float, deferredrev float, depamor float, deposits float, divyield float, dps float, ebit float, ebitda float, ebitdamargin float, ebitdausd float, ebitusd float, ebt float, eps float, epsdil float, epsusd float, equity float, equityavg float, equityusd float, ev float, evebit float, evebitda float, fcf float, fcfps float, fxusd float, gp float, grossmargin float, intangibles float, intexp float, invcap float, invcapavg float, inventory float, investments float, investmentsc float, investmentsnc float, liabilities float, liabilitiesc float, liabilitiesnc float, marketcap float, ncf float, ncfbus float, ncfcommon float, ncfdebt float, ncfdiv float, ncff float, ncfi float, ncfinv float, ncfo float, ncfx float, netinc float, netinccmn float, netinccmnusd float, netincdis float, netincnci float, netmargin float, opex float, opinc float, payables float, payoutratio float, pb float, pe float, pe1 float, ppnenet float, prefdivis float, price float, ps float, ps1 float, receivables float, retearn float, revenue float, revenueusd float, rnd float, roa float, roe float, roic float, ros float, sbcomp float, sgna float, sharefactor float, sharesbas float, shareswa float, shareswadil float, sps float, tangibles float, taxassets float, taxexp float, taxliabilities float, tbvps float, workingcapital float)",
|
||||
// "load sf1 from '/tmp/sf1.csv'",
|
||||
// "select * from sf1 where ticker = 'WFC'",
|
||||
"create table sf1 ( ticker varchar(8), dimension varchar(3), calendar_date date, date_key date, report_period date, last_updated date, accoci float, assets float, assetsavg float, assetsc float, assetsnc float, assetturnover float, bvps float, capex float, cashneq float, cashnequsd float, cor float, consolinc float, currentratio float, de float, debt float, debtc float, debtnc float, debtusd float, deferredrev float, depamor float, deposits float, divyield float, dps float, ebit float, ebitda float, ebitdamargin float, ebitdausd float, ebitusd float, ebt float, eps float, epsdil float, epsusd float, equity float, equityavg float, equityusd float, ev float, evebit float, evebitda float, fcf float, fcfps float, fxusd float, gp float, grossmargin float, intangibles float, intexp float, invcap float, invcapavg float, inventory float, investments float, investmentsc float, investmentsnc float, liabilities float, liabilitiesc float, liabilitiesnc float, marketcap float, ncf float, ncfbus float, ncfcommon float, ncfdebt float, ncfdiv float, ncff float, ncfi float, ncfinv float, ncfo float, ncfx float, netinc float, netinccmn float, netinccmnusd float, netincdis float, netincnci float, netmargin float, opex float, opinc float, payables float, payoutratio float, pb float, pe float, pe1 float, ppnenet float, prefdivis float, price float, ps float, ps1 float, receivables float, retearn float, revenue float, revenueusd float, rnd float, roa float, roe float, roic float, ros float, sbcomp float, sgna float, sharefactor float, sharesbas float, shareswa float, shareswadil float, sps float, tangibles float, taxassets float, taxexp float, taxliabilities float, tbvps float, workingcapital float)",
|
||||
"load sf1 from '/tmp/sf1.csv'",
|
||||
"select ticker, calendar_date from sf1 where calendar_date > to_date('2019-01-01', '%Y-%m-%d') limit 1",
|
||||
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'AI') and dimension = 'MRY' order by 3 desc",
|
||||
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'AI') and (dimension = 'MRY' or dimension = 'MRQ') order by 3 desc",
|
||||
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'WFC') and dimension = 'MRY' order by 3 desc",
|
||||
"create table a (i integer not null, s varchar(64), f float null, d date null, b boolean)",
|
||||
"insert into a (i, s, b) values(1, upper('one'), 'Y')",
|
||||
"insert into a (i, s, b) values(1 + 10000, upper('one'), 'Y')",
|
||||
"update a set i = i * 100, f = f + 0.01 where i > 1",
|
||||
// "create table a (i integer not null, s varchar(64), f float null, d date null, b boolean)",
|
||||
// "insert into a (i, s, b) values(1, upper('one'), 'Y')",
|
||||
// "insert into a (i, s, b, f) values(1 + 10000, upper('one'), 'Y', 3.1415)",
|
||||
// "update a set i = i * 100, f = f + 0.01 where i > 1",
|
||||
// "select to_string(i, '%d.%m.%Y %H:%M:%S'), i, s from a where i < to_date('20.12.2019', '%d.%m.%Y')",
|
||||
"select i + 2, i, s, b, f from a where i >=1 order by 1 desc offset 0 limit 1",
|
||||
// "select i + 2 as first, i, s, b, f from a where i >=1 order by 1 desc offset 0 limit 1",
|
||||
// "update table a set s = 'null string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'",
|
||||
// "update table a set i = null",
|
||||
// "insert into a (i, s) values(2, 'two')",
|
||||
|
|
@ -200,8 +200,7 @@ void debug() {
|
|||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
//debug();
|
||||
|
||||
// debug();
|
||||
repl();
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
14
parser.cpp
14
parser.cpp
|
|
@ -241,12 +241,18 @@ namespace usql {
|
|||
auto column_value = parse_expression();
|
||||
std::string column_alias;
|
||||
|
||||
if (column_value->node_type == NodeType::database_value) {
|
||||
column_alias = ((DatabaseValueNode*) column_value.get())->col_name;
|
||||
if (m_lexer.tokenType() == TokenType::keyword_as) {
|
||||
m_lexer.skipToken(TokenType::keyword_as);
|
||||
column_alias = m_lexer.consumeCurrentToken().token_string;
|
||||
} else {
|
||||
column_alias = "c" + std::to_string(i);
|
||||
i++;
|
||||
if (column_value->node_type == NodeType::database_value) {
|
||||
column_alias = ((DatabaseValueNode*) column_value.get())->col_name;
|
||||
} else {
|
||||
column_alias = "c" + std::to_string(i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
cols->push_back(SelectColNode{std::move(column_value), column_alias});
|
||||
}
|
||||
|
||||
|
|
|
|||
17
row.cpp
17
row.cpp
|
|
@ -164,14 +164,27 @@ int Row::compare(const Row &other) const {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void Row::print(const std::vector<int> &col_char_sizes) {
|
||||
void Row::print(const std::vector<ColDefNode> &col_defs) {
|
||||
std::string out{"| "};
|
||||
|
||||
for (int ci = 0; ci < m_columns.size(); ci++) {
|
||||
out.append(string_padd(m_columns[ci]->getStringValue(), col_char_sizes[ci], ' ', false) + " | ");
|
||||
auto & col_def = col_defs[ci];
|
||||
int col_size = print_get_column_size(col_def);
|
||||
|
||||
if (col_def.type==ColumnType::integer_type || col_def.type==ColumnType::float_type || col_def.type==ColumnType::bool_type)
|
||||
out.append(string_padd(m_columns[ci]->getStringValue(), col_size, ' ', false) + " | ");
|
||||
else
|
||||
out.append(string_padd(m_columns[ci]->getStringValue(), col_size, ' ', true) + " | ");
|
||||
}
|
||||
|
||||
std::cout << out << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int Row::print_get_column_size(const ColDefNode &col_def) {
|
||||
int col_size = col_def.type == ColumnType::varchar_type ? col_def.length :
|
||||
col_def.type == ColumnType::float_type ? 20 : 10;
|
||||
return col_size;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
|||
6
row.h
6
row.h
|
|
@ -61,7 +61,7 @@ namespace usql {
|
|||
ColumnType getColType() override { return ColumnType::float_type; };
|
||||
long getIntValue() override { return (long) m_double; };
|
||||
double getDoubleValue() override { return m_double; };
|
||||
std::string getStringValue() override { return std::to_string(m_double); };
|
||||
std::string getStringValue() override { return Settings::double_to_string(m_double); };
|
||||
long getDateValue() override { return (long) m_double; };
|
||||
bool getBoolValue() override { throw Exception("Not supported on ColDoubleValue"); };
|
||||
|
||||
|
|
@ -144,8 +144,8 @@ namespace usql {
|
|||
ColValue * ith_column(int i) const { return m_columns[i].get(); }
|
||||
int compare(const Row &other) const;
|
||||
|
||||
void print(const std::vector<int> & col_char_sizes);
|
||||
|
||||
void print(const std::vector<ColDefNode> &col_defs);
|
||||
static int print_get_column_size(const ColDefNode &col_def);
|
||||
private:
|
||||
std::vector<std::unique_ptr<ColValue>> m_columns;
|
||||
};
|
||||
|
|
|
|||
13
settings.cpp
13
settings.cpp
|
|
@ -8,7 +8,8 @@ namespace usql {
|
|||
std::vector<std::pair<std::string, std::string>> Settings::m_settings =
|
||||
{ std::make_pair("DATE_FORMAT", "%Y-%m-%d"),
|
||||
std::make_pair("BOOL_TRUE_LITERAL", "Y"),
|
||||
std::make_pair("BOOL_FALSE_LITERAL", "N") };
|
||||
std::make_pair("BOOL_FALSE_LITERAL", "N"),
|
||||
std::make_pair("DOUBLE_FORMAT", "%.2f") };
|
||||
|
||||
|
||||
long Settings::string_to_date(const std::string &datestr) {
|
||||
|
|
@ -20,6 +21,16 @@ std::string Settings::date_to_string(long date) {
|
|||
return ::date_to_string(date, get_setting("DATE_FORMAT"));
|
||||
}
|
||||
|
||||
std::string Settings::double_to_string(double d) {
|
||||
char buffer[32];
|
||||
int r, buf_size = 32;
|
||||
|
||||
r = snprintf(buffer, buf_size, get_setting("DOUBLE_FORMAT").c_str(), d);
|
||||
if (r > 0 && r < buf_size) return std::string(buffer);
|
||||
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
std::string Settings::get_setting(const std::string &name) {
|
||||
for(const auto& pair : m_settings) {
|
||||
if (pair.first == name) return pair.second;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ public:
|
|||
|
||||
static long string_to_date(const std::string &datestr);
|
||||
static std::string date_to_string(long date);
|
||||
static std::string double_to_string(double d);
|
||||
// TODO add bool_to_string and use it in ColBooleanValue
|
||||
|
||||
private:
|
||||
static std::vector<std::pair<std::string, std::string>> m_settings;
|
||||
|
|
|
|||
13
table.cpp
13
table.cpp
|
|
@ -155,14 +155,15 @@ long Table::string_to_long(const std::string &s) {
|
|||
void Table::print() {
|
||||
std::string out{"| "};
|
||||
std::string out2{"+-"};
|
||||
std::vector<int> col_char_sizes{};
|
||||
|
||||
for(const auto& col_def : m_col_defs) {
|
||||
int col_size = col_def.type == ColumnType::varchar_type ? col_def.length :
|
||||
col_def.type == ColumnType::float_type ? 20 : 10;
|
||||
col_char_sizes.push_back(col_size);
|
||||
int col_size = Row::print_get_column_size(col_def);
|
||||
|
||||
if (col_def.type==ColumnType::integer_type || col_def.type==ColumnType::float_type || col_def.type==ColumnType::bool_type)
|
||||
out.append(string_padd(col_def.name, col_size, ' ', false) + " | ");
|
||||
else
|
||||
out.append(string_padd(col_def.name, col_size, ' ', true) + " | ");
|
||||
|
||||
out.append(string_padd(col_def.name, col_size, ' ', true) + " | ");
|
||||
out2.append(string_padd("-", col_size, '-', true) + "-+ ");
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +172,7 @@ void Table::print() {
|
|||
std::cout << out2 << std::endl;
|
||||
|
||||
for(auto& row : m_rows) {
|
||||
row.print(col_char_sizes);
|
||||
row.print(m_col_defs);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue