~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_db.cc

  • Committer: Brian Aker
  • Date: 2008-08-12 06:19:25 UTC
  • Revision ID: brian@tangent.org-20080812061925-4ze0m29176903stu
Modified sql_db to now use Google Proto buffers instead of MySQL type.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
 
17
17
/* create and drop of databases */
 
18
#include <string>
 
19
#include <fstream>
18
20
#include <drizzled/serialize/serialize.h>
 
21
using namespace std;
19
22
#include <drizzled/server_includes.h>
20
23
#include <mysys/mysys_err.h>
21
24
#include <mysys/my_dir.h>
327
330
  1     Could not create file or write to it.  Error sent through my_error()
328
331
*/
329
332
 
330
 
static bool write_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create)
 
333
static bool write_db_opt(THD *thd, const char *path, const char *name, HA_CREATE_INFO *create)
331
334
{
332
 
  register File file;
333
 
  char buf[256]; // Should be enough for one option
334
335
  bool error= true;
 
336
  schema::Schema db;
 
337
 
 
338
  assert(name);
335
339
 
336
340
  if (!create->default_table_charset)
337
341
    create->default_table_charset= thd->variables.collation_server;
339
343
  if (put_dbopt(path, create))
340
344
    return 1;
341
345
 
342
 
  if ((file=my_create(path, CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0)
343
 
  {
344
 
    uint32_t length;
345
 
    length= (uint32_t) (strxnmov(buf, sizeof(buf)-1, "default-character-set=",
346
 
                              create->default_table_charset->csname,
347
 
                              "\ndefault-collation=",
348
 
                              create->default_table_charset->name,
349
 
                              "\n", NullS) - buf);
350
 
 
351
 
    /* Error is written by my_write */
352
 
    if (!my_write(file,(uchar*) buf, length, MYF(MY_NABP+MY_WME)))
353
 
      error= false;
354
 
    my_close(file,MYF(0));
355
 
  }
 
346
  db.set_name(name);
 
347
  db.set_characterset(create->default_table_charset->csname);
 
348
  db.set_collation(create->default_table_charset->name);
 
349
 
 
350
  fstream output(path, ios::out | ios::trunc | ios::binary);
 
351
  if (!db.SerializeToOstream(&output)) 
 
352
    error= false;
 
353
 
356
354
  return error;
357
355
}
358
356
 
374
372
 
375
373
bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create)
376
374
{
377
 
  File file;
378
 
  char buf[256];
379
375
  bool error=1;
380
 
  uint nbytes;
 
376
  schema::Schema db;
 
377
  string buffer;
381
378
 
382
379
  memset(create, 0, sizeof(*create));
383
380
  create->default_table_charset= thd->variables.collation_server;
386
383
  if (!get_dbopt(path, create))
387
384
    return(0);
388
385
 
389
 
  /* Otherwise, load options from the .opt file */
390
 
  if ((file=my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0)
391
 
    goto err1;
392
 
 
393
 
  IO_CACHE cache;
394
 
  if (init_io_cache(&cache, file, IO_SIZE, READ_CACHE, 0, 0, MYF(0)))
395
 
    goto err2;
396
 
 
397
 
  while ((int) (nbytes= my_b_gets(&cache, (char*) buf, sizeof(buf))) > 0)
398
 
  {
399
 
    char *pos= buf+nbytes-1;
400
 
    /* Remove end space and control characters */
401
 
    while (pos > buf && !my_isgraph(&my_charset_latin1, pos[-1]))
402
 
      pos--;
403
 
    *pos=0;
404
 
    if ((pos= strchr(buf, '=')))
405
 
    {
406
 
      if (!strncmp(buf,"default-character-set", (pos-buf)))
407
 
      {
408
 
        /*
409
 
           Try character set name, and if it fails
410
 
           try collation name, probably it's an old
411
 
           4.1.0 db.opt file, which didn't have
412
 
           separate default-character-set and
413
 
           default-collation commands.
414
 
        */
415
 
        if (!(create->default_table_charset=
416
 
        get_charset_by_csname(pos+1, MY_CS_PRIMARY, MYF(0))) &&
417
 
            !(create->default_table_charset=
418
 
              get_charset_by_name(pos+1, MYF(0))))
419
 
        {
420
 
          sql_print_error("Error while loading database options: '%s':",path);
421
 
          sql_print_error(ER(ER_UNKNOWN_CHARACTER_SET),pos+1);
422
 
          create->default_table_charset= default_charset_info;
423
 
        }
424
 
      }
425
 
      else if (!strncmp(buf,"default-collation", (pos-buf)))
426
 
      {
427
 
        if (!(create->default_table_charset= get_charset_by_name(pos+1,
428
 
                                                           MYF(0))))
429
 
        {
430
 
          sql_print_error("Error while loading database options: '%s':",path);
431
 
          sql_print_error(ER(ER_UNKNOWN_COLLATION),pos+1);
432
 
          create->default_table_charset= default_charset_info;
433
 
        }
434
 
      }
435
 
    }
436
 
  }
 
386
  fstream input(path, ios::in | ios::binary);
 
387
  if (!input)
 
388
    goto err1;
 
389
  else if (!db.ParseFromIstream(&input))
 
390
    goto err1;
 
391
 
 
392
  buffer= db.characterset();
 
393
  if (!(create->default_table_charset= get_charset_by_csname(buffer.c_str(), MY_CS_PRIMARY, MYF(0))))
 
394
  {
 
395
    sql_print_error("Error while loading database options: '%s':",path);
 
396
    sql_print_error(ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
397
    create->default_table_charset= default_charset_info;
 
398
  }
 
399
 
 
400
  buffer= db.collation();
 
401
  if (!(create->default_table_charset= get_charset_by_name(buffer.c_str(), MYF(0))))
 
402
  {
 
403
    sql_print_error("Error while loading database options: '%s':",path);
 
404
    sql_print_error(ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
405
    create->default_table_charset= default_charset_info;
 
406
  }
 
407
 
437
408
  /*
438
409
    Put the loaded value into the hash.
439
410
    Note that another thread could've added the same
443
414
  */
444
415
  error= put_dbopt(path, create);
445
416
 
446
 
  end_io_cache(&cache);
447
 
err2:
448
 
  my_close(file,MYF(0));
449
417
err1:
450
418
  return(error);
451
419
}
625
593
 
626
594
  path[path_len-1]= FN_LIBCHAR;
627
595
  strmake(path+path_len, MY_DB_OPT_FILE, sizeof(path)-path_len-1);
628
 
  if (write_db_opt(thd, path, create_info))
 
596
  if (write_db_opt(thd, path, db, create_info))
629
597
  {
630
598
    /*
631
599
      Could not create options file.
735
703
     "table name to file name" encoding.
736
704
  */
737
705
  build_table_filename(path, sizeof(path), db, "", MY_DB_OPT_FILE, 0);
738
 
  if ((error=write_db_opt(thd, path, create_info)))
 
706
  if ((error=write_db_opt(thd, path, db, create_info)))
739
707
    goto exit;
740
708
 
741
709
  /* Change options if current database is being altered. */