~skinny.moey/drizzle/innodb-replication

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Brian Aker
  • Date: 2010-11-08 22:35:57 UTC
  • mfrom: (1802.1.114 trunk)
  • Revision ID: brian@tangent.org-20101108223557-w3xzwp9hjjtjhtc1
MergeĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
118
118
  return (*str != '\0');
119
119
}
120
120
 
121
 
 
122
 
bool drizzled_show_create(Session *session, TableList *table_list, bool is_if_not_exists)
123
 
{
124
 
  char buff[2048];
125
 
  String buffer(buff, sizeof(buff), system_charset_info);
126
 
 
127
 
  /* Only one table for now, but VIEW can involve several tables */
128
 
  if (session->openTables(table_list))
129
 
  {
130
 
    if (session->is_error())
131
 
      return true;
132
 
 
133
 
    /*
134
 
      Clear all messages with 'error' level status and
135
 
      issue a warning with 'warning' level status in
136
 
      case of invalid view and last error is ER_VIEW_INVALID
137
 
    */
138
 
    drizzle_reset_errors(session, true);
139
 
    session->clear_error();
140
 
  }
141
 
 
142
 
  buffer.length(0);
143
 
 
144
 
  if (store_create_info(table_list, &buffer, is_if_not_exists))
145
 
    return true;
146
 
 
147
 
  List<Item> field_list;
148
 
  {
149
 
    field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
150
 
    // 1024 is for not to confuse old clients
151
 
    field_list.push_back(new Item_empty_string("Create Table",
152
 
                                               max(buffer.length(),(uint32_t)1024)));
153
 
  }
154
 
 
155
 
  if (session->client->sendFields(&field_list))
156
 
    return true;
157
 
  {
158
 
    session->client->store(table_list->table->getAlias());
159
 
  }
160
 
 
161
 
  session->client->store(buffer.ptr(), buffer.length());
162
 
 
163
 
  if (session->client->flush())
164
 
    return true;
165
 
 
166
 
  session->my_eof();
167
 
  return false;
168
 
}
169
 
 
170
 
/**
171
 
  Get a CREATE statement for a given database.
172
 
 
173
 
  The database is identified by its name, passed as @c dbname parameter.
174
 
  The name should be encoded using the system character set (UTF8 currently).
175
 
 
176
 
  Resulting statement is stored in the string pointed by @c buffer. The string
177
 
  is emptied first and its character set is set to the system character set.
178
 
 
179
 
  If is_if_not_exists is set, then
180
 
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
181
 
  in @c create_options are ignored.
182
 
 
183
 
  @param  session           The current thread instance.
184
 
  @param  dbname        The name of the database.
185
 
  @param  buffer        A String instance where the statement is stored.
186
 
  @param  create_info   If not NULL, the options member influences the resulting
187
 
                        CRATE statement.
188
 
 
189
 
  @returns true if errors are detected, false otherwise.
190
 
*/
191
 
 
192
 
static bool store_db_create_info(SchemaIdentifier &schema_identifier, string &buffer, bool if_not_exists)
193
 
{
194
 
  message::Schema schema;
195
 
 
196
 
  bool found= plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema);
197
 
  if (not found)
198
 
    return false;
199
 
 
200
 
  buffer.append("CREATE DATABASE ");
201
 
 
202
 
  if (if_not_exists)
203
 
    buffer.append("IF NOT EXISTS ");
204
 
 
205
 
  buffer.append("`");
206
 
  buffer.append(schema.name());
207
 
  buffer.append("`");
208
 
 
209
 
  if (schema.has_collation())
210
 
  {
211
 
    buffer.append(" COLLATE = ");
212
 
    buffer.append(schema.collation());
213
 
  }
214
 
 
215
 
  return true;
216
 
}
217
 
 
218
 
bool mysqld_show_create_db(Session &session, SchemaIdentifier &schema_identifier, bool if_not_exists)
219
 
{
220
 
  message::Schema schema_message;
221
 
  string buffer;
222
 
 
223
 
  if (not plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema_message))
224
 
  {
225
 
    /*
226
 
      This assumes that the only reason for which store_db_create_info()
227
 
      can fail is incorrect database name (which is the case now).
228
 
    */
229
 
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
230
 
    return true;
231
 
  }
232
 
 
233
 
  if (not store_db_create_info(schema_identifier, buffer, if_not_exists))
234
 
  {
235
 
    /*
236
 
      This assumes that the only reason for which store_db_create_info()
237
 
      can fail is incorrect database name (which is the case now).
238
 
    */
239
 
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
240
 
    return true;
241
 
  }
242
 
 
243
 
  List<Item> field_list;
244
 
  field_list.push_back(new Item_empty_string("Database",NAME_CHAR_LEN));
245
 
  field_list.push_back(new Item_empty_string("Create Database",1024));
246
 
 
247
 
  if (session.client->sendFields(&field_list))
248
 
    return true;
249
 
 
250
 
  session.client->store(schema_message.name());
251
 
  session.client->store(buffer);
252
 
 
253
 
  if (session.client->flush())
254
 
    return true;
255
 
 
256
 
  session.my_eof();
257
 
 
258
 
  return false;
259
 
}
260
 
 
261
121
/*
262
122
  Get the quote character for displaying an identifier.
263
123
 
283
143
  return '`';
284
144
}
285
145
 
286
 
 
287
 
#define LIST_PROCESS_HOST_LEN 64
288
 
 
289
 
/*
290
 
  Build a CREATE TABLE statement for a table.
291
 
 
292
 
  SYNOPSIS
293
 
    store_create_info()
294
 
    table_list        A list containing one table to write statement
295
 
                      for.
296
 
    packet            Pointer to a string where statement will be
297
 
                      written.
298
 
 
299
 
  NOTE
300
 
    Currently always return 0, but might return error code in the
301
 
    future.
302
 
 
303
 
  RETURN
304
 
    0       OK
305
 
 */
306
 
 
307
 
int store_create_info(TableList *table_list, String *packet, bool is_if_not_exists)
308
 
{
309
 
  Table *table= table_list->table;
310
 
 
311
 
  table->restoreRecordAsDefault(); // Get empty record
312
 
 
313
 
  string create_sql;
314
 
 
315
 
  enum drizzled::message::TransformSqlError transform_err;
316
 
 
317
 
  (void)is_if_not_exists;
318
 
 
319
 
  transform_err= message::transformTableDefinitionToSql(*(table->getShare()->getTableProto()),
320
 
                                                        create_sql,
321
 
                                                        message::DRIZZLE,
322
 
                                                        false);
323
 
 
324
 
  packet->append(create_sql.c_str(), create_sql.length(), default_charset_info);
325
 
 
326
 
  return(0);
327
 
}
328
 
 
329
 
/****************************************************************************
330
 
  Return info about all processes
331
 
  returns for each thread: thread id, user, host, db, command, info
332
 
****************************************************************************/
333
 
 
334
 
class thread_info
335
 
{
336
 
  thread_info();
337
 
public:
338
 
  uint64_t thread_id;
339
 
  time_t start_time;
340
 
  uint32_t   command;
341
 
  string user;
342
 
  string host;
343
 
  string db;
344
 
  string proc_info;
345
 
  string state_info;
346
 
  string query;
347
 
  thread_info(uint64_t thread_id_arg,
348
 
              time_t start_time_arg,
349
 
              uint32_t command_arg,
350
 
              const string &user_arg,
351
 
              const string &host_arg,
352
 
              const string &db_arg,
353
 
              const string &proc_info_arg,
354
 
              const string &state_info_arg,
355
 
              const string &query_arg)
356
 
    : thread_id(thread_id_arg), start_time(start_time_arg), command(command_arg),
357
 
      user(user_arg), host(host_arg), db(db_arg), proc_info(proc_info_arg),
358
 
      state_info(state_info_arg), query(query_arg)
359
 
  {}
360
 
};
361
 
 
362
146
} /* namespace drizzled */