A Store that persists objects into a MySQL database. To read documentation about the methods, consult the documentation for SqlStore and Store.
Here is some useful code to initialize your MySQL RDBMS for development. You probably want to be more careful with provileges on your production environment.
mysql> GRANT ALL PRIVELEGES ON keystone.* TO <$sys_dbuser name>@localhost IDENTIFIED BY ’(password)’ WITH GRANT OPTION;
- close
- commit
- create_db
- destroy_db
- exec_statement
- last_insert_id
- new
- primary_key_type
- query_statement
- rollback
- sql_update
- start
- table_info
- write_attr_boolean
Initialize the MySQL store.
Options
- :name, the name of the database.
- :user, the username for using the database.
- :password, the password of the database user.
- :address, the addres where the server is listening.
- :port, the port where the server is listening.
- :socket, is useful when the pure ruby driver is used.
this is the location of mysql.sock. For Ubuntu/Debian this is '/var/run/mysqld/mysqld.sock'. You can find the location for your system in my.cnf
[ show source ]
# File lib/og/adapter/mysql.rb, line 52
52: def initialize(options)
53: super
54:
55: @typemap.update(TrueClass => 'tinyint', Time => 'datetime')
56:
57: @conn = Mysql.connect(
58: options[:address] || options[:host] || 'localhost',
59: options[:user],
60: options[:password],
61: options[:name],
62: options[:port],
63: options[:socket]
64: )
65:
66: # You should set recconect to true to avoid MySQL has
67: # gone away errors.
68:
69: if @conn.respond_to? :reconnect
70: options[:reconnect] = true unless options.has_key?(:reconnect)
71: @conn.reconnect = options[:reconnect]
72: end
73:
74: rescue Object => ex
75: if database_does_not_exist_exception? ex
76: Logger.info "Database '#{options[:name]}' not found!"
77: create_db(options)
78: retry
79: end
80:
81: raise
82: end
[ show source ]
# File lib/og/adapter/mysql.rb, line 84
84: def close
85: @conn.close
86: super
87: end
Commit a transaction.
[ show source ]
# File lib/og/adapter/mysql.rb, line 150
150: def commit
151: # nop on myISAM based tables
152: exec_statement "COMMIT"
153: end
Create a database.
[ show source ]
# File lib/og/adapter/mysql.rb, line 91
91: def create_db(options)
92: # gmosx: system is used to avoid shell expansion.
93: system 'mysqladmin', '-f', "--user=#{options[:user]}",
94: "--password=#{options[:password]}",
95: "--host=#{options[:address]}",
96: "--port=#{options.fetch(:port, 3306)}" ,
97: 'create', options[:name]
98: super
99: end
Drop a database.
[ show source ]
# File lib/og/adapter/mysql.rb, line 103
103: def destroy_db(options)
104: system 'mysqladmin', '-f', "--user=#{options[:user]}",
105: "--password=#{options[:password]}",
106: "--host=#{options[:address]}",
107: "--port=#{options.fetch(:port, 3306)}" ,
108: 'drop', options[:name]
109: super
110: end
[ show source ]
# File lib/og/adapter/mysql.rb, line 123
123: def exec_statement(sql)
124: @conn.query_with_result = false
125: @conn.query(sql)
126: end
Return the last inserted row id.
[ show source ]
# File lib/og/adapter/mysql.rb, line 137
137: def last_insert_id(klass = nil)
138: @conn.insert_id
139: end
The type used for default primary keys.
[ show source ]
# File lib/og/adapter/mysql.rb, line 114
114: def primary_key_type
115: "integer AUTO_INCREMENT PRIMARY KEY"
116: end
[ show source ]
# File lib/og/adapter/mysql.rb, line 118
118: def query_statement(sql)
119: @conn.query_with_result = true
120: return @conn.query(sql)
121: end
Rollback a transaction.
[ show source ]
# File lib/og/adapter/mysql.rb, line 157
157: def rollback
158: # nop on myISAM based tables
159: exec_statement "ROLLBACK"
160: end
Perform an sql update, return the number of updated rows.
[ show source ]
# File lib/og/adapter/mysql.rb, line 130
130: def sql_update(sql)
131: exec(sql)
132: @conn.affected_rows
133: end
Start a transaction.
[ show source ]
# File lib/og/adapter/mysql.rb, line 143
143: def start
144: # nop on myISAM based tables
145: exec_statement "START TRANSACTION"
146: end
Returns the MySQL information of a table within the database or nil if it doesn’t exist. Mostly for internal usage.
[ show source ]
# File lib/og/adapter/mysql.rb, line 168
168: def table_info(table)
169: r = query_statement("SHOW TABLE STATUS FROM #{@options[:name]} LIKE '#{self.class.escape(table.to_s)}'")
170: return r && r.blank? ? nil : r.next
171: end
[ show source ]
# File lib/og/adapter/mysql.rb, line 162
162: def write_attr_boolean(value)
163: return value ? "'1'" : "NULL"
164: end