~linuxjedi/drizzle/trunk-bug-729642

« back to all changes in this revision

Viewing changes to plugin/slave/replication_schema.cc

  • Committer: Brian Aker
  • Date: 2011-02-25 17:02:30 UTC
  • mfrom: (2116.1.55 slave)
  • Revision ID: brian@tangent.org-20110225170230-zj0h32xlmr42ly2z
Merge in David's slave work

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
 
 
28
using namespace std;
 
29
using namespace drizzled;
 
30
 
 
31
namespace slave
 
32
{
 
33
 
 
34
bool ReplicationSchema::create()
 
35
{
 
36
  vector<string> sql;
 
37
 
 
38
  sql.push_back("COMMIT");
 
39
  sql.push_back("CREATE SCHEMA IF NOT EXISTS `sys_replication` REPLICATE=FALSE");
 
40
 
 
41
  if (not executeSQL(sql))
 
42
    return false;
 
43
 
 
44
  /*
 
45
   * Create our IO thread state information table if we need to.
 
46
   */
 
47
 
 
48
  sql.clear();
 
49
  sql.push_back("COMMIT");
 
50
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`io_state` ("
 
51
                " `status` VARCHAR(20) NOT NULL,"
 
52
                " `error_msg` VARCHAR(250))"
 
53
                " COMMENT = 'VERSION 1.0'");
 
54
 
 
55
  if (not executeSQL(sql))
 
56
    return false;
 
57
 
 
58
  sql.clear();
 
59
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`io_state`");
 
60
 
 
61
  {
 
62
    sql::ResultSet result_set(1);
 
63
    Execute execute(*(_session.get()), true);
 
64
    execute.run(sql[0], result_set);
 
65
    result_set.next();
 
66
    string count= result_set.getString(0);
 
67
 
 
68
    /* Must always be at least one row in the table */
 
69
    if (count == "0")
 
70
    {
 
71
      sql.clear();
 
72
      sql.push_back("INSERT INTO `sys_replication`.`io_state` (`status`)"
 
73
                    " VALUES ('STOPPED')");
 
74
      if (not executeSQL(sql))
 
75
        return false;
 
76
    }
 
77
  }
 
78
 
 
79
  /*
 
80
   * Create our applier thread state information table if we need to.
 
81
   */
 
82
 
 
83
  sql.clear();
 
84
  sql.push_back("COMMIT");
 
85
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`applier_state`"
 
86
                " (`last_applied_commit_id` BIGINT NOT NULL PRIMARY KEY,"
 
87
                " `status` VARCHAR(20) NOT NULL,"
 
88
                " `error_msg` VARCHAR(250))"
 
89
                " COMMENT = 'VERSION 1.0'");
 
90
 
 
91
  if (not executeSQL(sql))
 
92
    return false;
 
93
 
 
94
  sql.clear();
 
95
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`applier_state`");
 
96
 
 
97
  {
 
98
    sql::ResultSet result_set(1);
 
99
    Execute execute(*(_session.get()), true);
 
100
    execute.run(sql[0], result_set);
 
101
    result_set.next();
 
102
    string count= result_set.getString(0);
 
103
 
 
104
    /* Must always be at least one row in the table */
 
105
    if (count == "0")
 
106
    {
 
107
      sql.clear();
 
108
      sql.push_back("INSERT INTO `sys_replication`.`applier_state`"
 
109
                    " (`last_applied_commit_id`, `status`)"
 
110
                    " VALUES (0, 'STOPPED')");
 
111
      if (not executeSQL(sql))
 
112
        return false;
 
113
    }
 
114
  }
 
115
 
 
116
  /*
 
117
   * Create our message queue table if we need to.
 
118
   */
 
119
 
 
120
  sql.clear();
 
121
  sql.push_back("COMMIT");
 
122
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`queue`"
 
123
                " (`trx_id` BIGINT NOT NULL, `seg_id` INT NOT NULL,"
 
124
                " `commit_order` BIGINT, `msg` BLOB,"
 
125
                " PRIMARY KEY(`trx_id`, `seg_id`))"
 
126
                " COMMENT = 'VERSION 1.0'");
 
127
  if (not executeSQL(sql))
 
128
    return false;
 
129
 
 
130
  return true;
 
131
}
 
132
 
 
133
} /* namespace slave */