Value.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSQL/Value.h>
  7. #include <cstring>
  8. namespace SQL {
  9. Value::Value(SQLType sql_type)
  10. {
  11. setup(sql_type);
  12. }
  13. Value::Value(SQLType sql_type, ByteBuffer& buffer, size_t& offset)
  14. {
  15. setup(sql_type);
  16. m_deserialize(buffer, offset);
  17. m_is_null = false;
  18. }
  19. Value::Value(Value const& other)
  20. {
  21. setup(other.type());
  22. m_is_null = other.is_null();
  23. if (!m_is_null)
  24. m_assign_value(other);
  25. }
  26. Value::~Value()
  27. {
  28. }
  29. Value const& Value::null()
  30. {
  31. static Value s_null;
  32. return s_null;
  33. }
  34. Value& Value::operator=(Value const& other)
  35. {
  36. if (this != &other) {
  37. m_is_null = other.is_null();
  38. if (!m_is_null) {
  39. VERIFY(can_cast(other));
  40. m_assign_value(other);
  41. }
  42. }
  43. return (*this);
  44. }
  45. Value& Value::operator=(String const& value)
  46. {
  47. m_assign_string(value);
  48. m_is_null = false;
  49. return (*this);
  50. }
  51. Value& Value::operator=(int value)
  52. {
  53. m_assign_int(value);
  54. m_is_null = false;
  55. return (*this);
  56. }
  57. Value& Value::operator=(u32 value)
  58. {
  59. m_assign_int(static_cast<int>(value));
  60. m_is_null = false;
  61. return (*this);
  62. }
  63. Value& Value::operator=(double value)
  64. {
  65. m_assign_double(value);
  66. m_is_null = false;
  67. return (*this);
  68. }
  69. Value& Value::set_null()
  70. {
  71. m_is_null = true;
  72. return (*this);
  73. }
  74. Optional<String> Value::to_string() const
  75. {
  76. if (!m_is_null)
  77. return m_to_string();
  78. else
  79. return {};
  80. }
  81. Value::operator String() const
  82. {
  83. auto str = to_string();
  84. VERIFY(str.has_value());
  85. return str.value();
  86. }
  87. Optional<int> Value::to_int() const
  88. {
  89. if (!m_is_null) {
  90. return m_to_int();
  91. } else {
  92. return {};
  93. }
  94. }
  95. Value::operator int() const
  96. {
  97. auto i = to_int();
  98. VERIFY(i.has_value());
  99. return i.value();
  100. }
  101. Optional<u32> Value::to_u32() const
  102. {
  103. if (!m_is_null) {
  104. auto ret = m_to_int();
  105. if (ret.has_value())
  106. return static_cast<u32>(ret.value());
  107. else
  108. return {};
  109. } else {
  110. return {};
  111. }
  112. }
  113. Value::operator u32() const
  114. {
  115. auto i = to_u32();
  116. VERIFY(i.has_value());
  117. return i.value();
  118. }
  119. Optional<double> Value::to_double() const
  120. {
  121. if (!m_is_null)
  122. return m_to_double();
  123. else
  124. return {};
  125. }
  126. Value::operator double() const
  127. {
  128. auto dbl = to_double();
  129. VERIFY(dbl.has_value());
  130. return dbl.value();
  131. }
  132. bool Value::can_cast(Value const& other) const
  133. {
  134. if (other.is_null())
  135. return true;
  136. if (type() == other.type())
  137. return true;
  138. return m_can_cast(other);
  139. }
  140. bool Value::operator==(String const& other) const
  141. {
  142. return operator String() == other;
  143. }
  144. bool Value::operator==(int other) const
  145. {
  146. return operator int() == other;
  147. }
  148. bool Value::operator==(double other) const
  149. {
  150. return operator double() == other;
  151. }
  152. void Value::setup(SQLType sql_type)
  153. {
  154. m_type = sql_type;
  155. switch (sql_type) {
  156. case SQLType::Text:
  157. setup_text();
  158. break;
  159. case SQLType::Integer:
  160. setup_int();
  161. break;
  162. case SQLType::Float:
  163. setup_float();
  164. break;
  165. default:
  166. VERIFY_NOT_REACHED();
  167. }
  168. }
  169. void Value::setup_text()
  170. {
  171. m_impl = String("");
  172. m_type_name = []() { return "Text"; };
  173. m_size = []() { return 64 + sizeof(int); };
  174. m_deserialize = [&](ByteBuffer& buffer, size_t& at_offset) {
  175. int len;
  176. memcpy(&len, buffer.offset_pointer((int)at_offset), sizeof(int));
  177. at_offset += sizeof(int);
  178. m_impl = String((const char*)buffer.offset_pointer((int)at_offset));
  179. at_offset += 64;
  180. };
  181. m_serialize = [&](ByteBuffer& buffer) {
  182. char zeroes[64];
  183. int len = min((int)m_impl.get<String>().length(), 63);
  184. buffer.append(&len, sizeof(int));
  185. buffer.append(m_impl.get<String>().characters(), len);
  186. memset(zeroes, 0, 64);
  187. buffer.append(zeroes, 64 - len);
  188. };
  189. m_assign_value = [&](Value const& other) {
  190. auto str = other.to_string();
  191. VERIFY(str.has_value());
  192. m_impl = str.value();
  193. };
  194. m_assign_string = [&](String const& string) {
  195. m_impl = string;
  196. };
  197. m_assign_int = [&](int i) {
  198. m_impl = String::number(i);
  199. };
  200. m_assign_double = [&](double d) {
  201. m_impl = String::number(d);
  202. };
  203. m_to_string = [&]() -> Optional<String> {
  204. return m_impl.get<String>();
  205. };
  206. m_to_int = [&]() -> Optional<int> {
  207. return m_impl.get<String>().to_int();
  208. };
  209. m_to_double = [&]() -> Optional<double> {
  210. char* end_ptr;
  211. double ret = strtod(m_impl.get<String>().characters(), &end_ptr);
  212. if (end_ptr == m_impl.get<String>().characters()) {
  213. return {};
  214. }
  215. return ret;
  216. };
  217. m_compare = [&](Value const& other) -> int {
  218. auto s1 = to_string();
  219. auto s2 = other.to_string();
  220. VERIFY(s1.has_value());
  221. if (!s2.has_value()) {
  222. return 1;
  223. }
  224. if (s1.value() == s2.value())
  225. return 0;
  226. return (s1.value() < s2.value()) ? -1 : 1;
  227. };
  228. m_can_cast = [](Value const&) -> bool {
  229. return true;
  230. };
  231. m_hash = [&]() {
  232. return m_impl.get<String>().hash();
  233. };
  234. }
  235. void Value::setup_int()
  236. {
  237. m_impl.set<int>(0);
  238. m_type_name = []() { return "Integer"; };
  239. m_size = []() { return sizeof(int); };
  240. m_deserialize = [&](ByteBuffer& buffer, size_t& at_offset) {
  241. memcpy(m_impl.get_pointer<int>(), buffer.offset_pointer((int)at_offset), sizeof(int));
  242. at_offset += sizeof(int);
  243. };
  244. m_serialize = [&](ByteBuffer& buffer) {
  245. buffer.append(m_impl.get_pointer<int>(), sizeof(int));
  246. };
  247. m_assign_value = [&](Value const& other) {
  248. auto i = other.to_int();
  249. VERIFY(i.has_value());
  250. m_impl = i.value();
  251. };
  252. m_assign_string = [&](String const& string) {
  253. auto i = string.to_int();
  254. VERIFY(i.has_value());
  255. m_impl = i.value();
  256. };
  257. m_assign_int = [&](int i) {
  258. m_impl.set<int>(move(i));
  259. };
  260. m_assign_double = [&](double d) {
  261. m_impl.set<int>((int)d);
  262. };
  263. m_to_string = [&]() -> Optional<String> {
  264. StringBuilder builder;
  265. builder.appendff("{}", m_impl.get<int>());
  266. return builder.build();
  267. };
  268. m_to_int = [&]() -> Optional<int> {
  269. return m_impl.get<int>();
  270. };
  271. m_to_double = [&]() -> Optional<double> {
  272. return static_cast<double>(m_impl.get<int>());
  273. };
  274. m_compare = [&](Value const& other) -> int {
  275. auto casted = other.to_int();
  276. if (!casted.has_value()) {
  277. return 1;
  278. }
  279. return m_impl.get<int>() - casted.value();
  280. };
  281. m_can_cast = [](Value const& other) -> bool {
  282. auto i = other.to_int();
  283. return i.has_value();
  284. };
  285. m_hash = [&]() -> u32 {
  286. return int_hash(m_impl.get<int>());
  287. };
  288. }
  289. void Value::setup_float()
  290. {
  291. m_impl.set<double>(0.0);
  292. m_type_name = []() { return "Float"; };
  293. m_size = []() { return sizeof(double); };
  294. m_deserialize = [&](ByteBuffer& buffer, size_t& at_offset) {
  295. memcpy(m_impl.get_pointer<double>(), buffer.offset_pointer((int)at_offset), sizeof(double));
  296. at_offset += sizeof(double);
  297. };
  298. m_serialize = [&](ByteBuffer& buffer) {
  299. buffer.append(m_impl.get_pointer<double>(), sizeof(double));
  300. };
  301. m_to_string = [&]() -> Optional<String> {
  302. StringBuilder builder;
  303. builder.appendff("{}", m_impl.get<double>());
  304. return builder.build();
  305. };
  306. m_to_int = [&]() -> Optional<int> {
  307. return (int)m_impl.get<double>();
  308. };
  309. m_to_double = [&]() -> Optional<double> {
  310. return m_impl.get<double>();
  311. };
  312. m_assign_value = [&](Value const& other) {
  313. auto dbl = other.to_double();
  314. VERIFY(dbl.has_value());
  315. m_impl.set<double>(move(dbl.value()));
  316. };
  317. m_assign_string = [&](String const& string) {
  318. char* end_ptr;
  319. auto dbl = strtod(string.characters(), &end_ptr);
  320. VERIFY(end_ptr != string.characters());
  321. m_impl.set<double>(move(dbl));
  322. };
  323. m_assign_int = [&](int i) {
  324. m_impl.set<double>(static_cast<double>(i));
  325. };
  326. m_assign_double = [&](double d) {
  327. m_impl.set<double>(move(d));
  328. };
  329. m_compare = [&](Value const& other) -> int {
  330. auto casted = other.to_double();
  331. if (!casted.has_value()) {
  332. return 1;
  333. }
  334. auto diff = m_impl.get<double>() - casted.value();
  335. return (diff < NumericLimits<double>::epsilon()) ? 0 : ((diff > 0) ? 1 : -1);
  336. };
  337. m_can_cast = [](Value const& other) -> bool {
  338. auto dbl = other.to_double();
  339. return dbl.has_value();
  340. };
  341. // Using floats in hash functions is a bad idea. Let's disable that for now.
  342. m_hash = []() -> u32 {
  343. VERIFY_NOT_REACHED();
  344. };
  345. }
  346. }