lib/spiderfw/model/storage/db/db_storage.rb in spiderfw-0.5.13 vs lib/spiderfw/model/storage/db/db_storage.rb in spiderfw-0.5.14
- old
+ new
@@ -13,11 +13,12 @@
@reserved_keywords = ['from', 'order', 'where', 'to']
@type_synonyms = {}
@safe_conversions = {
'TEXT' => ['LONGTEXT'],
'INT' => ['TEXT', 'LONGTEXT', 'REAL'],
- 'REAL' => ['TEXT']
+ 'REAL' => ['TEXT'],
+ 'DATETIME' => ['DATE', 'TIME']
}
@capabilities = {
:autoincrement => false,
:sequences => true,
:transactions => true
@@ -84,10 +85,22 @@
subclass.instance_variable_set("@capabilities", @capabilities)
end
end
+ def query_start
+ curr[:query_start] = Time.now
+ end
+
+ def query_finished
+ now = Time.now
+ diff = now - curr[:query_start]
+ diff = 0 if diff < 0 # ???
+ diff = diff*1000
+ Spider.logger.info("Db query (#{@instance_name}) done in #{diff}ms")
+ end
+
def curr
Thread.current[:db_storages] ||= {}
Thread.current[:db_storages][@connection_params] ||= {
:transaction_nesting => 0, :savepoints => []
}
@@ -311,11 +324,11 @@
'INT'
when 'Float'
'REAL'
when 'BigDecimal', 'Spider::DataTypes::Decimal'
'DECIMAL'
- when 'Date', 'DateTime'
+ when 'Date', 'DateTime', 'Time'
'DATE'
when 'Spider::DataTypes::Binary'
'BLOB'
when 'Spider::DataTypes::Bool'
'INT'
@@ -409,11 +422,11 @@
rescue Iconv::InvalidCharacter
value = ''
end
end
when 'BigDecimal'
- value = value.to_f
+ value = value.to_f if value
end
return value
end
# Executes a select query (given in struct form).
@@ -540,10 +553,13 @@
end
sql = "#{key} #{comp} #{val0} AND #{val1}"
else
val = bound_vars ? '?' : value
sql = "#{key} #{comp} #{val}"
+ if comp == '<>'
+ sql = "(#{sql} or #{key} IS NULL)"
+ end
end
end
return sql
end
@@ -568,11 +584,15 @@
values = []
sql = joins.map{ |join|
to_t = join[:as] || join[:to]
sql_on = join[:keys].map{ |from_f, to_f|
to_field = to_f.is_a?(FieldExpression) ? to_f.expression : "#{to_t}.#{to_f.name}"
- "#{from_f} = #{to_field}"
+ if from_f.is_a?(FieldExpression)
+ "#{to_field} = #{from_f.expression}"
+ else
+ "#{from_f} = #{to_field}"
+ end
}.join(' AND ')
if (join[:condition])
condition_sql, condition_values = sql_condition({:condition => join[:condition]})
sql_on += " and #{condition_sql}"
values += condition_values
@@ -614,10 +634,10 @@
# Returns SQL and values for an update statement.
def sql_update(update)
curr[:last_query_type] = :update
values = []
- tables = update[:table]
+ tables = update[:table].to_s
if (update[:joins] && update[:joins][update[:table]])
join_str, join_values = sql_tables_join(update, update[:table])
tables += " "+join_str
values += join_values
end