hello I have a problem which is not related to Ruby but I want to use Ruby to solve it. I have a database with more then 100 tables (Oracle). I want to get all the tables from the database and then I want to iterate over all of them to get the attributes of the columns (especially the type of the column). I need this to automatically generate Data Access Objects and Data Transfer Objects for C++. can this be done with Active Record ? there is a Column Class but I do not know how to get all the tables from the Database. maybe someone can give me a hint. regards Markus ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de
Jeremy Kemper
2005-Oct-29 20:21 UTC
Re: Active Record: getting all tables from a database ?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 29, 2005, at 11:10 AM, Markus Jais wrote:> I have a database with more then 100 tables (Oracle). > I want to get all the tables from the database and > then I want to iterate over all of them to get the > attributes of the columns (especially the type of the > column). > > I need this to automatically generate Data Access > Objects and Data Transfer Objects for C++. > > can this be done with Active Record ? there is > a Column Class but I do not know how to get all the > tables from the Database.Yes. This is fun: # Grab the table names from Oracle. table_names = ActiveRecord::Base.connection.select_values(''select table_name from user_tables'') # Create a model class for each table. models = table_names.map do |table_name| # Class.new creates a subclass of AR::Base # table_name.camelize turns "foo_bar" into "FooBar" # const_set names the new class FooBar Object.const_set(table_name.camelize, Class.new(ActiveRecord::Base)) end # Generate your DAOs for each model class. puts "Generating DAOs" models.each do |model puts "#{model.name}: #{model.column_names.join('', '')}" generate_dao(model) end def generate_dao(model) model.columns.each do |column| # Work with column.type, etc. end end Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDY9m4AQHALep9HFYRAryUAKDjXyg2JXwZqmt8Sp0yimozAgBQ4ACbBz+C eMzewkaY/5STuIZ/N8U0/8U=ITQC -----END PGP SIGNATURE-----
Markus Jais
2005-Oct-30 08:37 UTC
Ant: Re: Active Record: getting all tables from a database ?
--- Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> schrieb:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Oct 29, 2005, at 11:10 AM, Markus Jais wrote: > > I have a database with more then 100 tables > (Oracle). > > I want to get all the tables from the database and > > then I want to iterate over all of them to get the > > attributes of the columns (especially the type of > the > > column). > > > > I need this to automatically generate Data Access > > Objects and Data Transfer Objects for C++. > > > > can this be done with Active Record ? there is > > a Column Class but I do not know how to get all > the > > tables from the Database. > > Yes. This is fun: > > # Grab the table names from Oracle. > table_names > ActiveRecord::Base.connection.select_values(''select > > table_name from user_tables'') > > # Create a model class for each table. > models = table_names.map do |table_name| > # Class.new creates a subclass of AR::Base > # table_name.camelize turns "foo_bar" into > "FooBar" > # const_set names the new class FooBar > Object.const_set(table_name.camelize, > Class.new(ActiveRecord::Base)) > end > > # Generate your DAOs for each model class. > puts "Generating DAOs" > models.each do |model > puts "#{model.name}: #{model.column_names.join('', > '')}" > generate_dao(model) > end > > def generate_dao(model) > model.columns.each do |column| > # Work with column.type, etc. > end > end > > Best, > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) >hi Jeremy thank you very much. this is what I was looking for. Markus ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de