Value.cpp 26 KB

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