RepCommandInterpreter.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:14k
源码类别:
MySQL数据库
开发平台:
Visual C++
- /* Copyright (C) 2003 MySQL AB
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
- #include "RepCommandInterpreter.hpp"
- static const char*
- helpTextRep =
- "+-------------------------------------------------------------------------+n"
- "| MySQL Replication Server |n"
- "| Commands should be executed on the standby Replication Server |n"
- "+-------------------------------------------------------------------------+n"
- "| Simple Commands |n"
- "+-------------------------------------------------------------------------+n"
- "| START Start replication |n"
- "| STATUS Show replication status |n"
- "+-------------------------------------------------------------------------+n"
- "| Advanced Commands |n"
- "+-------------------------------------------------------------------------+n"
- "| STOP <epoch no> Stop replication after epoch number <epoch no> |n"
- "| STOP IMMEDIATELY Stop replication after applying the current epoch |n"
- "| ADD TABLE <db>/<schema>/<tablename> |n"
- "| Note: <db>/<schema>/<tablename> is case sensitive! |n"
- "| Use 'STATUS' to see added tables. |n"
- "| REMOVE TABLE <db>/<schema>/<tablename> |n"
- "| Note: <db>/<schema>/<tablename> is case sensitive! |n"
- "| ENABLE <protocol> Starts protocol |n"
- "| DISABLE <protocol> Stops protocol |n"
- "| DEBUG Toggle logging of replication messages on console |n"
- "| |n"
- "| <protocol> ::= REQUESTOR | TRANSFER | APPLY | DELETE |n"
- "+-------------------------------------------------------------------------+n"
- ;
- /**
- * @todo
- "| <protocol> ::= SUBID | SUBSCRIPTION |n"
- "| <protocol> ::= METALOG | METASCAN | DATALOG | DATASCAN |n"
- "| <system> ::= PRIMARY | STANDBY | TWOWAY |n"
- "| CONNECT <system> Connects to NDB Cluster and other replication server |n"
- "| DELETE Removes all epochs stored in replication servers |n"
- "| DROP <tableid> Drops table in standby system identified by table id |n"
- "| <epoch> ::= Any integer (naming the last epoch to be applied) |n"
- */
- RepCommandInterpreter::RepCommandInterpreter(RepComponents * comps)
- {
- m_repComponents = comps;
- m_repState = comps->getRepState();
- }
- RepCommandInterpreter::~RepCommandInterpreter()
- {
- }
- /**
- * Read a string, and return a pointer to it.
- *
- * @return NULL on EOF.
- */
- char *
- RepCommandInterpreter::readline_gets() const
- {
- static char *line_read = (char *)NULL;
- // Disable the default file-name completion action of TAB
- // rl_bind_key ('t', rl_insert);
- /* If the buffer has already been allocated, return the memory
- to the free pool. */
- if (line_read)
- {
- NdbMem_Free(line_read);
- line_read = (char *)NULL;
- }
- /* Get a line from the user. */
- line_read = readline ("REP> ");
- /* If the line has any text in it, save it on the history. */
- if (line_read && *line_read)
- add_history (line_read);
- return (line_read);
- }
- bool emptyString(const char* s)
- {
- if (s == NULL) {
- return true;
- }
- for (unsigned int i = 0; i < strlen(s); ++i) {
- if (! isspace(s[i])) {
- return false;
- }
- }
- return true;
- }
- /**
- * Converts a string to a Uint32 pointed value!
- */
- bool convert(const char* s, Uint32 * val)
- {
- if (s == NULL) {
- return false;
- }
- if (strlen(s) == 0) {
- return false;
- }
- errno = 0;
- char* p;
- long v = strtol(s, &p, 10);
- if (errno != 0) {
- return false;
- }
- if (p != &s[strlen(s)]) {
- return false;
- }
- *val = v;
- return true;
- }
- void
- printError(GrepError::Code err)
- {
- if (err == GrepError::NO_ERROR) { ndbout << "Ok" << endl; }
- else { ndbout << GrepError::getErrorDesc(err) << endl; }
- }
- bool
- RepCommandInterpreter::readAndExecute()
- {
- GrepError::Code err;
- char* _line = readline_gets();
- char * line;
- if(_line == NULL) {
- ndbout << endl;
- return true;
- }
- line = strdup(_line);
- if (emptyString(line)) {
- return true;
- }
- /* I have to uncomment this, since otherwise <db>/<schema>/<table>
- is converted to capitals, but it is case sensitive!
- for (unsigned int i = 0; i < strlen(line); ++i) {
- line[i] = toupper(line[i]);
- }
- */
- // if there is anything in the line proceed
- char* firstToken = strtok(line, " ");
- for (unsigned int i = 0; i < strlen(firstToken); ++i) {
- firstToken[i] = toupper(firstToken[i]);
- }
- char* allAfterFirstToken = strtok(NULL, "