ruby-****@sourc*****
ruby-****@sourc*****
2003年 9月 25日 (木) 04:07:38 JST
------------------------- REMOTE_ADDR = 217.117.55.140 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/?tut-libgda-datamodels ------------------------- - = Managing data models + = Managing data models and values Each time we execute a normal query, we will obtain a Gda::DataModel object, which is the way to see what the query returned. Before continuing, we must say that it is possible to modify a data model, but as we are accessing using SQL, it is not recommended to modify it, so modifications on the database must be done using SQL. + + A data model contains a set of rows (Gda::Row), according to the query. Rows contain a set of values (Gda::Value) for each column of the data model. Values embed various kind of data (strings, integers, booleans, dates, money currencies, etc...). + + You can use Gda::Value#to_s to retrieve a string representation of a GDA value, and you can compare values using Gda::Value#<=>. + + [TODO: introduce Gda::Value#to_* conversion methods] == Example using direct cell access This method accesses the data model by directly accessing cells, using Gda::DataModel#get_value_at: def show_table(dm) # Loop for writing column names. dm.n_columns.times { |n_col| puts dm.get_column_title(n_col) } puts "" # Double loop accessing values using Gda::DataModel#get_value_at. dm.n_columns.times do |n_col| dm.n_rows.times do |n_row| # Data returned is a Gda::Value object. val = dm.get_value_at(n_col, n_row) puts val.to_s end end end == Example using row access This method accesses the data model by accessing rows, using Gda::DataModel#get_row and Gda::DataModel#get_value: def show_table2(dm) # Loop for writing column names. dm.n_columns.times { |n_col| puts dm.get_column_title(n_col) } puts "" # Outer loop obtaining rows using Gda::DataModel#get_row. dm.n_rows.times do |n_row| row = dm.get_row(n_row) # Inner loop obtaining the value using Gda::DataModel#get_value. dm.n_columns.times do |n_col| val = row.get_value(n_col) puts val.to_s end end end == Example using 'The Ruby Way' For your convenience, Ruby/Libgda provides a more rubish way to access values in data models. Just have a look at the following methods: Gda::DataModel#each_column, Gda::DataModel#columns, Gda::DataModel#each_row, Gda::DataModel#rows, Gda::Row#each_value and Gda::Row#values. Here is an example using iterators: def show_table3(dm) dm.each_column { |title| puts title } puts "" dm.each_row { |row| row.each_value { |val| puts val.to_s } } end Life is good, isn't it? ;-)