2015-10-07 06:30:10 -04:00
|
|
|
module Gitlab
|
|
|
|
module Database
|
2016-06-18 13:55:45 -04:00
|
|
|
# The max value of INTEGER type is the same between MySQL and PostgreSQL:
|
|
|
|
# https://www.postgresql.org/docs/9.2/static/datatype-numeric.html
|
|
|
|
# http://dev.mysql.com/doc/refman/5.7/en/integer-types.html
|
|
|
|
MAX_INT_VALUE = 2147483647
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
def self.adapter_name
|
|
|
|
connection.adapter_name
|
|
|
|
end
|
|
|
|
|
2015-10-07 06:30:10 -04:00
|
|
|
def self.mysql?
|
2016-04-27 18:52:32 -04:00
|
|
|
adapter_name.casecmp('mysql2').zero?
|
2015-10-07 06:30:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.postgresql?
|
2016-04-27 18:52:32 -04:00
|
|
|
adapter_name.casecmp('postgresql').zero?
|
2016-02-01 21:29:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.version
|
|
|
|
database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
|
2015-10-07 06:30:10 -04:00
|
|
|
end
|
2015-12-09 10:31:42 -05:00
|
|
|
|
2016-05-13 11:26:18 -04:00
|
|
|
def self.nulls_last_order(field, direction = 'ASC')
|
|
|
|
order = "#{field} #{direction}"
|
|
|
|
|
|
|
|
if Gitlab::Database.postgresql?
|
|
|
|
order << ' NULLS LAST'
|
|
|
|
else
|
|
|
|
# `field IS NULL` will be `0` for non-NULL columns and `1` for NULL
|
|
|
|
# columns. In the (default) ascending order, `0` comes first.
|
|
|
|
order.prepend("#{field} IS NULL, ") if direction == 'ASC'
|
|
|
|
end
|
|
|
|
|
|
|
|
order
|
|
|
|
end
|
|
|
|
|
2016-06-15 06:10:41 -04:00
|
|
|
def self.random
|
|
|
|
Gitlab::Database.postgresql? ? "RANDOM()" : "RAND()"
|
|
|
|
end
|
|
|
|
|
2015-12-09 10:31:42 -05:00
|
|
|
def true_value
|
2016-02-15 14:13:47 -05:00
|
|
|
if Gitlab::Database.postgresql?
|
2015-12-09 10:31:42 -05:00
|
|
|
"'t'"
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def false_value
|
2016-02-15 14:13:47 -05:00
|
|
|
if Gitlab::Database.postgresql?
|
2015-12-09 10:31:42 -05:00
|
|
|
"'f'"
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
2016-02-01 21:29:37 -05:00
|
|
|
|
|
|
|
def self.connection
|
|
|
|
ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
2016-07-19 08:21:39 -04:00
|
|
|
private_class_method :connection
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
def self.database_version
|
|
|
|
row = connection.execute("SELECT VERSION()").first
|
|
|
|
|
|
|
|
if postgresql?
|
|
|
|
row['version']
|
|
|
|
else
|
|
|
|
row.first
|
|
|
|
end
|
|
|
|
end
|
2016-07-19 08:21:39 -04:00
|
|
|
|
|
|
|
private_class_method :database_version
|
2015-10-07 06:30:10 -04:00
|
|
|
end
|
|
|
|
end
|