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, #quote_array, #table, #tableize, #timestamp
Methods inherited from SqlStore
#aggregate, #count, #delete_all, #enable_logging, #find, #find_one, #join, #load, #managed_tables, #reload, #select, #select_one, #type_cast, #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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/og/store/mysql.rb', line 118 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
91 92 93 94 95 96 97 98 |
# File 'lib/og/store/mysql.rb', line 91 def self.create() # gmosx: system is used to avoid shell expansion. system 'mysqladmin', '-f', "--user=#{[:user]}", "--password=#{[:password]}", "--host=#{[:address]}", 'create', [:name] super end |
.destroy(options) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/og/store/mysql.rb', line 100 def self.destroy() system 'mysqladmin', '-f', "--user=#{[:user]}", "--password=#{[:password]}", 'drop', "--host=#{[:address]}", [:name] super end |
Instance Method Details
#close ⇒ Object
149 150 151 152 |
# File 'lib/og/store/mysql.rb', line 149 def close @conn.close super end |
#commit ⇒ Object
Commit a transaction.
186 187 188 189 |
# File 'lib/og/store/mysql.rb', line 186 def commit # nop, not supported? # FIXME: InnoDB supports transactions. end |
#enchant(klass, manager) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/og/store/mysql.rb', line 154 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
171 172 173 174 175 176 177 |
# File 'lib/og/store/mysql.rb', line 171 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
163 164 165 166 167 168 169 |
# File 'lib/og/store/mysql.rb', line 163 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.
193 194 195 196 |
# File 'lib/og/store/mysql.rb', line 193 def rollback # nop, not supported? # FIXME: InnoDB supports transactions. end |
#sql_update(sql) ⇒ Object
198 199 200 201 |
# File 'lib/og/store/mysql.rb', line 198 def sql_update(sql) exec(sql) @conn.affected_rows end |
#start ⇒ Object
179 180 181 182 |
# File 'lib/og/store/mysql.rb', line 179 def start # nop # FIXME: InnoDB supports transactions. end |