12 keyFile = certPath + QString(
"server-key.pem" );
13 certFile = certPath + QString(
"server-cert.pem" );
14 caFile = certPath + QString(
"ca-cert.pem" );
18 db = mysql_init( NULL );
28 keyFile = certPath + QString(
"server-key.pem" );
29 certFile = certPath + QString(
"server-cert.pem" );
30 caFile = certPath + QString(
"ca-cert.pem" );
34 db = mysql_init( NULL );
37 if ( !
connect( masterPW, err ) )
40 error =
"US_DB2 error: could not connect\n" + err;
53 mysql_free_result(
result );
63 const QString&,
const QString&, QString& ) {
return false; }
66 const QString& host,
const QString& dbname,
67 const QString& user,
const QString& password,
72 MYSQL* conn = mysql_init( NULL );
75 error = QString(
"Test connection allocation error" );
79 QString uhost = host.section(
":", 0, 0 ).simplified();
80 int uport = host.section(
":", 1, 1 ).simplified().toInt();
82 bool status = mysql_real_connect(
88 uport, NULL, CLIENT_MULTI_STATEMENTS );
91 error = QString(
"Test connection open error\n" ) + mysql_error( conn );
101 const QString&,
const QString&,
102 const QString&,
const QString&,
103 const QString&,
const QString&,
104 QString& ) {
return false; }
107 const QString& host,
const QString& dbname,
108 const QString& user,
const QString& password,
109 const QString& email,
const QString& pw,
116 error = QString(
"Test secure connection allocation error" );
129 QString uhost = host.section(
":", 0, 0 ).simplified();
130 int uport = host.section(
":", 1, 1 ).simplified().toInt();
132 bool status = mysql_real_connect(
138 uport, NULL, CLIENT_MULTI_STATEMENTS );
142 error = QString(
"Test secure connection open error\n" ) +
144 err = mysql_error(
db );
150 QString q =
"CALL validate_user( '', '" + email +
"', '" + pw +
"' )";
175 if ( defaultDB.size() < 6 )
178 error =
"US_DB2 error: DB not configured";
183 QString user = defaultDB.at( 1 );
184 QString dbname = defaultDB.at( 2 );
185 QString host = defaultDB.at( 3 );
186 QString cipher = defaultDB.at( 4 );
187 QString iv = defaultDB.at( 5 );
203 QString uhost = host.section(
":", 0, 0 ).simplified();
204 int uport = host.section(
":", 1, 1 ).simplified().toInt();
213 uport, NULL, CLIENT_MULTI_STATEMENTS );
217 catch ( std::exception &e )
221 err =
"US_DB2: uncaught exception " +
error;
231 error = QString(
"Connect open error: " ) + mysql_error(
db );
236 email = defaultDB.at( 6 );
239 cipher = defaultDB.at( 7 );
240 iv = defaultDB.at( 8 );
242 guid = defaultDB.at( 9 );
244 QString q =
"CALL validate_user( '" +
guid +
"', '', '" +
userPW +
"' )";
261 for (
int i = 0; i < dbinfo.size(); i++ )
263 QStringList info = dbinfo.at( i );
265 if ( info.at( 9 ) ==
guid )
268 info.replace( 6,
email );
270 dbinfo.replace( i, info );
284 const QString&, QString& ){
return false; }
287 const QString& host,
const QString& dbname,
288 const QString& user,
const QString& password,
306 QString uhost = host.section(
":", 0, 0 ).simplified();
307 int uport = host.section(
":", 1, 1 ).simplified().toInt();
315 uport, NULL, CLIENT_MULTI_STATEMENTS );
318 catch ( std::exception &e )
328 error = QString(
"Connect open error: " ) + mysql_error(
db );
343 mysql_free_result(
result );
345 while ( mysql_next_result(
db ) == 0 )
348 mysql_free_result(
result );
352 if ( mysql_query(
db, sqlQuery.toAscii() ) != 0 )
353 error = QString(
"MySQL error: " ) + mysql_error(
db );
372 mysql_free_result(
result );
378 this->
rawQuery(
"SELECT last_error()" );
383 mysql_free_result(
result );
413 mysql_free_result(
result );
416 if ( mysql_next_result(
db ) == 0 )
421 if ( mysql_field_count(
db ) == 0 )
432 this->
rawQuery(
"SELECT last_error()" );
438 mysql_free_result(
result );
455 QString newquery =
"CALL " + arguments[ 0 ]
458 for (
int i = 1; i < arguments.size(); i++ )
460 QString arg = arguments[ i ];
461 arg.replace(
"'",
"\\'" );
463 newquery +=
", '" + arg +
"'";
474 QString newquery =
"SELECT " + arguments[ 0 ]
477 for (
int i = 1; i < arguments.size(); i++ )
479 QString arg = arguments[ i ];
480 arg.replace(
"'",
"\\'" );
482 newquery +=
", '" + arg +
"'";
499 if ( (
row = mysql_fetch_row(
result ) ) != NULL )
508 QVariant
US_DB2::value(
unsigned ){
return QVariant::Invalid; }
512 if (
row && ( index < mysql_field_count(
db ) ) )
515 return QVariant::Invalid;
529 return (
result )? ( (int) mysql_num_rows(
result ) ) : -1;
537 const QString& procedure,
const int tableID )
540 QFile fin( filename );
542 if ( ! fin.open( QIODevice::ReadOnly ) )
544 error = QString(
"writeBlob: cannot open file " ) + filename;
549 QByteArray blobData = fin.readAll();
552 if ( blobData.size() < 1 )
554 error = QString(
"writeBlob: no data in file " ) + filename;
561 error = QString(
"writeBlob: don't know which record data belongs to in " ) + filename;
567 QByteArray blobData_escaped;
571 QByteArray checksum =
572 QCryptographicHash::hash( blobData, QCryptographicHash::Md5 ).toHex();
575 QString queryPart1 =
"CALL " + procedure +
578 "', " + QString::number( tableID ) +
580 QByteArray sqlQuery( escaped_length
582 + checksum.size() + 7,
'\0' );
583 strcpy( sqlQuery.data(), queryPart1.toAscii() );
584 char* queryPtr = sqlQuery.data() + queryPart1.size();
585 memcpy( queryPtr, blobData_escaped.data(), escaped_length );
586 queryPtr += escaped_length;
587 strcpy( queryPtr,
"', '" );
589 memcpy( queryPtr, checksum.data(), checksum.size() );
590 queryPtr += checksum.size();
591 strcpy( queryPtr,
"')\0" );
609 mysql_free_result(
result );
611 while ( mysql_next_result(
db ) == 0 )
614 mysql_free_result(
result );
618 if ( mysql_query(
db, sqlQuery.data() ) != 0 )
620 error = QString(
"MySQL error: " ) + mysql_error(
db );
631 mysql_free_result(
result );
636 error = QString(
"writeBlob: data transmission error (MD5 checksum)" ) ;
656 const QString& procedure,
const int tableID )
659 QString sqlQuery =
"CALL " + procedure +
662 "', " + QString::number( tableID ) +
671 mysql_free_result(
result );
673 while ( mysql_next_result(
db ) == 0 )
676 mysql_free_result(
result );
680 if ( mysql_query(
db, sqlQuery.toAscii() ) !=
OK )
682 error = QString(
"MySQL error: " ) + mysql_error(
db );
692 mysql_free_result(
result );
696 if ( mysql_next_result(
db ) == 0 )
705 QByteArray aucData(
row[ 0 ], lengths[ 0 ] );
706 QByteArray checksum =
row[ 1 ];
707 QByteArray calculated =
708 QCryptographicHash::hash( aucData, QCryptographicHash::Md5 ).toHex();
710 mysql_free_result(
result );
714 QFile fout( filename );
715 if ( checksum != calculated )
717 error = QString(
"readBlob: data transmission error (MD5 checksum)" ) ;
722 else if ( ! fout.open( QIODevice::WriteOnly ) )
724 error = QString(
"readBlob: could not write file " ) + filename;
731 fout.write( aucData );
748 this->
rawQuery(
"SELECT last_insertID()" );
753 ID = atoi(
row[ 0 ] );
754 mysql_free_result(
result );
770 this->
rawQuery(
"SELECT last_debug()" );
775 mysql_free_result(
result );
788 to.resize( length * 2 + 1 );
790 const char* fromPtr = from.data();
791 char* toPtr = to.data();
793 ulong to_length = mysql_real_escape_string(
db, toPtr, fromPtr, length );
797 strcpy( toPtr,
"\0" );
800 to.resize( to_length + 1 );
812 int retCode = gz.
gzip( filename );
814 QString fn = filename;
834 QString fn = filename +
".gz";
843 t.open( QIODevice::ReadOnly );
848 if ( buf[ 0 ] ==
'\037' && buf[ 1 ] ==
'\213' )
851 retCode = gz.
gunzip( fn );
855 QFile::rename( fn, filename );