~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/slave/replication_schema.cc

  • Committer: Lee Bieber
  • Date: 2011-03-13 16:37:38 UTC
  • mfrom: (2227.4.18 session2)
  • Revision ID: kalebral@gmail.com-20110313163738-7ti21zk40o2xi3ew
Merge Olaf - Refactor Session

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2011 David Shrewsbury
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <plugin/slave/replication_schema.h>
 
23
#include <drizzled/execute.h>
 
24
#include <drizzled/sql/result_set.h>
 
25
#include <string>
 
26
#include <vector>
 
27
#include <boost/lexical_cast.hpp>
 
28
 
 
29
using namespace std;
 
30
using namespace drizzled;
 
31
using namespace boost;
 
32
 
 
33
namespace slave
 
34
{
 
35
 
 
36
bool ReplicationSchema::create()
 
37
{
 
38
  vector<string> sql;
 
39
 
 
40
  sql.push_back("COMMIT");
 
41
  sql.push_back("CREATE SCHEMA IF NOT EXISTS `sys_replication` REPLICATE=FALSE");
 
42
 
 
43
  if (not executeSQL(sql))
 
44
    return false;
 
45
 
 
46
  /*
 
47
   * Create our IO thread state information table if we need to.
 
48
   */
 
49
 
 
50
  sql.clear();
 
51
  sql.push_back("COMMIT");
 
52
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`io_state` ("
 
53
                " `status` VARCHAR(20) NOT NULL,"
 
54
                " `error_msg` VARCHAR(250))"
 
55
                " COMMENT = 'VERSION 1.0'");
 
56
 
 
57
  if (not executeSQL(sql))
 
58
    return false;
 
59
 
 
60
  sql.clear();
 
61
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`io_state`");
 
62
 
 
63
  {
 
64
    sql::ResultSet result_set(1);
 
65
    Execute execute(*(_session.get()), true);
 
66
    execute.run(sql[0], result_set);
 
67
    result_set.next();
 
68
    string count= result_set.getString(0);
 
69
 
 
70
    /* Must always be at least one row in the table */
 
71
    if (count == "0")
 
72
    {
 
73
      sql.clear();
 
74
      sql.push_back("INSERT INTO `sys_replication`.`io_state` (`status`)"
 
75
                    " VALUES ('STOPPED')");
 
76
      if (not executeSQL(sql))
 
77
        return false;
 
78
    }
 
79
  }
 
80
 
 
81
  /*
 
82
   * Create our applier thread state information table if we need to.
 
83
   */
 
84
 
 
85
  sql.clear();
 
86
  sql.push_back("COMMIT");
 
87
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`applier_state`"
 
88
                " (`last_applied_commit_id` BIGINT NOT NULL PRIMARY KEY,"
 
89
                " `status` VARCHAR(20) NOT NULL,"
 
90
                " `error_msg` VARCHAR(250))"
 
91
                " COMMENT = 'VERSION 1.0'");
 
92
 
 
93
  if (not executeSQL(sql))
 
94
    return false;
 
95
 
 
96
  sql.clear();
 
97
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`applier_state`");
 
98
 
 
99
  {
 
100
    sql::ResultSet result_set(1);
 
101
    Execute execute(*(_session.get()), true);
 
102
    execute.run(sql[0], result_set);
 
103
    result_set.next();
 
104
    string count= result_set.getString(0);
 
105
 
 
106
    /* Must always be at least one row in the table */
 
107
    if (count == "0")
 
108
    {
 
109
      sql.clear();
 
110
      sql.push_back("INSERT INTO `sys_replication`.`applier_state`"
 
111
                    " (`last_applied_commit_id`, `status`)"
 
112
                    " VALUES (0, 'STOPPED')");
 
113
      if (not executeSQL(sql))
 
114
        return false;
 
115
    }
 
116
  }
 
117
 
 
118
  /*
 
119
   * Create our message queue table if we need to.
 
120
   */
 
121
 
 
122
  sql.clear();
 
123
  sql.push_back("COMMIT");
 
124
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`queue`"
 
125
                " (`trx_id` BIGINT NOT NULL, `seg_id` INT NOT NULL,"
 
126
                " `commit_order` BIGINT, `msg` BLOB,"
 
127
                " PRIMARY KEY(`trx_id`, `seg_id`))"
 
128
                " COMMENT = 'VERSION 1.0'");
 
129
  if (not executeSQL(sql))
 
130
    return false;
 
131
 
 
132
  return true;
 
133
}
 
134
 
 
135
bool ReplicationSchema::setInitialMaxCommitId(uint64_t value)
 
136
{
 
137
  vector<string> sql;
 
138
 
 
139
  sql.push_back("UPDATE `sys_replication`.`applier_state`"
 
140
                " SET `last_applied_commit_id` = "
 
141
                + lexical_cast<string>(value));
 
142
 
 
143
  return executeSQL(sql);
 
144
}
 
145
 
 
146
} /* namespace slave */