/* test - test out something. */ #include "common.h" #include "portable.h" #include "jksql.h" #include "hash.h" #include "dystring.h" #include "linefile.h" #define sqlQuery sqlGetResult char *database = "test"; void dropAll() /* Drop all tables. */ { struct sqlConnection *conn = sqlConnect(database); sqlGetResult(conn, "drop table word"); sqlGetResult(conn, "drop table lineWords"); sqlGetResult(conn, "drop table lineSize"); sqlGetResult(conn, "drop table commaLine"); sqlDisconnect(&conn); } void createAll() /* Define all tables. */ { struct sqlConnection *conn = sqlConnect(database); sqlGetResult(conn, "CREATE table word (" "id smallint not null primary key," "word varchar(250) not null" ")" ); sqlGetResult(conn, "CREATE table lineSize (" "id int(8) not null primary key," "size smallint not null" ")" ); sqlGetResult(conn, "CREATE table lineWords (" "line int(8) not null," "word smallint not null," "pos smallint not null" ")" ); sqlGetResult(conn, "CREATE table commaLine (" "id int(8) not null primary key," "size smallint not null," "wordList blob not null" ")" ); sqlDisconnect(&conn); } void freshTables() /* Make database have defined but empty tables. */ { dropAll(); createAll(); } int tokSize(char *text, int size) /* Return size of next token of text. */ { char c; int i; c = text[0]; if (isspace(c)) { for (i=1; i 0; sizeLeft -= wordSize, line += wordSize) { wordSize = tokSize(line, sizeLeft); if (wordSize >= sizeof(wordBuf)) errAbort("Word too long line %d of %s", lf->lineIx, lf->fileName); memcpy(wordBuf, line, wordSize); wordBuf[wordSize] = 0; if (wordBuf[wordSize-1] == ' ') wordBuf[wordSize-1] = '_'; if ((hel = hashLookup(wordHash, wordBuf)) == NULL) { wordId = highestWordId++; hel = hashAdd(wordHash, wordBuf, nullPt + wordId); dyStringClear(query); if (wordBuf[0] == '\'') { dyStringPrintf(query, "INSERT into word values (%d, \"'\")", wordId); } else if (wordBuf[0] == '\\') { dyStringPrintf(query, "INSERT into word values (%d, '\\\\')", wordId); } else { dyStringPrintf(query, "INSERT into word values (%d, '%s')", wordId, wordBuf); } sqlGetResult(conn, query->string); } else { wordId = (char *)(hel->val) - nullPt; } if (wordCount >= ArraySize(lineBuf)) { errAbort("Too many words in line %d of %s", lf->lineIx, lf->fileName); } lineBuf[wordCount++] = wordId; } /* Store the words in the database */ dyStringClear(query); dyStringPrintf(query, "INSERT into commaLine values (%d, %d, '", lineCount, wordCount); for (i=0; istring); dyStringClear(query); dyStringPrintf(query, "INSERT into lineSize values (%d,%d)", lineCount, wordCount); sqlGetResult(conn, query->string); for (i=0; istring); } ++lineCount; } sqlDisconnect(&conn); } void usage() { errAbort("usage: test file\n"); } char **loadWords() /* Load words from table into array. */ { struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr; int wordCount; char **words = NULL; int i = 0; char **row; wordCount = sqlTableSize(conn, "word"); uglyf("Got %d words\n", wordCount); words = needMem(wordCount * sizeof(words[0])); sr = sqlQuery(conn, "select * from word"); while ((row = sqlNextRow(sr)) != NULL) { words[i] = cloneString(row[1]); ++i; } printf("wordCount %d i %d\n", wordCount, i); sqlFreeResult(&sr); sqlDisconnect(&conn); return words; } void noOutput(FILE *f, char *s) /* Write string to file (maybe) */ { } void fileOutput(FILE *f, char *s) /* Write string to file (maybe) */ { fputs(s, f); } void commaRecon(char *fileName) /* Do comma based reconstruction. */ { char **words; long start, end; struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr; char **row; FILE *f = mustOpen(fileName, "w"); start = clock1000(); words = loadWords(); end = clock1000(); printf("Time to load words: %4.3f\n", 0.001*(end-start)); start = clock1000(); sr = sqlQuery(conn, "SELECT * from commaLine"); while ((row = sqlNextRow(sr)) != NULL) { int wordCount = sqlUnsigned(row[1]); int i; char *s = row[2],*e; int wordIx; for (i=0; i