Class: Og::MysqlStore

Inherits:
SqlStore show all
Extended by:
MysqlUtils
Includes:
MysqlUtils
Defined in:
lib/og/store/mysql.rb

Overview

A Store that persists objects into a MySQL database. To read documentation about the methods, consult the documentation for SqlStore and Store.

Instance Attribute Summary

Attributes inherited from SqlStore

#conn

Attributes inherited from Store

#options, #transaction_nesting

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MysqlUtils

escape, quote

Methods included from SqlUtils

#date, #escape, #join_table, #parse_date, #parse_float, #parse_int, #parse_timestamp, #quote, #table, #timestamp

Methods inherited from SqlStore

#count, #enable_logging, #find, #find_one, #join, #load, #reload, #unjoin, #update, #update_properties

Methods inherited from Store

#count, #delete, #find, for_name, #insert, #load, #reload, #save, #transaction, #update, #update_properties

Constructor Details

#initialize(options) ⇒ MysqlStore

Returns a new instance of MysqlStore.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/og/store/mysql.rb', line 87

def initialize(options)
	super

	@typemap.update(TrueClass => 'tinyint')

	@conn = Mysql.connect(
		options[:address] || 'localhost', 
		options[:user],
		options[:password], 
		options[:name]
	)
rescue => ex
	if ex.errno == 1049 # database does not exist.
		Logger.info "Database '#{options[:name]}' not found!"
		self.class.create(options)
		retry
	end
	raise
end

Class Method Details

.create(options) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/og/store/mysql.rb', line 72

def self.create(options)
	# gmosx: system is used to avoid shell expansion.
	system 'mysqladmin', '-f', "--user=#{options[:user]}", 
			"--password=#{options[:password]}", 
			'create', options[:name]		
	super
end

.destroy(options) ⇒ Object



80
81
82
83
84
85
# File 'lib/og/store/mysql.rb', line 80

def self.destroy(options)
	system 'mysqladmin', '-f', "--user=#{options[:user]}", 
			"--password=#{options[:password]}", 'drop', 
			options[:name]
	super
end

Instance Method Details

#closeObject



107
108
109
110
# File 'lib/og/store/mysql.rb', line 107

def close
	@conn.close
	super
end

#commitObject

Commit a transaction.



140
141
142
143
# File 'lib/og/store/mysql.rb', line 140

def commit
	# nop, not supported?
	# FIXME: InnoDB supports transactions.
end

#enchant(klass, manager) ⇒ Object



112
113
114
115
# File 'lib/og/store/mysql.rb', line 112

def enchant(klass, manager)
	klass.property :oid, Fixnum, :sql => 'integer AUTO_INCREMENT PRIMARY KEY'
	super
end

#exec(sql) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/og/store/mysql.rb', line 125

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



117
118
119
120
121
122
123
# File 'lib/og/store/mysql.rb', line 117

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

#rollbackObject

Rollback a transaction.



147
148
149
150
# File 'lib/og/store/mysql.rb', line 147

def rollback
	# nop, not supported?
	# FIXME: InnoDB supports transactions.
end

#startObject



133
134
135
136
# File 'lib/og/store/mysql.rb', line 133

def start	
	# nop
	# FIXME: InnoDB supports transactions.
end