Class: Og::MysqlConnection

Inherits:
Connection show all
Defined in:
lib/og/adapters/mysql.rb

Overview

The MySQL connection.

Instance Attribute Summary

Attributes inherited from Connection

#db, #store

Instance Method Summary collapse

Methods inherited from Connection

#count, #create_table, #delete, #drop_table, #insert, #load, #load_all, #load_by_name, #load_by_oid, #save, #select, #select_one, #transaction, #update, #update_properties

Constructor Details

#initialize(db) ⇒ MysqlConnection

Returns a new instance of MysqlConnection.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/og/adapters/mysql.rb', line 199

def initialize(db)
	super

	config = db.config

	@store = Mysql.connect(
		config[:address] || 'localhost', 
		config[:user],
		config[:password], 
		config[:database]
	)
rescue => ex
	if ex.errno == 1049 # database does not exist.
		Logger.info "Database '#{config[:database]}' not found!"
		@db.adapter.create_db(config[:database], config[:user], config[:password])
		retry
	end
	raise
end

Instance Method Details

#closeObject



219
220
221
222
# File 'lib/og/adapters/mysql.rb', line 219

def close
	@store.close
	super
end

#commitObject



255
256
257
# File 'lib/og/adapters/mysql.rb', line 255

def commit
	# @store.commit
end

#exec(sql) ⇒ Object



240
241
242
243
244
245
246
247
248
249
# File 'lib/og/adapters/mysql.rb', line 240

def exec(sql)
	Logger.debug sql if $DBG
	begin
		@store.query_with_result = false
		@store.query(sql) 
	rescue => ex
		Logger.error "DB error #{ex}, [#{sql}]"
		Logger.error ex.backtrace.join("\n")
	end
end

#prepare(sql) ⇒ Object



224
225
226
# File 'lib/og/adapters/mysql.rb', line 224

def prepare(sql)
	raise 'Not implemented!'
end

#query(sql) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/og/adapters/mysql.rb', line 228

def query(sql)
	Logger.debug sql if $DBG
	begin
		@store.query_with_result = true 
		return @store.query(sql)
	rescue => ex
		Logger.error "DB error #{ex}, [#{sql}]"
		Logger.error ex.backtrace.join("\n")
		return nil
	end
end

#read_all(res, klass) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/og/adapters/mysql.rb', line 278

def read_all(res, klass)
	return [] unless valid_res?(res)

	objects = []

	for tuple in (0...res.num_rows)
		row = res.fetch_row

		obj = klass.new
		obj.og_read(row)
		
		objects << obj
	end

	res.free
	return objects 
end

#read_int(res, idx = 0) ⇒ Object



296
297
298
299
300
# File 'lib/og/adapters/mysql.rb', line 296

def read_int(res, idx = 0)
	val =  res.fetch_row[idx].to_i
	res.free
	return val
end

#read_one(res, klass) ⇒ Object



267
268
269
270
271
272
273
274
275
276
# File 'lib/og/adapters/mysql.rb', line 267

def read_one(res, klass)
	return nil unless valid_res?(res)
	
	row = res.fetch_row
	obj = klass.new
	obj.og_read(row)
	
	res.free
	return obj
end

#rollbackObject



259
260
261
# File 'lib/og/adapters/mysql.rb', line 259

def rollback
	# @store.rollback
end

#startObject



251
252
253
# File 'lib/og/adapters/mysql.rb', line 251

def start
	#	@store.transaction
end

#valid_res?(res) ⇒ Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/og/adapters/mysql.rb', line 263

def valid_res?(res)
	return !(res.nil? or 0 == res.num_rows)
end