| 74 | } |
| 75 | |
| 76 | static PGresult *db_postgres_do_exec(struct db_stmt *stmt) |
| 77 | { |
| 78 | int slots = stmt->query->placeholders; |
| 79 | const char *paramValues[slots]; |
| 80 | int paramLengths[slots]; |
| 81 | int paramFormats[slots]; |
| 82 | Oid paramTypes[slots]; |
| 83 | int resultFormat = 1; /* We always want binary results. */ |
| 84 | |
| 85 | /* Since we pass in raw pointers to elements converted to network |
| 86 | * byte-order we need a place to temporarily stash them. */ |
| 87 | s32 ints[slots]; |
| 88 | u64 u64s[slots]; |
| 89 | |
| 90 | for (size_t i=0; i<slots; i++) { |
| 91 | struct db_binding *b = &stmt->bindings[i]; |
| 92 | |
| 93 | switch (b->type) { |
| 94 | case DB_BINDING_UNINITIALIZED: |
| 95 | db_fatal(stmt->db, "DB binding not initialized: position=%zu, " |
| 96 | "query=\"%s\n", |
| 97 | i, stmt->query->query); |
| 98 | case DB_BINDING_UINT64: |
| 99 | paramLengths[i] = 8; |
| 100 | paramFormats[i] = 1; |
| 101 | u64s[i] = cpu_to_be64(b->v.u64); |
| 102 | paramValues[i] = (char*)&u64s[i]; |
| 103 | paramTypes[i] = INT8OID; |
| 104 | break; |
| 105 | case DB_BINDING_INT: |
| 106 | paramLengths[i] = 4; |
| 107 | paramFormats[i] = 1; |
| 108 | ints[i] = cpu_to_be32(b->v.i); |
| 109 | paramValues[i] = (char*)&ints[i]; |
| 110 | paramTypes[i] = INT4OID; |
| 111 | break; |
| 112 | case DB_BINDING_BLOB: |
| 113 | paramLengths[i] = b->len; |
| 114 | paramFormats[i] = 1; |
| 115 | paramValues[i] = (char*)b->v.blob; |
| 116 | paramTypes[i] = BYTEAOID; |
| 117 | break; |
| 118 | case DB_BINDING_TEXT: |
| 119 | paramLengths[i] = b->len; |
| 120 | paramFormats[i] = 1; |
| 121 | paramValues[i] = (char*)b->v.text; |
| 122 | paramTypes[i] = TEXTOID; |
| 123 | break; |
| 124 | case DB_BINDING_NULL: |
| 125 | paramLengths[i] = 0; |
| 126 | paramFormats[i] = 1; |
| 127 | paramValues[i] = NULL; |
| 128 | paramTypes[i] = 0; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | return PQexecParams(stmt->db->conn, stmt->query->query, slots, |
| 133 | paramTypes, paramValues, paramLengths, paramFormats, |
no test coverage detected