~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

fixed build warnings

Show diffs side-by-side

added added

removed removed

Lines of Context:
368
368
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
369
369
 
370
370
static int read_and_execute(bool interactive);
371
 
static int sql_connect(const string &host, const string &database, const string &user, const string &password,
372
 
                       uint32_t silent);
 
371
static int sql_connect(const string &host, const string &database, const string &user, const string &password);
373
372
static const char *server_version_string(drizzle_con_st *con);
374
373
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
375
374
                    const char *sql_state);
491
490
  Commands( "tee",    'T', com_tee,    1,
492
491
    N_("Set outfile [to_outfile]. Append everything into given outfile.") ),
493
492
  Commands( "use",    'u', com_use,    1,
494
 
    N_("Use another database. Takes database name as argument.") ),
 
493
    N_("Use another schema. Takes schema name as argument.") ),
495
494
  Commands( "shutdown",    'u', com_shutdown,    1,
496
495
    N_("Shutdown the instance you are connected too.") ),
497
496
  Commands( "warnings", 'W', com_warnings,  0,
1490
1489
  if (! isatty(0) || ! isatty(1))
1491
1490
  {
1492
1491
    status.setBatch(1); opt_silent=1;
1493
 
    ignore_errors=0;
1494
1492
  }
1495
1493
  else
1496
1494
    status.setAddToHistory(1);
1667
1665
  }
1668
1666
  if (vm.count("silent"))
1669
1667
  {
1670
 
    opt_silent++;
 
1668
    opt_silent= 2;
1671
1669
  }
1672
1670
  
1673
1671
  if (vm.count("help") || vm.count("version"))
1683
1681
           "This is free software,\n"
1684
1682
           "and you are welcome to modify and redistribute it "
1685
1683
           "under the GPL license\n"));
1686
 
    printf(_("Usage: drizzle [OPTIONS] [database]\n"));
 
1684
    printf(_("Usage: drizzle [OPTIONS] [schema]\n"));
1687
1685
    cout << long_options;
1688
1686
    exit(0);
1689
1687
  }
1695
1693
  }
1696
1694
 
1697
1695
  memset(&drizzle, 0, sizeof(drizzle));
1698
 
  if (sql_connect(current_host, current_db, current_user, opt_password,opt_silent))
 
1696
  if (sql_connect(current_host, current_db, current_user, opt_password))
1699
1697
  {
1700
1698
    quick= 1;          // Avoid history
1701
1699
    status.setExitStatus(1);
2537
2535
  drizzle_return_t ret;
2538
2536
  drizzle_result_st databases,tables,fields;
2539
2537
  drizzle_row_t database_row,table_row;
2540
 
  drizzle_column_st *sql_field;
2541
2538
  string tmp_str, tmp_str_lower;
 
2539
  std::string query;
2542
2540
 
2543
2541
  if (status.getBatch() || quick || current_db.empty())
2544
2542
    return;      // We don't need completion in batches
2558
2556
  /* hash Drizzle functions (to be implemented) */
2559
2557
 
2560
2558
  /* hash all database names */
2561
 
  if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
 
2559
  if (drizzle_query_str(&con, &databases, "select schema_name from information_schema.schemata", &ret) != NULL)
2562
2560
  {
2563
2561
    if (ret == DRIZZLE_RETURN_OK)
2564
2562
    {
2578
2576
    drizzle_result_free(&databases);
2579
2577
  }
2580
2578
 
2581
 
  /* hash all table names */
2582
 
  if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
 
2579
  query= "select table_name, column_name from information_schema.columns where table_schema='";
 
2580
  query.append(current_db);
 
2581
  query.append("' order by table_name");
 
2582
  
 
2583
  if (drizzle_query(&con, &fields, query.c_str(), query.length(),
 
2584
                    &ret) != NULL)
2583
2585
  {
2584
 
    if (ret != DRIZZLE_RETURN_OK)
2585
 
    {
2586
 
      drizzle_result_free(&tables);
2587
 
      return;
2588
 
    }
2589
 
 
2590
 
    if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
2591
 
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2592
 
    else
 
2586
    if (ret == DRIZZLE_RETURN_OK &&
 
2587
        drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2593
2588
    {
2594
2589
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2595
2590
      {
2599
2594
                      "You can turn off this feature to get a quicker "
2600
2595
                      "startup with -A\n\n"));
2601
2596
      }
2602
 
      while ((table_row=drizzle_row_next(&tables)))
 
2597
 
 
2598
      std::string table_name;
 
2599
      while ((table_row=drizzle_row_next(&fields)))
2603
2600
      {
 
2601
        if (table_name.compare(table_row[0]) != 0)
 
2602
        {
 
2603
          tmp_str= table_row[0];
 
2604
          tmp_str_lower= lower_string(tmp_str);
 
2605
          completion_map[tmp_str_lower]= tmp_str;
 
2606
          table_name= table_row[0];
 
2607
        }
2604
2608
        tmp_str= table_row[0];
2605
 
        tmp_str_lower= lower_string(tmp_str);
2606
 
        completion_map[tmp_str_lower]= tmp_str;
2607
 
      }
2608
 
    }
2609
 
  }
2610
 
  else
2611
 
    return;
2612
 
 
2613
 
  /* hash all field names, both with the table prefix and without it */
2614
 
  if (drizzle_result_row_count(&tables) == 0)
2615
 
  {
2616
 
    drizzle_result_free(&tables);
2617
 
    return;
2618
 
  }
2619
 
 
2620
 
  drizzle_row_seek(&tables, 0);
2621
 
 
2622
 
  while ((table_row=drizzle_row_next(&tables)))
2623
 
  {
2624
 
    string query;
2625
 
 
2626
 
    query.append("show fields in `");
2627
 
    query.append(table_row[0]);
2628
 
    query.append("`");
2629
 
    
2630
 
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2631
 
                      &ret) != NULL)
2632
 
    {
2633
 
      if (ret == DRIZZLE_RETURN_OK &&
2634
 
          drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2635
 
      {
2636
 
        while ((sql_field=drizzle_column_next(&fields)))
2637
 
        {
2638
 
          tmp_str=table_row[0];
2639
 
          tmp_str.append(".");
2640
 
          tmp_str.append(drizzle_column_name(sql_field));
2641
 
          tmp_str_lower= lower_string(tmp_str);
2642
 
          completion_map[tmp_str_lower]= tmp_str;
2643
 
 
2644
 
          tmp_str=drizzle_column_name(sql_field);
2645
 
          tmp_str_lower= lower_string(tmp_str);
2646
 
          completion_map[tmp_str_lower]= tmp_str;
2647
 
        }
2648
 
      }
2649
 
      drizzle_result_free(&fields);
2650
 
    }
2651
 
  }
2652
 
  drizzle_result_free(&tables);
 
2609
        tmp_str.append(".");
 
2610
        tmp_str.append(table_row[1]);
 
2611
        tmp_str_lower= lower_string(tmp_str);
 
2612
        completion_map[tmp_str_lower]= tmp_str;
 
2613
 
 
2614
        tmp_str= table_row[1];
 
2615
        tmp_str_lower= lower_string(tmp_str);
 
2616
        completion_map[tmp_str_lower]= tmp_str;
 
2617
      }
 
2618
    }
 
2619
  }
 
2620
  drizzle_result_free(&fields);
2653
2621
  completion_iter= completion_map.begin();
2654
2622
}
2655
2623
 
3086
3054
  {
3087
3055
    tee_fprintf(PAGER, _("Field %3u:  `%s`\n"
3088
3056
                "Catalog:    `%s`\n"
3089
 
                "Database:   `%s`\n"
 
3057
                "Schema:     `%s`\n"
3090
3058
                "Table:      `%s`\n"
3091
3059
                "Org_table:  `%s`\n"
3092
3060
                "Type:       UTF-8\n"
3725
3693
  }
3726
3694
  else
3727
3695
    opt_rehash= 0;
3728
 
  error=sql_connect(current_host, current_db, current_user, opt_password,0);
 
3696
  error=sql_connect(current_host, current_db, current_user, opt_password);
3729
3697
  opt_rehash= save_rehash;
3730
3698
 
3731
3699
  if (connected)
3732
3700
  {
3733
3701
    sprintf(buff, _("Connection id:    %u"), drizzle_con_thread_id(&con));
3734
3702
    put_info(buff,INFO_INFO,0,0);
3735
 
    sprintf(buff, _("Current database: %.128s\n"),
 
3703
    sprintf(buff, _("Current schema: %.128s\n"),
3736
3704
            !current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
3737
3705
    put_info(buff,INFO_INFO,0,0);
3738
3706
  }
3845
3813
  tmp= get_arg(buff, 0);
3846
3814
  if (!tmp || !*tmp)
3847
3815
  {
3848
 
    put_info(_("USE must be followed by a database name"), INFO_ERROR, 0, 0);
 
3816
    put_info(_("USE must be followed by a schema name"), INFO_ERROR, 0, 0);
3849
3817
    return 0;
3850
3818
  }
3851
3819
  /*
3913
3881
      build_completion_hash(opt_rehash, 1);
3914
3882
  }
3915
3883
 
3916
 
  put_info(_("Database changed"),INFO_INFO, 0, 0);
 
3884
  put_info(_("Schema changed"),INFO_INFO, 0, 0);
3917
3885
  return 0;
3918
3886
}
3919
3887
 
4034
4002
 
4035
4003
 
4036
4004
static int
4037
 
sql_connect(const string &host, const string &database, const string &user, const string &password,
4038
 
                 uint32_t silent)
 
4005
sql_connect(const string &host, const string &database, const string &user, const string &password)
4039
4006
{
4040
4007
  drizzle_return_t ret;
4041
4008
  if (connected)
4084
4051
*/
4085
4052
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
4086
4053
  {
4087
 
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
4088
 
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
 
4054
 
 
4055
    if (opt_silent < 2)
4089
4056
    {
4090
4057
      (void) put_error(&con, NULL);
4091
4058
      (void) fflush(stdout);
4130
4097
      drizzle_row_t cur=drizzle_row_next(&result);
4131
4098
      if (cur)
4132
4099
      {
4133
 
        tee_fprintf(stdout, _("Current database:\t%s\n"), cur[0] ? cur[0] : "");
 
4100
        tee_fprintf(stdout, _("Current schema:\t%s\n"), cur[0] ? cur[0] : "");
4134
4101
        tee_fprintf(stdout, _("Current user:\t\t%s\n"), cur[1]);
4135
4102
      }
4136
4103
      drizzle_result_free(&result);
4149
4116
  if (skip_updates)
4150
4117
  {
4151
4118
    vidattr(A_BOLD);
4152
 
    tee_fprintf(stdout, _("\nAll updates ignored to this database\n"));
 
4119
    tee_fprintf(stdout, _("\nAll updates ignored to this schema\n"));
4153
4120
    vidattr(A_NORMAL);
4154
4121
  }
4155
4122
  tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());