// OciTable.cpp: implementation of the OciTable class. // // #include "OciTable.h" // // Construction/Destruction // OciTable::OciTable() { bat = 100; fnum = 0; pos = 0; for (int i = 0; i < 256; i++) { tfields[ i ] = NULL; } memset(sql, 0, 1024*2); } OciTable::~OciTable() { for (int i = 0; i < fnum; i++) { delete tfields[ i ]; tfields[ i ] = NULL; } } int OciTable::SetSql(char* isql) { strcpy(sql, isql); sword status = 0; status = OCIStmtPrepare(pConn->mystmthp, pConn->myerrhp, (const unsigned char * )sql, (ub4) strlen((char *) sql), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT); if (status != OCI_SUCCESS) { printf("OCIStmtPrepare_ERROR failed!/n"); return -1; } return 1; } int OciTable::BindByName(ub2 type, int length, char* bname) { OCIBind *bnd1p = (OCIBind *) 0; /* the first bind handle */ // tfields[fnum] = new Field(type, bat, length); // fnum++; sword status = 0; sword blen = 0; if (type == SQLT_INT) { blen = sizeof(int); } else if (type == SQLT_FLT) { blen = sizeof(double); } else //str { blen = (sword)(length + 1); } //values (to_date(,'yyyymmddhhmiss')) to deal with the date type tfields[fnum] = new Field(type, bat, blen); // blen++; status = OCIBindByName(pConn->mystmthp, &bnd1p, pConn->myerrhp, (text*)bname, -1, (dvoid *) (tfields[fnum])->value, (sword) blen, type, (dvoid*) (tfields[fnum])->null, (ub2 *) 0, (ub2 *) 0, (ub4) bat, (ub4 *) 0, OCI_DEFAULT); if (status != OCI_SUCCESS) { // printf("DBCON_QUERY_ERROR failed!/n"); return -1; } fnum++; return 1; } int OciTable::Insert() { if (++pos >= bat) { return Flush(); } for (int i = 0; i < fnum; i++) { (tfields[ i ])->pos++; } return 1; } int OciTable::Flush() { sword status; // OCIStmtExecute(conn.mysvchp, conn.mystmthp, conn.myerrhp, 2, 0, NULL, NULL, OCI_DEFAULT); /// if (pos == 0) { return 0; } if (pos >= bat) { status = OCIStmtExecute(pConn->mysvchp, pConn->mystmthp, pConn->myerrhp, bat, 0, NULL, NULL, OCI_DEFAULT); // } else { status = OCIStmtExecute(pConn->mysvchp, pConn->mystmthp, pConn->myerrhp, pos, 0, NULL, NULL, OCI_DEFAULT); } if (status != OCI_SUCCESS) { printf("DBCON_QUERY_ERROR failed!/n"); sb4 errcodep; char errbuf[1024] = {0}; OCIErrorGet(pConn->myerrhp, (ub4)1, NULL, &errcodep, (text*)errbuf, (ub4)sizeof(errbuf), (ub4)OCI_HTYPE_ERROR); printf("OCIServerAttach error[%s]!/n",errbuf); return -1; } else { for (int i = 0; i < fnum; i++) { (tfields[ i ])->Clear(); } //set the pos pos = 0; return 1; } } -- // OciTable.h: interface for the OciTable class. // // #if !defined(AFX_OCITABLE_H__3147032F_524B_4B6E_AEA0_C438BB915F8E__INCLUDED_) #define AFX_OCITABLE_H__3147032F_524B_4B6E_AEA0_C438BB915F8E__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "Field.h" #include "Connection.h" class OciTable { public: OciTable(); virtual ~OciTable(); void SetTableName(char* name); int SetSql(char* isql); void SetBatsize(int isize) { bat = isize; } int BindByName(ub2 type, int length, char* bname); char sql[1024 * 2]; int SetConnection(CConnection* conn) { pConn = conn; return 1; } int Insert(); int Flush(); CConnection* pConn; Field* tfields[256]; int bat; int fnum; int pos; private: char tname[64]; }; #endif // !defined(AFX_OCITABLE_H__3147032F_524B_4B6E_AEA0_C438BB915F8E__INCLUDED_) -- // Field.cpp: implementation of the Field class. // // #include "Field.h" // // Construction/Destruction // Field::Field() { pos = 0; } Field::~Field() { if (value != NULL) { delete [] value; } if (null != NULL) { delete [] null; } } Field::Field(unsigned short dataType, int size, int length) { pos = 0; dtype = dataType; batsize = size; mlength = length; value = new char[batsize * (mlength)]; memset(value, 0, batsize * (mlength)); null = new int[batsize]; for (int i = 0; i < batsize; i++) { null[ i ] = -1;//default to be null } } int Field::Clear() { pos = 0; memset(value, 0, batsize * (mlength)); for (int i = 0; i < batsize; i++) { null[ i ] = -1;//default to be null } return 1; } -- // Field.h: interface for the Field class. // // #if !defined(AFX_FIELD_H__3B96F6EC_26EB_4644_96C3_FD83B966ADE9__INCLUDED_) #define AFX_FIELD_H__3B96F6EC_26EB_4644_96C3_FD83B966ADE9__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <string.h> class Field { public: Field(); virtual ~Field(); Field(unsigned short dataType, int size, int length = 8 ); int SetBatch(int size) { batsize = size; return 1; } double operator=(double dvalue) { *((double*)value + pos) = dvalue; null[pos] = 0; return dvalue; } float operator= (float fvalue) { return (float)operator = (double(fvalue)); } long operator= (long lvalue) { *((long*)value + pos) = lvalue; null[pos] = 0; return lvalue; } int operator= (int ivalue) { return (long)operator = (long(ivalue)); } char* operator= (const char* pvalue) { null[pos] = 0; if (pvalue == NULL) { return strcpy( (char*)value + (mlength) * pos , ""); } else { return strncpy( (char*)value + (mlength) * pos, pvalue, mlength - 1);/ } } int Clear(); char* value; int* null; unsigned short dtype; int batsize; int mlength; int pos; }; #endif // !defined(AFX_FIELD_H__3B96F6EC_26EB_4644_96C3_FD83B966ADE9__INCLUDED_) -- //main.cpp #include "Connection.h" #include <iostream.h> #include <stdlib.h> #include "Record.h" #include "OciTable.h" #if defined(WIN32) #pragma comment(lib,"oci.lib") #endif int main() { // cout << sizeof(int ) << endl; // cout << sizeof(long) << endl; int iret = 0; CConnection conn; iret = conn.connect("102_db", "test", "test"); if (iret < 0) { cout << conn.getMsg() << endl; return -1; } OciTable tb; // tb.SetBatsize(3); tb.SetConnection(&conn); iret = tb.SetSql("insert into student (id, name) values (:ID, :NAME)"); // iret = tb.SetSql("insert into student (id) values (:ID)"); if (iret < 0) { cout << "setsql failed" << endl; return -1; } iret = tb.BindByName(SQLT_INT, 4, ":ID"); if (iret < 0) { return -1; } iret = tb.BindByName(SQLT_STR, 32, ":NAME"); if (iret < 0) { return -1; } for (int i = 0; i < 1000; i++) { *(tb.tfields[0]) = i; *(tb.tfields[1]) = "test-12-31-2345678901234567890123"; tb.Insert(); // if (i % 100 == 0) // { // conn.commit(); // } } iret = tb.Flush(); if (iret < 0) { cout << conn.getMsg() << endl; return -1; } conn.commit(); conn.disconnect(); cout << "over" << endl; return 0; } 对接之前的程序