[Nitro] [OG] - Automated table modification / extension

Ilias Lazaridis ilias at lazaridis.com
Sat Apr 30 14:21:06 EDT 2005


Ilias Lazaridis wrote:
> Ilias Lazaridis wrote:
>> Ilias Lazaridis wrote:
>>
>>> I create a class, instantiate an object and save it within an
>>> sqlite3.
>>>
>>> I retrieve this object.
>>>
>>> [the table was created, and I load the object back]
>>>
>>>
>>> In the next step, I add a new "property" to the class.
>>>
>>> I instantiate an new object, and try to save it.
>>>
>>> but: the save fails with an "field not fould" exception.
>>>
>>> [the new property i've added]
>>>
>>> -
>>>
>>> How do I setup OG that it automaticaly match the table to the updated
>>>  class-definition? (= automaticly add columns).

see code below for a partial solution (one step missing to become 
functional for "add field").
> 
> [I'm not familar with SQL and the sqlite-ruby. I would need assistance 
> in executing the SQL and getting standard ruby objects]


> within sqlite.rb

	 
#----------------------------------------------------------------------------

	def table_fields(klass, db)
		conn = db.get_connection()	
		tfields = []
		begin
			conn.store.table_info( klass::DBTABLE ) {|field|
				tfields << field			
			}
		rescue Exception => ex
			# exception handling
		end
		
		return tfields
	end
	
	 
#---------------------------------------------------------------------------- 

	
	def table_field_count(klass, db)
		# returns number of fields defined in a table, 0 if none (table 
non-existent)
	
		conn = db.get_connection()			
		counter = 0
		begin
			conn.store.table_info( klass::DBTABLE) {|field|
				counter += 1		
			}
		rescue Exception => ex
			#TODO exception handling
		end
		
		return counter
	end
	
	 
#---------------------------------------------------------------------------- 


	def field_exists(field, tfield, idx)
		val = tfield[idx][1] + ' ' + tfield[idx][2]
		if val == field[idx]
			return true
		else
			return false
		end
	end

	 
#---------------------------------------------------------------------------- 


	def field_process(field,tfield)
	    for idx in field.size
	        if field_exists(field, tfield, idx)
	            p fiedl[idx]
	            # sql_alter_table
		end
	    end
	    #execute sql
	    #execute
	end
	
#---------------------------------------------------------------------------

- I cannot find the "alter table" command within the sqlite3-ruby library.

any ideas?

- the code is partially untested (field process method)

[btw: it is not funny to invest time without getting any feedback.]

-

> # -------------------------------------------------
> # Existend Core Method
> # -------------------------------------------------
> 
> def create_table(klass,db)
> 
>   conn = db.get_connection
> 
>   fields = create_fields(klass)   
> 
>   if table_exists?(klass::DBTABLE)
>     # cleanup
>     update_table()
>   end
>        
> # -------------------------------------------------
> 
> Additionally, the the answer to the question in code:
> 
> "# gmosx: any idea how to better test this?"
> 
> should be this:

see method "table_field_count"

>> please let me know if I should refine this further.

?

.

-- 
http://lazaridis.com




More information about the Nitro-general mailing list