~mordred/drizzle/codestyle

« back to all changes in this revision

Viewing changes to libdrizzle/libdrizzle.c

  • Committer: Monty Taylor
  • Date: 2008-09-14 19:39:48 UTC
  • mfrom: (383.1.43 client-split)
  • Revision ID: monty@inaugust.com-20080914193948-ns2p0lts4qtsqx7n
Merged from client-split.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
19
 
20
20
#include <drizzled/global.h>
21
 
#include <mysys/my_sys.h>
22
21
#include "drizzle.h"
23
22
#include "errmsg.h"
24
23
#include <sys/stat.h>
53
52
#include "client_settings.h"
54
53
#include <drizzled/version.h>
55
54
 
 
55
/* Borrowed from libicu header */
 
56
 
 
57
#define U8_IS_SINGLE(c) (((c)&0x80)==0)
 
58
#define U8_LENGTH(c) \
 
59
    ((uint32_t)(c)<=0x7f ? 1 : \
 
60
        ((uint32_t)(c)<=0x7ff ? 2 : \
 
61
            ((uint32_t)(c)<=0xd7ff ? 3 : \
 
62
                ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
 
63
                    ((uint32_t)(c)<=0xffff ? 3 : 4)\
 
64
                ) \
 
65
            ) \
 
66
        ) \
 
67
    )
 
68
 
 
69
 
56
70
#undef net_buffer_length
57
71
#undef max_allowed_packet
58
72
 
78
92
  return &drizzle_internal_parameters;
79
93
}
80
94
 
81
 
bool drizzle_thread_init()
82
 
{
83
 
  return my_thread_init();
84
 
}
85
 
 
86
 
void drizzle_thread_end()
87
 
{
88
 
  my_thread_end();
89
 
}
90
 
 
91
 
 
92
95
/*
93
96
  Expand wildcard to a sql string
94
97
*/
151
154
  char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
152
155
  char *end= buff;
153
156
  int rc;
154
 
  const CHARSET_INFO *saved_cs= drizzle->charset;
155
 
 
156
 
  /* Get the connection-default character set. */
157
 
 
158
 
  if (drizzle_init_character_set(drizzle))
159
 
  {
160
 
    drizzle->charset= saved_cs;
161
 
    return(true);
162
 
  }
163
157
 
164
158
  /* Use an empty string instead of NULL. */
165
159
 
185
179
  end= strncpy(end, db ? db : "", NAME_LEN) + NAME_LEN + 1;
186
180
 
187
181
  /* Add character set number. */
188
 
 
189
182
  if (drizzle->server_capabilities & CLIENT_SECURE_CONNECTION)
190
183
  {
191
 
    int2store(end, (ushort) drizzle->charset->number);
 
184
    int2store(end, (ushort) 45); // utf8mb4 number from mystrings/ctype-utf8.c
192
185
    end+= 2;
193
186
  }
194
187
 
212
205
    drizzle->passwd= strdup(passwd);
213
206
    drizzle->db= db ? strdup(db) : 0;
214
207
  }
215
 
  else
216
 
  {
217
 
    drizzle->charset= saved_cs;
218
 
  }
219
208
 
220
209
  return(rc);
221
210
}
316
305
    return NULL;
317
306
 
318
307
  drizzle->field_count= (uint) query->rows;
319
 
  return unpack_fields(query,&drizzle->field_alloc, drizzle->field_count, 1);
 
308
  return unpack_fields(query, drizzle->field_count, 1);
320
309
}
321
310
 
322
311
 
349
338
  memset(result, 0, sizeof(DRIZZLE_RES));
350
339
 
351
340
  result->methods= drizzle->methods;
352
 
  result->field_alloc=drizzle->field_alloc;
353
341
  drizzle->fields=0;
354
342
  result->field_count = drizzle->field_count;
355
343
  result->fields= fields;
373
361
  field_count=(uint) net_field_length(&pos);
374
362
  if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0, 7)))
375
363
    return(NULL);
376
 
  if (!(drizzle->fields=unpack_fields(fields,&drizzle->field_alloc,field_count,0)))
 
364
  if (!(drizzle->fields=unpack_fields(fields, field_count, 0)))
377
365
    return(0);
378
366
  drizzle->status=DRIZZLE_STATUS_GET_RESULT;
379
367
  drizzle->field_count=field_count;
533
521
  return drizzle->thread_id;
534
522
}
535
523
 
536
 
const char * drizzle_character_set_name(const DRIZZLE *drizzle)
537
 
{
538
 
  return drizzle->charset->csname;
539
 
}
540
 
 
541
 
void drizzle_get_character_set_info(const DRIZZLE *drizzle, MY_CHARSET_INFO *csinfo)
542
 
{
543
 
  csinfo->number   = drizzle->charset->number;
544
 
  csinfo->state    = drizzle->charset->state;
545
 
  csinfo->csname   = drizzle->charset->csname;
546
 
  csinfo->name     = drizzle->charset->name;
547
 
  csinfo->comment  = drizzle->charset->comment;
548
 
  csinfo->mbminlen = drizzle->charset->mbminlen;
549
 
  csinfo->mbmaxlen = drizzle->charset->mbmaxlen;
550
 
 
551
 
  if (drizzle->options.charset_dir)
552
 
    csinfo->dir = drizzle->options.charset_dir;
553
 
  else
554
 
    csinfo->dir = charsets_dir;
555
 
}
556
 
 
557
 
uint drizzle_thread_safe(void)
558
 
{
559
 
  return 1;
560
 
}
561
 
 
562
 
 
563
 
bool drizzle_embedded(void)
564
 
{
565
 
  return false;
566
 
}
567
 
 
568
524
/****************************************************************************
569
525
  Some support functions
570
526
****************************************************************************/
596
552
  Each character needs two bytes, and you need room for the terminating
597
553
  null byte. When drizzle_hex_string() returns, the contents of "to" will
598
554
  be a null-terminated string. The return value is the length of the
599
 
  encoded string, not including the terminating null character.
600
 
 
601
 
  The return value does not contain any leading 0x or a leading X' and
 
555
  encoded string, not including the terminating null character.  The return value does not contain any leading 0x or a leading X' and
602
556
  trailing '. The caller must supply whichever of those is desired.
603
557
*/
604
558
 
626
580
uint32_t
627
581
drizzle_escape_string(char *to,const char *from, uint32_t length)
628
582
{
629
 
  return escape_string_for_drizzle(default_charset_info, to, 0, from, length);
630
 
}
631
 
 
632
 
uint32_t
633
 
drizzle_real_escape_string(DRIZZLE *drizzle, char *to,const char *from,
634
 
       uint32_t length)
635
 
{
636
 
  if (drizzle->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
637
 
    return escape_quotes_for_drizzle(drizzle->charset, to, 0, from, length);
638
 
  return escape_string_for_drizzle(drizzle->charset, to, 0, from, length);
639
 
}
640
 
 
641
 
void
642
 
myodbc_remove_escape(const DRIZZLE *drizzle, char *name)
643
 
{
644
 
  char *to;
645
 
#ifdef USE_MB
646
 
  bool use_mb_flag= use_mb(drizzle->charset);
647
 
  char *end=NULL;
648
 
  if (use_mb_flag)
649
 
    for (end=name; *end ; end++) ;
650
 
#endif
651
 
 
652
 
  for (to=name ; *name ; name++)
 
583
  const char *to_start= to;
 
584
  const char *end, *to_end=to_start + 2*length;
 
585
  bool overflow= false;
 
586
  for (end= from + length; from < end; from++)
653
587
  {
654
 
#ifdef USE_MB
655
 
    int l;
656
 
    if (use_mb_flag && (l = my_ismbchar( drizzle->charset, name , end ) ) )
 
588
    uint32_t tmp_length;
 
589
    char escape= 0;
 
590
    if (!U8_IS_SINGLE(*from))
657
591
    {
658
 
      while (l--)
659
 
        *to++ = *name++;
660
 
      name--;
 
592
      tmp_length= U8_LENGTH(*from);
 
593
      if (to + tmp_length > to_end)
 
594
      {
 
595
        overflow= true;
 
596
        break;
 
597
      }
 
598
      while (tmp_length--)
 
599
        *to++= *from++;
 
600
      from--;
661
601
      continue;
662
602
    }
663
 
#endif
664
 
    if (*name == '\\' && name[1])
665
 
      name++;
666
 
    *to++= *name;
 
603
    switch (*from) {
 
604
    case 0:                             /* Must be escaped for 'mysql' */
 
605
      escape= '0';
 
606
      break;
 
607
    case '\n':                          /* Must be escaped for logs */
 
608
      escape= 'n';
 
609
      break;
 
610
    case '\r':
 
611
      escape= 'r';
 
612
      break;
 
613
    case '\\':
 
614
      escape= '\\';
 
615
      break;
 
616
    case '\'':
 
617
      escape= '\'';
 
618
      break;
 
619
    case '"':                           /* Better safe than sorry */
 
620
      escape= '"';
 
621
      break;
 
622
    case '\032':                        /* This gives problems on Win32 */
 
623
      escape= 'Z';
 
624
      break;
 
625
    }
 
626
    if (escape)
 
627
    {
 
628
      if (to + 2 > to_end)
 
629
      {
 
630
        overflow= true;
 
631
        break;
 
632
      }
 
633
      *to++= '\\';
 
634
      *to++= escape;
 
635
    }
 
636
    else
 
637
    {
 
638
      if (to + 1 > to_end)
 
639
      {
 
640
        overflow= true;
 
641
        break;
 
642
      }
 
643
      *to++= *from;
 
644
    }
667
645
  }
668
 
  *to=0;
 
646
  *to= 0;
 
647
  return overflow ? (size_t) -1 : (size_t) (to - to_start);
669
648
}
670
649
 
671
650
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)