Class: GData::Client::FusionTables::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/fusion_tables/data/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ Table

configures headers hash and sets up

eg options: => “x”, :name => “y”

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
# File 'lib/fusion_tables/data/table.rb', line 25

def initialize client, options
  raise ArgumentError, "need ft client" if client.class != GData::Client::FusionTables 
  raise ArgumentError, "need table_id and name hash" if !options.has_key?(:name) || !options.has_key?(:table_id) 
  @client = client
  @id = options[:table_id]
  @name = options[:name]
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



19
20
21
# File 'lib/fusion_tables/data/table.rb', line 19

def headers
  @headers
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/fusion_tables/data/table.rb', line 19

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/fusion_tables/data/table.rb', line 19

def name
  @name
end

Instance Method Details

#count(conditions = nil) ⇒ Object

Returns a count of rows. SQL conditions optional

Note: handles odd FT response: when table has 0 rows, returns empty array.



54
55
56
57
# File 'lib/fusion_tables/data/table.rb', line 54

def count conditions=nil
  result = select("count()", conditions)
  result.empty? ? 0 : result.first[:"count()"].to_i          
end

#delete(row_id) ⇒ Object

delete row



97
98
99
# File 'lib/fusion_tables/data/table.rb', line 97

def delete row_id
  @client.execute "DELETE FROM #{@id} WHERE rowid='#{row_id}'"
end

#describeObject

Sets up data types from google



35
36
37
# File 'lib/fusion_tables/data/table.rb', line 35

def describe
  @client.execute "DESCRIBE #{@id}"
end

#encode(data) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/fusion_tables/data/table.rb', line 110

def encode data
  data.inject([]) do |ar,h|
    ret = {}
    h.each do |key, value|              
      ret["'#{key}'"] = encode_value(get_datatype(key), value)
    end
    ar << ret
    ar      
  end
end

#get_datatype(column_name) ⇒ Object

Returns datatype of given column name

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
# File 'lib/fusion_tables/data/table.rb', line 124

def get_datatype column_name
  get_headers
  
  @headers.each do |h|
    return h[:type] if h[:name] == column_name.to_s
  end            
  raise ArgumentError, "The column doesn't exist"
end

#get_headersObject



106
107
108
# File 'lib/fusion_tables/data/table.rb', line 106

def get_headers
  @headers ||= describe
end

#insert(data) ⇒ Object

Outputs data to an array of concatenated INSERT SQL statements

format should be:

=> data, :col2 => data, => data, :col2 => data

Fields are escaped and formatted for FT based on type



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fusion_tables/data/table.rb', line 68

def insert data
  data = [data] unless data.respond_to?(:to_ary)

  # encode values to insert
  data = encode data
  
  # Chunk up the data and send
  chunk = ""
  data.each_with_index do |d,i|            
    chunk << "INSERT INTO #{@id} (#{ d.keys.join(",") }) VALUES (#{ d.values.join(",") });"
    if (i+1) % 500 == 0 || (i+1) == data.size
      begin
        @client.sql_post(chunk)
        chunk = ""
      rescue => e
        raise "INSERT to table:#{@id} failed on row #{i} with #{e}"
      end  
    end  
  end
end

#select(columns = "*", conditions = nil) ⇒ Object

Runs select and returns data obj

Define columns and SQL conditions separatly

See code.google.com/apis/fusiontables/docs/developers_reference.html#Select

use columns=ROWID to select row ids



47
48
49
# File 'lib/fusion_tables/data/table.rb', line 47

def select columns="*", conditions=nil
  @client.execute "SELECT #{columns} FROM #{@id} #{conditions}"
end

#truncate!Object

delete all rows



102
103
104
# File 'lib/fusion_tables/data/table.rb', line 102

def truncate!
  @client.execute "DELETE FROM #{@id}"
end

#update(row_id, data) ⇒ Object

Runs update on row specified and return data obj



90
91
92
93
94
# File 'lib/fusion_tables/data/table.rb', line 90

def update row_id, data          
  data = encode([data]).first
  data = data.to_a.map{|x| x.join("=")}.join(", ")
  @client.execute "UPDATE #{@id} SET #{data} WHERE ROWID = '#{row_id}'"
end