# --------------------------------------------------------------------------
# sqlite.rb -- ruby interface for enhancing the core SQLite routines
# Copyright (C) 2003 Jamis Buck (jgb3@email.byu.edu)
# --------------------------------------------------------------------------
# This file is part of the SQLite ruby interface.
# 
# The SQLite/Ruby Interface is free software; you can redistribute it and/or
# modify  it  under the terms of the GNU General Public License as published
# by  the  Free  Software  Foundation;  either  version 2 of the License, or
# (at your option) any later version.
# 
# The SQLite/Ruby Interface is distributed in the hope that it will be useful,
# but   WITHOUT   ANY   WARRANTY;  without  even  the  implied  warranty  of
# MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.   See  the  GNU
# General Public License for more details.
# 
# You  should  have  received  a  copy  of  the  GNU  General Public License
# along with the SQLite/Ruby Interface;  if  not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# --------------------------------------------------------------------------
# This defines further enhancements to the SQLite::Database class.
#
# Author: Jamis Buck (jgb3@email.byu.edu)
# Date: June 2003
# --------------------------------------------------------------------------

require '_sqlite'

module SQLite
  class Database
    def execute( sql, arg=nil, &block )
      if block_given?
        exec( sql, block, arg )
      else
        rows = []
        exec( sql, proc { |row| rows.push row }, arg )
        return rows
      end
    end

    def cache_size
      execute( "PRAGMA cache_size" ) { |row| return row[1] }
    end

    def cache_size=( size )
      execute( "PRAGMA cache_size=#{size}" )
    end

    def database_list( &block )
      execute( "PRAGMA database_list", &block )
    end

    def default_cache_size
      execute( "PRAGMA default_cache_size" ) { |row| return row[1] }
    end

    def default_cache_size=( size )
      execute( "PRAGMA default_cache_size=#{size}" )
    end

    def default_synchronous
      execute( "PRAGMA default_synchronous" ) { |row| return row[1] }
    end

    def default_synchronous=( mode )
      execute( "PRAGMA default_synchronous=#{mode}" )
    end

    def default_temp_store
      execute( "PRAGMA default_temp_store" ) { |row| return row[1] }
    end

    def default_temp_store=( mode )
      execute( "PRAGMA default_temp_store=#{mode}" )
    end

    def empty_result_callbacks=( mode )
      execute( "PRAGMA empty_result_callbacks=#{mode}" )
    end

    def full_column_names=( mode )
      execute( "PRAGMA full_column_names=#{mode}" )
    end

    def index_info( index, &block )
      execute( "PRAGMA index_info( #{index} )", &block )
    end

    def index_list( table, &block )
      execute( "PRAGMA index_list( #{table} )", &block )
    end

    def integrity_check
      execute( "PRAGMA integrity_check" ) do |row|
        raise DatabaseException, row[1] if row[1] != "ok"
      end
    end

    def parser_trace=( mode )
      execute( "PRAGMA parser_trace=#{mode}" )
    end

    def show_datatypes=( mode )
      execute( "PRAGMA show_datatypes=#{mode}" )
    end

    def synchronous
      execute( "PRAGMA synchronous" ) { |row| return row[1] }
    end

    def synchronous=( mode )
      execute( "PRAGMA synchronous=#{mode}" )
    end

    def table_info( table, &block )
      execute( "PRAGMA table_info( #{table} )", &block )
    end

    def temp_store
      execute( "PRAGMA temp_store" ) { |row| return row[1] }
    end

    def temp_store=( mode )
      execute( "PRAGMA temp_store=#{mode}" )
    end

    def vdbe_trace=( mode )
      execute( "PRAGMA vdbe_trace=#{mode}" )
    end
  end
end
