~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_db.cc

  • Committer: Monty Taylor
  • Date: 2008-07-09 15:52:52 UTC
  • mto: (77.6.1 glibclient-merge)
  • mto: This revision was merged to the branch mainline in revision 112.
  • Revision ID: monty@inaugust.com-20080709155252-lnzmxxje1g40z3a7
Warning fixes. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
95
95
{
96
96
  my_dblock_t *opt;
97
97
  my_bool error= 0;
 
98
  DBUG_ENTER("lock_db_insert");
98
99
  
99
100
  safe_mutex_assert_owner(&LOCK_lock_db);
100
101
 
120
121
  }
121
122
 
122
123
end:
123
 
  return(error);
 
124
  DBUG_RETURN(error);
124
125
}
125
126
 
126
127
 
310
311
  my_dbopt_t *opt;
311
312
  uint length;
312
313
  my_bool error= 0;
 
314
  DBUG_ENTER("put_dbopt");
313
315
 
314
316
  length= (uint) strlen(dbname);
315
317
  
342
344
 
343
345
end:
344
346
  rw_unlock(&LOCK_dboptions);  
345
 
  return(error);
 
347
  DBUG_RETURN(error);
346
348
}
347
349
 
348
350
 
421
423
{
422
424
  File file;
423
425
  char buf[256];
 
426
  DBUG_ENTER("load_db_opt");
424
427
  bool error=1;
425
428
  uint nbytes;
426
429
 
429
432
 
430
433
  /* Check if options for this database are already in the hash */
431
434
  if (!get_dbopt(path, create))
432
 
    return(0);
 
435
    DBUG_RETURN(0);
433
436
 
434
437
  /* Otherwise, load options from the .opt file */
435
438
  if ((file=my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0)
492
495
err2:
493
496
  my_close(file,MYF(0));
494
497
err1:
495
 
  return(error);
 
498
  DBUG_RETURN(error);
496
499
}
497
500
 
498
501
 
513
516
    the operation, it is useless usually and should be ignored. The problem
514
517
    is that there are 1) system databases ("mysql") and 2) virtual
515
518
    databases ("information_schema"), which do not contain options file.
516
 
    So, load_db_opt[_by_name]() returns false for these databases, but this
 
519
    So, load_db_opt[_by_name]() returns FALSE for these databases, but this
517
520
    is not an error.
518
521
 
519
522
    load_db_opt[_by_name]() clears db_create_info structure in any case, so
522
525
    db_create_info right after that.
523
526
 
524
527
  RETURN VALUES (read NOTE!)
525
 
    false   Success
526
 
    true    Failed to retrieve options
 
528
    FALSE   Success
 
529
    TRUE    Failed to retrieve options
527
530
*/
528
531
 
529
532
bool load_db_opt_by_name(THD *thd, const char *db_name,
592
595
   (The 'silent' flags turns off 1 and 3.)
593
596
 
594
597
  RETURN VALUES
595
 
  false ok
596
 
  true  Error
 
598
  FALSE ok
 
599
  TRUE  Error
597
600
 
598
601
*/
599
602
 
607
610
  struct stat stat_info;
608
611
  uint create_options= create_info ? create_info->options : 0;
609
612
  uint path_len;
 
613
  DBUG_ENTER("mysql_create_db");
610
614
 
611
615
  /* do not create 'information_schema' db */
612
616
  if (!my_strcasecmp(system_charset_info, db, INFORMATION_SCHEMA_NAME.str))
613
617
  {
614
618
    my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
615
 
    return(-1);
 
619
    DBUG_RETURN(-1);
616
620
  }
617
621
 
618
622
  /*
714
718
    if (mysql_bin_log.is_open())
715
719
    {
716
720
      Query_log_event qinfo(thd, query, query_length, 0, 
717
 
                            /* suppress_use */ true);
 
721
                            /* suppress_use */ TRUE);
718
722
 
719
723
      /*
720
724
        Write should use the database being created as the "current
746
750
  VOID(pthread_mutex_unlock(&LOCK_mysql_create_db));
747
751
  start_waiting_global_read_lock(thd);
748
752
exit2:
749
 
  return(error);
 
753
  DBUG_RETURN(error);
750
754
}
751
755
 
752
756
 
757
761
  char path[FN_REFLEN+16];
758
762
  long result=1;
759
763
  int error= 0;
 
764
  DBUG_ENTER("mysql_alter_db");
760
765
 
761
766
  /*
762
767
    Do not alter database if another thread is holding read lock.
801
806
  if (mysql_bin_log.is_open())
802
807
  {
803
808
    Query_log_event qinfo(thd, thd->query, thd->query_length, 0,
804
 
                          /* suppress_use */ true);
 
809
                          /* suppress_use */ TRUE);
805
810
 
806
811
    /*
807
812
      Write should use the database being created as the "current
821
826
  VOID(pthread_mutex_unlock(&LOCK_mysql_create_db));
822
827
  start_waiting_global_read_lock(thd);
823
828
exit2:
824
 
  return(error);
 
829
  DBUG_RETURN(error);
825
830
}
826
831
 
827
832
 
838
843
    silent              Don't generate errors
839
844
 
840
845
  RETURN
841
 
    false ok (Database dropped)
 
846
    FALSE ok (Database dropped)
842
847
    ERROR Error
843
848
*/
844
849
 
850
855
  MY_DIR *dirp;
851
856
  uint length;
852
857
  TABLE_LIST* dropped_tables= 0;
 
858
  DBUG_ENTER("mysql_rm_db");
853
859
 
854
860
  if (db && (strcmp(db, "information_schema") == 0))
855
861
  {
856
862
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
857
 
    return(true);
 
863
    DBUG_RETURN(TRUE);
858
864
  }
859
865
 
860
866
  /*
936
942
    if (mysql_bin_log.is_open())
937
943
    {
938
944
      Query_log_event qinfo(thd, query, query_length, 0, 
939
 
                            /* suppress_use */ true);
 
945
                            /* suppress_use */ TRUE);
940
946
      /*
941
947
        Write should use the database being created as the "current
942
948
        database" and not the threads current database, which is the
1004
1010
  VOID(pthread_mutex_unlock(&LOCK_mysql_create_db));
1005
1011
  start_waiting_global_read_lock(thd);
1006
1012
exit2:
1007
 
  return(error);
 
1013
  DBUG_RETURN(error);
1008
1014
}
1009
1015
 
1010
1016
/*
1022
1028
  char filePath[FN_REFLEN];
1023
1029
  TABLE_LIST *tot_list=0, **tot_list_next;
1024
1030
  List<String> raid_dirs;
 
1031
  DBUG_ENTER("mysql_rm_known_files");
 
1032
  DBUG_PRINT("enter",("path: %s", org_path));
1025
1033
 
1026
1034
  tot_list_next= &tot_list;
1027
1035
 
1031
1039
  {
1032
1040
    FILEINFO *file=dirp->dir_entry+idx;
1033
1041
    char *extension;
 
1042
    DBUG_PRINT("info",("Examining: %s", file->name));
1034
1043
 
1035
1044
    /* skiping . and .. */
1036
1045
    if (file->name[0] == '.' && (!file->name[1] ||
1053
1062
      length= unpack_filename(newpath,newpath);
1054
1063
      if ((new_dirp = my_dir(newpath,MYF(MY_DONT_SORT))))
1055
1064
      {
 
1065
        DBUG_PRINT("my",("New subdir found: %s", newpath));
1056
1066
        if ((mysql_rm_known_files(thd, new_dirp, NullS, newpath,1,0)) < 0)
1057
1067
          goto err;
1058
1068
        if (!(copy_of_path= (char*) thd->memdup(newpath, length+1)) ||
1075
1085
      (void) unpack_filename(newpath, newpath);
1076
1086
      if ((new_dirp = my_dir(newpath, MYF(MY_DONT_SORT))))
1077
1087
      {
 
1088
        DBUG_PRINT("my",("Archive subdir found: %s", newpath));
1078
1089
        if ((mysql_rm_arc_files(thd, new_dirp, newpath)) < 0)
1079
1090
          goto err;
1080
1091
        continue;
1149
1160
  if (found_other_files)
1150
1161
  {
1151
1162
    my_error(ER_DB_DROP_RMDIR, MYF(0), org_path, EEXIST);
1152
 
    return(-1);
 
1163
    DBUG_RETURN(-1);
1153
1164
  }
1154
1165
  else
1155
1166
  {
1156
1167
    /* Don't give errors if we can't delete 'RAID' directory */
1157
1168
    if (rm_dir_w_symlink(org_path, level == 0))
1158
 
      return(-1);
 
1169
      DBUG_RETURN(-1);
1159
1170
  }
1160
1171
 
1161
 
  return(deleted);
 
1172
  DBUG_RETURN(deleted);
1162
1173
 
1163
1174
err:
1164
1175
  my_dirend(dirp);
1165
 
  return(-1);
 
1176
  DBUG_RETURN(-1);
1166
1177
}
1167
1178
 
1168
1179
 
1182
1193
{
1183
1194
  char tmp_path[FN_REFLEN], *pos;
1184
1195
  char *path= tmp_path;
 
1196
  DBUG_ENTER("rm_dir_w_symlink");
1185
1197
  unpack_filename(tmp_path, org_path);
1186
1198
#ifdef HAVE_READLINK
1187
1199
  int error;
1193
1205
    *--pos=0;
1194
1206
 
1195
1207
  if ((error= my_readlink(tmp2_path, path, MYF(MY_WME))) < 0)
1196
 
    return(1);
 
1208
    DBUG_RETURN(1);
1197
1209
  if (!error)
1198
1210
  {
1199
1211
    if (my_delete(path, MYF(send_error ? MY_WME : 0)))
1200
1212
    {
1201
 
      return(send_error);
 
1213
      DBUG_RETURN(send_error);
1202
1214
    }
1203
1215
    /* Delete directory symbolic link pointed at */
1204
1216
    path= tmp2_path;
1212
1224
  if (rmdir(path) < 0 && send_error)
1213
1225
  {
1214
1226
    my_error(ER_DB_DROP_RMDIR, MYF(0), path, errno);
1215
 
    return(1);
 
1227
    DBUG_RETURN(1);
1216
1228
  }
1217
 
  return(0);
 
1229
  DBUG_RETURN(0);
1218
1230
}
1219
1231
 
1220
1232
 
1237
1249
  long deleted= 0;
1238
1250
  ulong found_other_files= 0;
1239
1251
  char filePath[FN_REFLEN];
 
1252
  DBUG_ENTER("mysql_rm_arc_files");
 
1253
  DBUG_PRINT("enter", ("path: %s", org_path));
1240
1254
 
1241
1255
  for (uint idx=0 ;
1242
1256
       idx < (uint) dirp->number_off_files && !thd->killed ;
1244
1258
  {
1245
1259
    FILEINFO *file=dirp->dir_entry+idx;
1246
1260
    char *extension, *revision;
 
1261
    DBUG_PRINT("info",("Examining: %s", file->name));
1247
1262
 
1248
1263
    /* skiping . and .. */
1249
1264
    if (file->name[0] == '.' && (!file->name[1] ||
1283
1298
  */
1284
1299
  if (!found_other_files &&
1285
1300
      rm_dir_w_symlink(org_path, 0))
1286
 
    return(-1);
1287
 
  return(deleted);
 
1301
    DBUG_RETURN(-1);
 
1302
  DBUG_RETURN(deleted);
1288
1303
 
1289
1304
err:
1290
1305
  my_dirend(dirp);
1291
 
  return(-1);
 
1306
  DBUG_RETURN(-1);
1292
1307
}
1293
1308
 
1294
1309
 
1382
1397
 
1383
1398
 
1384
1399
/**
1385
 
  Return true if db1_name is equal to db2_name, false otherwise.
 
1400
  Return TRUE if db1_name is equal to db2_name, FALSE otherwise.
1386
1401
 
1387
1402
  The function allows to compare database names according to the MySQL
1388
1403
  rules. The database names db1 and db2 are equal if:
1410
1425
 
1411
1426
  @param thd          thread handle
1412
1427
  @param new_db_name  database name
1413
 
  @param force_switch if force_switch is false, then the operation will fail if
 
1428
  @param force_switch if force_switch is FALSE, then the operation will fail if
1414
1429
 
1415
1430
                        - new_db_name is NULL or empty;
1416
1431
 
1421
1436
 
1422
1437
                        - OR new database does not exist;
1423
1438
 
1424
 
                      if force_switch is true, then
 
1439
                      if force_switch is TRUE, then
1425
1440
 
1426
1441
                        - if new_db_name is NULL or empty, the current
1427
1442
                          database will be NULL, @@collation_database will
1463
1478
  the stack address was long gone.
1464
1479
 
1465
1480
  @return Operation status
1466
 
    @retval false Success
1467
 
    @retval true  Error
 
1481
    @retval FALSE Success
 
1482
    @retval TRUE  Error
1468
1483
*/
1469
1484
 
1470
1485
bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch)
1472
1487
  LEX_STRING new_db_file_name;
1473
1488
  CHARSET_INFO *db_default_cl;
1474
1489
 
 
1490
  DBUG_ENTER("mysql_change_db");
 
1491
  DBUG_PRINT("enter",("name: '%s'", new_db_name->str));
 
1492
 
1475
1493
  if (new_db_name == NULL ||
1476
1494
      new_db_name->length == 0)
1477
1495
  {
1489
1507
 
1490
1508
      mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
1491
1509
 
1492
 
      return(false);
 
1510
      DBUG_RETURN(FALSE);
1493
1511
    }
1494
1512
    else
1495
1513
    {
1496
1514
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
1497
1515
 
1498
 
      return(true);
 
1516
      DBUG_RETURN(TRUE);
1499
1517
    }
1500
1518
  }
1501
1519
 
1506
1524
 
1507
1525
    mysql_change_db_impl(thd, &INFORMATION_SCHEMA_NAME, system_charset_info);
1508
1526
 
1509
 
    return(false);
 
1527
    DBUG_RETURN(FALSE);
1510
1528
  }
1511
1529
 
1512
1530
  /*
1521
1539
  new_db_file_name.length= new_db_name->length;
1522
1540
 
1523
1541
  if (new_db_file_name.str == NULL)
1524
 
    return(true);                             /* the error is set */
 
1542
    DBUG_RETURN(TRUE);                             /* the error is set */
1525
1543
 
1526
1544
  /*
1527
1545
    NOTE: if check_db_name() fails, we should throw an error in any case,
1540
1558
    if (force_switch)
1541
1559
      mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
1542
1560
 
1543
 
    return(true);
 
1561
    DBUG_RETURN(TRUE);
1544
1562
  }
1545
1563
 
 
1564
  DBUG_PRINT("info",("Use database: %s", new_db_file_name.str));
 
1565
 
 
1566
 
1546
1567
  if (check_db_dir_existence(new_db_file_name.str))
1547
1568
  {
1548
1569
    if (force_switch)
1561
1582
 
1562
1583
      /* The operation succeed. */
1563
1584
 
1564
 
      return(false);
 
1585
      DBUG_RETURN(FALSE);
1565
1586
    }
1566
1587
    else
1567
1588
    {
1572
1593
 
1573
1594
      /* The operation failed. */
1574
1595
 
1575
 
      return(true);
 
1596
      DBUG_RETURN(TRUE);
1576
1597
    }
1577
1598
  }
1578
1599
 
1585
1606
 
1586
1607
  mysql_change_db_impl(thd, &new_db_file_name, db_default_cl);
1587
1608
 
1588
 
  return(false);
 
1609
  DBUG_RETURN(FALSE);
1589
1610
}
1590
1611
 
1591
1612
 
1618
1639
  *cur_db_changed= !cmp_db_names(thd->db, new_db_name->str);
1619
1640
 
1620
1641
  if (!*cur_db_changed)
1621
 
    return false;
 
1642
    return FALSE;
1622
1643
 
1623
1644
  backup_current_db_name(thd, saved_db_name);
1624
1645
 
1704
1725
  TABLE_LIST *table_list;
1705
1726
  SELECT_LEX *sl= thd->lex->current_select;
1706
1727
  LEX_STRING new_db;
 
1728
  DBUG_ENTER("mysql_upgrade_db");
1707
1729
 
1708
1730
  if ((old_db->length <= MYSQL50_TABLE_NAME_PREFIX_LENGTH) ||
1709
1731
      (strncmp(old_db->str,
1713
1735
    my_error(ER_WRONG_USAGE, MYF(0),
1714
1736
             "ALTER DATABASE UPGRADE DATA DIRECTORY NAME",
1715
1737
             "name");
1716
 
    return(1);
 
1738
    DBUG_RETURN(1);
1717
1739
  }
1718
1740
 
1719
1741
  /* `#mysql50#<name>` converted to encoded `<name>` */
1722
1744
 
1723
1745
  if (lock_databases(thd, old_db->str, old_db->length,
1724
1746
                          new_db.str, new_db.length))
1725
 
    return(1);
 
1747
    DBUG_RETURN(1);
1726
1748
 
1727
1749
  /*
1728
1750
    Let's remember if we should do "USE newdb" afterwards.
1758
1780
      FILEINFO *file= dirp->dir_entry + idx;
1759
1781
      char *extension, tname[FN_REFLEN];
1760
1782
      LEX_STRING table_str;
 
1783
      DBUG_PRINT("info",("Examining: %s", file->name));
1761
1784
 
1762
1785
      /* skiping non-FRM files */
1763
1786
      if (my_strcasecmp(files_charset_info,
1818
1841
    {
1819
1842
      FILEINFO *file= dirp->dir_entry + idx;
1820
1843
      char oldname[FN_REFLEN], newname[FN_REFLEN];
 
1844
      DBUG_PRINT("info",("Examining: %s", file->name));
1821
1845
 
1822
1846
      /* skiping . and .. and MY_DB_OPT_FILE */
1823
1847
      if ((file->name[0] == '.' &&
1846
1870
  /* Step8: logging */
1847
1871
  if (mysql_bin_log.is_open())
1848
1872
  {
1849
 
    Query_log_event qinfo(thd, thd->query, thd->query_length, 0, true);
 
1873
    Query_log_event qinfo(thd, thd->query, thd->query_length, 0, TRUE);
1850
1874
    thd->clear_error();
1851
1875
    mysql_bin_log.write(&qinfo);
1852
1876
  }
1853
1877
 
1854
1878
  /* Step9: Let's do "use newdb" if we renamed the current database */
1855
1879
  if (change_to_newdb)
1856
 
    error|= mysql_change_db(thd, & new_db, false);
 
1880
    error|= mysql_change_db(thd, & new_db, FALSE);
1857
1881
 
1858
1882
exit:
1859
1883
  pthread_mutex_lock(&LOCK_lock_db);
1865
1889
  pthread_cond_signal(&COND_refresh);
1866
1890
  pthread_mutex_unlock(&LOCK_lock_db);
1867
1891
 
1868
 
  return(error);
 
1892
  DBUG_RETURN(error);
1869
1893
}
1870
1894
 
1871
1895
 
1878
1902
    db_name   database name
1879
1903
 
1880
1904
  RETURN VALUES
1881
 
    false   There is directory for the specified database name.
1882
 
    true    The directory does not exist.
 
1905
    FALSE   There is directory for the specified database name.
 
1906
    TRUE    The directory does not exist.
1883
1907
*/
1884
1908
 
1885
1909
bool check_db_dir_existence(const char *db_name)