Value.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. return (diff < NumericLimits<double>::epsilon()) ? 0 : ((diff > 0) ? 1 : -1);
  667. }
  668. String BooleanImpl::to_string() const
  669. {
  670. return (value()) ? "true" : "false";
  671. }
  672. Optional<int> BooleanImpl::to_int() const
  673. {
  674. return (value()) ? 1 : 0;
  675. }
  676. Optional<double> BooleanImpl::to_double()
  677. {
  678. return {};
  679. }
  680. Optional<bool> BooleanImpl::to_bool() const
  681. {
  682. return value();
  683. }
  684. void BooleanImpl::assign(Value const& other_value)
  685. {
  686. auto b = other_value.to_bool();
  687. if (!b.has_value())
  688. m_value = {};
  689. else
  690. m_value = b.value();
  691. }
  692. void BooleanImpl::assign_string(String const& string_value)
  693. {
  694. return assign(Value(string_value));
  695. }
  696. void BooleanImpl::assign_int(int int_value)
  697. {
  698. m_value = (int_value != 0);
  699. }
  700. void BooleanImpl::assign_double(double)
  701. {
  702. m_value = {};
  703. }
  704. void BooleanImpl::assign_bool(bool bool_value)
  705. {
  706. m_value = bool_value;
  707. }
  708. bool BooleanImpl::can_cast(Value const& other_value)
  709. {
  710. return other_value.to_bool().has_value();
  711. }
  712. int BooleanImpl::compare(Value const& other) const
  713. {
  714. auto casted = other.to_bool();
  715. if (!casted.has_value()) {
  716. return 1;
  717. }
  718. return value() ^ casted.value(); // xor - zero if both true or both false, 1 otherwise.
  719. }
  720. u32 BooleanImpl::hash() const
  721. {
  722. return int_hash(value());
  723. }
  724. void ContainerValueImpl::assign_vector(Vector<Value> const& vector_values)
  725. {
  726. if (!validate_before_assignment(vector_values)) {
  727. m_value = {};
  728. return;
  729. }
  730. m_value = Vector<BaseTypeImpl>();
  731. for (auto& value : vector_values) {
  732. if (!append(value)) {
  733. m_value = {};
  734. return;
  735. }
  736. }
  737. if (!validate_after_assignment())
  738. m_value = {};
  739. }
  740. bool ContainerValueImpl::to_vector(Vector<Value>& vector) const
  741. {
  742. vector.clear();
  743. for (auto& value : value()) {
  744. vector.empend(Value(value));
  745. }
  746. return true;
  747. }
  748. Vector<String> ContainerValueImpl::to_string_vector() const
  749. {
  750. Vector<String> ret;
  751. for (auto& value : value()) {
  752. ret.append(Value(value).to_string());
  753. }
  754. return ret;
  755. }
  756. String ContainerValueImpl::to_string() const
  757. {
  758. StringBuilder builder;
  759. builder.append("(");
  760. StringBuilder joined;
  761. joined.join(", ", to_string_vector());
  762. builder.append(joined.string_view());
  763. builder.append(")");
  764. return builder.build();
  765. }
  766. u32 ContainerValueImpl::hash() const
  767. {
  768. u32 ret = 0u;
  769. for (auto& value : value()) {
  770. Value v(value);
  771. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  772. if (!ret)
  773. ret = v.hash();
  774. else
  775. ret = int_hash((ret * 209) ^ (v.hash() * 413));
  776. }
  777. return ret;
  778. }
  779. bool ContainerValueImpl::append(Value const& value)
  780. {
  781. if (value.type() == SQLType::Tuple || value.type() == SQLType::Array)
  782. return false;
  783. return append(value.downcast_to_basetype());
  784. }
  785. bool ContainerValueImpl::append(BaseTypeImpl const& impl)
  786. {
  787. if (!validate(impl))
  788. return false;
  789. m_value.value().empend(impl);
  790. return true;
  791. }
  792. void ContainerValueImpl::serialize_values(Serializer& serializer) const
  793. {
  794. serializer.serialize((u32)size());
  795. for (auto& impl : value()) {
  796. serializer.serialize<Value>(Value(impl));
  797. }
  798. }
  799. void ContainerValueImpl::deserialize_values(Serializer& serializer)
  800. {
  801. auto sz = serializer.deserialize<u32>();
  802. m_value = Vector<BaseTypeImpl>();
  803. for (auto ix = 0u; ix < sz; ix++) {
  804. append(serializer.deserialize<Value>());
  805. }
  806. }
  807. size_t ContainerValueImpl::length() const
  808. {
  809. size_t len = sizeof(u32);
  810. for (auto& impl : value()) {
  811. len += Value(impl).length();
  812. }
  813. return len;
  814. }
  815. void TupleImpl::assign(Value const& other)
  816. {
  817. if (other.type() != SQLType::Tuple) {
  818. m_value = {};
  819. return;
  820. }
  821. auto& other_impl = other.get_impl<TupleImpl>({});
  822. auto other_descriptor = other_impl.m_descriptor;
  823. if (m_descriptor && other_descriptor && m_descriptor->compare_ignoring_names(*other_descriptor)) {
  824. m_value = {};
  825. return;
  826. }
  827. assign_vector(other.to_vector().value());
  828. }
  829. size_t TupleImpl::length() const
  830. {
  831. return m_descriptor->length() + ContainerValueImpl::length();
  832. }
  833. bool TupleImpl::can_cast(Value const& other_value) const
  834. {
  835. if (other_value.type() != SQLType::Tuple)
  836. return false;
  837. return (m_descriptor == other_value.get_impl<TupleImpl>({}).m_descriptor);
  838. }
  839. int TupleImpl::compare(Value const& other) const
  840. {
  841. if (other.type() != SQLType::Tuple)
  842. return 1;
  843. auto& other_impl = other.get_impl<TupleImpl>({});
  844. if (m_descriptor && other_impl.m_descriptor && m_descriptor->compare_ignoring_names(*other_impl.m_descriptor))
  845. return 1;
  846. auto other_values = other_impl.value();
  847. if (size() != other_impl.size())
  848. return (int)value().size() - (int)other_impl.size();
  849. for (auto ix = 0u; ix < value().size(); ix++) {
  850. auto ret = Value(value()[ix]).compare(Value(other_impl.value()[ix]));
  851. if (ret != 0) {
  852. if (m_descriptor && (ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  853. ret = -ret;
  854. return ret;
  855. }
  856. }
  857. return 0;
  858. }
  859. void TupleImpl::serialize(Serializer& serializer) const
  860. {
  861. serializer.serialize<TupleDescriptor>(*m_descriptor);
  862. serialize_values(serializer);
  863. }
  864. void TupleImpl::deserialize(Serializer& serializer)
  865. {
  866. m_descriptor = serializer.adopt_and_deserialize<TupleDescriptor>();
  867. deserialize_values(serializer);
  868. }
  869. void TupleImpl::infer_descriptor()
  870. {
  871. if (!m_descriptor) {
  872. m_descriptor = adopt_ref(*new TupleDescriptor);
  873. m_descriptor_inferred = true;
  874. }
  875. }
  876. void TupleImpl::extend_descriptor(Value const& value)
  877. {
  878. VERIFY(m_descriptor_inferred);
  879. m_descriptor->empend("", "", "", value.type(), Order::Ascending);
  880. }
  881. bool TupleImpl::validate_before_assignment(Vector<Value> const& values)
  882. {
  883. if (m_descriptor_inferred)
  884. m_descriptor = nullptr;
  885. if (!m_descriptor) {
  886. infer_descriptor();
  887. if (values.size() > m_descriptor->size()) {
  888. for (auto ix = m_descriptor->size(); ix < values.size(); ix++) {
  889. extend_descriptor(values[ix]);
  890. }
  891. }
  892. }
  893. return true;
  894. }
  895. bool TupleImpl::validate(BaseTypeImpl const& value)
  896. {
  897. if (!m_descriptor)
  898. infer_descriptor();
  899. if (m_descriptor_inferred && (this->value().size() == m_descriptor->size()))
  900. extend_descriptor(Value(value));
  901. if (m_descriptor->size() == this->value().size())
  902. return false;
  903. auto required_type = (*m_descriptor)[this->value().size()].type;
  904. return Value(value).type() == required_type;
  905. }
  906. bool TupleImpl::validate_after_assignment()
  907. {
  908. for (auto ix = value().size(); ix < m_descriptor->size(); ++ix) {
  909. auto required_type = (*m_descriptor)[ix].type;
  910. append(Value(required_type));
  911. }
  912. return true;
  913. }
  914. void ArrayImpl::assign(Value const& other)
  915. {
  916. if (other.type() != SQLType::Array) {
  917. m_value = {};
  918. return;
  919. }
  920. auto& other_impl = other.get_impl<ArrayImpl>({});
  921. if (m_max_size != other_impl.m_max_size || m_element_type != other_impl.m_element_type) {
  922. m_value = {};
  923. return;
  924. }
  925. assign_vector(other.to_vector().value());
  926. }
  927. size_t ArrayImpl::length() const
  928. {
  929. return sizeof(u8) + sizeof(u32) + ContainerValueImpl::length();
  930. }
  931. bool ArrayImpl::can_cast(Value const& other_value) const
  932. {
  933. if (other_value.type() != SQLType::Array)
  934. return false;
  935. auto& other_impl = other_value.get_impl<ArrayImpl>({});
  936. return (m_max_size != other_impl.m_max_size || m_element_type != other_impl.m_element_type);
  937. }
  938. int ArrayImpl::compare(Value const& other) const
  939. {
  940. if (other.type() != SQLType::Array)
  941. return 1;
  942. auto other_impl = other.get_impl<ArrayImpl>({});
  943. if (other_impl.m_element_type != m_element_type)
  944. return 1;
  945. if (other_impl.m_max_size.has_value() && m_max_size.has_value() && other_impl.m_max_size != m_max_size)
  946. return (int)m_max_size.value() - (int)other_impl.m_max_size.value();
  947. if (size() != other_impl.size())
  948. return (int)size() - (int)other_impl.size();
  949. for (auto ix = 0u; ix < size(); ix++) {
  950. auto ret = Value(value()[ix]).compare(Value(other_impl.value()[ix]));
  951. if (ret != 0) {
  952. return ret;
  953. }
  954. }
  955. return 0;
  956. }
  957. void ArrayImpl::serialize(Serializer& serializer) const
  958. {
  959. serializer.serialize((u8)m_element_type);
  960. if (m_max_size.has_value())
  961. serializer.serialize((u32)m_max_size.value());
  962. else
  963. serializer.serialize((u32)0);
  964. serialize_values(serializer);
  965. }
  966. void ArrayImpl::deserialize(Serializer& serializer)
  967. {
  968. m_element_type = (SQLType)serializer.deserialize<u8>();
  969. auto max_sz = serializer.deserialize<u32>();
  970. if (max_sz)
  971. m_max_size = max_sz;
  972. else
  973. m_max_size = {};
  974. deserialize_values(serializer);
  975. }
  976. bool ArrayImpl::validate(BaseTypeImpl const& impl)
  977. {
  978. if (m_max_size.has_value() && (size() >= m_max_size.value()))
  979. return false;
  980. return Value(impl).type() == m_element_type;
  981. }
  982. }