Class: FluentQuery::Drivers::Shared::Tokens::SQL::Insert

Inherits:
FluentQuery::Drivers::Shared::Tokens::SQLToken show all
Defined in:
lib/fluent-query/drivers/shared/tokens/sql/insert.rb

Overview

Generic SQL query INSERT token.

Constant Summary

Constants inherited from FluentQuery::Drivers::Shared::Tokens::SQLToken

FluentQuery::Drivers::Shared::Tokens::SQLToken::TRANSFORMER

Instance Method Summary collapse

Methods inherited from FluentQuery::Drivers::Shared::Tokens::SQLToken

#initialize

Constructor Details

This class inherits a constructor from FluentQuery::Drivers::Shared::Tokens::SQLToken

Instance Method Details

#render!(mode = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent-query/drivers/shared/tokens/sql/insert.rb', line 24

def render!(mode = nil)
    processor = @_query.processor
    result = "INSERT INTO "

    @_subtokens.each do |token|
        arguments = token.arguments

        # INSERT token
        if token.name == :insert
            values = arguments.second.values

            # Checks for arguments
            if (not arguments.first.symbol?) or (not arguments.second.hash?)
               raise FluentQuery::Drivers::Exception::new("Symbol and Hash arguments expected for #insert method.")
            end

            # Process
            table = processor.quote_identifier(arguments.first)
            fields = processor.process_identifiers(arguments.second.keys)
            
            if mode == :prepare
                values = values.map do |item|
                    if item != ??
                        processor.quote_value(i)
                    else
                        item
                    end
                end
                
                values = values.join(', ')
            else
                values = processor.process_array(values)
            end
            
            result << table << " (" << fields << ") VALUES (" << values << ")"

        # Unknown tokens renders directly
        else
            result = self.unknown_token::new(@_driver, @_query, token).render!
        end
    end

    return result
end