Class: Tramp::Engine

Inherits:
Object
  • Object
show all
Includes:
Quoting
Defined in:
lib/tramp/engine.rb,
lib/tramp/engine/connection.rb

Defined Under Namespace

Classes: Connection

Instance Method Summary collapse

Methods included from Quoting

#quote, #quote_column_name, #quote_string, #quote_table_name, #quoted_date, #quoted_false, #quoted_string_prefix, #quoted_true

Constructor Details

#initialize(settings) ⇒ Engine

Returns a new instance of Engine.



9
10
11
12
# File 'lib/tramp/engine.rb', line 9

def initialize(settings)
  @connection = Connection.new settings
  @quoted_column_names, @quoted_table_names = {}, {}
end

Instance Method Details

#adapter_nameObject



38
39
40
# File 'lib/tramp/engine.rb', line 38

def adapter_name
  "mysql"
end

#columns(table_name, name = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/tramp/engine.rb', line 57

def columns(table_name, name = nil)
  sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
  columns = []
  result = @connection.execute_now(sql)

  result.each { |field| columns << Column.new(field[0], field[4], field[1], field[2] == "YES") }
  result.free
  columns
end

#connectionObject



42
43
44
45
# File 'lib/tramp/engine.rb', line 42

def connection
  # Arel apparently uses this method to check whether the engine is connected or not
  @connection
end

#create(relation, &block) ⇒ Object



14
15
16
17
18
# File 'lib/tramp/engine.rb', line 14

def create(relation, &block)
  query = relation.to_sql
  log_query(query)
  @connection.insert(query) {|rows| yield(rows) if block_given? }
end

#delete(relation) ⇒ Object



32
33
34
35
36
# File 'lib/tramp/engine.rb', line 32

def delete(relation)
  query = relation.to_sql
  log_query(query)
  @connection.delete(relation.to_sql) {|rows| yield(rows) if block_given? }
end

#read(relation, &block) ⇒ Object



20
21
22
23
24
# File 'lib/tramp/engine.rb', line 20

def read(relation, &block)
  query = relation.to_sql
  log_query(query)
  @connection.select(query) {|rows| yield(rows) }
end

#tablesObject



47
48
49
50
51
52
53
54
55
# File 'lib/tramp/engine.rb', line 47

def tables
  sql = "SHOW TABLES"
  tables = []
  result = @connection.execute_now(sql)

  result.each { |field| tables << field[0] }
  result.free
  tables
end

#update(relation) ⇒ Object



26
27
28
29
30
# File 'lib/tramp/engine.rb', line 26

def update(relation)
  query = relation.to_sql
  log_query(query)
  @connection.update(query) {|rows| yield(rows) if block_given? }
end