bool OWStatement::WriteBlob( OCILobLocator* phLocator, void* pBuffer, int nSize ) { ub4 nAmont = (ub4) nSize; if( CheckError( OCILobWrite( poConnection->hSvcCtx, hError, phLocator, (ub4*) &nAmont, (ub4) 1, (dvoid*) pBuffer, (ub4) nSize, (ub1) OCI_ONE_PIECE, (dvoid*) NULL, NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT ), hError ) ) { return false; } return ( nAmont == (ub4) nSize ); }
void Blob::setData(Connection* conn_, const char* data, ub4 count) { conn = conn_; if (lob == 0) ociDescriptorAlloc(); log_debug("OCILobWrite"); sword ret = OCILobWrite(conn->getSvcCtxHandle(), conn->getErrorHandle(), lob, &count, 0, const_cast<char*>(data), count, OCI_ONE_PIECE, 0, 0, 0, 0); conn->checkError(ret, "OCILobWrite"); }
Blob::Blob(Connection* conn_, const char* data, ub4 count) : conn(conn_), lob(0), release(true) { log_debug("create oracle::Blob from data; size=" << count); ociDescriptorAlloc(); log_debug("OCILobWrite"); sword ret = OCILobWrite(conn->getSvcCtxHandle(), conn->getErrorHandle(), lob, &count, 0, const_cast<char*>(data), count, OCI_ONE_PIECE, 0, 0, 0, 0); conn->checkError(ret, "OCILobWrite"); }
void LOBBuffering_proc(OCILobLocator *Lob_loc, OCIEnv *envhp, OCIError *errhp, OCISvcCtx *svchp, OCIStmt *stmthp) { ub4 amt; ub4 offset; sword retval; ub1 bufp[MAXBUFLEN]; ub4 buflen; printf ("----------- OCI LOB Buffering Demo --------------\n"); /* Open the CLOB: */ checkerr (errhp, (OCILobOpen(svchp, errhp, Lob_loc, OCI_LOB_READWRITE))); /* Enable LOB Buffering: */ printf (" enable LOB buffering\n"); checkerr (errhp, OCILobEnableBuffering(svchp, errhp, Lob_loc)); printf (" write data to LOB\n"); /* Write data into the LOB: */ amt = sizeof(bufp); buflen = sizeof(bufp); offset = 1; checkerr (errhp, OCILobWrite (svchp, errhp, Lob_loc, &amt, offset, (void *)bufp, buflen, OCI_ONE_PIECE, (void *)0, (sb4 (*)(void*,void*,ub4*,ub1 *))0, 0, SQLCS_IMPLICIT)); /* Flush the buffer: */ printf(" flush the LOB buffers\n"); checkerr (errhp, OCILobFlushBuffer(svchp, errhp, Lob_loc, (ub4)OCI_LOB_BUFFER_FREE)); /* Disable Buffering: */ printf (" disable LOB buffering\n"); checkerr (errhp, OCILobDisableBuffering(svchp, errhp, Lob_loc)); /* Subsequent LOB WRITEs will not use the LOB Buffering Subsystem: */ /* Closing the CLOB is mandatory if you have opened it: */ checkerr (errhp, OCILobClose(svchp, errhp, Lob_loc)); return; }
static size_t oci_blob_write(php_stream *stream, const char *buf, size_t count) { struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract; ub4 amt; sword r; amt = count; r = OCILobWrite(self->S->H->svc, self->S->err, self->lob, &amt, self->offset, (char*)buf, count, OCI_ONE_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT); if (r != OCI_SUCCESS) { return (size_t)-1; } self->offset += amt; return amt; }
void OWStatement::WriteCLob( OCILobLocator** pphLocator, char* pszData ) { nNextCol++; CheckError( OCIDescriptorAlloc( poConnection->hEnv, (void**) pphLocator, OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0), hError ); CheckError( OCILobCreateTemporary( poConnection->hSvcCtx, poConnection->hError, (OCILobLocator*) *pphLocator, (ub4) OCI_DEFAULT, (ub1) OCI_DEFAULT, (ub1) OCI_TEMP_CLOB, false, OCI_DURATION_SESSION ), hError ); ub4 nAmont = (ub4) strlen(pszData); CheckError( OCILobWrite( poConnection->hSvcCtx, hError, *pphLocator, (ub4*) &nAmont, (ub4) 1, (dvoid*) pszData, (ub4) strlen(pszData), (ub1) OCI_ONE_PIECE, (dvoid*) NULL, NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT ), hError ); }
// Перекодировка is_null, in bind vars, закачка блобов и потом ora_exec int ora_exec(database *db) { t_ora *o = db->h; db_col *c; int i; if (!o) return ora_error(db); //if (!o->execnum) // { // o->execnum = 1; if (!ora_check_bind(db)) return 0; // Если первый раз - подвязать переменные... // } debugf(" ..ora - check bind ok!\n"); o->execnum++; // Номер запуска exec... for(i=0,c=db->in.cols;i<db->in.count;i++,c++) if ((c->dbflag & 2)==0) { // Все ин-параметры if(c->type==dbDate) // Перекодируем дату { unsigned char *D=(void*)c->dbvalue; double uval = *(double*)c->value; int Year,Month,Day,Hour,Min,Sec; if (!uval) c->null=ORA_NULL; else { c->null=0; dt_decode(uval,&Year,&Month,&Day,&Hour,&Min,&Sec); D[0]=100+Year/100; D[1]=Year%100+100; D[2]=Month; D[3]=Day; D[4]=Hour+1; D[5]=Min+1; D[6]=Sec+1; } //printf("ORA uval=%lf\n",uval); getch(); } else if (c->type == dbBlob && (!(c->dbflag & 2))) { // Закачиваем блобы... db_blob_handle *b = (void*) c->value; // Странно, но я храню блоб тута... int cnt_write = 0, offset = 0, code; cnt_write = b->len; // Сколько записывать... code = OCILobTrim(o->svchp, o->errhp, o->blob,0); //printf("2LobTrimmed data=%d len=%d olob=%d code=%d!\n",b->data,b->len,o->blob,code); c->null = ORA_NULL; if (b->data && b->len>0) { c->null = 0; if ( !ora_ok(db->err_code = OCILobWrite(o->svchp, o->errhp, o->blob, &cnt_write, 1, (dvoid *) b->data, (ub4) b->len, OCI_ONE_PIECE, 0, 0 csid_csfrm) )) { debugf("F**k! cant write blob err_code=%d ND=%d!\n",db->err_code,OCI_NEED_DATA); //if (db->err_code!=-2) // ZU? return ora_error(db); } debugf("vora- ok write %d bytes of lob\n",b->len); } } } debugf(" ..ora - begin OCIStmtExecute stmt=%p!\n",o->stmt); db->err_code = OCIStmtExecute(o->svchp, o->stmt, o->errhp, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL,OCI_DEFAULT); debugf("ORARES on exec = %d\n",db->err_code); if (db->err_code == OCI_NO_DATA) return 1; // ExecOK, данные не сфетчились... if (!ora_ok(db->err_code)) return ora_error(db); // А это уже ошибка... debugf("ORA - decode out\n"); //return 1; // Далее - идет обратная декодировка. Пока не отлаживаем for(i = 0, c = db->in.cols;i<db->in.count; i++, c++) if (c->dbflag & 2) { // Смотрим на аут-параметры int is_null; debugf("----ORA --- chek OUT HERE=%s\n",c->name); //printf("NULL=%d on %s\n",*(short*)c->null,c->name); //is_null = c->null && ((*(short*)(c->null)) ); //if (c->null) *(int*)(c->null) = is_null; //printf("Check in var=%s is_null = %d val=%d\n",c->name,is_null,*(int*)c->value); //if (is_null) continue; if ( c->type==dbDate) { unsigned char *D=(void*)c->dbvalue; if (c->null) *(double*)c->value=0; else *(double*)c->value=dt_encode((D[0]-100)*100+(D[1]-100), D[2],D[3],D[4]-1,D[5]-1,D[6]-1); } else if (c->type==dbBlob) { // Вытягиваем блобу... db_blob_handle *b=(void*)c->value; // Это указатель на "вытащенный блоб", его нужно установить на... int cnt_read = 0, offset = 0, blob_length = 0; debugf("ORA --- chek out blob null==%d\n",c->null); if (c->null) {b->len=0; continue;} /// OCILobLock ??? //db->err_code = OCILobOpen ( o->svchp, o->errhp, o->blob_read, OCI_LOB_READONLY ); //printf("Lob opened error = %d\n", db->err_code); //if (db->out.size<1) { // db->out.data = realloc(db->out.data,1); // db->out.size = 1; // } //OCILobRead(o->svchp,o->errhp,o->blob_read, // Интересно, такое бывает? //&cnt_read, 1, db->out.blob, 1 , 0, 0); // Just for FUN ... // printf("Read = %d bytes\n", cnt_read ); if (!ora_ok(db->err_code=OCILobGetLength (o->svchp,o->errhp,o->blob_read, &blob_length))) { debugf("Fail get blob length code=%d result=%d!\n",db->err_code,blob_length); blob_length = 100* 1024 ; // return 1; // ZUZUKA ora_error(db); } //printf("Getting out blob len = %d..\n",blob_length); // - Сюда сливаются БЛОБЫ от SELCETA -- db->out.blob,&db->out.bsize,&db->out.blen if (blob_length +1 >= db->out.bsize) { // Еще место нужно... Realloc... db->out.bsize = blob_length+1024; // Новая длина db->out.blob = realloc(db->out.blob,db->out.bsize); // Думаем что памяти достаточно } if (!ora_ok(db->err_code=OCILobRead(o->svchp,o->errhp,o->blob_read, // Интересно, такое бывает? &cnt_read, 1, db->out.blob, db->out.bsize, 0, 0 csid_csfrm))) { //printf("Fail read blob err=%d!\n"); return ora_error(db); } //OCILobTrim(o->svchp, o->errhp, o->blob_read,0); db->out.blen = cnt_read+1; // Zero termanited на всякий случай... //printf("Read blob len = %d bh=%d\n",cnt_read,b); // Корректируем указатель, т.к. блоб мог реаллокнуться.. b->data = db->out.blob; // Все заточено под один БЛОБ b->len = cnt_read; b->data[b->len] = 0; // Терминирование нулем - вещь полезная... } } return 1; }
static int oci_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */ { pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data; /* we're only interested in parameters for prepared SQL right now */ if (param->is_param) { pdo_oci_bound_param *P; sb4 value_sz = -1; P = (pdo_oci_bound_param*)param->driver_data; switch (event_type) { case PDO_PARAM_EVT_FETCH_PRE: case PDO_PARAM_EVT_FETCH_POST: case PDO_PARAM_EVT_NORMALIZE: /* Do nothing */ break; case PDO_PARAM_EVT_FREE: P = param->driver_data; if (P) { efree(P); } break; case PDO_PARAM_EVT_ALLOC: P = (pdo_oci_bound_param*)ecalloc(1, sizeof(pdo_oci_bound_param)); param->driver_data = P; /* figure out what we're doing */ switch (PDO_PARAM_TYPE(param->param_type)) { case PDO_PARAM_STMT: return 0; case PDO_PARAM_LOB: /* P->thing is now an OCILobLocator * */ P->oci_type = SQLT_BLOB; value_sz = sizeof(OCILobLocator*); break; case PDO_PARAM_STR: default: P->oci_type = SQLT_CHR; value_sz = param->max_value_len; if (param->max_value_len == 0) { value_sz = 1332; /* maximum size before value is interpreted as a LONG value */ } } if (param->name) { STMT_CALL(OCIBindByName, (S->stmt, &P->bind, S->err, (text*)param->name, param->namelen, 0, value_sz, P->oci_type, &P->indicator, 0, &P->retcode, 0, 0, OCI_DATA_AT_EXEC)); } else { STMT_CALL(OCIBindByPos, (S->stmt, &P->bind, S->err, param->paramno+1, 0, value_sz, P->oci_type, &P->indicator, 0, &P->retcode, 0, 0, OCI_DATA_AT_EXEC)); } STMT_CALL(OCIBindDynamic, (P->bind, S->err, param, oci_bind_input_cb, param, oci_bind_output_cb)); return 1; case PDO_PARAM_EVT_EXEC_PRE: P->indicator = 0; P->used_for_output = 0; if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) { ub4 empty = 0; STMT_CALL(OCIDescriptorAlloc, (S->H->env, &P->thing, OCI_DTYPE_LOB, 0, NULL)); STMT_CALL(OCIAttrSet, (P->thing, OCI_DTYPE_LOB, &empty, 0, OCI_ATTR_LOBEMPTY, S->err)); S->have_blobs = 1; } return 1; case PDO_PARAM_EVT_EXEC_POST: /* fixup stuff set in motion in oci_bind_output_cb */ if (P->used_for_output) { if (P->indicator == -1) { /* set up a NULL value */ if (Z_TYPE_P(param->parameter) == IS_STRING #if ZEND_EXTENSION_API_NO < 220040718 && Z_STRVAL_P(param->parameter) != empty_string #endif ) { /* OCI likes to stick non-terminated strings in things */ *Z_STRVAL_P(param->parameter) = '\0'; } zval_dtor(param->parameter); ZVAL_NULL(param->parameter); } else if (Z_TYPE_P(param->parameter) == IS_STRING #if ZEND_EXTENSION_API_NO < 220040718 && Z_STRVAL_P(param->parameter) != empty_string #endif ) { Z_STRLEN_P(param->parameter) = P->actual_len; Z_STRVAL_P(param->parameter) = erealloc(Z_STRVAL_P(param->parameter), P->actual_len+1); Z_STRVAL_P(param->parameter)[P->actual_len] = '\0'; } } else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB && P->thing) { php_stream *stm; if (Z_TYPE_P(param->parameter) == IS_NULL) { /* if the param is NULL, then we assume that they * wanted to bind a lob locator into it from the query * */ stm = oci_create_lob_stream(stmt, (OCILobLocator*)P->thing); if (stm) { OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE); php_stream_to_zval(stm, param->parameter); P->thing = NULL; } } else { /* we're a LOB being used for insert; transfer the data now */ size_t n; ub4 amt, offset = 1; char *consume; php_stream_from_zval_no_verify(stm, ¶m->parameter); if (stm) { OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE); do { char buf[8192]; n = php_stream_read(stm, buf, sizeof(buf)); if ((int)n <= 0) { break; } consume = buf; do { amt = n; OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing, &amt, offset, consume, n, OCI_ONE_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT); offset += amt; n -= amt; consume += amt; } while (n); } while (1); OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing); OCILobFlushBuffer(S->H->svc, S->err, (OCILobLocator*)P->thing, 0); } else if (Z_TYPE_P(param->parameter) == IS_STRING) { /* stick the string into the LOB */ consume = Z_STRVAL_P(param->parameter); n = Z_STRLEN_P(param->parameter); if (n) { OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE); while (n) { amt = n; OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing, &amt, offset, consume, n, OCI_ONE_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT); consume += amt; n -= amt; } OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing); } } OCIDescriptorFree(P->thing, OCI_DTYPE_LOB); P->thing = NULL; } } return 1; } } return 1; } /* }}} */