Class: Og::MysqlStore
Overview
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;
Instance Attribute Summary
Attributes inherited from SqlStore
Attributes inherited from Store
#options, #transaction_nesting
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#commit ⇒ Object
Commit a transaction.
- #enchant(klass, manager) ⇒ Object
- #exec(sql) ⇒ Object
-
#initialize(options) ⇒ MysqlStore
constructor
Initialize the MySQL store.
- #query(sql) ⇒ Object
-
#rollback ⇒ Object
Rollback a transaction.
- #sql_update(sql) ⇒ Object
- #start ⇒ Object
Methods included from MysqlUtils
Methods included from SqlUtils
#blob, #build_join_name, #create_join_table_sql, #date, #escape, #join_class_ordering, #join_object_ordering, #join_table, #join_table_index, #join_table_info, #join_table_key, #join_table_keys, #ordered_join_table_keys, #parse_blob, #parse_boolean, #parse_date, #parse_float, #parse_int, #parse_timestamp, #quote, #table, #tableize, #timestamp
Methods inherited from SqlStore
#aggregate, #delete_all, #enable_logging, #find, #find_one, #join, #load, #managed_tables, #reload, #select, #select_one, #unjoin, #unmanaged_tables, #update, #update_by_sql, #update_properties
Methods inherited from Store
#count, #delete, #find, for_name, #force_save!, #insert, #load, #reload, #save, #transaction, #update, #update_properties
Constructor Details
#initialize(options) ⇒ MysqlStore
Initialize the MySQL store.
Options
-
:address, the addres 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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/og/store/mysql.rb', line 134 def initialize() super @typemap.update(TrueClass => 'tinyint', Time => 'datetime') @conn = Mysql.connect( [:address] || 'localhost', [:user], [:password], [:name], [:port], [:socket] ) # You should set recconect to true to avoid MySQL has # gone away errors. if @conn.respond_to? :reconnect [:reconnect] = true unless .has_key?(:reconnect) @conn.reconnect = [:reconnect] end rescue => ex if ex.errno == 1049 # database does not exist. Logger.info "Database '#{[:name]}' not found!" self.class.create() retry end raise end |
Class Method Details
.create(options) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/og/store/mysql.rb', line 109 def self.create() # gmosx: system is used to avoid shell expansion. system 'mysqladmin', '-f', "--user=#{[:user]}", "--password=#{[:password]}", 'create', [:name] super end |
.destroy(options) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/og/store/mysql.rb', line 117 def self.destroy() system 'mysqladmin', '-f', "--user=#{[:user]}", "--password=#{[:password]}", 'drop', [:name] super end |
Instance Method Details
#close ⇒ Object
165 166 167 168 |
# File 'lib/og/store/mysql.rb', line 165 def close @conn.close super end |
#commit ⇒ Object
Commit a transaction.
202 203 204 205 |
# File 'lib/og/store/mysql.rb', line 202 def commit # nop, not supported? # FIXME: InnoDB supports transactions. end |
#enchant(klass, manager) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/og/store/mysql.rb', line 170 def enchant(klass, manager) if klass.ann.self.primary_key.symbol == :oid unless klass.properties.include? :oid klass.property :oid, Fixnum, :sql => 'integer AUTO_INCREMENT PRIMARY KEY' end end super end |
#exec(sql) ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/og/store/mysql.rb', line 187 def exec(sql) Logger.debug sql if $DBG @conn.query_with_result = false @conn.query(sql) rescue => ex handle_sql_exception(ex, sql) end |
#query(sql) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/og/store/mysql.rb', line 179 def query(sql) Logger.debug sql if $DBG @conn.query_with_result = true return @conn.query(sql) rescue => ex handle_sql_exception(ex, sql) end |
#rollback ⇒ Object
Rollback a transaction.
209 210 211 212 |
# File 'lib/og/store/mysql.rb', line 209 def rollback # nop, not supported? # FIXME: InnoDB supports transactions. end |
#sql_update(sql) ⇒ Object
214 215 216 217 |
# File 'lib/og/store/mysql.rb', line 214 def sql_update(sql) exec(sql) @conn.affected_rows end |
#start ⇒ Object
195 196 197 198 |
# File 'lib/og/store/mysql.rb', line 195 def start # nop # FIXME: InnoDB supports transactions. end |