mysql/common.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #include "common.h"
9 #include "soci-backend.h"
10 #include <ciso646>
11 #include <cstdlib>
12 #include <cstring>
13 #include <ctime>
14 
15 namespace // anonymous
16 {
17 
18 // helper function for parsing decimal data (for std::tm)
19 long parse10(char const *&p1, char *&p2, const char *msg)
20 {
21  long v = std::strtol(p1, &p2, 10);
22  if (p2 != p1)
23  {
24  p1 = p2 + 1;
25  return v;
26  }
27  else
28  {
29  throw soci::soci_error(msg);
30  }
31 }
32 
33 } // namespace anonymous
34 
35 
36 void soci::details::mysql::parse_std_tm(char const *buf, std::tm &t)
37 {
38  char const *p1 = buf;
39  char *p2;
40  long year, month, day;
41  long hour = 0, minute = 0, second = 0;
42 
43  const char *errMsg = "Cannot convert data to std::tm.";
44 
45  if (strchr(buf, '-') != NULL)
46  {
47  year = parse10(p1, p2, errMsg);
48  month = parse10(p1, p2, errMsg);
49  day = parse10(p1, p2, errMsg);
50  }
51  else
52  {
53  year = 2000;
54  month = 1;
55  day = 1;
56  }
57 
58 
59  if (strchr(buf, ':') != NULL)
60  {
61  // there is also the time of day available
62  hour = parse10(p1, p2, errMsg);
63  minute = parse10(p1, p2, errMsg);
64  second = parse10(p1, p2, errMsg);
65  }
66 
67  t.tm_isdst = -1;
68  t.tm_year = year - 1900;
69  t.tm_mon = month - 1;
70  t.tm_mday = day;
71  t.tm_hour = hour;
72  t.tm_min = minute;
73  t.tm_sec = second;
74 
75  std::mktime(&t);
76 }
77 
78 char * soci::details::mysql::quote(MYSQL * conn, const char *s, int len)
79 {
80  char *retv = new char[2 * len + 3];
81  retv[0] = '\'';
82  int len_esc = mysql_real_escape_string(conn, retv + 1, s, len);
83  retv[len_esc + 1] = '\'';
84  retv[len_esc + 2] = '\0';
85 
86  return retv;
87 }
void parse_std_tm(char const *buf, std::tm &t)
char * quote(MYSQL *conn, const char *s, int len)


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40