# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 require 'active_record' require 'rtm/backend/active_record/001_initial_schema' require "rtm/backend/active_record/io/from_xtm2" # require "rtm/backend/active_record/io/from_xtm2_libxml" module RTM class RTMAR include Singleton include RTM::TopicMapSystem def create(base_locator) AR::TopicMap.create(base_locator) end def topic_maps AR::TopicMap.topic_maps end def [](*args) if args.size == 0 return AR::TopicMap.topic_maps end args.map { |m| AR::TopicMap.topic_maps[m] } end # Assignes a logger to active record (or STDOUT as default) which outputs # the sql statements. def log(to=STDOUT) ActiveRecord::Base.logger = Logger.new(to) ActiveRecord::Base.colorize_logging = false if PLATFORM =~ /mswin/ end # This function generates the database schema using the default # ActiveRecord connection. def generate_database RTM::AR::TMDM::InitialSchema.migrate(:up) end # Connects to the sqlite3 database given in the path or to # a default database if no path is given. def connect_sqlite3(dbname="tmdm.sqlite3") ActiveRecord::Base.establish_connection( :adapter => platform_specific_adapter("sqlite3"), :database => dbname ) end # Connects to a mysql database. Parameters are # either # database, username, password, host # or # only a keyword hash with these parameters. def connect_mysql(database="rtm_development", username=nil, password=nil, host="localhost") if database.is_a? Hash return ActiveRecord::Base.establish_connection(database) end ActiveRecord::Base.establish_connection( :adapter => platform_specific_adapter("mysql"), :database => database, :username => username, :password => password, :host => host ) end # Connects to a in-memory database. No parameters required. # Schema will automatically be generated. def connect_memory ActiveRecord::Base.establish_connection( :adapter => platform_specific_adapter("sqlite3"), :database => ":memory:" ) no_output do generate_database end true end # Connects Active Record to a database or in Memory database if no parameters given. def connect(*args) if args.size == 0 return connect_memory end ActiveRecord::Base.establish_connection(*args) end private def platform_specific_adapter(adapter) if PLATFORM && PLATFORM =~ /java/ return "jdbc#{adapter}" end return adapter end end end