Value.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSQL/AST/AST.h>
  7. #include <LibSQL/Serializer.h>
  8. #include <LibSQL/Value.h>
  9. #include <math.h>
  10. #include <string.h>
  11. namespace SQL {
  12. Value::Value(SQLType sql_type)
  13. {
  14. setup(sql_type);
  15. }
  16. void Value::setup(SQLType type)
  17. {
  18. switch (type) {
  19. #undef __ENUMERATE_SQL_TYPE
  20. #define __ENUMERATE_SQL_TYPE(name, cardinal, type, impl, size) \
  21. case SQLType::type: \
  22. m_impl.set<type##Impl>(type##Impl()); \
  23. break;
  24. ENUMERATE_SQL_TYPES(__ENUMERATE_SQL_TYPE)
  25. #undef __ENUMERATE_SQL_TYPE
  26. default:
  27. VERIFY_NOT_REACHED();
  28. }
  29. }
  30. Value::Value(SQLType sql_type, Value const& value)
  31. : Value(sql_type)
  32. {
  33. assign(value);
  34. }
  35. Value::Value(SQLType sql_type, String const& string)
  36. : Value(sql_type)
  37. {
  38. assign(string);
  39. }
  40. Value::Value(SQLType sql_type, char const* string)
  41. : Value(sql_type)
  42. {
  43. assign(String(string));
  44. }
  45. Value::Value(SQLType sql_type, int integer)
  46. : Value(sql_type)
  47. {
  48. assign(integer);
  49. }
  50. Value::Value(SQLType sql_type, double dbl)
  51. : Value(sql_type)
  52. {
  53. assign(dbl);
  54. }
  55. Value::Value(SQLType sql_type, bool boolean)
  56. : Value(sql_type)
  57. {
  58. assign(boolean);
  59. }
  60. Value::Value(String const& string)
  61. : Value(SQLType::Text)
  62. {
  63. assign(string);
  64. }
  65. Value::Value(char const* string)
  66. : Value(SQLType::Text)
  67. {
  68. assign(String(string));
  69. }
  70. Value::Value(int integer)
  71. : Value(SQLType::Integer)
  72. {
  73. assign(integer);
  74. }
  75. Value::Value(u32 unsigned_integer)
  76. : Value(SQLType::Integer)
  77. {
  78. assign(unsigned_integer);
  79. }
  80. Value::Value(double dbl)
  81. : Value(SQLType::Float)
  82. {
  83. assign(dbl);
  84. }
  85. Value::Value(bool boolean)
  86. : Value(SQLType::Boolean)
  87. {
  88. assign(boolean);
  89. }
  90. Value Value::create_tuple(NonnullRefPtr<TupleDescriptor> const& tuple_descriptor)
  91. {
  92. return Value(Value::SetImplementationSingleton, TupleImpl(tuple_descriptor));
  93. }
  94. Value Value::create_array(SQLType element_type, Optional<size_t> const& max_size)
  95. {
  96. return Value(Value::SetImplementationSingleton, ArrayImpl(element_type, max_size));
  97. }
  98. Value const& Value::null()
  99. {
  100. static Value s_null(SQLType::Null);
  101. return s_null;
  102. }
  103. bool Value::is_null() const
  104. {
  105. return m_impl.visit([&](auto& impl) { return impl.is_null(); });
  106. }
  107. SQLType Value::type() const
  108. {
  109. return m_impl.visit([&](auto& impl) { return impl.type(); });
  110. }
  111. String Value::type_name() const
  112. {
  113. return m_impl.visit([&](auto& impl) { return impl.type_name(); });
  114. }
  115. BaseTypeImpl Value::downcast_to_basetype() const
  116. {
  117. return m_impl.downcast<NullImpl, TextImpl, IntegerImpl, FloatImpl, BooleanImpl>();
  118. }
  119. String Value::to_string() const
  120. {
  121. if (is_null())
  122. return "(null)";
  123. return m_impl.visit([&](auto& impl) { return impl.to_string(); });
  124. }
  125. Optional<int> Value::to_int() const
  126. {
  127. if (is_null())
  128. return {};
  129. return m_impl.visit([&](auto& impl) { return impl.to_int(); });
  130. }
  131. Optional<u32> Value::to_u32() const
  132. {
  133. if (is_null())
  134. return {};
  135. auto ret = to_int();
  136. if (ret.has_value())
  137. return static_cast<u32>(ret.value());
  138. return {};
  139. }
  140. Optional<double> Value::to_double() const
  141. {
  142. if (is_null())
  143. return {};
  144. return m_impl.visit([&](auto& impl) { return impl.to_double(); });
  145. }
  146. Optional<bool> Value::to_bool() const
  147. {
  148. if (is_null())
  149. return {};
  150. return m_impl.visit([&](auto& impl) { return impl.to_bool(); });
  151. }
  152. Optional<Vector<Value>> Value::to_vector() const
  153. {
  154. if (is_null())
  155. return {};
  156. Vector<Value> vector;
  157. if (m_impl.visit([&](auto& impl) { return impl.to_vector(vector); }))
  158. return vector;
  159. else
  160. return {};
  161. }
  162. void Value::assign(Value const& other_value)
  163. {
  164. m_impl.visit([&](auto& impl) { impl.assign(other_value); });
  165. }
  166. void Value::assign(String const& string_value)
  167. {
  168. m_impl.visit([&](auto& impl) { impl.assign_string(string_value); });
  169. }
  170. void Value::assign(int int_value)
  171. {
  172. m_impl.visit([&](auto& impl) { impl.assign_int(int_value); });
  173. }
  174. void Value::assign(u32 unsigned_int_value)
  175. {
  176. m_impl.visit([&](auto& impl) { impl.assign_int(unsigned_int_value); });
  177. }
  178. void Value::assign(double double_value)
  179. {
  180. m_impl.visit([&](auto& impl) { impl.assign_double(double_value); });
  181. }
  182. void Value::assign(bool bool_value)
  183. {
  184. m_impl.visit([&](auto& impl) { impl.assign_bool(bool_value); });
  185. }
  186. void Value::assign(Vector<Value> const& values)
  187. {
  188. m_impl.visit([&](auto& impl) { impl.assign_vector(values); });
  189. }
  190. Value& Value::operator=(Value const& other)
  191. {
  192. if (this != &other) {
  193. if (other.is_null()) {
  194. assign(null());
  195. } else if (is_null()) {
  196. assign(other);
  197. } else {
  198. VERIFY(can_cast(other));
  199. assign(other);
  200. }
  201. }
  202. return (*this);
  203. }
  204. Value& Value::operator=(String const& value)
  205. {
  206. assign(value);
  207. return (*this);
  208. }
  209. Value& Value::operator=(char const* value)
  210. {
  211. assign(String(value));
  212. return (*this);
  213. }
  214. Value& Value::operator=(int value)
  215. {
  216. assign(value);
  217. return (*this);
  218. }
  219. Value& Value::operator=(u32 value)
  220. {
  221. assign(static_cast<int>(value));
  222. return (*this);
  223. }
  224. Value& Value::operator=(double value)
  225. {
  226. assign(value);
  227. return (*this);
  228. }
  229. Value& Value::operator=(bool value)
  230. {
  231. assign(value);
  232. return (*this);
  233. }
  234. Value& Value::operator=(Vector<Value> const& vector)
  235. {
  236. assign(vector);
  237. return (*this);
  238. }
  239. size_t Value::length() const
  240. {
  241. return m_impl.visit([&](auto& impl) { return impl.length(); });
  242. }
  243. u32 Value::hash() const
  244. {
  245. return (is_null()) ? 0u : m_impl.visit([&](auto& impl) { return impl.hash(); });
  246. }
  247. bool Value::can_cast(Value const& other_value) const
  248. {
  249. if (type() == other_value.type())
  250. return true;
  251. return m_impl.visit([&](auto& impl) { return impl.can_cast(other_value); });
  252. }
  253. int Value::compare(Value const& other) const
  254. {
  255. if (is_null())
  256. return -1;
  257. if (other.is_null())
  258. return 1;
  259. return m_impl.visit([&](auto& impl) { return impl.compare(other); });
  260. }
  261. bool Value::operator==(Value const& other) const
  262. {
  263. return compare(other) == 0;
  264. }
  265. bool Value::operator==(String const& string_value) const
  266. {
  267. return to_string() == string_value;
  268. }
  269. bool Value::operator==(int int_value) const
  270. {
  271. auto i = to_int();
  272. if (!i.has_value())
  273. return false;
  274. return i.value() == int_value;
  275. }
  276. bool Value::operator==(double double_value) const
  277. {
  278. auto d = to_double();
  279. if (!d.has_value())
  280. return false;
  281. return d.value() == double_value;
  282. }
  283. bool Value::operator!=(Value const& other) const
  284. {
  285. return compare(other) != 0;
  286. }
  287. bool Value::operator<(Value const& other) const
  288. {
  289. return compare(other) < 0;
  290. }
  291. bool Value::operator<=(Value const& other) const
  292. {
  293. return compare(other) <= 0;
  294. }
  295. bool Value::operator>(Value const& other) const
  296. {
  297. return compare(other) > 0;
  298. }
  299. bool Value::operator>=(Value const& other) const
  300. {
  301. return compare(other) >= 0;
  302. }
  303. static Result invalid_type_for_numeric_operator(AST::BinaryOperator op)
  304. {
  305. return { SQLCommand::Unknown, SQLErrorCode::NumericOperatorTypeMismatch, BinaryOperator_name(op) };
  306. }
  307. ResultOr<Value> Value::add(Value const& other) const
  308. {
  309. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  310. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  311. return Value(double_maybe.value() + other_double_maybe.value());
  312. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  313. return Value(double_maybe.value() + (double)int_maybe.value());
  314. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  315. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  316. return Value(other_double_maybe.value() + (double)int_maybe.value());
  317. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  318. return Value(int_maybe.value() + other_int_maybe.value());
  319. }
  320. return invalid_type_for_numeric_operator(AST::BinaryOperator::Plus);
  321. }
  322. ResultOr<Value> Value::subtract(Value const& other) const
  323. {
  324. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  325. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  326. return Value(double_maybe.value() - other_double_maybe.value());
  327. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  328. return Value(double_maybe.value() - (double)int_maybe.value());
  329. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  330. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  331. return Value((double)int_maybe.value() - other_double_maybe.value());
  332. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  333. return Value(int_maybe.value() - other_int_maybe.value());
  334. }
  335. return invalid_type_for_numeric_operator(AST::BinaryOperator::Minus);
  336. }
  337. ResultOr<Value> Value::multiply(Value const& other) const
  338. {
  339. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  340. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  341. return Value(double_maybe.value() * other_double_maybe.value());
  342. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  343. return Value(double_maybe.value() * (double)int_maybe.value());
  344. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  345. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  346. return Value((double)int_maybe.value() * other_double_maybe.value());
  347. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  348. return Value(int_maybe.value() * other_int_maybe.value());
  349. }
  350. return invalid_type_for_numeric_operator(AST::BinaryOperator::Multiplication);
  351. }
  352. ResultOr<Value> Value::divide(Value const& other) const
  353. {
  354. if (auto double_maybe = to_double(); double_maybe.has_value()) {
  355. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  356. return Value(double_maybe.value() / other_double_maybe.value());
  357. if (auto int_maybe = other.to_int(); int_maybe.has_value())
  358. return Value(double_maybe.value() / (double)int_maybe.value());
  359. } else if (auto int_maybe = to_int(); int_maybe.has_value()) {
  360. if (auto other_double_maybe = other.to_double(); other_double_maybe.has_value())
  361. return Value((double)int_maybe.value() / other_double_maybe.value());
  362. if (auto other_int_maybe = other.to_int(); other_int_maybe.has_value())
  363. return Value(int_maybe.value() / other_int_maybe.value());
  364. }
  365. return invalid_type_for_numeric_operator(AST::BinaryOperator::Division);
  366. }
  367. ResultOr<Value> Value::modulo(Value const& other) const
  368. {
  369. auto int_maybe_1 = to_int();
  370. auto int_maybe_2 = other.to_int();
  371. if (!int_maybe_1.has_value() || !int_maybe_2.has_value())
  372. return invalid_type_for_numeric_operator(AST::BinaryOperator::Modulo);
  373. return Value(int_maybe_1.value() % int_maybe_2.value());
  374. }
  375. ResultOr<Value> Value::shift_left(Value const& other) const
  376. {
  377. auto u32_maybe = to_u32();
  378. auto num_bytes_maybe = other.to_int();
  379. if (!u32_maybe.has_value() || !num_bytes_maybe.has_value())
  380. return invalid_type_for_numeric_operator(AST::BinaryOperator::ShiftLeft);
  381. return Value(u32_maybe.value() << num_bytes_maybe.value());
  382. }
  383. ResultOr<Value> Value::shift_right(Value const& other) const
  384. {
  385. auto u32_maybe = to_u32();
  386. auto num_bytes_maybe = other.to_int();
  387. if (!u32_maybe.has_value() || !num_bytes_maybe.has_value())
  388. return invalid_type_for_numeric_operator(AST::BinaryOperator::ShiftRight);
  389. return Value(u32_maybe.value() >> num_bytes_maybe.value());
  390. }
  391. ResultOr<Value> Value::bitwise_or(Value const& other) const
  392. {
  393. auto u32_maybe_1 = to_u32();
  394. auto u32_maybe_2 = other.to_u32();
  395. if (!u32_maybe_1.has_value() || !u32_maybe_2.has_value())
  396. return invalid_type_for_numeric_operator(AST::BinaryOperator::BitwiseOr);
  397. return Value(u32_maybe_1.value() | u32_maybe_2.value());
  398. }
  399. ResultOr<Value> Value::bitwise_and(Value const& other) const
  400. {
  401. auto u32_maybe_1 = to_u32();
  402. auto u32_maybe_2 = other.to_u32();
  403. if (!u32_maybe_1.has_value() || !u32_maybe_2.has_value())
  404. return invalid_type_for_numeric_operator(AST::BinaryOperator::BitwiseAnd);
  405. return Value(u32_maybe_1.value() & u32_maybe_2.value());
  406. }
  407. void Value::serialize(Serializer& serializer) const
  408. {
  409. u8 type_flags = (u8)type();
  410. if (is_null())
  411. type_flags |= (u8)SQLType::Null;
  412. serializer.serialize<u8>(type_flags);
  413. if (!is_null())
  414. m_impl.visit([&](auto& impl) { serializer.serialize(impl); });
  415. }
  416. void Value::deserialize(Serializer& serializer)
  417. {
  418. auto type_flags = serializer.deserialize<u8>();
  419. bool is_null = false;
  420. if ((type_flags & (u8)SQLType::Null) && (type_flags != (u8)SQLType::Null)) {
  421. type_flags &= ~((u8)SQLType::Null);
  422. is_null = true;
  423. }
  424. auto type = (SQLType)type_flags;
  425. VERIFY(!is_null || (type != SQLType::Tuple && type != SQLType::Array));
  426. setup(type);
  427. if (!is_null) {
  428. m_impl.visit([&](auto& impl) { impl.deserialize(serializer); });
  429. }
  430. }
  431. bool NullImpl::can_cast(Value const& value)
  432. {
  433. return value.is_null();
  434. }
  435. int NullImpl::compare(Value const& other)
  436. {
  437. return other.type() == SQLType::Null;
  438. }
  439. String TextImpl::to_string() const
  440. {
  441. return value();
  442. }
  443. Optional<int> TextImpl::to_int() const
  444. {
  445. if (!m_value.has_value())
  446. return {};
  447. return value().to_int();
  448. }
  449. Optional<double> TextImpl::to_double() const
  450. {
  451. if (!m_value.has_value())
  452. return {};
  453. char* end_ptr;
  454. double ret = strtod(value().characters(), &end_ptr);
  455. if (end_ptr == value().characters()) {
  456. return {};
  457. }
  458. return ret;
  459. }
  460. Optional<bool> TextImpl::to_bool() const
  461. {
  462. if (!m_value.has_value())
  463. return {};
  464. if (value().equals_ignoring_case("true"sv) || value().equals_ignoring_case("t"sv))
  465. return true;
  466. if (value().equals_ignoring_case("false"sv) || value().equals_ignoring_case("f"sv))
  467. return false;
  468. return {};
  469. }
  470. void TextImpl::assign(Value const& other_value)
  471. {
  472. if (other_value.type() == SQLType::Null) {
  473. m_value = {};
  474. } else {
  475. m_value = other_value.to_string();
  476. }
  477. }
  478. void TextImpl::assign_string(String const& string_value)
  479. {
  480. m_value = string_value;
  481. }
  482. void TextImpl::assign_int(int int_value)
  483. {
  484. m_value = String::number(int_value);
  485. }
  486. void TextImpl::assign_double(double double_value)
  487. {
  488. m_value = String::number(double_value);
  489. }
  490. void TextImpl::assign_bool(bool bool_value)
  491. {
  492. m_value = (bool_value) ? "true" : "false";
  493. }
  494. size_t TextImpl::length() const
  495. {
  496. return (is_null()) ? 0 : sizeof(u32) + value().length();
  497. }
  498. int TextImpl::compare(Value const& other) const
  499. {
  500. if (is_null())
  501. return -1;
  502. auto s1 = value();
  503. auto s2 = other.to_string();
  504. if (s1 == s2)
  505. return 0;
  506. return (s1 < s2) ? -1 : 1;
  507. }
  508. u32 TextImpl::hash() const
  509. {
  510. return value().hash();
  511. }
  512. String IntegerImpl::to_string() const
  513. {
  514. return String::formatted("{}", value());
  515. }
  516. Optional<int> IntegerImpl::to_int() const
  517. {
  518. return value();
  519. }
  520. Optional<double> IntegerImpl::to_double() const
  521. {
  522. return static_cast<double>(value());
  523. }
  524. Optional<bool> IntegerImpl::to_bool() const
  525. {
  526. return value() != 0;
  527. }
  528. void IntegerImpl::assign(Value const& other_value)
  529. {
  530. auto i = other_value.to_int();
  531. if (!i.has_value())
  532. m_value = {};
  533. else
  534. m_value = i.value();
  535. }
  536. void IntegerImpl::assign_string(String const& string_value)
  537. {
  538. auto i = string_value.to_int();
  539. if (!i.has_value())
  540. m_value = {};
  541. else
  542. m_value = i.value();
  543. }
  544. void IntegerImpl::assign_int(int int_value)
  545. {
  546. m_value = int_value;
  547. }
  548. void IntegerImpl::assign_double(double double_value)
  549. {
  550. m_value = static_cast<int>(round(double_value));
  551. }
  552. void IntegerImpl::assign_bool(bool bool_value)
  553. {
  554. m_value = (bool_value) ? 1 : 0;
  555. }
  556. bool IntegerImpl::can_cast(Value const& other_value)
  557. {
  558. return other_value.to_int().has_value();
  559. }
  560. int IntegerImpl::compare(Value const& other) const
  561. {
  562. auto casted = other.to_int();
  563. if (!casted.has_value())
  564. return 1;
  565. if (value() == casted.value())
  566. return 0;
  567. return value() < casted.value() ? -1 : 1;
  568. }
  569. u32 IntegerImpl::hash() const
  570. {
  571. return int_hash(value());
  572. }
  573. String FloatImpl::to_string() const
  574. {
  575. return String::formatted("{}", value());
  576. }
  577. Optional<int> FloatImpl::to_int() const
  578. {
  579. return static_cast<int>(round(value()));
  580. }
  581. Optional<bool> FloatImpl::to_bool() const
  582. {
  583. return fabs(value()) > NumericLimits<double>::epsilon();
  584. }
  585. Optional<double> FloatImpl::to_double() const
  586. {
  587. return value();
  588. }
  589. void FloatImpl::assign(Value const& other_value)
  590. {
  591. auto i = other_value.to_double();
  592. if (!i.has_value())
  593. m_value = {};
  594. else
  595. m_value = i.value();
  596. }
  597. void FloatImpl::assign_string(String const& string_value)
  598. {
  599. char* end_ptr;
  600. auto dbl = strtod(string_value.characters(), &end_ptr);
  601. if (end_ptr == string_value.characters())
  602. m_value = {};
  603. else
  604. m_value = dbl;
  605. }
  606. void FloatImpl::assign_int(int int_value)
  607. {
  608. m_value = int_value;
  609. }
  610. void FloatImpl::assign_double(double double_value)
  611. {
  612. m_value = double_value;
  613. }
  614. bool FloatImpl::can_cast(Value const& other_value)
  615. {
  616. return other_value.to_double().has_value();
  617. }
  618. int FloatImpl::compare(Value const& other) const
  619. {
  620. auto casted = other.to_double();
  621. if (!casted.has_value()) {
  622. return 1;
  623. }
  624. auto diff = value() - casted.value();
  625. if (fabs(diff) < NumericLimits<double>::epsilon())
  626. return 0;
  627. return diff < 0 ? -1 : 1;
  628. }
  629. String BooleanImpl::to_string() const
  630. {
  631. return (value()) ? "true" : "false";
  632. }
  633. Optional<int> BooleanImpl::to_int() const
  634. {
  635. return (value()) ? 1 : 0;
  636. }
  637. Optional<double> BooleanImpl::to_double()
  638. {
  639. return {};
  640. }
  641. Optional<bool> BooleanImpl::to_bool() const
  642. {
  643. return value();
  644. }
  645. void BooleanImpl::assign(Value const& other_value)
  646. {
  647. auto b = other_value.to_bool();
  648. if (!b.has_value())
  649. m_value = {};
  650. else
  651. m_value = b.value();
  652. }
  653. void BooleanImpl::assign_string(String const& string_value)
  654. {
  655. return assign(Value(string_value));
  656. }
  657. void BooleanImpl::assign_int(int int_value)
  658. {
  659. m_value = (int_value != 0);
  660. }
  661. void BooleanImpl::assign_double(double)
  662. {
  663. m_value = {};
  664. }
  665. void BooleanImpl::assign_bool(bool bool_value)
  666. {
  667. m_value = bool_value;
  668. }
  669. bool BooleanImpl::can_cast(Value const& other_value)
  670. {
  671. return other_value.to_bool().has_value();
  672. }
  673. int BooleanImpl::compare(Value const& other) const
  674. {
  675. auto casted = other.to_bool();
  676. if (!casted.has_value()) {
  677. return 1;
  678. }
  679. return value() ^ casted.value(); // xor - zero if both true or both false, 1 otherwise.
  680. }
  681. u32 BooleanImpl::hash() const
  682. {
  683. return int_hash(value());
  684. }
  685. void ContainerValueImpl::assign_vector(Vector<Value> const& vector_values)
  686. {
  687. if (!validate_before_assignment(vector_values)) {
  688. m_value = {};
  689. return;
  690. }
  691. m_value = Vector<BaseTypeImpl>();
  692. for (auto& value : vector_values) {
  693. if (!append(value)) {
  694. m_value = {};
  695. return;
  696. }
  697. }
  698. if (!validate_after_assignment())
  699. m_value = {};
  700. }
  701. bool ContainerValueImpl::to_vector(Vector<Value>& vector) const
  702. {
  703. vector.clear();
  704. for (auto& value : value()) {
  705. vector.empend(Value(value));
  706. }
  707. return true;
  708. }
  709. Vector<String> ContainerValueImpl::to_string_vector() const
  710. {
  711. Vector<String> ret;
  712. for (auto& value : value()) {
  713. ret.append(Value(value).to_string());
  714. }
  715. return ret;
  716. }
  717. String ContainerValueImpl::to_string() const
  718. {
  719. StringBuilder builder;
  720. builder.append('(');
  721. StringBuilder joined;
  722. joined.join(", "sv, to_string_vector());
  723. builder.append(joined.string_view());
  724. builder.append(')');
  725. return builder.build();
  726. }
  727. u32 ContainerValueImpl::hash() const
  728. {
  729. u32 ret = 0u;
  730. for (auto& value : value()) {
  731. Value v(value);
  732. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  733. if (!ret)
  734. ret = v.hash();
  735. else
  736. ret = int_hash((ret * 209) ^ (v.hash() * 413));
  737. }
  738. return ret;
  739. }
  740. bool ContainerValueImpl::append(Value const& value)
  741. {
  742. if (value.type() == SQLType::Tuple || value.type() == SQLType::Array)
  743. return false;
  744. return append(value.downcast_to_basetype());
  745. }
  746. bool ContainerValueImpl::append(BaseTypeImpl const& impl)
  747. {
  748. if (!validate(impl))
  749. return false;
  750. m_value.value().empend(impl);
  751. return true;
  752. }
  753. void ContainerValueImpl::serialize_values(Serializer& serializer) const
  754. {
  755. serializer.serialize((u32)size());
  756. for (auto& impl : value()) {
  757. serializer.serialize<Value>(Value(impl));
  758. }
  759. }
  760. void ContainerValueImpl::deserialize_values(Serializer& serializer)
  761. {
  762. auto sz = serializer.deserialize<u32>();
  763. m_value = Vector<BaseTypeImpl>();
  764. for (auto ix = 0u; ix < sz; ix++) {
  765. append(serializer.deserialize<Value>());
  766. }
  767. }
  768. size_t ContainerValueImpl::length() const
  769. {
  770. size_t len = sizeof(u32);
  771. for (auto& impl : value()) {
  772. len += Value(impl).length();
  773. }
  774. return len;
  775. }
  776. void TupleImpl::assign(Value const& other)
  777. {
  778. if (other.type() != SQLType::Tuple) {
  779. m_value = {};
  780. return;
  781. }
  782. auto& other_impl = other.get_impl<TupleImpl>({});
  783. auto other_descriptor = other_impl.m_descriptor;
  784. if (m_descriptor && other_descriptor && m_descriptor->compare_ignoring_names(*other_descriptor)) {
  785. m_value = {};
  786. return;
  787. }
  788. assign_vector(other.to_vector().value());
  789. }
  790. size_t TupleImpl::length() const
  791. {
  792. return m_descriptor->length() + ContainerValueImpl::length();
  793. }
  794. bool TupleImpl::can_cast(Value const& other_value) const
  795. {
  796. if (other_value.type() != SQLType::Tuple)
  797. return false;
  798. return (m_descriptor == other_value.get_impl<TupleImpl>({}).m_descriptor);
  799. }
  800. int TupleImpl::compare(Value const& other) const
  801. {
  802. if (other.type() != SQLType::Tuple) {
  803. if (size() == 1)
  804. return Value(value().at(0)).compare(other);
  805. return 1;
  806. }
  807. auto& other_impl = other.get_impl<TupleImpl>({});
  808. if (m_descriptor && other_impl.m_descriptor && m_descriptor->compare_ignoring_names(*other_impl.m_descriptor))
  809. return 1;
  810. auto other_values = other_impl.value();
  811. if (size() != other_impl.size())
  812. return (int)value().size() - (int)other_impl.size();
  813. for (auto ix = 0u; ix < value().size(); ix++) {
  814. auto ret = Value(value()[ix]).compare(Value(other_impl.value()[ix]));
  815. if (ret != 0) {
  816. if (m_descriptor && (ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  817. ret = -ret;
  818. return ret;
  819. }
  820. }
  821. return 0;
  822. }
  823. Optional<bool> TupleImpl::to_bool() const
  824. {
  825. for (auto const& value : value()) {
  826. auto as_bool = Value(value).to_bool();
  827. if (!as_bool.has_value())
  828. return {};
  829. if (!as_bool.value())
  830. return false;
  831. }
  832. return true;
  833. }
  834. void TupleImpl::serialize(Serializer& serializer) const
  835. {
  836. serializer.serialize<TupleDescriptor>(*m_descriptor);
  837. serialize_values(serializer);
  838. }
  839. void TupleImpl::deserialize(Serializer& serializer)
  840. {
  841. m_descriptor = serializer.adopt_and_deserialize<TupleDescriptor>();
  842. deserialize_values(serializer);
  843. }
  844. void TupleImpl::infer_descriptor()
  845. {
  846. if (!m_descriptor) {
  847. m_descriptor = adopt_ref(*new TupleDescriptor);
  848. m_descriptor_inferred = true;
  849. }
  850. }
  851. void TupleImpl::extend_descriptor(Value const& value)
  852. {
  853. VERIFY(m_descriptor_inferred);
  854. m_descriptor->empend("", "", "", value.type(), Order::Ascending);
  855. }
  856. bool TupleImpl::validate_before_assignment(Vector<Value> const& values)
  857. {
  858. if (m_descriptor_inferred)
  859. m_descriptor = nullptr;
  860. if (!m_descriptor) {
  861. infer_descriptor();
  862. if (values.size() > m_descriptor->size()) {
  863. for (auto ix = m_descriptor->size(); ix < values.size(); ix++) {
  864. extend_descriptor(values[ix]);
  865. }
  866. }
  867. }
  868. return true;
  869. }
  870. bool TupleImpl::validate(BaseTypeImpl const& value)
  871. {
  872. if (!m_descriptor)
  873. infer_descriptor();
  874. if (m_descriptor_inferred && (this->value().size() == m_descriptor->size()))
  875. extend_descriptor(Value(value));
  876. if (m_descriptor->size() == this->value().size())
  877. return false;
  878. auto required_type = (*m_descriptor)[this->value().size()].type;
  879. return Value(value).type() == required_type;
  880. }
  881. bool TupleImpl::validate_after_assignment()
  882. {
  883. for (auto ix = value().size(); ix < m_descriptor->size(); ++ix) {
  884. auto required_type = (*m_descriptor)[ix].type;
  885. append(Value(required_type));
  886. }
  887. return true;
  888. }
  889. void ArrayImpl::assign(Value const& other)
  890. {
  891. if (other.type() != SQLType::Array) {
  892. m_value = {};
  893. return;
  894. }
  895. auto& other_impl = other.get_impl<ArrayImpl>({});
  896. if (m_max_size != other_impl.m_max_size || m_element_type != other_impl.m_element_type) {
  897. m_value = {};
  898. return;
  899. }
  900. assign_vector(other.to_vector().value());
  901. }
  902. size_t ArrayImpl::length() const
  903. {
  904. return sizeof(u8) + sizeof(u32) + ContainerValueImpl::length();
  905. }
  906. bool ArrayImpl::can_cast(Value const& other_value) const
  907. {
  908. if (other_value.type() != SQLType::Array)
  909. return false;
  910. auto& other_impl = other_value.get_impl<ArrayImpl>({});
  911. return (m_max_size != other_impl.m_max_size || m_element_type != other_impl.m_element_type);
  912. }
  913. int ArrayImpl::compare(Value const& other) const
  914. {
  915. if (other.type() != SQLType::Array)
  916. return 1;
  917. auto other_impl = other.get_impl<ArrayImpl>({});
  918. if (other_impl.m_element_type != m_element_type)
  919. return 1;
  920. if (other_impl.m_max_size.has_value() && m_max_size.has_value() && other_impl.m_max_size != m_max_size)
  921. return (int)m_max_size.value() - (int)other_impl.m_max_size.value();
  922. if (size() != other_impl.size())
  923. return (int)size() - (int)other_impl.size();
  924. for (auto ix = 0u; ix < size(); ix++) {
  925. auto ret = Value(value()[ix]).compare(Value(other_impl.value()[ix]));
  926. if (ret != 0) {
  927. return ret;
  928. }
  929. }
  930. return 0;
  931. }
  932. void ArrayImpl::serialize(Serializer& serializer) const
  933. {
  934. serializer.serialize((u8)m_element_type);
  935. if (m_max_size.has_value())
  936. serializer.serialize((u32)m_max_size.value());
  937. else
  938. serializer.serialize((u32)0);
  939. serialize_values(serializer);
  940. }
  941. void ArrayImpl::deserialize(Serializer& serializer)
  942. {
  943. m_element_type = (SQLType)serializer.deserialize<u8>();
  944. auto max_sz = serializer.deserialize<u32>();
  945. if (max_sz)
  946. m_max_size = max_sz;
  947. else
  948. m_max_size = {};
  949. deserialize_values(serializer);
  950. }
  951. bool ArrayImpl::validate(BaseTypeImpl const& impl)
  952. {
  953. if (m_max_size.has_value() && (size() >= m_max_size.value()))
  954. return false;
  955. return Value(impl).type() == m_element_type;
  956. }
  957. }