/*! \brief Load data records (&D) Call VFKReader::OpenFile() before this function. \return number of data records or -1 on error */ int VFKReaderSQLite::ReadDataRecords(IVFKDataBlock *poDataBlock) { int nDataRecords; const char *pszName; CPLString osSQL; sqlite3_stmt *hStmt; nDataRecords = 0; /* table name */ pszName = poDataBlock->GetName(); /* check for existing records (re-use already inserted data) */ osSQL.Printf("SELECT num_records FROM vfk_blocks WHERE " "table_name = '%s'", pszName); hStmt = PrepareStatement(osSQL.c_str()); nDataRecords = -1; if (ExecuteSQL(hStmt) == OGRERR_NONE) { nDataRecords = sqlite3_column_int(hStmt, 0); } sqlite3_finalize(hStmt); poDataBlock->SetFeatureCount(0); /* avoid recursive calls */ if (nDataRecords > -1) { /* read from DB */ long iFID; VFKFeatureSQLite *poNewFeature = NULL; if (EQUAL(pszName, "SBP")) { return 0; /* see LoadGeometry() */ } osSQL.Printf("SELECT %s FROM %s", FID_COLUMN, pszName); hStmt = PrepareStatement(osSQL.c_str()); nDataRecords = 1; while (ExecuteSQL(hStmt) == OGRERR_NONE) { iFID = sqlite3_column_int(hStmt, 0); poNewFeature = new VFKFeatureSQLite(poDataBlock, nDataRecords++, iFID); poDataBlock->AddFeature(poNewFeature); } } else { /* read from VFK file and insert records into DB */ bool bUnique; const char *pszKey; bUnique = !CSLTestBoolean(CPLGetConfigOption("OGR_VFK_DB_IGNORE_DUPLICATES", "NO")); /* begin transaction */ ExecuteSQL("BEGIN"); /* create indeces */ osSQL.Printf("%s_%s", pszName, FID_COLUMN); CreateIndex(osSQL.c_str(), pszName, FID_COLUMN, !EQUAL(pszName, "SBP")); pszKey = ((VFKDataBlockSQLite *) poDataBlock)->GetKey(); if (pszKey) { osSQL.Printf("%s_%s", pszName, pszKey); CreateIndex(osSQL.c_str(), pszName, pszKey, bUnique); } if (EQUAL(pszName, "SBP")) { /* create extra indices for SBP */ CreateIndex("SBP_OB", pszName, "OB_ID", FALSE); CreateIndex("SBP_HP", pszName, "HP_ID", FALSE); CreateIndex("SBP_DPM", pszName, "DPM_ID", FALSE); CreateIndex("SBP_OB_HP_DPM", pszName, "OB_ID,HP_ID,DPM_ID", bUnique); CreateIndex("SBP_OB_POR", pszName, "OB_ID,PORADOVE_CISLO_BODU", FALSE); CreateIndex("SBP_HP_POR", pszName, "HP_ID,PORADOVE_CISLO_BODU", FALSE); CreateIndex("SBP_DPM_POR", pszName, "DPM_ID,PORADOVE_CISLO_BODU", FALSE); } else if (EQUAL(pszName, "HP")) { /* create extra indices for HP */ CreateIndex("HP_PAR1", pszName, "PAR_ID_1", FALSE); CreateIndex("HP_PAR2", pszName, "PAR_ID_2", FALSE); } else if (EQUAL(pszName, "OB")) { /* create extra indices for OP */ CreateIndex("OB_BUD", pszName, "BUD_ID", FALSE); } /* INSERT ... */ nDataRecords = VFKReader::ReadDataRecords(poDataBlock); /* update 'vfk_blocks' table */ osSQL.Printf("UPDATE vfk_blocks SET num_records = %d WHERE " "table_name = '%s'", nDataRecords, pszName); ExecuteSQL(osSQL); /* commit transaction */ ExecuteSQL("COMMIT"); } return nDataRecords; }
int tablesearch(char* filename,char (*tableindex)[TABLE_NAME_MAX],Column** columnindex,Data*** database) { FILE* file; int isnosearch=0; // Is there no restriction to the column name int colname_print[COL_READ_MAX]; // Store the number of column to print -1 is the end int colname_do[COL_READ_MAX]; // the order os Column to operte -1 is the end char colnameprints[COL_READ_MAX][COLUMN_NAME_MAX];// recieve every columname to print char colvalue[COL_READ_MAX][COLUMN_NAME_MAX]; //recieve evey column_value int colname_order; //the order of column to sort by int colnumber=0; int andor[AND_OR_MAX]; // and or ARRAY or=0 and=1 -1 is the end int likeequal[LIKE_EQUAL_MAX]; //like = Array -1 is the end 0=like 1 = = // char buffer[LINE_MAX]; //Buffer of file int countavalible=0; //Items that has been read int tableoperate; // The order of operting table int countnumber=0; //How Many columns to print int numberofprint=0; //how many lines to print if 0 means all int asc=ASC; if((file=fopen(filename,"r"))==NULL) { printf("Error in opening the file!\n"); return 0; } start=over=(time_t)0; //count the time start=clock(); checkline1(file,colname_print,&numberofprint,colnameprints,&tableoperate ,tableindex,columnindex); if(database[tableoperate] == NULL ) {printf("The table hasn't been Initialized!\n"); return 0;} char *revalue=checkline2(file, andor, likeequal,colvalue,colname_do,tableoperate,columnindex); char* command=revalue; if(command==NULL) { colname_order=0; asc=ASC; } else { if(!mystrcmp(command,"order")) { char buffer[LINE_MAX]; command=getwords(file,buffer); //by if(mystrcmp("by",buffer)) { printf("You should follow order a by\n"); return 0; } command=getwords(file,buffer); //column_name colname_order= columnnow(buffer,tableoperate,columnindex); if(colname_order == -1) {printf("Error in lcating the %s column\n",buffer); return 0; } command=getwords(file,buffer); //ASC DESC or EOF if(command==NULL) asc=ASC; else if(!mystrcmp("ASC",buffer)) asc=ASC; else asc=DESC; } } fclose(file); int *index=CreateIndex(tableoperate,colname_do,colname_order,likeequal,colvalue,andor,columnindex,database); // Create index of table to print int _coltype= columnindex[tableoperate][colname_order].nametype; //Style of data to sort by int numberitems=0; //How many lines to print while( index[numberitems] != -1) numberitems++; shellsort(index ,numberitems,_coltype,asc,database[tableoperate][colname_order]); //Put the result to file and return how many has been searched int countsearch = fileprint("074090_select.txt",numberofprint,colname_print,database[tableoperate],columnindex[tableoperate],index); free(index); //Free the index return countsearch; }
BOOL DBCFile::OpenFromTXT(const CHAR* szFileName) { assert(szFileName); //---------------------------------------------------- //打开文件 FILE* fp = fopen(szFileName, "r"); if(NULL == fp) return FALSE; //---------------------------------------------------- //分析列数和类型 CHAR szLine[1024*10]; //读第一行 fgets(szLine, 1024*10, fp); size_t nLen; if((nLen=strlen(szLine)) != 0) if(szLine[nLen-1] == '\r' || szLine[nLen-1] == '\n') szLine[nLen-1] = '\0'; if((nLen=strlen(szLine)) != 0) if(szLine[nLen-1] == '\r' || szLine[nLen-1] == '\n') szLine[nLen-1] = '\0'; //分解 std::vector< std::string > vRet; _ConvertStringToVector(szLine, vRet, "\t", TRUE, TRUE); if(vRet.empty()) return FALSE; //生成Field Types FILEDS_TYPE vFieldsType; vFieldsType.resize(vRet.size()); for(INT i=0; i<(INT)vRet.size(); i++) { if(vRet[i] == "INT") vFieldsType[i] = T_INT; else if(vRet[i] == "FLOAT") vFieldsType[i] = T_FLOAT; else if(vRet[i] == "STRING") vFieldsType[i] = T_STRING; else { fclose(fp); return FALSE; } } //-------------------------------------------------------------- //初始化 INT nRecordsNum = 0; INT nFieldsNum = (INT)vFieldsType.size(); //临时字符串区 std::vector< std::pair< std::string, INT > > vStringBuf; //检索表 std::map< std::string, INT > mapStringBuf; //-------------------------------------------------------------- //开始读取 fgets(szLine, 1024*10, fp); //空读一行 INT nStringBufSize = 0; do { //读取一行 if(NULL == fgets(szLine, 1024*10, fp)) break; if((nLen=strlen(szLine)) == 0) continue; if(szLine[nLen-1] == '\r' || szLine[nLen-1] == '\n') szLine[nLen-1] = '\0'; if((nLen=strlen(szLine)) == 0) continue; if(szLine[nLen-1] == '\r' || szLine[nLen-1] == '\n') szLine[nLen-1] = '\0'; //分解 _ConvertStringToVector(szLine, vRet, "\t", TRUE, FALSE); //列数不对 if(vRet.empty()) continue; if(vRet.size() != nFieldsNum) { //补上空格 if((INT)vRet.size() < nFieldsNum) { INT nSubNum = nFieldsNum-(INT)vRet.size(); for(INT i=0; i<nSubNum; i++) { vRet.push_back(""); } } } //第一列不能为空 if(vRet[0].empty()) continue; for(register INT i=0; i<(INT)vRet.size(); i++) { FIELD newField; switch(vFieldsType[i]) { case T_INT: newField.iValue = atoi(vRet[i].c_str()); m_vDataBuf.push_back(newField); break; case T_FLOAT: newField.fValue = (FLOAT)atof(vRet[i].c_str()); m_vDataBuf.push_back(newField); break; case T_STRING: if(vRet[i].empty()) { newField.iValue = 0; } else { const CHAR * p = vRet[i].c_str(); std::map< std::string, INT >::iterator it = mapStringBuf.find(vRet[i]); if(it == mapStringBuf.end()) { vStringBuf.push_back(std::make_pair(vRet[i], nStringBufSize)); mapStringBuf.insert(std::make_pair(vRet[i], (INT)vStringBuf.size()-1)); newField.iValue = nStringBufSize + 1; // first CHAR is '\0' for blank string nStringBufSize += (INT)strlen(vRet[i].c_str()) + 1; } else { newField.iValue = vStringBuf[it->second].second + 1; } } m_vDataBuf.push_back(newField); break; } } nRecordsNum++; }while(TRUE); fclose(fp); //-------------------------------------------------------- //生成正式数据库 m_nRecordsNum = nRecordsNum; m_nFieldsNum = nFieldsNum; m_nStringBufSize = nStringBufSize+1; //Create String Blok m_pStringBuf = new CHAR[m_nStringBufSize]; if(!m_pStringBuf) return FALSE; //------------------------------------------------------ // Create Field Types m_theType = vFieldsType; //------------------------------------------------------ // Create String Block UCHAR byBlank = '\0'; m_pStringBuf[0] = '\0'; register CHAR* p = m_pStringBuf + 1; for(INT i=0; i<(INT)vStringBuf.size(); i++) { memcpy(p, vStringBuf[i].first.c_str(), vStringBuf[i].first.size()); p += vStringBuf[i].first.size(); *(p++) = '\0'; } //------------------------------------------------------ // Relocate String Block for(register INT i=0; i<nFieldsNum; i++) { if(vFieldsType[i] != T_STRING) continue; for(register INT j=0; j<nRecordsNum; j++) { FIELD& theField = m_vDataBuf[j*nFieldsNum+i]; theField.pString = m_pStringBuf + theField.iValue; } } //------------------------------------------------------ //生成索引列 CreateIndex(szFileName, 0); return TRUE; }
BOOL DBCFile::OpenFromMemoryImpl_Text(const CHAR* pMemory, const CHAR* pDeadEnd, const CHAR* szFileName) { const int MAX_LINE_BYTES = 1024*64; //---------------------------------------------------- //分析列数和类型 CHAR szLine[MAX_LINE_BYTES] = {0}; //读第一行 register const char* pMem = pMemory; pMem = _GetLineFromMemory(szLine, MAX_LINE_BYTES, pMem, pDeadEnd); if(!pMem) return FALSE; //分解 std::vector< std::string > vRet; _ConvertStringToVector(szLine, vRet, "\t", TRUE, TRUE); if(vRet.empty()) return FALSE; //生成Field Types FILEDS_TYPE vFieldsType; vFieldsType.resize(vRet.size()); for(INT i=0; i<(INT)vRet.size(); i++) { if(vRet[i] == "INT") vFieldsType[i] = T_INT; else if(vRet[i] == "FLOAT") vFieldsType[i] = T_FLOAT; else if(vRet[i] == "STRING") vFieldsType[i] = T_STRING; else { return FALSE; } } //-------------------------------------------------------------- //初始化 INT nRecordsNum = 0; INT nFieldsNum = (INT)vFieldsType.size(); //临时字符串区 std::vector< std::pair< std::string, INT > > vStringBuf; //检索表 std::map< std::string, INT > mapStringBuf; //-------------------------------------------------------------- //开始读取 //空读一行 pMem = _GetLineFromMemory(szLine, MAX_LINE_BYTES, pMem, pDeadEnd); if(!pMem) return FALSE; INT nStringBufSize = 0; do { //读取一行 pMem = _GetLineFromMemory(szLine, MAX_LINE_BYTES, pMem, pDeadEnd); if(!pMem) break;; //是否是注释行 if(szLine[0] == '#') continue; //分解 _ConvertStringToVector(szLine, vRet, "\t", TRUE, FALSE); //列数不对 if(vRet.empty()) continue; if(vRet.size() != nFieldsNum) { //补上空格 if((INT)vRet.size() < nFieldsNum) { INT nSubNum = nFieldsNum-(INT)vRet.size(); for(INT i=0; i<nSubNum; i++) { vRet.push_back(""); } } } //第一列不能为空 if(vRet[0].empty()) continue; for(register INT i=0; i<nFieldsNum; i++) { FIELD newField; switch(vFieldsType[i]) { case T_INT: newField.iValue = atoi(vRet[i].c_str()); m_vDataBuf.push_back(newField); break; case T_FLOAT: newField.fValue = (FLOAT)atof(vRet[i].c_str()); m_vDataBuf.push_back(newField); break; case T_STRING: if(vRet[i].empty()) { newField.iValue = 0; } else { const CHAR * p = vRet[i].c_str(); std::map< std::string, INT >::iterator it = mapStringBuf.find(vRet[i]); if(it == mapStringBuf.end()) { vStringBuf.push_back(std::make_pair(vRet[i], nStringBufSize)); mapStringBuf.insert(std::make_pair(vRet[i], (INT)vStringBuf.size()-1)); newField.iValue = nStringBufSize + 1; // first CHAR is '\0' for blank string nStringBufSize += (INT)strlen(vRet[i].c_str()) + 1; } else { newField.iValue = vStringBuf[it->second].second + 1; } } m_vDataBuf.push_back(newField); break; } } nRecordsNum++; }while(TRUE); //-------------------------------------------------------- //生成正式数据库 m_nRecordsNum = nRecordsNum; m_nFieldsNum = nFieldsNum; m_nStringBufSize = nStringBufSize+1; //Create String Blok m_pStringBuf = new CHAR[m_nStringBufSize]; if(!m_pStringBuf) return FALSE; //------------------------------------------------------ // Create Field Types m_theType = vFieldsType; //------------------------------------------------------ // Create String Block UCHAR byBlank = '\0'; m_pStringBuf[0] = '\0'; register CHAR* p = m_pStringBuf + 1; for(INT i=0; i<(INT)vStringBuf.size(); i++) { memcpy(p, vStringBuf[i].first.c_str(), vStringBuf[i].first.size()); p += vStringBuf[i].first.size(); *(p++) = '\0'; } //------------------------------------------------------ // Relocate String Block for(register INT i=0; i<nFieldsNum; i++) { if(vFieldsType[i] != T_STRING) continue; for(register INT j=0; j<nRecordsNum; j++) { FIELD& theField = m_vDataBuf[j*nFieldsNum+i]; theField.pString = m_pStringBuf + theField.iValue; } } //------------------------------------------------------ //生成索引列 CreateIndex(0, szFileName); return TRUE; }
BOOL DBCFile::OpenFromMemoryImpl_Binary(const CHAR* pMemory, const CHAR* pDeadEnd, const CHAR* szFileName) { register const char* pMem = pMemory; //---------------------------------------------------- //Read Head FILE_HEAD theHead; memcpy(&theHead, pMem, sizeof(FILE_HEAD)); if(theHead.m_Identify != 0XDDBBCC00 ) { return FALSE; } //check memory size if( sizeof(FILE_HEAD) + sizeof(UINT)*theHead.m_nFieldsNum + sizeof(FIELD)*theHead.m_nRecordsNum * theHead.m_nFieldsNum + theHead.m_nStringBlockSize > (DWORD)(pDeadEnd-pMemory)) { return FALSE; } pMem += sizeof(FILE_HEAD); //---------------------------------------------------- //Init m_nRecordsNum = theHead.m_nRecordsNum; m_nFieldsNum = theHead.m_nFieldsNum; m_nStringBufSize = theHead.m_nStringBlockSize; //--------------------------------------------- //Create String Blok m_pStringBuf = new CHAR[theHead.m_nStringBlockSize]; if(!m_pStringBuf) return FALSE; //------------------------------------------------------ // Read Field Types std::vector< UINT > vFieldType; vFieldType.resize(theHead.m_nFieldsNum); memcpy(&(vFieldType[0]), pMem, sizeof(UINT)*theHead.m_nFieldsNum); pMem += sizeof(UINT)*theHead.m_nFieldsNum; //Check it! m_theType.resize(theHead.m_nFieldsNum); for(INT i=0; i<(INT)theHead.m_nFieldsNum; i++) { switch(vFieldType[i]) { case T_INT: case T_FLOAT: case T_STRING: m_theType[i] = (FIELD_TYPE)vFieldType[i]; break; default: delete[] (m_pStringBuf); return FALSE; } } //------------------------------------------------------ // Read All Field m_vDataBuf.resize(theHead.m_nRecordsNum * theHead.m_nFieldsNum); memcpy(&(m_vDataBuf[0]), pMem, sizeof(FIELD)*theHead.m_nRecordsNum * theHead.m_nFieldsNum); pMem += sizeof(FIELD)*theHead.m_nRecordsNum * theHead.m_nFieldsNum; //------------------------------------------------------ // Read String Block memcpy(m_pStringBuf, pMem, m_nStringBufSize); m_pStringBuf[m_nStringBufSize-1]=0; //------------------------------------------------------ // TO runtime address for(INT j=0; j<(INT)theHead.m_nFieldsNum; j++) { if(vFieldType[j] != T_STRING) continue; for(INT i=0; i<(INT)theHead.m_nRecordsNum; i++) { m_vDataBuf[i*GetFieldsNum()+j].pString += reinterpret_cast<unsigned long long>(m_pStringBuf); } } //------------------------------------------------------ //生成索引列 CreateIndex(0, szFileName); return TRUE; }
/*! \brief Create DB table from VFKDataBlock (SQLITE only) \param poDataBlock pointer to VFKDataBlock instance */ void VFKReaderSQLite::AddDataBlock(IVFKDataBlock *poDataBlock, const char *pszDefn) { const char *pszBlockName; const char *pszKey; CPLString osCommand, osColumn; bool bUnique; VFKPropertyDefn *poPropertyDefn; sqlite3_stmt *hStmt; bUnique = !CSLTestBoolean(CPLGetConfigOption("OGR_VFK_DB_IGNORE_DUPLICATES", "NO")); pszBlockName = poDataBlock->GetName(); /* register table in VFK_DB_TABLE */ osCommand.Printf("SELECT COUNT(*) FROM %s WHERE " "table_name = '%s'", VFK_DB_TABLE, pszBlockName); hStmt = PrepareStatement(osCommand.c_str()); if (ExecuteSQL(hStmt) == OGRERR_NONE && sqlite3_column_int(hStmt, 0) == 0) { osCommand.Printf("CREATE TABLE '%s' (", pszBlockName); for (int i = 0; i < poDataBlock->GetPropertyCount(); i++) { poPropertyDefn = poDataBlock->GetProperty(i); if (i > 0) osCommand += ","; osColumn.Printf("%s %s", poPropertyDefn->GetName(), poPropertyDefn->GetTypeSQL().c_str()); osCommand += osColumn; } osColumn.Printf(",%s integer", FID_COLUMN); osCommand += osColumn; if (poDataBlock->GetGeometryType() != wkbNone) { osColumn.Printf(",%s blob", GEOM_COLUMN); osCommand += osColumn; } osCommand += ")"; ExecuteSQL(osCommand.c_str()); /* CREATE TABLE */ /* create indeces */ osCommand.Printf("%s_%s", pszBlockName, FID_COLUMN); CreateIndex(osCommand.c_str(), pszBlockName, FID_COLUMN, !EQUAL(pszBlockName, "SBP")); pszKey = ((VFKDataBlockSQLite *) poDataBlock)->GetKey(); if (pszKey) { osCommand.Printf("%s_%s", pszBlockName, pszKey); CreateIndex(osCommand.c_str(), pszBlockName, pszKey, bUnique); } if (EQUAL(pszBlockName, "SBP")) { /* create extra indices for SBP */ CreateIndex("SBP_OB", pszBlockName, "OB_ID", FALSE); CreateIndex("SBP_HP", pszBlockName, "HP_ID", FALSE); CreateIndex("SBP_DPM", pszBlockName, "DPM_ID", FALSE); CreateIndex("SBP_OB_HP_DPM", pszBlockName, "OB_ID,HP_ID,DPM_ID", bUnique); CreateIndex("SBP_OB_POR", pszBlockName, "OB_ID,PORADOVE_CISLO_BODU", FALSE); CreateIndex("SBP_HP_POR", pszBlockName, "HP_ID,PORADOVE_CISLO_BODU", FALSE); CreateIndex("SBP_DPM_POR", pszBlockName, "DPM_ID,PORADOVE_CISLO_BODU", FALSE); } else if (EQUAL(pszBlockName, "HP")) { /* create extra indices for HP */ CreateIndex("HP_PAR1", pszBlockName, "PAR_ID_1", FALSE); CreateIndex("HP_PAR2", pszBlockName, "PAR_ID_2", FALSE); } else if (EQUAL(pszBlockName, "OB")) { /* create extra indices for OP */ CreateIndex("OB_BUD", pszBlockName, "BUD_ID", FALSE); } /* update VFK_DB_TABLE meta-table */ osCommand.Printf("INSERT INTO %s (file_name, table_name, " "num_records, num_features, num_geometries, table_defn) VALUES " "('%s', '%s', -1, 0, 0, '%s')", VFK_DB_TABLE, m_pszFilename, pszBlockName, pszDefn); ExecuteSQL(osCommand.c_str()); sqlite3_finalize(hStmt); } return VFKReader::AddDataBlock(poDataBlock, NULL); }
int Load(const char* _filename,const char *base,bool mainaload) { String filename=_filename; struct stat st; filename.ToLower(); if(stat(filename,&st)==-1)return MEFailedToOpen; TagFileInfo *fi=NULL; for(int i=0;i<files.Count();i++) { if(files[i]->filename==filename) { fi=files[i]; break; } } if(!fi) { fi=new TagFileInfo; fi->filename=filename; fi->modtm=st.st_mtime; fi->mainaload=mainaload; fi->addToLoadBases(base); files.Push(fi); }else { fi->addToLoadBases(base); if(fi->modtm==st.st_mtime) { return 1; } } struct stat sti; if(fi->indexFile.Length()>0) { if(stat(fi->indexFile,&sti)!=-1 && sti.st_mtime==st.st_mtime) { LoadIndex(fi); return 1; } }else if(FindIdx(fi)) { if(stat(fi->indexFile,&sti)!=-1 && sti.st_mtime==st.st_mtime) { LoadIndex(fi); return 1; } } if(fi->indexFile.Length()==0) { for(;;) { fi->indexFile=fi->filename; fi->indexFile+=".idx"; FILE *f=fopen(fi->indexFile,"wb"); if(f) { fclose(f); remove(fi->indexFile); break; } char idxdir[512]; const char *dirs[]={"TAGS_INDEX","TEMP","TMP",NULL}; const char **dir=dirs; for(;*dir;dir++) { if(GetEnvironmentVariable(*dir,idxdir,sizeof(idxdir))) { fi->indexFile=fi->filename+".idx"; fi->indexFile.Replace('\\','_'); fi->indexFile.Replace(':','_'); if(idxdir[strlen(idxdir)-1]!='\\') { fi->indexFile.Insert(0,"\\"); } fi->indexFile.Insert(0,idxdir); f=fopen(fi->indexFile,"wb"); if(f) { fclose(f); remove(fi->indexFile); break; } fi->indexFile=""; } } break; } } if(fi->indexFile.Length()==0) { return MFailedToWriteIndex; } fi->modtm=st.st_mtime; CreateIndex(fi); return 0; }