Value.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSQL/Serialize.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(double dbl)
  75. : Value(SQLType::Float)
  76. {
  77. assign(dbl);
  78. }
  79. Value::Value(bool boolean)
  80. : Value(SQLType::Boolean)
  81. {
  82. assign(boolean);
  83. }
  84. Value Value::create_tuple(NonnullRefPtr<TupleDescriptor> const& tuple_descriptor)
  85. {
  86. return Value(Value::SetImplementationSingleton, TupleImpl(tuple_descriptor));
  87. }
  88. Value Value::create_array(SQLType element_type, Optional<size_t> const& max_size)
  89. {
  90. return Value(Value::SetImplementationSingleton, ArrayImpl(element_type, max_size));
  91. }
  92. Value const& Value::null()
  93. {
  94. static Value s_null(SQLType::Null);
  95. return s_null;
  96. }
  97. bool Value::is_null() const
  98. {
  99. return m_impl.visit([&](auto& impl) { return impl.is_null(); });
  100. }
  101. SQLType Value::type() const
  102. {
  103. return m_impl.visit([&](auto& impl) { return impl.type(); });
  104. }
  105. String Value::type_name() const
  106. {
  107. return m_impl.visit([&](auto& impl) { return impl.type_name(); });
  108. }
  109. BaseTypeImpl Value::downcast_to_basetype() const
  110. {
  111. return m_impl.downcast<NullImpl, TextImpl, IntegerImpl, FloatImpl, BooleanImpl>();
  112. }
  113. String Value::to_string() const
  114. {
  115. if (is_null())
  116. return "(null)";
  117. return m_impl.visit([&](auto& impl) { return impl.to_string(); });
  118. }
  119. Optional<int> Value::to_int() const
  120. {
  121. if (is_null())
  122. return {};
  123. return m_impl.visit([&](auto& impl) { return impl.to_int(); });
  124. }
  125. Optional<u32> Value::to_u32() const
  126. {
  127. if (is_null())
  128. return {};
  129. auto ret = to_int();
  130. if (ret.has_value())
  131. return static_cast<u32>(ret.value());
  132. return {};
  133. }
  134. Optional<double> Value::to_double() const
  135. {
  136. if (is_null())
  137. return {};
  138. return m_impl.visit([&](auto& impl) { return impl.to_double(); });
  139. }
  140. Optional<bool> Value::to_bool() const
  141. {
  142. if (is_null())
  143. return {};
  144. return m_impl.visit([&](auto& impl) { return impl.to_bool(); });
  145. }
  146. Optional<Vector<Value>> Value::to_vector() const
  147. {
  148. if (is_null())
  149. return {};
  150. Vector<Value> vector;
  151. if (m_impl.visit([&](auto& impl) { return impl.to_vector(vector); }))
  152. return vector;
  153. else
  154. return {};
  155. }
  156. Value::operator String() const
  157. {
  158. return to_string();
  159. }
  160. Value::operator int() const
  161. {
  162. auto i = to_int();
  163. VERIFY(i.has_value());
  164. return i.value();
  165. }
  166. Value::operator u32() const
  167. {
  168. auto i = to_u32();
  169. VERIFY(i.has_value());
  170. return i.value();
  171. }
  172. Value::operator double() const
  173. {
  174. auto d = to_double();
  175. VERIFY(d.has_value());
  176. return d.value();
  177. }
  178. Value::operator bool() const
  179. {
  180. auto b = to_bool();
  181. VERIFY(b.has_value());
  182. return b.value();
  183. }
  184. void Value::assign(Value const& other_value)
  185. {
  186. m_impl.visit([&](auto& impl) { impl.assign(other_value); });
  187. }
  188. void Value::assign(String const& string_value)
  189. {
  190. m_impl.visit([&](auto& impl) { impl.assign_string(string_value); });
  191. }
  192. void Value::assign(int const& int_value)
  193. {
  194. m_impl.visit([&](auto& impl) { impl.assign_int(int_value); });
  195. }
  196. void Value::assign(double const& double_value)
  197. {
  198. m_impl.visit([&](auto& impl) { impl.assign_double(double_value); });
  199. }
  200. void Value::assign(bool const& bool_value)
  201. {
  202. m_impl.visit([&](auto& impl) { impl.assign_bool(bool_value); });
  203. }
  204. void Value::assign(Vector<Value> const& values)
  205. {
  206. m_impl.visit([&](auto& impl) { impl.assign_vector(values); });
  207. }
  208. Value& Value::operator=(Value const& other)
  209. {
  210. if (this != &other) {
  211. if (other.is_null()) {
  212. assign(null());
  213. } else {
  214. VERIFY(can_cast(other));
  215. assign(other);
  216. }
  217. }
  218. return (*this);
  219. }
  220. Value& Value::operator=(String const& value)
  221. {
  222. assign(value);
  223. return (*this);
  224. }
  225. Value& Value::operator=(char const* value)
  226. {
  227. assign(String(value));
  228. return (*this);
  229. }
  230. Value& Value::operator=(int value)
  231. {
  232. assign(value);
  233. return (*this);
  234. }
  235. Value& Value::operator=(u32 value)
  236. {
  237. assign(static_cast<int>(value));
  238. return (*this);
  239. }
  240. Value& Value::operator=(double value)
  241. {
  242. assign(value);
  243. return (*this);
  244. }
  245. Value& Value::operator=(bool value)
  246. {
  247. assign(value);
  248. return (*this);
  249. }
  250. Value& Value::operator=(Vector<Value> const& vector)
  251. {
  252. assign(vector);
  253. return (*this);
  254. }
  255. size_t Value::length() const
  256. {
  257. return m_impl.visit([&](auto& impl) { return impl.length(); });
  258. }
  259. u32 Value::hash() const
  260. {
  261. return (is_null()) ? 0u : m_impl.visit([&](auto& impl) { return impl.hash(); });
  262. }
  263. bool Value::can_cast(Value const& other_value) const
  264. {
  265. if (type() == other_value.type())
  266. return true;
  267. return m_impl.visit([&](auto& impl) { return impl.can_cast(other_value); });
  268. }
  269. int Value::compare(Value const& other) const
  270. {
  271. if (is_null())
  272. return -1;
  273. if (other.is_null())
  274. return 1;
  275. return m_impl.visit([&](auto& impl) { return impl.compare(other); });
  276. }
  277. bool Value::operator==(Value const& other) const
  278. {
  279. return compare(other) == 0;
  280. }
  281. bool Value::operator==(String const& string_value) const
  282. {
  283. return to_string() == string_value;
  284. }
  285. bool Value::operator==(int int_value) const
  286. {
  287. auto i = to_int();
  288. if (!i.has_value())
  289. return false;
  290. return i.value() == int_value;
  291. }
  292. bool Value::operator==(double double_value) const
  293. {
  294. auto d = to_double();
  295. if (!d.has_value())
  296. return false;
  297. return d.value() == double_value;
  298. }
  299. bool Value::operator!=(Value const& other) const
  300. {
  301. return compare(other) != 0;
  302. }
  303. bool Value::operator<(Value const& other) const
  304. {
  305. return compare(other) < 0;
  306. }
  307. bool Value::operator<=(Value const& other) const
  308. {
  309. return compare(other) <= 0;
  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. void Value::serialize_to(ByteBuffer& buffer) const
  320. {
  321. u8 type_flags = (u8)type();
  322. if (is_null())
  323. type_flags |= (u8)SQLType::Null;
  324. SQL::serialize_to(buffer, type_flags);
  325. if (!is_null())
  326. m_impl.visit([&](auto& impl) { impl.serialize(buffer); });
  327. }
  328. void Value::deserialize(ByteBuffer& buffer, size_t& offset_at)
  329. {
  330. m_impl.visit([&](auto& impl) { impl.deserialize(buffer, offset_at); });
  331. }
  332. Value Value::deserialize_from(ByteBuffer& buffer, size_t& at_offset)
  333. {
  334. u8 type_flags;
  335. SQL::deserialize_from(buffer, at_offset, type_flags);
  336. bool is_null = false;
  337. if ((type_flags & (u8)SQLType::Null) && (type_flags != (u8)SQLType::Null)) {
  338. type_flags &= ~((u8)SQLType::Null);
  339. is_null = true;
  340. }
  341. auto type = (SQLType)type_flags;
  342. VERIFY(!is_null || (type != SQLType::Tuple && type != SQLType::Array));
  343. Value ret(type);
  344. if (!is_null) {
  345. ret.deserialize(buffer, at_offset);
  346. }
  347. return ret;
  348. }
  349. // -----------------------------------------------------------------
  350. bool NullImpl::can_cast(Value const& value)
  351. {
  352. return value.is_null();
  353. }
  354. int NullImpl::compare(Value const& other)
  355. {
  356. return other.type() == SQLType::Null;
  357. }
  358. // -----------------------------------------------------------------
  359. String TextImpl::to_string() const
  360. {
  361. return value();
  362. }
  363. Optional<int> TextImpl::to_int() const
  364. {
  365. if (!m_value.has_value())
  366. return {};
  367. return value().to_int();
  368. }
  369. Optional<double> TextImpl::to_double() const
  370. {
  371. if (!m_value.has_value())
  372. return {};
  373. char* end_ptr;
  374. double ret = strtod(value().characters(), &end_ptr);
  375. if (end_ptr == value().characters()) {
  376. return {};
  377. }
  378. return ret;
  379. }
  380. Optional<bool> TextImpl::to_bool() const
  381. {
  382. if (!m_value.has_value())
  383. return {};
  384. if (value().equals_ignoring_case("true") || value().equals_ignoring_case("t"))
  385. return true;
  386. if (value().equals_ignoring_case("false") || value().equals_ignoring_case("f"))
  387. return false;
  388. return {};
  389. }
  390. void TextImpl::assign(Value const& other_value)
  391. {
  392. if (other_value.type() == SQLType::Null) {
  393. m_value = {};
  394. } else {
  395. m_value = other_value.to_string();
  396. }
  397. }
  398. void TextImpl::assign_string(String const& string_value)
  399. {
  400. m_value = string_value;
  401. }
  402. void TextImpl::assign_int(int int_value)
  403. {
  404. m_value = String::number(int_value);
  405. }
  406. void TextImpl::assign_double(double double_value)
  407. {
  408. m_value = String::number(double_value);
  409. }
  410. void TextImpl::assign_bool(bool bool_value)
  411. {
  412. m_value = (bool_value) ? "true" : "false";
  413. }
  414. size_t TextImpl::length() const
  415. {
  416. return (is_null()) ? 0 : sizeof(u32) + min(value().length(), 64) + 1;
  417. }
  418. int TextImpl::compare(Value const& other) const
  419. {
  420. if (is_null())
  421. return -1;
  422. auto s1 = value();
  423. auto s2 = other.to_string();
  424. if (s1 == s2)
  425. return 0;
  426. return (s1 < s2) ? -1 : 1;
  427. }
  428. u32 TextImpl::hash() const
  429. {
  430. return value().hash();
  431. }
  432. String IntegerImpl::to_string() const
  433. {
  434. return String::formatted("{}", value());
  435. }
  436. Optional<int> IntegerImpl::to_int() const
  437. {
  438. return value();
  439. }
  440. Optional<double> IntegerImpl::to_double() const
  441. {
  442. return static_cast<double>(value());
  443. }
  444. Optional<bool> IntegerImpl::to_bool() const
  445. {
  446. return value() != 0;
  447. }
  448. void IntegerImpl::assign(Value const& other_value)
  449. {
  450. auto i = other_value.to_int();
  451. if (!i.has_value())
  452. m_value = {};
  453. else
  454. m_value = i.value();
  455. }
  456. void IntegerImpl::assign_string(String const& string_value)
  457. {
  458. auto i = string_value.to_int();
  459. if (!i.has_value())
  460. m_value = {};
  461. else
  462. m_value = i.value();
  463. }
  464. void IntegerImpl::assign_int(int int_value)
  465. {
  466. m_value = int_value;
  467. }
  468. void IntegerImpl::assign_double(double double_value)
  469. {
  470. m_value = static_cast<int>(round(double_value));
  471. }
  472. void IntegerImpl::assign_bool(bool bool_value)
  473. {
  474. m_value = (bool_value) ? 1 : 0;
  475. }
  476. bool IntegerImpl::can_cast(Value const& other_value)
  477. {
  478. return other_value.to_int().has_value();
  479. }
  480. int IntegerImpl::compare(Value const& other) const
  481. {
  482. auto casted = other.to_int();
  483. if (!casted.has_value()) {
  484. return 1;
  485. }
  486. return value() - casted.value();
  487. }
  488. u32 IntegerImpl::hash() const
  489. {
  490. return int_hash(value());
  491. }
  492. String FloatImpl::to_string() const
  493. {
  494. return String::formatted("{}", value());
  495. }
  496. Optional<int> FloatImpl::to_int() const
  497. {
  498. return static_cast<int>(round(value()));
  499. }
  500. Optional<double> FloatImpl::to_double() const
  501. {
  502. return value();
  503. }
  504. void FloatImpl::assign(Value const& other_value)
  505. {
  506. auto i = other_value.to_double();
  507. if (!i.has_value())
  508. m_value = {};
  509. else
  510. m_value = i.value();
  511. }
  512. void FloatImpl::assign_string(String const& string_value)
  513. {
  514. char* end_ptr;
  515. auto dbl = strtod(string_value.characters(), &end_ptr);
  516. if (end_ptr == string_value.characters())
  517. m_value = {};
  518. else
  519. m_value = dbl;
  520. }
  521. void FloatImpl::assign_int(int int_value)
  522. {
  523. m_value = int_value;
  524. }
  525. void FloatImpl::assign_double(double double_value)
  526. {
  527. m_value = double_value;
  528. }
  529. bool FloatImpl::can_cast(Value const& other_value)
  530. {
  531. return other_value.to_double().has_value();
  532. }
  533. int FloatImpl::compare(Value const& other) const
  534. {
  535. auto casted = other.to_double();
  536. if (!casted.has_value()) {
  537. return 1;
  538. }
  539. auto diff = value() - casted.value();
  540. return (diff < NumericLimits<double>::epsilon()) ? 0 : ((diff > 0) ? 1 : -1);
  541. }
  542. String BooleanImpl::to_string() const
  543. {
  544. return (value()) ? "true" : "false";
  545. }
  546. Optional<int> BooleanImpl::to_int() const
  547. {
  548. return (value()) ? 1 : 0;
  549. }
  550. Optional<double> BooleanImpl::to_double()
  551. {
  552. return {};
  553. }
  554. Optional<bool> BooleanImpl::to_bool() const
  555. {
  556. return value();
  557. }
  558. void BooleanImpl::assign(Value const& other_value)
  559. {
  560. auto b = other_value.to_bool();
  561. if (!b.has_value())
  562. m_value = {};
  563. else
  564. m_value = b.value();
  565. }
  566. void BooleanImpl::assign_string(String const& string_value)
  567. {
  568. return assign(Value(string_value));
  569. }
  570. void BooleanImpl::assign_int(int int_value)
  571. {
  572. m_value = (int_value != 0);
  573. }
  574. void BooleanImpl::assign_double(double)
  575. {
  576. m_value = {};
  577. }
  578. void BooleanImpl::assign_bool(bool bool_value)
  579. {
  580. m_value = bool_value;
  581. }
  582. bool BooleanImpl::can_cast(Value const& other_value)
  583. {
  584. return other_value.to_bool().has_value();
  585. }
  586. int BooleanImpl::compare(Value const& other) const
  587. {
  588. auto casted = other.to_bool();
  589. if (!casted.has_value()) {
  590. return 1;
  591. }
  592. return value() ^ casted.value(); // xor - zero if both true or both false, 1 otherwise.
  593. }
  594. u32 BooleanImpl::hash() const
  595. {
  596. return int_hash(value());
  597. }
  598. void ContainerValueImpl::assign_vector(Vector<Value> const& vector_values)
  599. {
  600. if (!validate_before_assignment(vector_values)) {
  601. m_value = {};
  602. return;
  603. }
  604. m_value = Vector<BaseTypeImpl>();
  605. for (auto& value : vector_values) {
  606. if (!append(value)) {
  607. m_value = {};
  608. return;
  609. }
  610. }
  611. if (!validate_after_assignment())
  612. m_value = {};
  613. }
  614. bool ContainerValueImpl::to_vector(Vector<Value>& vector) const
  615. {
  616. vector.clear();
  617. for (auto& value : value()) {
  618. vector.empend(Value(value));
  619. }
  620. return true;
  621. }
  622. Vector<String> ContainerValueImpl::to_string_vector() const
  623. {
  624. Vector<String> ret;
  625. for (auto& value : value()) {
  626. ret.append(Value(value).to_string());
  627. }
  628. return ret;
  629. }
  630. String ContainerValueImpl::to_string() const
  631. {
  632. StringBuilder builder;
  633. builder.append("(");
  634. StringBuilder joined;
  635. joined.join(", ", to_string_vector());
  636. builder.append(joined.string_view());
  637. builder.append(")");
  638. return builder.build();
  639. }
  640. u32 ContainerValueImpl::hash() const
  641. {
  642. u32 ret = 0u;
  643. for (auto& value : value()) {
  644. Value v(value);
  645. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  646. if (!ret)
  647. ret = v.hash();
  648. else
  649. ret = int_hash((ret * 209) ^ (v.hash() * 413));
  650. }
  651. return ret;
  652. }
  653. bool ContainerValueImpl::append(Value const& value)
  654. {
  655. if (value.type() == SQLType::Tuple || value.type() == SQLType::Array)
  656. return false;
  657. return append(value.downcast_to_basetype());
  658. }
  659. bool ContainerValueImpl::append(BaseTypeImpl const& impl)
  660. {
  661. if (m_max_size.has_value() && (size() >= m_max_size.value()))
  662. return false;
  663. if (!validate(impl))
  664. return false;
  665. m_value.value().empend(impl);
  666. return true;
  667. }
  668. void ContainerValueImpl::serialize_values(ByteBuffer& buffer) const
  669. {
  670. serialize_to(buffer, (u32)size());
  671. for (auto& value : value()) {
  672. Value(value).serialize_to(buffer);
  673. }
  674. }
  675. void ContainerValueImpl::deserialize_values(ByteBuffer& buffer, size_t& at_offset)
  676. {
  677. u32 sz;
  678. deserialize_from(buffer, at_offset, sz);
  679. m_value = Vector<BaseTypeImpl>();
  680. for (auto ix = 0u; ix < sz; ix++) {
  681. append(Value::deserialize_from(buffer, at_offset));
  682. }
  683. }
  684. void TupleImpl::assign(Value const& other)
  685. {
  686. if (other.type() != SQLType::Tuple) {
  687. m_value = {};
  688. return;
  689. }
  690. auto& other_impl = other.get_impl<TupleImpl>({});
  691. auto other_descriptor = other_impl.m_descriptor;
  692. if (m_descriptor && other_descriptor && m_descriptor->compare_ignoring_names(*other_descriptor)) {
  693. m_value = {};
  694. return;
  695. }
  696. assign_vector(other.to_vector().value());
  697. }
  698. size_t TupleImpl::length() const
  699. {
  700. return (m_descriptor) ? m_descriptor->data_length() : 0;
  701. }
  702. bool TupleImpl::can_cast(Value const& other_value) const
  703. {
  704. if (other_value.type() != SQLType::Tuple)
  705. return false;
  706. return (m_descriptor == other_value.get_impl<TupleImpl>({}).m_descriptor);
  707. }
  708. int TupleImpl::compare(Value const& other) const
  709. {
  710. if (other.type() != SQLType::Tuple)
  711. return 1;
  712. auto& other_impl = other.get_impl<TupleImpl>({});
  713. if (m_descriptor && other_impl.m_descriptor && m_descriptor->compare_ignoring_names(*other_impl.m_descriptor))
  714. return 1;
  715. auto other_values = other_impl.value();
  716. if (size() != other_impl.size())
  717. return (int)value().size() - (int)other_impl.size();
  718. for (auto ix = 0u; ix < value().size(); ix++) {
  719. auto ret = Value(value()[ix]).compare(Value(other_impl.value()[ix]));
  720. if (ret != 0) {
  721. if (m_descriptor && (ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  722. ret = -ret;
  723. return ret;
  724. }
  725. }
  726. return 0;
  727. }
  728. void TupleImpl::serialize(ByteBuffer& buffer) const
  729. {
  730. if (m_descriptor) {
  731. serialize_to(buffer, (u32)m_descriptor->size());
  732. for (auto& tuple_element : *m_descriptor) {
  733. u8 elem_type = (u8)tuple_element.type;
  734. serialize_to(buffer, elem_type);
  735. u8 elem_order = (u8)tuple_element.order;
  736. serialize_to(buffer, elem_order);
  737. }
  738. } else {
  739. serialize_to(buffer, (u32)-1);
  740. }
  741. serialize_values(buffer);
  742. }
  743. void TupleImpl::deserialize(ByteBuffer& buffer, size_t& at_offset)
  744. {
  745. u32 sz;
  746. deserialize_from(buffer, at_offset, sz);
  747. if (sz != (u32)-1) {
  748. NonnullRefPtr<TupleDescriptor> serialized_descriptor = adopt_ref(*new TupleDescriptor);
  749. for (auto ix = 0u; ix < sz; ix++) {
  750. u8 elem_type, elem_order;
  751. deserialize_from(buffer, at_offset, elem_type);
  752. deserialize_from(buffer, at_offset, elem_order);
  753. serialized_descriptor->empend("", (SQLType)elem_type, (Order)elem_order);
  754. }
  755. m_descriptor = serialized_descriptor;
  756. m_max_size = m_descriptor->size();
  757. } else {
  758. m_descriptor = nullptr;
  759. m_max_size = {};
  760. }
  761. deserialize_values(buffer, at_offset);
  762. }
  763. bool TupleImpl::validate(BaseTypeImpl const& value)
  764. {
  765. if (!m_descriptor)
  766. return true;
  767. auto required_type = (*m_descriptor)[size()].type;
  768. return Value(value).type() == required_type;
  769. }
  770. bool TupleImpl::validate_after_assignment()
  771. {
  772. if (!m_descriptor)
  773. return true;
  774. for (auto ix = value().size(); ix < m_descriptor->size(); ++ix) {
  775. auto required_type = (*m_descriptor)[ix].type;
  776. append(Value(required_type));
  777. }
  778. return true;
  779. }
  780. void ArrayImpl::assign(Value const& other)
  781. {
  782. if (other.type() != SQLType::Array) {
  783. m_value = {};
  784. return;
  785. }
  786. auto& other_impl = other.get_impl<ArrayImpl>({});
  787. if (m_max_size != other_impl.m_max_size || m_element_type != other_impl.m_element_type) {
  788. m_value = {};
  789. return;
  790. }
  791. assign_vector(other.to_vector().value());
  792. }
  793. size_t ArrayImpl::length() const
  794. {
  795. size_t ret = 0;
  796. for (auto& value : value()) {
  797. ret += Value(value).length();
  798. }
  799. return ret;
  800. }
  801. bool ArrayImpl::can_cast(Value const& other_value) const
  802. {
  803. if (other_value.type() != SQLType::Array)
  804. return false;
  805. auto& other_impl = other_value.get_impl<ArrayImpl>({});
  806. return (m_max_size != other_impl.m_max_size || m_element_type != other_impl.m_element_type);
  807. }
  808. int ArrayImpl::compare(Value const& other) const
  809. {
  810. if (other.type() != SQLType::Array)
  811. return 1;
  812. auto other_impl = other.get_impl<ArrayImpl>({});
  813. if (other_impl.m_element_type != m_element_type)
  814. return 1;
  815. if (other_impl.m_max_size.has_value() && m_max_size.has_value() && other_impl.m_max_size != m_max_size)
  816. return (int)m_max_size.value() - (int)other_impl.m_max_size.value();
  817. if (size() != other_impl.size())
  818. return (int)size() - (int)other_impl.size();
  819. for (auto ix = 0u; ix < size(); ix++) {
  820. auto ret = Value(value()[ix]).compare(Value(other_impl.value()[ix]));
  821. if (ret != 0) {
  822. return ret;
  823. }
  824. }
  825. return 0;
  826. }
  827. void ArrayImpl::serialize(ByteBuffer& buffer) const
  828. {
  829. serialize_to(buffer, (u8)m_element_type);
  830. if (m_max_size.has_value())
  831. serialize_to(buffer, (u32)m_max_size.value());
  832. else
  833. serialize_to(buffer, (u32)0);
  834. serialize_values(buffer);
  835. }
  836. void ArrayImpl::deserialize(ByteBuffer& buffer, size_t& at_offset)
  837. {
  838. u8 elem_type;
  839. deserialize_from(buffer, at_offset, elem_type);
  840. m_element_type = (SQLType)elem_type;
  841. u32 max_sz;
  842. deserialize_from(buffer, at_offset, max_sz);
  843. if (max_sz)
  844. m_max_size = max_sz;
  845. else
  846. m_max_size = {};
  847. deserialize_values(buffer, at_offset);
  848. }
  849. bool ArrayImpl::validate(BaseTypeImpl const& impl)
  850. {
  851. return Value(impl).type() == m_element_type;
  852. }
  853. }