Value.cpp 27 KB

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