Class: FluentQuery::Drivers::SQL

Inherits:
FluentQuery::Driver
  • Object
show all
Defined in:
lib/fluent-query/drivers/sql.rb

Overview

Represents abstract SQL driver.

Defined Under Namespace

Classes: TOKEN

Constant Summary collapse

RELEVANT =

Contains relevant methods index for this driver.

[:select, :insert, :update, :delete, :truncate, :set, :begin, :commit, :union]
ORDERING =

Contains ordering for typicall queries.

{
    :select => [
        :select, :from, :join, :groupBy, :having, :where, :orderBy, :limit, :offset
    ],
    :insert => [
        :insert, :values
    ],
    :update => [
        :update, :set, :where
    ],
    :delete => [
        :delete, :where
    ],
    :truncate => [
        :truncate, :table, :cascade
    ],
    :set => [
        :set
    ],
    :union => [
        :union
    ]
}
OPERATORS =

Contains operators list.

Operators are defined as tokens whose multiple parameters in Array are appropriate to join by itself.

{
    :and => "AND",
    :or => "OR"
}
AGREGATE =

Indicates, appropriate token should be present by one real token, but more input tokens.

[:where, :orderBy, :select, :groupBy, :having]
ALIASES =

Indicates token aliases.

{
    :leftJoin => :join,
    :rightJoin => :join,
    :fullJoin => :join
}

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ SQL

Returns a new instance of SQL.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fluent-query/drivers/sql.rb', line 145

def initialize(connection)
    if self.instance_of? SQL
        not_implemented
    end

    super(connection)

    @relevant = SQL::RELEVANT
    @ordering = SQL::ORDERING
    @operators = SQL::OPERATORS
    @aliases = SQL::ALIASES

    @agregate = { }
    SQL::AGREGATE.each do |i| 
        @agregate[i] = true
    end
    
    @_tokens_required = { }
end

Instance Method Details

#build_query(query, mode = :build) ⇒ Object



206
207
208
# File 'lib/fluent-query/drivers/sql.rb', line 206

def build_query(query, mode = :build)
    self._build_query(query, FluentQuery::Drivers::Shared::Tokens::SQLToken, mode)
end

#execute_conditionally(query, sym, *args, &block) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/fluent-query/drivers/sql.rb', line 533

def execute_conditionally(query, sym, *args, &block)
    case query.type
        when :insert
            if (args.first.symbol?) and (args.second.hash?)
                result = query.do!
            end
        when :begin
            if args.empty?
                result = query.execute!
            end
        when :truncate
            if args.first.symbol?
                result = query.execute!
            end
        when :commit, :rollback
            result = query.execute!
        else
            result = nil
    end
    
    return result
end

#known_token?(group, token_name, cache) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/fluent-query/drivers/sql.rb', line 181

def known_token?(group, token_name, cache)

    # Checks
    if @ordering.has_key? group
        # Creates index
        if not cache.has_key? group  
            @ordering[group].each do |item|
                cache[group][item] = true
            end
        end
    
        result = cache[group].has_key? token_name.to_sym
    else
        result = false
    end

    return result
    
end

#nullObject



479
480
481
# File 'lib/fluent-query/drivers/sql.rb', line 479

def null
    "NULL"
end

#open_connection(settings) ⇒ Object



521
522
523
# File 'lib/fluent-query/drivers/sql.rb', line 521

def open_connection(settings)
    not_implemented
end

#operator_token?(token_name) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/fluent-query/drivers/sql.rb', line 215

def operator_token?(token_name)
    @operators.has_key? token_name
end

#query_classObject



262
263
264
# File 'lib/fluent-query/drivers/sql.rb', line 262

def query_class
    FluentQuery::Queries::SQL
end

#quote_boolean(boolean) ⇒ Object



488
489
490
# File 'lib/fluent-query/drivers/sql.rb', line 488

def quote_boolean(boolean)
    boolean ? 1 : 0
end

#quote_date_time(date) ⇒ Object



497
498
499
# File 'lib/fluent-query/drivers/sql.rb', line 497

def quote_date_time(date)
    self.quote_string(date.to_s)
end

#quote_equality(datatype, mode = :comparing) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/fluent-query/drivers/sql.rb', line 237

def quote_equality(datatype, mode = :comparing)                    
    if not datatype.kind_of? Class
        datatype = datatype.class
    end
    
    if mode == :comparing
        if (datatype == TrueClass) or (datatype == FalseClass) or (datatype == NilClass)
            result = "IS"
        elsif datatype == Array
            result = "IN"
        else
            result = "="
        end
    else
        result = "="
    end

    return result
end

#quote_float(float) ⇒ Object



461
462
463
# File 'lib/fluent-query/drivers/sql.rb', line 461

def quote_float(float)
    float.to_s
end

#quote_identifier(field) ⇒ Object



470
471
472
# File 'lib/fluent-query/drivers/sql.rb', line 470

def quote_identifier(field)
    '"' << field.to_s.gsub(".", '"."') << '"'
end

#quote_integer(integer) ⇒ Object



452
453
454
# File 'lib/fluent-query/drivers/sql.rb', line 452

def quote_integer(integer)
    integer.to_s
end

#quote_operator(operator) ⇒ Object



224
225
226
# File 'lib/fluent-query/drivers/sql.rb', line 224

def quote_operator(operator)
    @operators[operator]
end

#quote_string(string) ⇒ Object



440
441
442
443
444
445
# File 'lib/fluent-query/drivers/sql.rb', line 440

def quote_string(string)
    string = string.gsub("'", "''")
    string.gsub!("\\", "\\\\\\\\")
    
    return "'" << string << "'"
end

#quote_subquery(subquery) ⇒ Object



506
507
508
# File 'lib/fluent-query/drivers/sql.rb', line 506

def quote_subquery(subquery)
    "(" << subquery << ")"
end

#relevant_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/fluent-query/drivers/sql.rb', line 172

def relevant_method?(name)
    @relevant.include? name
end