2015-07-06 05:17:03 +08:00
|
|
|
module ImportScripts::PhpBB3
|
|
|
|
class DatabaseBase
|
|
|
|
# @param database_client [Mysql2::Client]
|
|
|
|
# @param database_settings [ImportScripts::PhpBB3::DatabaseSettings]
|
|
|
|
def initialize(database_client, database_settings)
|
|
|
|
@database_client = database_client
|
|
|
|
|
|
|
|
@batch_size = database_settings.batch_size
|
|
|
|
@table_prefix = database_settings.table_prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Executes a database query.
|
2016-01-15 05:43:19 +08:00
|
|
|
def query(sql, *last_columns)
|
|
|
|
rows = @database_client.query(sql, cache_rows: true, symbolize_keys: true)
|
|
|
|
return rows if last_columns.length == 0
|
|
|
|
|
|
|
|
result = [rows]
|
|
|
|
last_row = find_last_row(rows)
|
|
|
|
|
|
|
|
last_columns.each { |column| result.push(last_row ? last_row[column] : nil) }
|
|
|
|
result
|
2015-07-06 05:17:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Executes a database query and returns the value of the 'count' column.
|
|
|
|
def count(sql)
|
|
|
|
query(sql).first[:count]
|
|
|
|
end
|
2016-01-15 05:43:19 +08:00
|
|
|
|
|
|
|
def escape(value)
|
|
|
|
@database_client.escape(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_last_row(rows)
|
|
|
|
last_index = rows.size - 1
|
|
|
|
|
|
|
|
rows.each_with_index do |row, index|
|
|
|
|
return row if index == last_index
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2015-07-06 05:17:03 +08:00
|
|
|
end
|
|
|
|
end
|