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

Ilias Lazaridis ilias at lazaridis.com
Thu Apr 28 15:06:50 EDT 2005


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).

[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

# -------------------------------------------------
# Helper Methods
# -------------------------------------------------

def table_exists?(tablename)
   # implementation based on:
   # http://www.sqlite.org/pragma.html#schema
end

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

def table_fields(tablename)
   # implementation based on:
   # http://www.sqlite.org/pragma.html#schema
end

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

def column_add(column_definition)
   #implementation based on:
   #http://www.sqlite.org/lang_altertable.html
end

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

def column_delete(column_definition)
   # currently not directly implementable
   # column can remain within table
   # http://www.sqlite.org/lang_altertable.html
end

# -------------------------------------------------
# New Core Method
# -------------------------------------------------

def update_table(kalss,db)

   conn = db.get_connection

   fields = create_fields(klass)	

   fields = create_fields(klass)	

   table_fields = table_fields_get(klass::DTABLE)

   # loop to compare fields/table_fields
     # call to column_add / column_delete

   # execute sql

   # cleanup

end

# -------------------------------------------------
# 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:

http://www.sqlite.org/pragma.html#schema

> Implementation Suggestion:
> 
> in pseudo-code:
> 
> #-------------------------------------
> 
> if store.freezed?
>   return appStoreFreezedError
> 
> for each store.classes (aClass)
> 
>   if not aClass.store.freezed?
>     if not aClass.store.actual?
>       aClass.store.restructure()
> 
> next
> 
> #-------------------------------------
> 
> aClass::store::restructure()
> 
> for each class.field (aField)
> 
>   action = aField.detectActionToPerform()
>   if action
>     # call action
>   else
>     # doNothing
> 
> next
> 
>   # actions prioritized:
>   # add fields to table
>   # removde field (optionally keep removed fields)
>   # change field type
>   # execute "SQL restructure"
> 
> #-------------------------------------
> 
> store.addField(newField)
>   # SQL TABLE ALTER - ADD COLUMN
>   # http://www.sqlite.org/omitted.html
> 
> #-------------------------------------
> 
> -
> 
> please let me know if I should refine this further.
> 
> .
> 


-- 
http://lazaridis.com




More information about the Nitro-general mailing list