Class: Og::MysqlAdapter
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
Instance Method Summary collapse
- #close ⇒ Object
-
#commit ⇒ Object
Commit a transaction.
-
#create_db(options) ⇒ Object
Create a database.
-
#destroy_db(options) ⇒ Object
Drop a database.
- #exec_statement(sql) ⇒ Object
-
#initialize(options) ⇒ MysqlAdapter
constructor
Initialize the MySQL store.
-
#last_insert_id(klass = nil) ⇒ Object
Return the last inserted row id.
-
#primary_key_type ⇒ Object
The type used for default primary keys.
- #query_statement(sql) ⇒ Object
-
#rollback ⇒ Object
Rollback a transaction.
-
#sql_update(sql) ⇒ Object
Perform an sql update, return the number of updated rows.
-
#start ⇒ Object
Start a transaction.
-
#table_info(table) ⇒ Object
Returns the MySQL information of a table within the database or nil if it doesn’t exist.
- #write_attr(s, a) ⇒ 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, #create_field_map, #create_table, #delete_all, #drop_table, #enchant, #eval_og_allocate, #eval_og_create_schema, #eval_og_delete, #eval_og_insert, #eval_og_read, #eval_og_update, #exec, #field_for_attribute, #field_sql_for_attribute, #fields_for_class, #find, #find_one, #force_primary_key, #handle_sql_exception, #insert_sql, #join, #load, #pk_field, #prepare_statement, #query, #read_all, #read_attr, #read_field, #read_join_relations, #read_one, #read_row, #reload, #resolve_limit_options, #resolve_options, #select, #select_one, #serializable_attributes_for_class, #sql_indices_for_class, #sql_type_for_class, #table_exists?, #type_cast, #unjoin, #update, #update_by_sql, #update_condition
Methods inherited from Store
#count, #delete, #delete_all, #enchant, #find, #force_save!, #insert, #load, #reload, #save, #transaction, #update, #update_attributes
Constructor Details
#initialize(options) ⇒ MysqlAdapter
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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/og/adapter/mysql.rb', line 52 def initialize() super @typemap.update(TrueClass => 'tinyint', Time => 'datetime') @conn = Mysql.connect( [:address] || [:host] || '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 Object => ex if database_does_not_exist_exception? ex Logger.info "Database '#{[:name]}' not found!" create_db() retry end raise end |
Instance Method Details
#close ⇒ Object
84 85 86 87 |
# File 'lib/og/adapter/mysql.rb', line 84 def close @conn.close super end |
#commit ⇒ Object
Commit a transaction.
150 151 152 153 |
# File 'lib/og/adapter/mysql.rb', line 150 def commit # nop on myISAM based tables exec_statement "COMMIT" end |
#create_db(options) ⇒ Object
Create a database.
91 92 93 94 95 96 97 98 99 |
# File 'lib/og/adapter/mysql.rb', line 91 def create_db() # gmosx: system is used to avoid shell expansion. system 'mysqladmin', '-f', "--user=#{[:user]}", "--password=#{[:password]}", "--host=#{[:address]}", "--port=#{.fetch(:port, 3306)}" , 'create', [:name] super end |
#destroy_db(options) ⇒ Object
Drop a database.
103 104 105 106 107 108 109 110 |
# File 'lib/og/adapter/mysql.rb', line 103 def destroy_db() system 'mysqladmin', '-f', "--user=#{[:user]}", "--password=#{[:password]}", "--host=#{[:address]}", "--port=#{.fetch(:port, 3306)}" , 'drop', [:name] super end |
#exec_statement(sql) ⇒ Object
123 124 125 126 |
# File 'lib/og/adapter/mysql.rb', line 123 def exec_statement(sql) @conn.query_with_result = false @conn.query(sql) end |
#last_insert_id(klass = nil) ⇒ Object
Return the last inserted row id.
137 138 139 |
# File 'lib/og/adapter/mysql.rb', line 137 def last_insert_id(klass = nil) @conn.insert_id end |
#primary_key_type ⇒ Object
The type used for default primary keys.
114 115 116 |
# File 'lib/og/adapter/mysql.rb', line 114 def primary_key_type 'integer AUTO_INCREMENT PRIMARY KEY' end |
#query_statement(sql) ⇒ Object
118 119 120 121 |
# File 'lib/og/adapter/mysql.rb', line 118 def query_statement(sql) @conn.query_with_result = true return @conn.query(sql) end |
#rollback ⇒ Object
Rollback a transaction.
157 158 159 160 |
# File 'lib/og/adapter/mysql.rb', line 157 def rollback # nop on myISAM based tables exec_statement "ROLLBACK" end |
#sql_update(sql) ⇒ Object
Perform an sql update, return the number of updated rows.
130 131 132 133 |
# File 'lib/og/adapter/mysql.rb', line 130 def sql_update(sql) exec(sql) @conn.affected_rows end |
#start ⇒ Object
Start a transaction.
143 144 145 146 |
# File 'lib/og/adapter/mysql.rb', line 143 def start # nop on myISAM based tables exec_statement "START TRANSACTION" end |
#table_info(table) ⇒ Object
Returns the MySQL information of a table within the database or nil if it doesn’t exist. Mostly for internal usage.
195 196 197 198 |
# File 'lib/og/adapter/mysql.rb', line 195 def table_info(table) r = query_statement("SHOW TABLE STATUS FROM #{@options[:name]} LIKE '#{self.class.escape(table.to_s)}'") return r && r.blank? ? nil : r.next end |
#write_attr(s, a) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/og/adapter/mysql.rb', line 162 def write_attr(s, a) store = self.class if a.class.ancestor? Integer "#\{@#{s} || 'NULL'\}" elsif a.class.ancestor? Float "#\{@#{s} || 'NULL'\}" elsif a.class.ancestor? String %|#\{@#{s} ? "'#\{#{store}.escape(@#{s})\}'" : 'NULL'\}| elsif a.class.ancestor? Time %|#\{@#{s} ? "'#\{#{store}.timestamp(@#{s})\}'" : 'NULL'\}| elsif a.class.ancestor? Date %|#\{@#{s} ? "'#\{#{store}.date(@#{s})\}'" : 'NULL'\}| elsif a.class.ancestor? TrueClass "#\{@#{s} ? \"'1'\" : 'NULL' \}" elsif a.class.ancestor? Og::Blob %|#\{@#{s} ? "'#\{#{store}.escape(#{store}.blob(@#{s}))\}'" : 'NULL'\}| else # keep the '' for nil symbols. %|#\{@#{s} ? "'#\{#{store}.escape(@#{s}.to_yaml)\}'" : "''"\}| end end |