~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/timestamp.cc

Style cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
22
 
#include <boost/lexical_cast.hpp>
 
21
 
 
22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/timestamp.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/tztime.h>
26
26
#include <drizzled/table.h>
27
27
#include <drizzled/session.h>
28
28
 
29
 
#include <math.h>
30
 
 
31
 
#include <sstream>
32
 
 
33
29
#include "drizzled/temporal.h"
34
30
 
35
 
namespace drizzled
36
 
{
37
 
 
38
31
/**
39
32
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
40
33
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix
87
80
                                 TableShare *share,
88
81
                                 const CHARSET_INFO * const cs)
89
82
  :Field_str(ptr_arg,
90
 
             DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
83
             drizzled::DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
91
84
             null_ptr_arg,
92
85
             null_bit_arg,
93
86
             field_name_arg,
96
89
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
97
90
  flags|= UNSIGNED_FLAG;
98
91
  unireg_check= unireg_check_arg;
99
 
  if (! share->getTimestampField() && unireg_check != NONE)
 
92
  if (! share->timestamp_field && unireg_check != NONE)
100
93
  {
101
94
    /* This timestamp has auto-update */
102
 
    share->setTimestampField(this);
103
 
    flags|= FUNCTION_DEFAULT_FLAG;
 
95
    share->timestamp_field= this;
 
96
    flags|= TIMESTAMP_FLAG;
104
97
    if (unireg_check != TIMESTAMP_DN_FIELD)
105
98
      flags|= ON_UPDATE_NOW_FLAG;
106
99
  }
110
103
                                 const char *field_name_arg,
111
104
                                 const CHARSET_INFO * const cs)
112
105
  :Field_str((unsigned char*) NULL,
113
 
             DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
106
             drizzled::DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
114
107
             maybe_null_arg ? (unsigned char*) "": 0,
115
108
             0,
116
109
             field_name_arg,
142
135
      function should be called only for first of them (i.e. the one
143
136
      having auto-set property).
144
137
    */
145
 
    assert(getTable()->timestamp_field == this);
 
138
    assert(table->timestamp_field == this);
146
139
    /* Fall-through */
147
140
  case TIMESTAMP_DNUN_FIELD:
148
141
    return TIMESTAMP_AUTO_SET_ON_BOTH;
160
153
                           uint32_t len,
161
154
                           const CHARSET_INFO * const )
162
155
{
163
 
  Timestamp temporal;
 
156
  drizzled::Timestamp temporal;
164
157
 
165
158
  ASSERT_COLUMN_MARKED_FOR_WRITE;
166
159
 
187
180
    std::stringstream ss;
188
181
    std::string tmp;
189
182
    ss.precision(18); /* 18 places should be fine for error display of double input. */
190
 
    ss << from; 
191
 
    ss >> tmp;
 
183
    ss << from; ss >> tmp;
192
184
 
193
185
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
194
186
    return 2;
204
196
   * Try to create a DateTime from the supplied integer.  Throw an error
205
197
   * if unable to create a valid DateTime.  
206
198
   */
207
 
  Timestamp temporal;
 
199
  drizzled::Timestamp temporal;
208
200
  if (! temporal.from_int64_t(from))
209
201
  {
210
 
    /* Convert the integer to a string using boost::lexical_cast */
211
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
202
    /* Convert the integer to a string using stringstream */
 
203
    std::stringstream ss;
 
204
    std::string tmp;
 
205
    ss << from; ss >> tmp;
212
206
 
213
207
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
214
208
    return 2;
228
222
 
229
223
int64_t Field_timestamp::val_int(void)
230
224
{
231
 
  uint64_t temp;
 
225
  uint32_t temp;
232
226
 
233
227
  ASSERT_COLUMN_MARKED_FOR_READ;
234
228
 
235
229
#ifdef WORDS_BIGENDIAN
236
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
237
 
    temp= uint8korr(ptr);
 
230
  if (table && table->s->db_low_byte_first)
 
231
    temp= uint4korr(ptr);
238
232
  else
239
233
#endif
240
 
    int64_tget(temp, ptr);
 
234
    longget(temp, ptr);
241
235
 
242
 
  Timestamp temporal;
 
236
  drizzled::Timestamp temporal;
243
237
  (void) temporal.from_time_t((time_t) temp);
244
238
 
245
239
  /* We must convert into a "timestamp-formatted integer" ... */
250
244
 
251
245
String *Field_timestamp::val_str(String *val_buffer, String *)
252
246
{
253
 
  uint64_t temp;
 
247
  uint32_t temp;
254
248
  char *to;
255
249
  int to_len= field_length + 1;
256
250
 
258
252
  to= (char *) val_buffer->ptr();
259
253
 
260
254
#ifdef WORDS_BIGENDIAN
261
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
262
 
    temp= uint8korr(ptr);
 
255
  if (table && table->s->db_low_byte_first)
 
256
    temp= uint4korr(ptr);
263
257
  else
264
258
#endif
265
 
    int64_tget(temp, ptr);
 
259
    longget(temp, ptr);
266
260
 
267
261
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
268
262
 
269
 
  Timestamp temporal;
 
263
  drizzled::Timestamp temporal;
270
264
  (void) temporal.from_time_t((time_t) temp);
271
265
 
272
266
  int rlen;
279
273
 
280
274
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
281
275
{
282
 
  uint64_t temp;
 
276
  uint32_t temp;
283
277
 
284
278
#ifdef WORDS_BIGENDIAN
285
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
286
 
    temp= uint8korr(ptr);
 
279
  if (table && table->s->db_low_byte_first)
 
280
    temp= uint4korr(ptr);
287
281
  else
288
282
#endif
289
 
    int64_tget(temp, ptr);
 
283
    longget(temp, ptr);
290
284
  
291
285
  memset(ltime, 0, sizeof(*ltime));
292
286
 
293
 
  Timestamp temporal;
 
287
  drizzled::Timestamp temporal;
294
288
  (void) temporal.from_time_t((time_t) temp);
295
289
 
296
290
  /* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
313
307
 
314
308
int Field_timestamp::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
315
309
{
316
 
  int64_t a,b;
 
310
  int32_t a,b;
317
311
#ifdef WORDS_BIGENDIAN
318
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
312
  if (table && table->s->db_low_byte_first)
319
313
  {
320
 
    a=sint8korr(a_ptr);
321
 
    b=sint8korr(b_ptr);
 
314
    a=sint4korr(a_ptr);
 
315
    b=sint4korr(b_ptr);
322
316
  }
323
317
  else
324
318
#endif
325
319
  {
326
 
    int64_tget(a, a_ptr);
327
 
    int64_tget(b, b_ptr);
 
320
  longget(a,a_ptr);
 
321
  longget(b,b_ptr);
328
322
  }
329
 
  return ((uint64_t) a < (uint64_t) b) ? -1 : ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
323
  return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
330
324
}
331
325
 
332
326
 
333
327
void Field_timestamp::sort_string(unsigned char *to,uint32_t )
334
328
{
335
329
#ifdef WORDS_BIGENDIAN
336
 
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
 
330
  if (!table || !table->s->db_low_byte_first)
337
331
  {
338
332
    to[0] = ptr[0];
339
333
    to[1] = ptr[1];
340
334
    to[2] = ptr[2];
341
335
    to[3] = ptr[3];
342
 
    to[4] = ptr[4];
343
 
    to[5] = ptr[5];
344
 
    to[6] = ptr[6];
345
 
    to[7] = ptr[7];
346
336
  }
347
337
  else
348
338
#endif
349
339
  {
350
 
    to[0] = ptr[7];
351
 
    to[1] = ptr[6];
352
 
    to[2] = ptr[5];
353
 
    to[3] = ptr[4];
354
 
    to[4] = ptr[3];
355
 
    to[5] = ptr[2];
356
 
    to[6] = ptr[1];
357
 
    to[7] = ptr[0];
 
340
    to[0] = ptr[3];
 
341
    to[1] = ptr[2];
 
342
    to[2] = ptr[1];
 
343
    to[3] = ptr[0];
358
344
  }
359
345
}
360
346
 
365
351
 
366
352
void Field_timestamp::set_time()
367
353
{
368
 
  Session *session= getTable() ? getTable()->in_use : current_session;
369
 
  time_t tmp= session->query_start();
 
354
  Session *session= table ? table->in_use : current_session;
 
355
  long tmp= (long) session->query_start();
370
356
  set_notnull();
371
357
  store_timestamp(tmp);
372
358
}
373
359
 
374
360
void Field_timestamp::set_default()
375
361
{
376
 
  if (getTable()->timestamp_field == this &&
 
362
  if (table->timestamp_field == this &&
377
363
      unireg_check != TIMESTAMP_UN_FIELD)
378
364
    set_time();
379
365
  else
385
371
  if ((*null_value= is_null()))
386
372
    return 0;
387
373
#ifdef WORDS_BIGENDIAN
388
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
389
 
    return sint8korr(ptr);
 
374
  if (table && table->s->db_low_byte_first)
 
375
    return sint4korr(ptr);
390
376
#endif
391
 
  int64_t tmp;
392
 
  int64_tget(tmp, ptr);
 
377
  long tmp;
 
378
  longget(tmp,ptr);
393
379
  return tmp;
394
380
}
395
381
 
396
 
void Field_timestamp::store_timestamp(int64_t timestamp)
 
382
void Field_timestamp::store_timestamp(time_t timestamp)
397
383
{
398
384
#ifdef WORDS_BIGENDIAN
399
 
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
385
  if (table && table->s->db_low_byte_first)
400
386
  {
401
 
    int8store(ptr, timestamp);
 
387
    int4store(ptr,timestamp);
402
388
  }
403
389
  else
404
390
#endif
405
 
    int64_tstore(ptr, timestamp);
 
391
    longstore(ptr,(uint32_t) timestamp);
406
392
}
407
 
 
408
 
} /* namespace drizzled */