require "uri" class MicroSql end require_relative "micro_sql/pg_adapter" require_relative "micro_sql/sqlite_adapter" require_relative "micro_sql/table" require_relative "micro_sql/settings" class MicroSql (class << self; self; end).class_eval do private :new def create(url) scheme = URI.parse(url).scheme klass_name = if !scheme "MicroSql::SqliteAdapter" else "MicroSql::#{scheme[0...1].upcase}#{scheme[1..-1]}Adapter" end eval(klass_name).send(:new, url) end end def ask(sql, *args) r = exec(sql, *args) return r unless r.is_a?(Array) r = r.first return r unless r.is_a?(Array) return r unless r.length == 1 r.first end def exec!(sql, *args) execute :no_prepare, sql, *args end def exec(sql, *args) execute :prepare, sql, *args end def transaction(&block) @impl.transaction(&block) end def settings @settings ||= MicroSql::SettingsTable.new(self) end private def prepared_queries @prepared_queries ||= {} end def prepare(sql) key = sql.to_uid.to_s prepared_queries.fetch(key) rescue KeyError prepared_queries[key] = prepare_query(key, sql) end end # url = "pg://sqdb:sqdb@localhost:5433/sqdb" # MicroSql.create(url)