Class: RDO::Postgres::StatementExecutor
- Inherits:
-
Object
- Object
- RDO::Postgres::StatementExecutor
- Defined in:
- ext/rdo_postgres/statements.c
Instance Method Summary collapse
-
#command ⇒ Object
Accessor for the @command ivar.
-
#execute(*, self) ⇒ Object
Execute with PQexecPrepared() and return a Result.
-
#initialize(driver) ⇒ Object
constructor
Initialize the StatementExecutor with the given driver and command.
Constructor Details
#initialize(driver) ⇒ Object
Initialize the StatementExecutor with the given driver and command
154 155 156 157 |
# File 'ext/rdo_postgres/statements.c', line 154
static VALUE rdo_postgres_statement_executor_initialize(VALUE self, VALUE driver) {
rdo_postgres_statement_executor_prepare(self);
return self;
}
|
Instance Method Details
#command ⇒ Object
Accessor for the @command ivar
160 161 162 163 164 |
# File 'ext/rdo_postgres/statements.c', line 160
static VALUE rdo_postgres_statement_executor_command(VALUE self) {
RDOPostgresStatementExecutor * executor;
Data_Get_Struct(self, RDOPostgresStatementExecutor, executor);
return rb_str_new2(executor->cmd);
}
|
#execute(*, self) ⇒ Object
Execute with PQexecPrepared() and return a Result
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'ext/rdo_postgres/statements.c', line 167
static VALUE rdo_postgres_statement_executor_execute(int argc, VALUE * args,
VALUE self) {
RDOPostgresStatementExecutor * executor;
Data_Get_Struct(self, RDOPostgresStatementExecutor, executor);
if (!(executor->driver->is_open)) {
RDO_ERROR("Unable to execute statement: connection is not open");
}
if (argc != executor->nparams) {
rb_raise(rb_eArgError,
"Bind parameter count mismatch: wanted %i, got %i",
executor->nparams, argc);
}
char * values[argc];
size_t lengths[argc];
int i;
for (i = 0; i < argc; ++i) {
if (TYPE(args[i]) == T_NIL) {
values[i] = NULL;
lengths[i] = 0;
} else {
if (TYPE(args[i]) == T_ARRAY) {
if (executor->param_types[i] == RDO_PG_BYTEAARRAYOID) {
args[i] = RDO_PG_WRAP_ARRAY("Bytea", args[i]);
} else {
args[i] = RDO_PG_WRAP_ARRAY("Text", args[i]);
}
}
if (TYPE(args[i]) != T_STRING) {
args[i] = RDO_OBJ_TO_S(args[i]);
}
if (executor->param_types[i] == RDO_PG_BYTEAOID) {
values[i] = (char *) PQescapeByteaConn(executor->driver->conn_ptr,
(unsigned char *) RSTRING_PTR(args[i]),
RSTRING_LEN(args[i]),
&(lengths[i]));
} else {
values[i] = RSTRING_PTR(args[i]);
lengths[i] = RSTRING_LEN(args[i]);
}
}
}
PGresult * res = PQexecPrepared(
executor->driver->conn_ptr,
executor->stmt_name,
argc,
(const char **) values,
(const int *) lengths,
RDO_PG_TEXT_INPUT,
RDO_PG_TEXT_OUTPUT);
for (i = 0; i < argc; ++i) {
if (executor->param_types[i] == RDO_PG_BYTEAOID) {
PQfreemem(values[i]);
}
}
ExecStatusType status = PQresultStatus(res);
if (status == PGRES_BAD_RESPONSE || status == PGRES_FATAL_ERROR) {
PQclear(res);
RDO_ERROR("Failed to execute statement: %s", PQresultErrorMessage(res));
}
return RDO_RESULT(rdo_postgres_tuple_list_new(res, executor->driver->encoding),
rdo_postgres_result_info_new(res));
}
|