houdini/lib/psql.rb
Bradley M. Kuhn 6772312ea7 Relicense all .rb files under new project license.
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

with some specific files to be licensed under the one of two licenses:
   CC0-1.0
   LGPL-3.0-or-later

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

40 lines
1.3 KiB
Ruby

# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# Some convenience wrappers around the postgresql gem, allowing us to avoid activerecord dependency
# combine usage of this library with Qexpr
require 'colorize'
require 'qx'
# Initialize the database connection
module Psql
# Execute a sql statement (string)
def self.execute(statement)
puts statement if ENV['RAILS_ENV'] != 'production' && ENV['RAILS_LOG_LEVEL'] == 'debug' # log to STDOUT on dev/staging
return Qx.execute_raw(raw_expr_str(statement))
end
# A variation of execute that returns a vector of vectors rather than a vector of hashes
# Useful and faster for creating CSV's
def self.execute_vectors(statement)
puts statement if ENV['RAILS_ENV'] != 'production' && ENV['RAILS_LOG_LEVEL'] == 'debug' # log to STDOUT on dev/staging
raw_str = statement.to_s.uncolorize.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
return Qx.execute_raw(raw_expr_str(statement), format: 'csv')
end
def self.transaction(&block)
Qx.transaction do
yield block
end
end
private
# Raw expression string
def self.raw_expr_str(statement)
statement.to_s.uncolorize.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end
end