SQLite::Database (Class)

In: lib/rq-3.0.0/sqlite.rb
Parent: Object
TypeTranslator Database lib/rq-3.0.0/sqlite.rb SQLite Module: SQLite

The Database class represents a single SQLite database.

Public Class methods

Returns an object that was serialized in the given string.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 57
57:     def self.decode( string )
58:       Marshal.load( Base64.decode64( string ) )
59:     end

Defines a getter and setter for the named boolean pragma. A boolean pragma is one that is either true or false.

[Source]

     # File lib/rq-3.0.0/sqlite.rb, line 106
106:     def self.define_boolean_pragma( name )
107:       class_eval "def \#{name}\nget_first_value( \"PRAGMA \#{name}\" ) != \"0\"\nend\n\ndef \#{name}=( mode )\nexecute( \"PRAGMA \#{name}=\\\#{fix_pragma_parm(mode)}\" )\nend\n"
108:     end

Defines a getter and setter for an enumeration pragma, which is like the boolean pragma except that it accepts a range of discrete values.

[Source]

     # File lib/rq-3.0.0/sqlite.rb, line 144
144:     def self.define_enum_pragma( name, *enums )
145:       cases = ""
146:       enums.each do |enum|
147:         cases << "when \"" <<
148:                  enum.map { |i| i.to_s.downcase }.join( '", "' ) <<
149:                  "\": mode = \"" <<
150:                  enum.first.upcase << "\"\n"
151:       end
152: 
153:       class_eval "def \#{name}\nget_first_value( \"PRAGMA \#{name}\" )\nend\n\ndef \#{name}=( mode )\ncase mode.to_s.downcase\n\#{cases}\nelse\nraise DatabaseException, \"unrecognized \#{name} '\\\#{mode}'\"\nend\n\nexecute( \"PRAGMA \#{name}='\\\#{mode}'\" )\nend\n"
154:     end

Defines a getter and setter for a pragma that accepts (or returns) an integer pragma.

[Source]

     # File lib/rq-3.0.0/sqlite.rb, line 172
172:     def self.define_int_pragma( name, *enums )
173:       class_eval "def \#{name}\nget_first_value( \"PRAGMA \#{name}\" ).to_i\nend\n\ndef \#{name}=( value )\nexecute( \"PRAGMA \#{name}=\\\#{value.to_i}\" )\nend\n"
174:     end

Defines a method for invoking the named query pragma, with the given parameters. A query pragma is one that accepts an optional callback block and invokes it for each row that the pragma returns.

[Source]

     # File lib/rq-3.0.0/sqlite.rb, line 122
122:     def self.define_query_pragma( name, *parms )
123:       if parms.empty?
124:         definition = "def \#{name}( &block )\nexecute( \"PRAGMA \#{name}\", &block )\nend\n"
125:       else
126:         definition = "def \#{name}( \#{parms.join(',')}, &block )\nexecute( \"PRAGMA \#{name}( '\\\#{\#{parms.join(\"}','\\\#{\")}}' )\", &block )\nend\n"
127:       end
128: 
129:       class_eval definition
130:     end

Returns a string that represents the serialization of the given object. The string may safely be used in an SQL statement.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 52
52:     def self.encode( object )
53:       Base64.encode64( Marshal.dump( object ) ).strip
54:     end

An alias for new, with the exception that the mode parameter is optional and defaults to 0 if unspecified.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 40
40:     def self.open( name, mode=0 )
41:       new( name, mode )
42:     end

Returns a string with special characters escaped, such that the string may be safely used as a string literal in an SQL query.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 46
46:     def self.quote( string )
47:       return string.gsub( /'/, "''" )
48:     end

Public Instance methods

This is a convenience method for querying the database. If the (optional) block is not specified, then an array of rows will be returned. Otherwise, the given block will be executed one for each row in the result set.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 64
64:     def execute( sql, arg=nil, &block )
65:       if block_given?
66:         exec( sql, block, arg )
67:       else
68:         rows = []
69:         exec( sql, proc { |row| rows.push row }, arg )
70:         return rows
71:       end
72:     end

A convenience method for retrieving the first row of the result set returned by the given query.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 76
76:     def get_first_row( sql )
77:       result = nil
78:       execute( sql ) do |row|
79:         result = row
80:         SQLite::ABORT
81:       end
82:       result
83:     end

A convenience method for retrieving the first column of the first row of the result set returned by the given query.

[Source]

    # File lib/rq-3.0.0/sqlite.rb, line 87
87:     def get_first_value( sql )
88:       result = nil
89:       execute( sql ) do |row|
90:         result = row[0]
91:         SQLite::ABORT
92:       end
93:       result
94:     end

Performs an integrity check on the database. If there is a problem, it will raise an exception, otherwise the database is fine.

[Source]

     # File lib/rq-3.0.0/sqlite.rb, line 98
 98:     def integrity_check
 99:       execute( "PRAGMA integrity_check" ) do |row|
100:         raise DatabaseException, row[0] if row[0] != "ok"
101:       end
102:     end

Private Instance methods

An internal method for converting the pragma parameter of the boolean pragmas to something that SQLite can understand.

[Source]

     # File lib/rq-3.0.0/sqlite.rb, line 187
187:     def fix_pragma_parm( parm )
188:       case parm
189:         when String
190:           case parm.downcase
191:             when "on", "yes", "true", "y", "t": return "'ON'"
192:             when "off", "no", "false", "n", "f": return "'OFF'"
193:             else
194:               raise DatabaseException, "unrecognized pragma parameter '#{parm}'"
195:           end
196:         when true, 1
197:           return "ON"
198:         when false, 0, nil
199:           return "OFF"
200:         else
201:           raise DatabaseException, "unrecognized pragma parameter '#{parm.inspect}'"
202:       end
203:     end

[Validate]