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.

Set::new [:select, :insert, :update, :delete, :truncate, :set, :begin, :commit, :union, :rollback]
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.

Set::new [: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.



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

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

    @_tokens_required = { }
end

Instance Method Details

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



204
205
206
# File 'lib/fluent-query/drivers/sql.rb', line 204

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

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



532
533
534
# File 'lib/fluent-query/drivers/sql.rb', line 532

def check_conditionally(query, sym, *args, &block)
    self.execute_conditionally(query, sym, *args, &block)
end

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



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/fluent-query/drivers/sql.rb', line 544

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)


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

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



477
478
479
# File 'lib/fluent-query/drivers/sql.rb', line 477

def null
    "NULL"
end

#open_connection(settings) ⇒ Object



519
520
521
# File 'lib/fluent-query/drivers/sql.rb', line 519

def open_connection(settings)
    not_implemented
end

#operator_token?(token_name) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/fluent-query/drivers/sql.rb', line 213

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

#query_classObject



260
261
262
# File 'lib/fluent-query/drivers/sql.rb', line 260

def query_class
    FluentQuery::Queries::SQL
end

#quote_boolean(boolean) ⇒ Object



486
487
488
# File 'lib/fluent-query/drivers/sql.rb', line 486

def quote_boolean(boolean)
    boolean ? 1 : 0
end

#quote_date_time(date) ⇒ Object



495
496
497
# File 'lib/fluent-query/drivers/sql.rb', line 495

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

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



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

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



459
460
461
# File 'lib/fluent-query/drivers/sql.rb', line 459

def quote_float(float)
    float.to_s
end

#quote_identifier(field) ⇒ Object



468
469
470
# File 'lib/fluent-query/drivers/sql.rb', line 468

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

#quote_integer(integer) ⇒ Object



450
451
452
# File 'lib/fluent-query/drivers/sql.rb', line 450

def quote_integer(integer)
    integer.to_s
end

#quote_operator(operator) ⇒ Object



222
223
224
# File 'lib/fluent-query/drivers/sql.rb', line 222

def quote_operator(operator)
    @operators[operator]
end

#quote_string(string) ⇒ Object



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

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

#quote_subquery(subquery) ⇒ Object



504
505
506
# File 'lib/fluent-query/drivers/sql.rb', line 504

def quote_subquery(subquery)
    "(" + subquery + ")"
end

#relevant_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/fluent-query/drivers/sql.rb', line 170

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