Value.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/NumericLimits.h>
  8. #include <LibIPC/Decoder.h>
  9. #include <LibIPC/Encoder.h>
  10. #include <LibSQL/AST/AST.h>
  11. #include <LibSQL/Serializer.h>
  12. #include <LibSQL/TupleDescriptor.h>
  13. #include <LibSQL/Value.h>
  14. #include <math.h>
  15. #include <string.h>
  16. namespace SQL {
  17. Value::Value(SQLType type)
  18. : m_type(type)
  19. {
  20. }
  21. Value::Value(DeprecatedString value)
  22. : m_type(SQLType::Text)
  23. , m_value(move(value))
  24. {
  25. }
  26. Value::Value(int value)
  27. : m_type(SQLType::Integer)
  28. , m_value(value)
  29. {
  30. }
  31. Value::Value(u32 value)
  32. : m_type(SQLType::Integer)
  33. , m_value(static_cast<int>(value)) // FIXME: Handle signed overflow.
  34. {
  35. }
  36. Value::Value(double value)
  37. : m_type(SQLType::Float)
  38. , m_value(value)
  39. {
  40. }
  41. Value::Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values)
  42. : m_type(SQLType::Tuple)
  43. , m_value(TupleValue { move(descriptor), move(values) })
  44. {
  45. }
  46. Value::Value(Value const& other)
  47. : m_type(other.m_type)
  48. , m_value(other.m_value)
  49. {
  50. }
  51. Value::Value(Value&& other)
  52. : m_type(other.m_type)
  53. , m_value(move(other.m_value))
  54. {
  55. }
  56. Value::~Value() = default;
  57. ResultOr<Value> Value::create_tuple(NonnullRefPtr<TupleDescriptor> descriptor)
  58. {
  59. Vector<Value> values;
  60. TRY(values.try_resize(descriptor->size()));
  61. for (size_t i = 0; i < descriptor->size(); ++i)
  62. values[i].m_type = descriptor->at(i).type;
  63. return Value { move(descriptor), move(values) };
  64. }
  65. ResultOr<Value> Value::create_tuple(Vector<Value> values)
  66. {
  67. auto descriptor = TRY(infer_tuple_descriptor(values));
  68. return Value { move(descriptor), move(values) };
  69. }
  70. SQLType Value::type() const
  71. {
  72. return m_type;
  73. }
  74. StringView Value::type_name() const
  75. {
  76. switch (type()) {
  77. #undef __ENUMERATE_SQL_TYPE
  78. #define __ENUMERATE_SQL_TYPE(name, cardinal, type, impl, size) \
  79. case SQLType::type: \
  80. return name##sv;
  81. ENUMERATE_SQL_TYPES(__ENUMERATE_SQL_TYPE)
  82. #undef __ENUMERATE_SQL_TYPE
  83. default:
  84. VERIFY_NOT_REACHED();
  85. }
  86. }
  87. bool Value::is_type_compatible_with(SQLType other_type) const
  88. {
  89. switch (type()) {
  90. case SQLType::Null:
  91. return false;
  92. case SQLType::Integer:
  93. case SQLType::Float:
  94. return other_type == SQLType::Integer || other_type == SQLType::Float;
  95. default:
  96. break;
  97. }
  98. return type() == other_type;
  99. }
  100. bool Value::is_null() const
  101. {
  102. return !m_value.has_value();
  103. }
  104. DeprecatedString Value::to_deprecated_string() const
  105. {
  106. if (is_null())
  107. return "(null)"sv;
  108. return m_value->visit(
  109. [](DeprecatedString const& value) -> DeprecatedString { return value; },
  110. [](int value) -> DeprecatedString { return DeprecatedString::number(value); },
  111. [](double value) -> DeprecatedString { return DeprecatedString::number(value); },
  112. [](bool value) -> DeprecatedString { return value ? "true"sv : "false"sv; },
  113. [](TupleValue const& value) -> DeprecatedString {
  114. StringBuilder builder;
  115. builder.append('(');
  116. builder.join(',', value.values);
  117. builder.append(')');
  118. return builder.build();
  119. });
  120. }
  121. Optional<int> Value::to_int() const
  122. {
  123. if (is_null())
  124. return {};
  125. return m_value->visit(
  126. [](DeprecatedString const& value) -> Optional<int> { return value.to_int(); },
  127. [](int value) -> Optional<int> { return value; },
  128. [](double value) -> Optional<int> {
  129. if (value > static_cast<double>(NumericLimits<int>::max()))
  130. return {};
  131. if (value < static_cast<double>(NumericLimits<int>::min()))
  132. return {};
  133. return static_cast<int>(round(value));
  134. },
  135. [](bool value) -> Optional<int> { return static_cast<int>(value); },
  136. [](TupleValue const&) -> Optional<int> { return {}; });
  137. }
  138. Optional<u32> Value::to_u32() const
  139. {
  140. // FIXME: Handle negative values.
  141. if (auto result = to_int(); result.has_value())
  142. return static_cast<u32>(result.value());
  143. return {};
  144. }
  145. Optional<double> Value::to_double() const
  146. {
  147. if (is_null())
  148. return {};
  149. return m_value->visit(
  150. [](DeprecatedString const& value) -> Optional<double> {
  151. char* end = nullptr;
  152. double result = strtod(value.characters(), &end);
  153. if (end == value.characters())
  154. return {};
  155. return result;
  156. },
  157. [](int value) -> Optional<double> { return static_cast<double>(value); },
  158. [](double value) -> Optional<double> { return value; },
  159. [](bool value) -> Optional<double> { return static_cast<double>(value); },
  160. [](TupleValue const&) -> Optional<double> { return {}; });
  161. }
  162. Optional<bool> Value::to_bool() const
  163. {
  164. if (is_null())
  165. return {};
  166. return m_value->visit(
  167. [](DeprecatedString const& value) -> Optional<bool> {
  168. if (value.equals_ignoring_case("true"sv) || value.equals_ignoring_case("t"sv))
  169. return true;
  170. if (value.equals_ignoring_case("false"sv) || value.equals_ignoring_case("f"sv))
  171. return false;
  172. return {};
  173. },
  174. [](int value) -> Optional<bool> { return static_cast<bool>(value); },
  175. [](double value) -> Optional<bool> { return fabs(value) > NumericLimits<double>::epsilon(); },
  176. [](bool value) -> Optional<bool> { return value; },
  177. [](TupleValue const& value) -> Optional<bool> {
  178. for (auto const& element : value.values) {
  179. auto as_bool = element.to_bool();
  180. if (!as_bool.has_value())
  181. return {};
  182. if (!as_bool.value())
  183. return false;
  184. }
  185. return true;
  186. });
  187. }
  188. Optional<Vector<Value>> Value::to_vector() const
  189. {
  190. if (is_null() || (type() != SQLType::Tuple))
  191. return {};
  192. auto const& tuple = m_value->get<TupleValue>();
  193. return tuple.values;
  194. }
  195. Value& Value::operator=(Value value)
  196. {
  197. m_type = value.m_type;
  198. m_value = move(value.m_value);
  199. return *this;
  200. }
  201. Value& Value::operator=(DeprecatedString value)
  202. {
  203. m_type = SQLType::Text;
  204. m_value = move(value);
  205. return *this;
  206. }
  207. Value& Value::operator=(int value)
  208. {
  209. m_type = SQLType::Integer;
  210. m_value = value;
  211. return *this;
  212. }
  213. Value& Value::operator=(u32 value)
  214. {
  215. m_type = SQLType::Integer;
  216. m_value = static_cast<int>(value); // FIXME: Handle signed overflow.
  217. return *this;
  218. }
  219. Value& Value::operator=(double value)
  220. {
  221. m_type = SQLType::Float;
  222. m_value = value;
  223. return *this;
  224. }
  225. ResultOr<void> Value::assign_tuple(NonnullRefPtr<TupleDescriptor> descriptor)
  226. {
  227. Vector<Value> values;
  228. TRY(values.try_resize(descriptor->size()));
  229. for (size_t i = 0; i < descriptor->size(); ++i)
  230. values[i].m_type = descriptor->at(i).type;
  231. m_type = SQLType::Tuple;
  232. m_value = TupleValue { move(descriptor), move(values) };
  233. return {};
  234. }
  235. ResultOr<void> Value::assign_tuple(Vector<Value> values)
  236. {
  237. if (is_null() || (type() != SQLType::Tuple)) {
  238. auto descriptor = TRY(infer_tuple_descriptor(values));
  239. m_type = SQLType::Tuple;
  240. m_value = TupleValue { move(descriptor), move(values) };
  241. return {};
  242. }
  243. auto& tuple = m_value->get<TupleValue>();
  244. if (values.size() > tuple.descriptor->size())
  245. return Result { SQLCommand::Unknown, SQLErrorCode::InvalidNumberOfValues };
  246. for (size_t i = 0; i < values.size(); ++i) {
  247. if (values[i].type() != tuple.descriptor->at(i).type)
  248. return Result { SQLCommand::Unknown, SQLErrorCode::InvalidType, SQLType_name(values[i].type()) };
  249. }
  250. if (values.size() < tuple.descriptor->size()) {
  251. size_t original_size = values.size();
  252. MUST(values.try_resize(tuple.descriptor->size()));
  253. for (size_t i = original_size; i < values.size(); ++i)
  254. values[i].m_type = tuple.descriptor->at(i).type;
  255. }
  256. m_value = TupleValue { move(tuple.descriptor), move(values) };
  257. return {};
  258. }
  259. size_t Value::length() const
  260. {
  261. if (is_null())
  262. return 0;
  263. // FIXME: This seems to be more of an encoded byte size rather than a length.
  264. return m_value->visit(
  265. [](DeprecatedString const& value) -> size_t { return sizeof(u32) + value.length(); },
  266. [](int value) -> size_t { return sizeof(value); },
  267. [](double value) -> size_t { return sizeof(value); },
  268. [](bool value) -> size_t { return sizeof(value); },
  269. [](TupleValue const& value) -> size_t {
  270. auto size = value.descriptor->length() + sizeof(u32);
  271. for (auto const& element : value.values)
  272. size += element.length();
  273. return size;
  274. });
  275. }
  276. u32 Value::hash() const
  277. {
  278. if (is_null())
  279. return 0;
  280. return m_value->visit(
  281. [](DeprecatedString const& value) -> u32 { return value.hash(); },
  282. [](int value) -> u32 { return int_hash(value); },
  283. [](double) -> u32 { VERIFY_NOT_REACHED(); },
  284. [](bool value) -> u32 { return int_hash(value); },
  285. [](TupleValue const& value) -> u32 {
  286. u32 hash = 0;
  287. for (auto const& element : value.values) {
  288. if (hash == 0)
  289. hash = element.hash();
  290. else
  291. hash = pair_int_hash(hash, element.hash());
  292. }
  293. return hash;
  294. });
  295. }
  296. int Value::compare(Value const& other) const
  297. {
  298. if (is_null())
  299. return -1;
  300. if (other.is_null())
  301. return 1;
  302. return m_value->visit(
  303. [&](DeprecatedString const& value) -> int { return value.view().compare(other.to_deprecated_string()); },
  304. [&](int value) -> int {
  305. auto casted = other.to_int();
  306. if (!casted.has_value())
  307. return 1;
  308. if (value == *casted)
  309. return 0;
  310. return value < *casted ? -1 : 1;
  311. },
  312. [&](double value) -> int {
  313. auto casted = other.to_double();
  314. if (!casted.has_value())
  315. return 1;
  316. auto diff = value - *casted;
  317. if (fabs(diff) < NumericLimits<double>::epsilon())
  318. return 0;
  319. return diff < 0 ? -1 : 1;
  320. },
  321. [&](bool value) -> int {
  322. auto casted = other.to_bool();
  323. if (!casted.has_value())
  324. return 1;
  325. return value ^ *casted;
  326. },
  327. [&](TupleValue const& value) -> int {
  328. if (other.is_null() || (other.type() != SQLType::Tuple)) {
  329. if (value.values.size() == 1)
  330. return value.values[0].compare(other);
  331. return 1;
  332. }
  333. auto const& other_value = other.m_value->get<TupleValue>();
  334. if (auto result = value.descriptor->compare_ignoring_names(*other_value.descriptor); result != 0)
  335. return 1;
  336. if (value.values.size() != other_value.values.size())
  337. return value.values.size() < other_value.values.size() ? -1 : 1;
  338. for (size_t i = 0; i < value.values.size(); ++i) {
  339. auto result = value.values[i].compare(other_value.values[i]);
  340. if (result == 0)
  341. continue;
  342. if (value.descriptor->at(i).order == Order::Descending)
  343. result = -result;
  344. return result;
  345. }
  346. return 0;
  347. });
  348. }
  349. bool Value::operator==(Value const& value) const
  350. {
  351. return compare(value) == 0;
  352. }
  353. bool Value::operator==(StringView value) const
  354. {
  355. return to_deprecated_string() == value;
  356. }
  357. bool Value::operator==(int value) const
  358. {
  359. return to_int() == value;
  360. }
  361. bool Value::operator==(u32 value) const
  362. {
  363. return to_u32() == value;
  364. }
  365. bool Value::operator==(double value) const
  366. {
  367. return to_double() == value;
  368. }
  369. bool Value::operator!=(Value const& value) const
  370. {
  371. return compare(value) != 0;
  372. }
  373. bool Value::operator<(Value const& value) const
  374. {
  375. return compare(value) < 0;
  376. }
  377. bool Value::operator<=(Value const& value) const
  378. {
  379. return compare(value) <= 0;
  380. }
  381. bool Value::operator>(Value const& value) const
  382. {
  383. return compare(value) > 0;
  384. }
  385. bool Value::operator>=(Value const& value) const
  386. {
  387. return compare(value) >= 0;
  388. }
  389. static Result invalid_type_for_numeric_operator(AST::BinaryOperator op)
  390. {
  391. return { SQLCommand::Unknown, SQLErrorCode::NumericOperatorTypeMismatch, BinaryOperator_name(op) };
  392. }
  393. ResultOr<Value> Value::add(Value const& other) const
  394. {
  395. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  396. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  397. return Value(double_maybe.value() + other_double_maybe.value());
  398. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  399. return Value(double_maybe.value() + (double)int_maybe.value());
  400. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  401. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  402. return Value(other_double_maybe.value() + (double)int_maybe.value());
  403. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  404. return Value(int_maybe.value() + other_int_maybe.value());
  405. }
  406. return invalid_type_for_numeric_operator(AST::BinaryOperator::Plus);
  407. }
  408. ResultOr<Value> Value::subtract(Value const& other) const
  409. {
  410. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  411. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  412. return Value(double_maybe.value() - other_double_maybe.value());
  413. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  414. return Value(double_maybe.value() - (double)int_maybe.value());
  415. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  416. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  417. return Value((double)int_maybe.value() - other_double_maybe.value());
  418. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  419. return Value(int_maybe.value() - other_int_maybe.value());
  420. }
  421. return invalid_type_for_numeric_operator(AST::BinaryOperator::Minus);
  422. }
  423. ResultOr<Value> Value::multiply(Value const& other) const
  424. {
  425. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  426. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  427. return Value(double_maybe.value() * other_double_maybe.value());
  428. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  429. return Value(double_maybe.value() * (double)int_maybe.value());
  430. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  431. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  432. return Value((double)int_maybe.value() * other_double_maybe.value());
  433. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  434. return Value(int_maybe.value() * other_int_maybe.value());
  435. }
  436. return invalid_type_for_numeric_operator(AST::BinaryOperator::Multiplication);
  437. }
  438. ResultOr<Value> Value::divide(Value const& other) const
  439. {
  440. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  441. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  442. return Value(double_maybe.value() / other_double_maybe.value());
  443. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  444. return Value(double_maybe.value() / (double)int_maybe.value());
  445. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  446. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  447. return Value((double)int_maybe.value() / other_double_maybe.value());
  448. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  449. return Value(int_maybe.value() / other_int_maybe.value());
  450. }
  451. return invalid_type_for_numeric_operator(AST::BinaryOperator::Division);
  452. }
  453. ResultOr<Value> Value::modulo(Value const& other) const
  454. {
  455. auto int_maybe_1 = to_int();
  456. auto int_maybe_2 = other.to_int();
  457. if (!int_maybe_1.has_value() || !int_maybe_2.has_value())
  458. return invalid_type_for_numeric_operator(AST::BinaryOperator::Modulo);
  459. return Value(int_maybe_1.value() % int_maybe_2.value());
  460. }
  461. ResultOr<Value> Value::shift_left(Value const& other) const
  462. {
  463. auto u32_maybe = to_u32();
  464. auto num_bytes_maybe = other.to_int();
  465. if (!u32_maybe.has_value() || !num_bytes_maybe.has_value())
  466. return invalid_type_for_numeric_operator(AST::BinaryOperator::ShiftLeft);
  467. return Value(u32_maybe.value() << num_bytes_maybe.value());
  468. }
  469. ResultOr<Value> Value::shift_right(Value const& other) const
  470. {
  471. auto u32_maybe = to_u32();
  472. auto num_bytes_maybe = other.to_int();
  473. if (!u32_maybe.has_value() || !num_bytes_maybe.has_value())
  474. return invalid_type_for_numeric_operator(AST::BinaryOperator::ShiftRight);
  475. return Value(u32_maybe.value() >> num_bytes_maybe.value());
  476. }
  477. ResultOr<Value> Value::bitwise_or(Value const& other) const
  478. {
  479. auto u32_maybe_1 = to_u32();
  480. auto u32_maybe_2 = other.to_u32();
  481. if (!u32_maybe_1.has_value() || !u32_maybe_2.has_value())
  482. return invalid_type_for_numeric_operator(AST::BinaryOperator::BitwiseOr);
  483. return Value(u32_maybe_1.value() | u32_maybe_2.value());
  484. }
  485. ResultOr<Value> Value::bitwise_and(Value const& other) const
  486. {
  487. auto u32_maybe_1 = to_u32();
  488. auto u32_maybe_2 = other.to_u32();
  489. if (!u32_maybe_1.has_value() || !u32_maybe_2.has_value())
  490. return invalid_type_for_numeric_operator(AST::BinaryOperator::BitwiseAnd);
  491. return Value(u32_maybe_1.value() & u32_maybe_2.value());
  492. }
  493. static constexpr auto sql_type_null_as_flag = static_cast<u8>(SQLType::Null);
  494. void Value::serialize(Serializer& serializer) const
  495. {
  496. auto type_flags = static_cast<u8>(type());
  497. if (is_null())
  498. type_flags |= sql_type_null_as_flag;
  499. serializer.serialize<u8>(type_flags);
  500. if (is_null())
  501. return;
  502. m_value->visit(
  503. [&](TupleValue const& value) {
  504. serializer.serialize<TupleDescriptor>(*value.descriptor);
  505. serializer.serialize(static_cast<u32>(value.values.size()));
  506. for (auto const& element : value.values)
  507. serializer.serialize<Value>(element);
  508. },
  509. [&](auto const& value) { serializer.serialize(value); });
  510. }
  511. void Value::deserialize(Serializer& serializer)
  512. {
  513. auto type_flags = serializer.deserialize<u8>();
  514. bool has_value = true;
  515. if ((type_flags & sql_type_null_as_flag) && (type_flags != sql_type_null_as_flag)) {
  516. type_flags &= ~sql_type_null_as_flag;
  517. has_value = false;
  518. }
  519. m_type = static_cast<SQLType>(type_flags);
  520. if (!has_value)
  521. return;
  522. switch (m_type) {
  523. case SQLType::Null:
  524. VERIFY_NOT_REACHED();
  525. break;
  526. case SQLType::Text:
  527. m_value = serializer.deserialize<DeprecatedString>();
  528. break;
  529. case SQLType::Integer:
  530. m_value = serializer.deserialize<int>(0);
  531. break;
  532. case SQLType::Float:
  533. m_value = serializer.deserialize<double>(0.0);
  534. break;
  535. case SQLType::Boolean:
  536. m_value = serializer.deserialize<bool>(false);
  537. break;
  538. case SQLType::Tuple: {
  539. auto descriptor = serializer.adopt_and_deserialize<TupleDescriptor>();
  540. auto size = serializer.deserialize<u32>();
  541. Vector<Value> values;
  542. values.ensure_capacity(size);
  543. for (size_t i = 0; i < size; ++i)
  544. values.unchecked_append(serializer.deserialize<Value>());
  545. m_value = TupleValue { move(descriptor), move(values) };
  546. break;
  547. }
  548. }
  549. }
  550. TupleElementDescriptor Value::descriptor() const
  551. {
  552. return { "", "", "", type(), Order::Ascending };
  553. }
  554. ResultOr<NonnullRefPtr<TupleDescriptor>> Value::infer_tuple_descriptor(Vector<Value> const& values)
  555. {
  556. auto descriptor = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SQL::TupleDescriptor));
  557. TRY(descriptor->try_ensure_capacity(values.size()));
  558. for (auto const& element : values)
  559. descriptor->unchecked_append({ ""sv, ""sv, ""sv, element.type(), Order::Ascending });
  560. return descriptor;
  561. }
  562. }
  563. template<>
  564. bool IPC::encode(Encoder& encoder, SQL::Value const& value)
  565. {
  566. auto type_flags = to_underlying(value.type());
  567. if (value.is_null())
  568. type_flags |= SQL::sql_type_null_as_flag;
  569. encoder << type_flags;
  570. if (value.is_null())
  571. return true;
  572. switch (value.type()) {
  573. case SQL::SQLType::Null:
  574. break;
  575. case SQL::SQLType::Text:
  576. encoder << value.to_deprecated_string();
  577. break;
  578. case SQL::SQLType::Integer:
  579. encoder << value.to_int().value();
  580. break;
  581. case SQL::SQLType::Float:
  582. encoder << value.to_double().value();
  583. break;
  584. case SQL::SQLType::Boolean:
  585. encoder << value.to_bool().value();
  586. break;
  587. case SQL::SQLType::Tuple:
  588. encoder << value.to_vector().value();
  589. break;
  590. }
  591. return true;
  592. }
  593. template<>
  594. ErrorOr<void> IPC::decode(Decoder& decoder, SQL::Value& value)
  595. {
  596. UnderlyingType<SQL::SQLType> type_flags;
  597. TRY(decoder.decode(type_flags));
  598. if ((type_flags & SQL::sql_type_null_as_flag) && (type_flags != SQL::sql_type_null_as_flag)) {
  599. type_flags &= ~SQL::sql_type_null_as_flag;
  600. value = SQL::Value(static_cast<SQL::SQLType>(type_flags));
  601. return {};
  602. }
  603. switch (static_cast<SQL::SQLType>(type_flags)) {
  604. case SQL::SQLType::Null:
  605. break;
  606. case SQL::SQLType::Text: {
  607. DeprecatedString text;
  608. TRY(decoder.decode(text));
  609. value = move(text);
  610. break;
  611. }
  612. case SQL::SQLType::Integer: {
  613. int number { 0 };
  614. TRY(decoder.decode(number));
  615. value = number;
  616. break;
  617. }
  618. case SQL::SQLType::Float: {
  619. double number { 0.0 };
  620. TRY(decoder.decode(number));
  621. value = number;
  622. break;
  623. }
  624. case SQL::SQLType::Boolean: {
  625. bool boolean { false };
  626. TRY(decoder.decode(boolean));
  627. value = boolean;
  628. break;
  629. }
  630. case SQL::SQLType::Tuple: {
  631. Vector<SQL::Value> tuple;
  632. TRY(decoder.decode(tuple));
  633. if (auto result = value.assign_tuple(move(tuple)); result.is_error())
  634. return Error::from_errno(to_underlying(result.error().error()));
  635. break;
  636. }
  637. }
  638. return {};
  639. }