2018-01-18 03:14:13 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-08 11:00:33 +02:00
|
|
|
|
|
|
|
#pragma once
|
2017-09-09 21:20:53 +02:00
|
|
|
#define HAVE_IRCD_DB_JSON_H
|
2017-09-08 11:00:33 +02:00
|
|
|
|
|
|
|
namespace ircd::db
|
|
|
|
{
|
2017-09-22 01:59:58 +02:00
|
|
|
template<class tuple> bool assign(tuple &, const cell &);
|
|
|
|
template<class tuple> bool assign(tuple &, const cell &, const string_view &keyeq);
|
|
|
|
|
|
|
|
template<class tuple> size_t assign(tuple &, const row &);
|
|
|
|
template<class tuple> size_t assign(tuple &, const row &, const string_view &keyeq);
|
|
|
|
|
|
|
|
template<class tuple, class... args> tuple make_tuple(args&&...);
|
2017-09-08 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:59:58 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
template<class tuple> void _assign_invalid(tuple &, const cell &);
|
|
|
|
template<class tuple> void _assign_valid(tuple &, const cell &);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class... args>
|
|
|
|
tuple
|
|
|
|
ircd::db::make_tuple(args&&... a)
|
|
|
|
{
|
|
|
|
tuple ret;
|
|
|
|
assign(ret, std::forward<args>(a)...);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
size_t
|
|
|
|
ircd::db::assign(tuple &t,
|
|
|
|
const row &row,
|
|
|
|
const string_view &keyeq)
|
|
|
|
{
|
|
|
|
return std::count_if(std::begin(row), std::end(row), [&t, &keyeq]
|
|
|
|
(const auto &cell)
|
|
|
|
{
|
|
|
|
return assign(t, cell, keyeq);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
size_t
|
|
|
|
ircd::db::assign(tuple &t,
|
|
|
|
const row &row)
|
2017-09-08 11:00:33 +02:00
|
|
|
{
|
2017-09-22 01:59:58 +02:00
|
|
|
return std::count_if(std::begin(row), std::end(row), [&t]
|
|
|
|
(const auto &cell)
|
2017-09-08 11:00:33 +02:00
|
|
|
{
|
2017-09-22 01:59:58 +02:00
|
|
|
return assign(t, cell);
|
2017-09-08 11:00:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:59:58 +02:00
|
|
|
template<class tuple>
|
|
|
|
bool
|
|
|
|
ircd::db::assign(tuple &t,
|
|
|
|
const cell &cell)
|
|
|
|
{
|
|
|
|
const bool valid
|
|
|
|
{
|
|
|
|
cell.valid()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(valid)
|
|
|
|
_assign_valid(t, cell);
|
|
|
|
else
|
|
|
|
_assign_invalid(t, cell);
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
bool
|
|
|
|
ircd::db::assign(tuple &t,
|
|
|
|
const cell &cell,
|
|
|
|
const string_view &keyeq)
|
|
|
|
{
|
|
|
|
const bool valid
|
|
|
|
{
|
|
|
|
cell.valid(keyeq)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(valid)
|
|
|
|
_assign_valid(t, cell);
|
|
|
|
else
|
|
|
|
_assign_invalid(t, cell);
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
2017-09-08 11:00:33 +02:00
|
|
|
void
|
2017-09-22 01:59:58 +02:00
|
|
|
ircd::db::_assign_invalid(tuple &t,
|
|
|
|
const cell &cell)
|
2017-09-08 11:00:33 +02:00
|
|
|
{
|
2019-06-23 01:12:14 +02:00
|
|
|
const column &c(cell);
|
2018-05-20 06:50:11 +02:00
|
|
|
if(!c)
|
|
|
|
return;
|
|
|
|
|
2017-09-22 01:59:58 +02:00
|
|
|
const auto &descriptor
|
|
|
|
{
|
|
|
|
describe(c)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool is_string
|
|
|
|
{
|
|
|
|
descriptor.type.second == typeid(string_view)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(is_string)
|
2017-10-03 12:56:37 +02:00
|
|
|
{
|
2017-09-22 01:59:58 +02:00
|
|
|
json::set(t, cell.col(), string_view{});
|
2017-10-03 12:56:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool is_integer
|
|
|
|
{
|
|
|
|
descriptor.type.second == typeid(int64_t) ||
|
|
|
|
descriptor.type.second == typeid(double)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(is_integer)
|
|
|
|
{
|
2018-05-20 07:10:58 +02:00
|
|
|
json::set(t, cell.col(), json::undefined_number);
|
2017-10-03 12:56:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 11:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
2017-09-22 01:59:58 +02:00
|
|
|
void
|
|
|
|
ircd::db::_assign_valid(tuple &t,
|
|
|
|
const cell &cell)
|
2017-09-08 11:00:33 +02:00
|
|
|
{
|
2019-06-23 01:12:14 +02:00
|
|
|
const column &c(cell);
|
2017-09-22 01:59:58 +02:00
|
|
|
const auto &descriptor
|
|
|
|
{
|
|
|
|
describe(c)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool is_string
|
|
|
|
{
|
|
|
|
descriptor.type.second == typeid(string_view)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(is_string)
|
|
|
|
json::set(t, cell.col(), cell.val());
|
|
|
|
else
|
|
|
|
json::set(t, cell.col(), byte_view<string_view>{cell.val()});
|
2017-09-08 11:00:33 +02:00
|
|
|
}
|