Class: FrontbaseAdapter

Inherits:
DataMapper::Adapters::AbstractAdapter
  • Object
show all
Defined in:
lib/dm-frontbase-adapter.rb,
lib/dm-frontbase-adapter/adapter.rb,
lib/dm-frontbase-adapter/sql_query.rb,
lib/dm-frontbase-adapter/connection.rb

Defined Under Namespace

Classes: Connection, SQLQuery

Constant Summary collapse

Inflector =
::DataMapper.const_defined?(:Inflector) ? ::DataMapper::Inflector : ::Extlib::Inflection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ FrontbaseAdapter

Returns a new instance of FrontbaseAdapter.



21
22
23
24
25
26
27
28
29
# File 'lib/dm-frontbase-adapter/adapter.rb', line 21

def initialize(name, options)

  # store our options in a hash :sym => value
  @options = options.inject({}) {|memo, (k,v)| memo[k.to_sym] = v; memo}
  @options[:encoding] ||= 'iso-8859-1'
  
  # initialize abstract adapter
  super(name, @options)
end

Instance Attribute Details

#dataObject

cache data per operation accessor



8
9
10
# File 'lib/dm-frontbase-adapter/adapter.rb', line 8

def data
  @data
end

#models_operationsObject

frontbase operations accessor



11
12
13
# File 'lib/dm-frontbase-adapter/adapter.rb', line 11

def models_operations
  @models_operations
end

Instance Method Details

#connectionObject



35
36
37
# File 'lib/dm-frontbase-adapter/adapter.rb', line 35

def connection
  @connection ||= FrontbaseAdapter::Connection.new(@options)
end

#describe(storage_name) ⇒ Object



99
100
101
102
103
# File 'lib/dm-frontbase-adapter/adapter.rb', line 99

def describe storage_name
  with_connection do |connection|
    connection.describe storage_name
  end
end

#field_naming_conventionObject



13
14
15
# File 'lib/dm-frontbase-adapter/adapter.rb', line 13

def field_naming_convention
   proc {|property| property.name.to_s }
end

#log(msg) ⇒ Object



31
32
33
# File 'lib/dm-frontbase-adapter/adapter.rb', line 31

def log msg
  DataMapper.logger.info "FrontbaseAdapter: #{msg}"
end

#read(query) ⇒ Object

Simple read method that take a DataMapper::Query object that represent the query Returns a filtered data hash built from the query model operation returned xml



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dm-frontbase-adapter/adapter.rb', line 49

def read(query)

  properties = query.fields
  
  statement = SQLQuery.new(query, :select).to_s
  
  log statement
  
  records = with_connection { |connection|
    response_to_a(connection.query(statement), properties)
  }
  
  query.filter_records(records)
end

#resource_naming_conventionObject



17
18
19
# File 'lib/dm-frontbase-adapter/adapter.rb', line 17

def resource_naming_convention
  proc {|resource| resource.to_s }
end

#response_to_a(response, props = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dm-frontbase-adapter/adapter.rb', line 64

def response_to_a response, props = nil
  columns = response.columns
  result = response.result 

  result.inject([]) do |arr, record|
    
    record = Hash[*columns.zip(record).flatten].inject({}) do |hash, (column, value)| 
      if props && (prop = props.find {|prop| prop.field.to_sym == column.to_sym })
        
        case
        when prop.is_a?(::DataMapper::Property::Boolean)
          value = case
          when [1.0, 1, true, "true"].include?(value)
            true
          else
            false
          end
        when prop.kind_of?(::DataMapper::Property::String)
          value = value.to_s.force_encoding('ISO-8859-1').encode('UTF-8', :undef => :replace, :invalid => :replace)
        end
        
        value = prop.typecast(value)
        
      elsif value.kind_of? String
        value = value.to_s.force_encoding('ISO-8859-1').encode('UTF-8', :undef => :replace, :invalid => :replace)
      end
      
      hash[column] = value
      hash
    end
    arr << record
    arr
  end
end

#show_tablesObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/dm-frontbase-adapter/adapter.rb', line 105

def show_tables
  with_connection do |connection|
    
    stmt = 'SELECT * FROM INFORMATION_SCHEMA.SCHEMATA T0, INFORMATION_SCHEMA.TABLES T1 WHERE T0."SCHEMA_PK" = T1."SCHEMA_PK";'
    log stmt

    records = response_to_a(connection.query(stmt))
    records.find_all {|record| record[:SCHEMA_NAME] != 'INFORMATION SCHEMA' && record[:TABLE_TYPE] == 'BASE_TABLE'}.map {|record| record[:TABLE_NAME]}
  end
end

#with_connection(&block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/dm-frontbase-adapter/adapter.rb', line 39

def with_connection &block
  block.call(connection) if block
rescue
  raise $!
ensure
  connection.close
end