Value.cpp 8.8 KB

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