o2on svn commit
o2on-****@lists*****
2008年 4月 7日 (月) 00:02:33 JST
Revision: 71 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=o2on&view=rev&rev=71 Author: k-uehara Date: 2008-04-07 00:02:33 +0900 (Mon, 07 Apr 2008) Log Message: ----------- fFirebird対å¿: select 追å ãã°ãã£ãã¯ã¹ Modified Paths: -------------- trunk/o2on/src.o2on/O2DatDB.cpp trunk/o2on/src.o2on/O2DatDB.h Modified: trunk/o2on/src.o2on/O2DatDB.cpp =================================================================== --- trunk/o2on/src.o2on/O2DatDB.cpp 2008-04-03 14:40:13 UTC (rev 70) +++ trunk/o2on/src.o2on/O2DatDB.cpp 2008-04-06 15:02:33 UTC (rev 71) @@ -108,7 +108,7 @@ errmsg += msg; errmsg += " "; } - Logger->AddLog(O2LT_ERROR, L"Firebird", 0, 0, "%s", errmsg); + Logger->AddLog(O2LT_ERROR, L"Firebird", 0, 0, "%s", errmsg.c_str()); } #else void @@ -324,6 +324,9 @@ isc_dsql_describe_bind(status, &stmt, 3, outda); else isc_dsql_describe(status, &stmt, 3, outda); + } else if (outda->sqld ==0) { + delete(outda); + return NULL; } int i; for (var = outda->sqlvar, i=0; i < outda->sqld; i++, var++) { @@ -354,11 +357,7 @@ return; XSQLVAR* var = sqlda->sqlvar; for (int i=0; i < sqlda->sqln; i++,var++){ - if ((var->sqltype & ~1) != SQL_VARYING) { - if (var->sqldata) delete(var->sqldata); - } else { - delete(var->sqldata); - } + delete(var->sqldata); if (var->sqltype & 1) { delete(var->sqlind); } @@ -495,7 +494,7 @@ if (isc_attach_database(status, 0, dbfilenameA.c_str(), &db, dpblen, dpb_buff)) goto error; - char *sql = + char *sql0 = "EXECUTE BLOCK AS BEGIN " "if (not exists(select 1 from rdb$relations where rdb$relation_name = 'DAT')) then " "execute statement 'create table DAT (" @@ -513,10 +512,26 @@ ");';" "END"; + char *sql1 = + "EXECUTE BLOCK AS BEGIN " + "if (not exists(select 1 from RDB$INDICES where RDB$INDEX_NAME = 'IDX_DAT_LASTPUBLISH')) then " + "execute statement 'create index idx_dat_lastpublish on dat (lastpublish);';" + "END"; + + char *sql2 = + "EXECUTE BLOCK AS BEGIN " + "if (not exists(select 1 from RDB$INDICES where RDB$INDEX_NAME = 'IDX_DAT_DOMAIN_BBSNAME_DATNAME')) then " + "execute statement 'create index idx_dat_domain_bbsname_datname on dat (bbsname, domainname, datname);';" + "END"; + if (isc_start_transaction(status, &tr, 1, &db, 0, NULL)) goto error; - if (isc_dsql_execute_immediate(status, &db, &tr, 0, sql, 3, NULL)) + if (isc_dsql_execute_immediate(status, &db, &tr, 0, sql0, 3, NULL)) goto error; + if (isc_dsql_execute_immediate(status, &db, &tr, 0, sql1, 3, NULL)) + goto error; + if (isc_dsql_execute_immediate(status, &db, &tr, 0, sql2, 3, NULL)) + goto error; if (isc_commit_transaction(status, &tr)) goto error; if (isc_detach_database(status, &db)) @@ -659,44 +674,54 @@ goto error; if (isc_dsql_prepare(status, &tr, &stmt, 0, sqlstr.c_str(), 3, NULL)) goto error; - XSQLDA *sqlda = prepare_xsqlda(stmt, false); + outda = prepare_xsqlda(stmt, false); - if (isc_dsql_execute(status, &tr, &stmt, 3, NULL)) + //if (outda == NULL) { + // if(isc_dsql_execute(status, &tr, &stmt,3, NULL)) + // goto error; + // goto fin; + //} else if (isc_dsql_execute2(status, &tr, &stmt, 3, outda)) + // goto error; + fetch = isc_dsql_execute2(status, &tr, &stmt, 3, NULL, outda); + if(fetch !=0 &&isc_sqlcode(status) != 100L) goto error; - if (isc_dsql_fetch(status, &stmt, 3, sqlda)) - goto error; if (out.empty()) { //êsÚ cols.clear(); - get_column_names(sqlda, cols); + get_column_names(outda, cols); out.push_back(cols); cols.clear(); - get_columns(sqlda, cols); + get_columns(outda, cols); out.push_back(cols); ct++; } - while (fetch = isc_dsql_fetch(status, &stmt, 3, sqlda) == 0){ + while (fetch = isc_dsql_fetch(status, &stmt, 3, outda) == 0){ //2sÚÈ~ cols.clear(); - get_columns(sqlda, cols); + get_columns(outda, cols); out.push_back(cols); ct++; } +fin: if (isc_commit_transaction(status, &tr)) goto error; if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); if (db) isc_detach_database(status, &db); - free_xsqlda(sqlda); + free_xsqlda(outda); return (ct); error: + out.clear(); + cols.clear(); + cols.push_back(L"SQL error"); + out.push_back(cols); log(status); if (tr) isc_rollback_transaction(status, &tr); if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); if (db) isc_detach_database(status, &db); - free_xsqlda(sqlda); + free_xsqlda(outda); return (0); } @@ -792,11 +817,10 @@ outda = prepare_xsqlda(stmt, false); inda = prepare_xsqlda(stmt, true); - //inda->sqlvar->sqldata = (char*) hash.block; - memcpy(inda->sqlvar->sqldata, hash.data(), hash.size()); + if(!bind(inda, 0, hash)) + goto error; - long st = isc_dsql_execute2(status, &tr, &stmt, 3, inda, outda); - if (st == 0) { + if (!isc_dsql_execute2(status, &tr, &stmt, 3, inda, outda)) { get_columns(outda, out); ret = true; } else if (isc_sqlcode(status) != 100L) @@ -820,12 +844,164 @@ return (false); } +bool +O2DatDB:: +select(O2DatRec &out, const wchar_t *domain, const wchar_t *bbsname) +{ +#if TRACE_SQL_EXEC_TIME + stopwatch sw("select by domain bbsname"); +#endif + isc_db_handle db = NULL; + isc_tr_handle tr = NULL; + ISC_STATUS_ARRAY status; + isc_stmt_handle stmt = NULL; + XSQLDA *outda = NULL, *inda = NULL; + bool ret = false; + if (isc_attach_database(status, 0, dbfilenameA.c_str(), &db, dpblen, dpb_buff)) + goto error; + char *sql = + "select first 1" + COLUMNSA + " from dat" + " where domainname = ?" + " and bbsname = ?" + " order by rand();"; + if (isc_dsql_allocate_statement(status, &db, &stmt)) + goto error; + if (isc_start_transaction(status, &tr, 1, &db, 0, NULL)) + goto error; + if (isc_dsql_prepare(status, &tr, &stmt, 0, sql, 3, NULL)) + goto error; + outda = prepare_xsqlda(stmt, false); + inda = prepare_xsqlda(stmt, true); + + if (!bind(inda, 0, domain)) + goto error; + if (!bind(inda, 1, bbsname)) + goto error; + + if (!isc_dsql_execute2(status, &tr, &stmt, 3, inda, outda)) { + get_columns(outda, out); + ret = true; + } else if (isc_sqlcode(status) != 100L) + goto error; + + if (isc_commit_transaction(status, &tr)) + goto error; + if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); + if (db) isc_detach_database(status, &db); + free_xsqlda(inda); + free_xsqlda(outda); + return (ret); + +error: + log(status); + if (tr) isc_rollback_transaction(status, &tr); + if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); + if (db) isc_detach_database(status, &db); + free_xsqlda(inda); + free_xsqlda(outda); + return (false); +/* + + wchar_t *sql = + L"select" + COLUMNS + L" from dat" + L" where domain = ?" + L" and bbsname = ?" + L" order by random() limit 1;"; + +*/ +} + + + + bool O2DatDB:: +select(O2DatRec &out, const wchar_t *domain, const wchar_t *bbsname, const wchar_t *datname) +{ +#if TRACE_SQL_EXEC_TIME + stopwatch sw("select by domain bbsname datname"); +#endif + + isc_db_handle db = NULL; + isc_tr_handle tr = NULL; + ISC_STATUS_ARRAY status; + isc_stmt_handle stmt = NULL; + XSQLDA *outda = NULL, *inda = NULL; + bool ret = false; + + if (isc_attach_database(status, 0, dbfilenameA.c_str(), &db, dpblen, dpb_buff)) + goto error; + + char *sql = + "select" + COLUMNSA + " from dat" + " where domainname = ?" + " and bbsname = ?" + " and datname = ?;"; + if (isc_dsql_allocate_statement(status, &db, &stmt)) + goto error; + if (isc_start_transaction(status, &tr, 1, &db, 0, NULL)) + goto error; + if (isc_dsql_prepare(status, &tr, &stmt, 0, sql, 3, NULL)) + goto error; + outda = prepare_xsqlda(stmt, false); + inda = prepare_xsqlda(stmt, true); + + if (!bind(inda, 0, domain)) + goto error; + if (!bind(inda, 1, bbsname)) + goto error; + if (!bind(inda, 2, datname)) + goto error; + + if (!isc_dsql_execute2(status, &tr, &stmt, 3, inda, outda)) { + get_columns(outda, out); + ret = true; + } else if (isc_sqlcode(status) != 100L) + goto error; + + if (isc_commit_transaction(status, &tr)) + goto error; + if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); + if (db) isc_detach_database(status, &db); + free_xsqlda(inda); + free_xsqlda(outda); + return (ret); + +error: + log(status); + if (tr) isc_rollback_transaction(status, &tr); + if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); + if (db) isc_detach_database(status, &db); + free_xsqlda(inda); + free_xsqlda(outda); + return (false); +/* + + wchar_t *sql = + L"select" + COLUMNS + L" from dat" + L" where domain = ?" + L" and bbsname = ?" + L" and datname = ?;"; +*/ +} + + + + +bool +O2DatDB:: select(O2DatRecList &out) { #if TRACE_SQL_EXEC_TIME @@ -1044,6 +1220,7 @@ ISC_STATUS_ARRAY status; isc_stmt_handle stmt = NULL; isc_tr_handle tr = NULL; + XSQLDA *sqlda = NULL; if (isc_attach_database(status, 0, dbfilenameA.c_str(), &db, dpblen, dpb_buff)) goto error; @@ -1054,9 +1231,9 @@ goto error; if (isc_start_transaction(status, &tr, 1, &db, 0, NULL)) goto error; - if (isc_dsql_prepare(status, &tr, &stmt, 0, sql, 1, NULL)) + if (isc_dsql_prepare(status, &tr, &stmt, 0, sql, 3, NULL)) goto error; - XSQLDA *sqlda = prepare_xsqlda(stmt, false); + sqlda = prepare_xsqlda(stmt, false); if (isc_dsql_execute(status, &tr, &stmt, 1, NULL)) goto error; @@ -1066,9 +1243,9 @@ if (isc_commit_transaction(status, &tr)) goto error; - free_xsqlda(sqlda); if (stmt) isc_dsql_free_statement(status, &stmt, DSQL_drop); if (db) isc_detach_database(status, &db); + free_xsqlda(sqlda); return (count); @@ -1100,6 +1277,7 @@ uint64 total = 0; long num; long fetch; + XSQLDA *sqlda = NULL; if (isc_attach_database(status, 0, dbfilenameA.c_str(), &db, dpblen, dpb_buff)) goto error; @@ -1113,7 +1291,7 @@ goto error; if (isc_dsql_prepare(status, &tr, &stmt, 0, sql, 3, NULL)) goto error; - XSQLDA *sqlda = prepare_xsqlda(stmt, false); + sqlda = prepare_xsqlda(stmt, false); if (isc_dsql_execute(status, &tr, &stmt, 3, NULL)) goto error; @@ -1166,6 +1344,7 @@ ISC_STATUS_ARRAY status; isc_stmt_handle stmt = NULL; isc_tr_handle tr = NULL; + XSQLDA *sqlda = NULL; if (isc_attach_database(status, 0, dbfilenameA.c_str(), &db, dpblen, dpb_buff)) goto error; @@ -1178,13 +1357,13 @@ goto error; if (isc_dsql_prepare(status, &tr, &stmt, 0, sql, 3, NULL)) goto error; - XSQLDA *sqlda = prepare_xsqlda(stmt, false); + sqlda = prepare_xsqlda(stmt, false); if (isc_dsql_execute(status, &tr, &stmt, 3, NULL)) goto error; if (isc_dsql_fetch(status, &stmt, 1, sqlda)) goto error; - uint64 totalsize = *(uint64 *)sqlda->sqlvar[0].sqldata;//over flow? + uint64 totalsize = *(long *)sqlda->sqlvar[0].sqldata;//over flow? if (isc_commit_transaction(status, &tr)) goto error; @@ -1231,15 +1410,14 @@ goto error; outda = prepare_xsqlda(stmt, false); inda = prepare_xsqlda(stmt, true); + if (!bind(inda, 0, time(NULL)-publish_tt)) + goto error; - inda->sqlvar->sqltype = SQL_INT64; - *(ISC_INT64*)inda->sqlvar->sqldata = publish_tt; - if (!isc_dsql_execute2(status, &tr, &stmt, 3, inda, outda)) if (isc_sqlcode(status) == 100L) goto error; - uint64 ret = *(ISC_INT64*)outda->sqlvar->sqldata; + uint64 ret = *(long*)outda->sqlvar->sqldata; if (isc_commit_transaction(status, &tr)) goto error; @@ -1262,6 +1440,16 @@ +#if 0 +bool +O2DatDB:: +update(O2DatRec &in, bool is_origurl) +{ +} +#endif + + + void O2DatDB:: update(O2DatRecList &in) @@ -1343,7 +1531,7 @@ if (!bind(da_insert, 10, (uint64)0)) goto error; if (isc_dsql_execute(status, &tr, &stmt_insert, 3, da_insert)) - goto error; + ;//goto error; free_xsqlda(da_insert); } else if (it->userdata = 0) { da_update = prepare_xsqlda(stmt_update, true); @@ -1386,7 +1574,7 @@ return ; error: - log(status); + log(status); if (tr) isc_rollback_transaction(status, &tr); if (stmt_insert) isc_dsql_free_statement(status, &stmt_insert, DSQL_drop); if (stmt_update) isc_dsql_free_statement(status, &stmt_update, DSQL_drop); @@ -1400,8 +1588,21 @@ + +#if 0 bool O2DatDB:: +update_lastpublish(const hashT &hash) +{ + +} +#endif + + + + +bool +O2DatDB:: remove(const hashT &hash) { #if TRACE_SQL_EXEC_TIME Modified: trunk/o2on/src.o2on/O2DatDB.h =================================================================== --- trunk/o2on/src.o2on/O2DatDB.h 2008-04-03 14:40:13 UTC (rev 70) +++ trunk/o2on/src.o2on/O2DatDB.h 2008-04-06 15:02:33 UTC (rev 71) @@ -98,29 +98,7 @@ O2DatDB(O2Logger *lgr, const wchar_t *filename); ~O2DatDB(); -#ifdef O2_DB_FIREBIRD - bool create_table(void); - bool reindex(const char *target); - size_t select(const wchar_t *sql, SQLResultList &out); - bool select(O2DatRec &out); - bool select(O2DatRec &out, const hashT hash); - //bool select(O2DatRec &out, const wchar_t *domain, const wchar_t *bbsname); - bool select(O2DatRecList &out); - bool select(O2DatRecList &out, const wchar_t *domain, const wchar_t *bbsname); - //bool select(O2DatRec &out, const wchar_t *domain, const wchar_t *bbsname, const wchar_t *datname); - bool select(O2DatRecList &out, time_t publish_tt, size_t limit); - uint64 select_datcount(void); - uint64 select_datcount(wstrnummap &out); - uint64 select_totaldisksize(void); - uint64 select_publishcount(time_t publish_tt); - -// bool update(O2DatRec &in, bool is_origurl); - void update(O2DatRecList &in); -// bool update_lastpublish(const hashT &hash); - - bool remove(const hashT &hash); -#else bool create_table(void); bool reindex(const char *target); @@ -142,7 +120,6 @@ // bool update_lastpublish(const hashT &hash); bool remove(const hashT &hash); -#endif void AddUpdateQueue(O2DatRec &in); void AddUpdateQueue(const hashT &hash);