Class: DataObject::Postgres::Command

Inherits:
Command
  • Object
show all
Defined in:
lib/do_postgres.rb

Instance Method Summary collapse

Instance Method Details

#execute_non_query(*args) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/do_postgres.rb', line 175

def execute_non_query(*args)
  super
  sql = escape_sql(args)
  @connection.logger.debug { sql }
  results = Postgres_c.PQexec(@connection.db, sql)
  status = Postgres_c.PQresultStatus(results)
  if status == Postgres_c::PGRES_TUPLES_OK
    Postgres_c.PQclear(results)
    raise QueryError, "Your query failed or you tried to execute a SELECT query through execute_non_reader\n#{Postgres_c.PQerrorMessage(@connection.db)}\nQUERY: \"#{sql}\""
  elsif status != Postgres_c::PGRES_COMMAND_OK
    Postgres_c.PQclear(results)
    raise QueryError, "Your query failed.\n#{Postgres_c.PQerrorMessage(@connection.db)}\nQUERY: \"#{sql}\""
  end
  rows_affected = Postgres_c.PQcmdTuples(results).to_i
  Postgres_c.PQclear(results)
  ResultData.new(@connection, rows_affected)
end

#execute_reader(*args) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/do_postgres.rb', line 156

def execute_reader(*args)
  super
  sql = escape_sql(args)
  @connection.logger.debug { sql }
  ptr = Postgres_c.PQexec(@connection.db, sql)
  unless [Postgres_c::PGRES_COMMAND_OK, Postgres_c::PGRES_TUPLES_OK].include?(Postgres_c.PQresultStatus(ptr))
    raise QueryError, "Your query failed.\n#{Postgres_c.PQerrorMessage(@connection.db)}QUERY: \"#{sql}\""
  else
    reader = Reader.new(@connection.db, ptr)
    if block_given?
      return_value = yield(reader)
      reader.close
      return_value
    else
      reader
    end
  end
end