lib/do_postgres.rb in do_postgres-0.2.3 vs lib/do_postgres.rb in do_postgres-0.2.4
- old
+ new
@@ -32,11 +32,52 @@
def create_command(text)
Command.new(self, text)
end
+ def begin_transaction
+ Transaction.new(self)
+ end
+
end
+
+ class Transaction < Connection
+
+ attr_reader :connection
+
+ def initialize(conn)
+ @connection = conn
+ exec_sql("BEGIN")
+ end
+
+ # Commits the transaction
+ def commit
+ exec_sql("COMMIT")
+ end
+
+ # Rolls back the transaction
+ def rollback(savepoint = nil)
+ exec_sql("ROLLBACK#{savepoint ? " TO " + savepoint : ""}")
+ end
+
+ # Creates a savepoint for rolling back later (not commonly supported)
+ def save(name)
+ exec_sql("SAVEPOINT #{name}")
+ end
+
+ def create_command(*args)
+ @connection.create_command(*args)
+ end
+
+ protected
+
+ def exec_sql(sql)
+ @connection.logger.debug(sql)
+ Postgres_c.PQexec(@connection.db, "COMMIT")
+ end
+
+ end
class Reader < DataObject::Reader
def initialize(db, reader)
@reader = reader
@@ -113,16 +154,16 @@
def native_type(col)
TYPES[Postgres_c.PQftype(@reader, col)]
end
def typecast(val, field_type)
- return nil if val.nil?
+ return nil if val.nil? || val == "NULL"
case TYPES[field_type]
when "BOOL"
val == "t"
when "INT2", "INT4", "OID", "TID", "XID", "CID", "INT8"
- val.to_i
+ val == '' ? nil : val.to_i
when "FLOAT4", "FLOAT8", "NUMERIC", "CASH"
val.to_f
when "TIMESTAMP", "TIMETZ", "TIMESTAMPTZ"
DateTime.parse(val) rescue nil
when "TIME"
@@ -188,9 +229,18 @@
rows_affected = Postgres_c.PQcmdTuples(results).to_i
Postgres_c.PQclear(results)
ResultData.new(@connection, rows_affected)
end
+ def quote_string(value)
+ if value =~ /[\x00-\x80]/
+ raise "String cannot contain $Text$ in the body" if value.include?("$Text$")
+ "$Text$#{value}$Text$"
+ else
+ super
+ end
+ end
+
end
end
-end
\ No newline at end of file
+end