Datenbank.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. #include "Datenbank.h"
  2. // Inhalt der CSDatenbank Klasse aus Datenbank.h
  3. // Konstruktor
  4. CSDatenbank::CSDatenbank( InitDatei *zIni )
  5. {
  6. if( !zIni->wertExistiert( "DBBenutzer" ) )
  7. zIni->addWert( "DBBenutzer", "chatserveru" );
  8. if( !zIni->wertExistiert( "DBPasswort" ) )
  9. zIni->addWert( "DBPasswort", "LTChatServerPW" );
  10. if( !zIni->wertExistiert( "DBName" ) )
  11. zIni->addWert( "DBName", "lenck_tech_db" );
  12. if( !zIni->wertExistiert( "DBIP" ) )
  13. zIni->addWert( "DBIP", "127.0.0.1" );
  14. if( !zIni->wertExistiert( "DBPort" ) )
  15. zIni->addWert( "DBPort", "5432" );
  16. datenbank = new Datenbank( zIni->zWert( "DBBenutzer" )->getText(), zIni->zWert( "DBPasswort" )->getText(),
  17. zIni->zWert( "DBName" )->getText(), zIni->zWert( "DBIP" )->getText(),
  18. (unsigned short)TextZuInt( zIni->zWert( "DBPort" )->getText(), 10 ) );
  19. InitializeCriticalSection( &cs );
  20. ref = 1;
  21. }
  22. // Destruktor
  23. CSDatenbank::~CSDatenbank()
  24. {
  25. datenbank->release();
  26. DeleteCriticalSection( &cs );
  27. }
  28. // nicht constant
  29. void CSDatenbank::lock()
  30. {
  31. EnterCriticalSection( &cs );
  32. }
  33. void CSDatenbank::unlock()
  34. {
  35. LeaveCriticalSection( &cs );
  36. }
  37. int CSDatenbank::istAdministrator( const char *name, const char *passwort )
  38. {
  39. Text *befehl = new Text( "SELECT id FROM benutzer WHERE name = '" );
  40. Text n( name );
  41. n.ersetzen( "'", "''" );
  42. befehl->append( (char*)n );
  43. befehl->append( "' AND passwort = '" );
  44. Text p( passwort );
  45. p.ersetzen( "'", "''" );
  46. befehl->append( (char*)p );
  47. befehl->append( "'" );
  48. lock();
  49. datenbank->befehl( befehl->getText() );
  50. Result res = datenbank->getResult();
  51. unlock();
  52. befehl->release();
  53. int ret = 0;
  54. if( res.zeilenAnzahl > 0 )
  55. ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  56. res.destroy();
  57. return ret;
  58. }
  59. bool CSDatenbank::adminHatRecht( int id, int recht )
  60. {
  61. Text *befehl = new Text( "SELECT * FROM benutzer_rechte WHERE benutzer_id = " );
  62. befehl->append( id );
  63. befehl->append( " AND rechte_id = " );
  64. befehl->append( recht );
  65. lock();
  66. datenbank->befehl( befehl->getText() );
  67. int ret = datenbank->getZeilenAnzahl();
  68. unlock();
  69. befehl->release();
  70. return ret != 0;
  71. }
  72. bool CSDatenbank::proveKlient( int num, int sNum )
  73. {
  74. Text *befehl = new Text( "SELECT * FROM server_chat_clients WHERE server_chat_id = " );
  75. befehl->append( sNum );
  76. befehl->append( " AND client_id = " );
  77. befehl->append( num );
  78. lock();
  79. datenbank->befehl( befehl->getText() );
  80. Result res = datenbank->getResult();
  81. unlock();
  82. befehl->release();
  83. bool ret = 0;
  84. if( res.zeilenAnzahl == 1 )
  85. ret = 1;
  86. res.destroy();
  87. return ret;
  88. }
  89. Text *CSDatenbank::getKlientKey( int cId )
  90. {
  91. lock();
  92. if( !datenbank->befehl( Text( "SELECT schluessel FROM clients WHERE id = " ) += cId ) )
  93. {
  94. unlock();
  95. return 0;
  96. }
  97. Result res = datenbank->getResult();
  98. unlock();
  99. if( !res.zeilenAnzahl )
  100. {
  101. res.destroy();
  102. return 0;
  103. }
  104. Text *ret = new Text( res.values[ 0 ].getText() );
  105. res.destroy();
  106. return ret;
  107. }
  108. void CSDatenbank::unregisterKlient( int num, int sNum )
  109. {
  110. Text *befehl = new Text( "DELETE FROM server_chat_clients WHERE client_id = " );
  111. befehl->append( num );
  112. befehl->append( " AND server_chat_id = " );
  113. befehl->append( sNum );
  114. lock();
  115. datenbank->befehl( befehl->getText() );
  116. int za = datenbank->getZeilenAnzahl();
  117. unlock();
  118. if( za == 1 )
  119. {
  120. befehl->setText( "UPDATE server_chat SET clients = clients - 1 WHERE id = " );
  121. befehl->append( sNum );
  122. lock();
  123. datenbank->befehl( befehl->getText() );
  124. unlock();
  125. }
  126. befehl->release();
  127. }
  128. bool CSDatenbank::serverAnmelden( InitDatei *zIni )
  129. {
  130. if( !zIni->wertExistiert( "ServerId" ) )
  131. zIni->addWert( "ServerId", "0" );
  132. if( !zIni->wertExistiert( "ServerName" ) )
  133. zIni->addWert( "ServerName", "Name" );
  134. if( !zIni->wertExistiert( "ServerPort" ) )
  135. zIni->addWert( "ServerPort", "49144" );
  136. if( !zIni->wertExistiert( "ServerIP" ) )
  137. zIni->addWert( "ServerIP", "127.0.0.1" );
  138. if( !zIni->wertExistiert( "AdminServerPort" ) )
  139. zIni->addWert( "AdminServerPort", "49143" );
  140. if( !zIni->wertExistiert( "Aktiv" ) )
  141. zIni->addWert( "Aktiv", "FALSE" );
  142. if( !zIni->wertExistiert( "MaxClients" ) )
  143. zIni->addWert( "MaxClients", "50" );
  144. bool insert = 0;
  145. int id = *zIni->zWert( "ServerId" );
  146. if( id )
  147. {
  148. lock();
  149. if( !datenbank->befehl( Text( "SELECT id FROM server_chat WHERE id = " ) += id ) )
  150. {
  151. unlock();
  152. return 0;
  153. }
  154. int anz = datenbank->getZeilenAnzahl();
  155. unlock();
  156. insert = anz == 0;
  157. if( !insert )
  158. {
  159. lock();
  160. if( !datenbank->befehl( Text( "SELECT id FROM server_chat WHERE server_status_id = 1 AND id = " ) += id ) )
  161. {
  162. unlock();
  163. return 0;
  164. }
  165. int anz = datenbank->getZeilenAnzahl();
  166. unlock();
  167. if( !anz ) // Server läuft bereits
  168. return 0;
  169. }
  170. }
  171. if( insert || !id )
  172. { // Neuer Eintrag in Tabelle server_chat
  173. Text *befehl = new Text( "INSERT INTO server_chat( " );
  174. if( id )
  175. *befehl += "id, ";
  176. *befehl += "name, ip, port, admin_port, server_status_id, max_clients ) VALUES( ";
  177. if( id )
  178. {
  179. *befehl += id;
  180. *befehl += ", ";
  181. }
  182. *befehl += "'";
  183. *befehl += zIni->zWert( "ServerName" )->getText();
  184. *befehl += "', '";
  185. *befehl += zIni->zWert( "ServerIP" )->getText();
  186. *befehl += "', ";
  187. *befehl += zIni->zWert( "ServerPort" )->getText();
  188. *befehl += ", ";
  189. *befehl += zIni->zWert( "AdminServerPort" )->getText();
  190. *befehl += ", 1, ";
  191. *befehl += zIni->zWert( "MaxClients" )->getText();
  192. *befehl += " ) RETURNING id";
  193. lock();
  194. if( !datenbank->befehl( *befehl ) )
  195. {
  196. unlock();
  197. befehl->release();
  198. return 0;
  199. }
  200. Result res = datenbank->getResult();
  201. unlock();
  202. befehl->release();
  203. if( !res.zeilenAnzahl )
  204. {
  205. res.destroy();
  206. return 0;
  207. }
  208. zIni->setWert( "ServerId", res.values[ 0 ] );
  209. return 1;
  210. }
  211. else
  212. { // Alten Eintrag aus Tabelle server_chat ändern
  213. Text *befehl = new Text( "UPDATE server_chat SET name = '" );
  214. *befehl += zIni->zWert( "ServerName" )->getText();
  215. *befehl += "', port = ";
  216. *befehl += zIni->zWert( "ServerPort" )->getText();
  217. *befehl += ", ip = '";
  218. *befehl += zIni->zWert( "ServerIP" )->getText();
  219. *befehl += "', max_clients = ";
  220. *befehl += zIni->zWert( "MaxClients" )->getText();
  221. *befehl += ", admin_port = ";
  222. *befehl += zIni->zWert( "AdminServerPort" )->getText();
  223. *befehl += " WHERE id = ";
  224. *befehl += id;
  225. lock();
  226. bool ret = datenbank->befehl( *befehl );
  227. unlock();
  228. befehl->release();
  229. return ret;
  230. }
  231. }
  232. bool CSDatenbank::setServerStatus( int id, int status )
  233. {
  234. Text *befehl = new Text( "UPDATE server_chat SET server_status_id = " );
  235. *befehl += status;
  236. *befehl += "WHERE id = ";
  237. *befehl += id;
  238. lock();
  239. if( !datenbank->befehl( befehl->getText() ) )
  240. {
  241. unlock();
  242. befehl->release();
  243. return 0;
  244. }
  245. bool ret = datenbank->getZeilenAnzahl() != 0;
  246. unlock();
  247. befehl->release();
  248. return ret;
  249. }
  250. bool CSDatenbank::setMaxClients( int id, int maxC )
  251. {
  252. Text *befehl = new Text( "UPDATE server_chat SET max_clients = " );
  253. befehl->append( maxC );
  254. befehl->append( " WHERE id = " );
  255. befehl->append( id );
  256. lock();
  257. if( !datenbank->befehl( befehl->getText() ) )
  258. {
  259. unlock();
  260. befehl->release();
  261. return 0;
  262. }
  263. bool ret = datenbank->getZeilenAnzahl() > 0;
  264. unlock();
  265. befehl->release();
  266. return ret;
  267. }
  268. int CSDatenbank::getAdminPort( int id )
  269. {
  270. Text *befehl = new Text( "SELECT admin_port FROM server_chat WHERE id = " );
  271. befehl->append( id );
  272. lock();
  273. if( !datenbank->befehl( befehl->getText() ) )
  274. {
  275. unlock();
  276. befehl->release();
  277. return 0;
  278. }
  279. Result res = datenbank->getResult();
  280. unlock();
  281. befehl->release();
  282. if( !res.zeilenAnzahl )
  283. {
  284. res.destroy();
  285. return 0;
  286. }
  287. int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  288. res.destroy();
  289. return ret;
  290. }
  291. bool CSDatenbank::serverIstNichtPausiert( int id )
  292. {
  293. Text *befehl = new Text( "SELECT server_status_id FROM server_chat WHERE id = " );
  294. befehl->append( id );
  295. lock();
  296. if( !datenbank->befehl( befehl->getText() ) )
  297. {
  298. unlock();
  299. befehl->release();
  300. return 0;
  301. }
  302. Result res = datenbank->getResult();
  303. unlock();
  304. befehl->release();
  305. if( !res.zeilenAnzahl )
  306. {
  307. res.destroy();
  308. return 0;
  309. }
  310. bool ret = (int)res.values[ 0 ] == 3;
  311. res.destroy();
  312. return ret;
  313. }
  314. int CSDatenbank::getKlientAccountId( int klientId )
  315. {
  316. Text *befehl = new Text( "SELECT account_id FROM account_clients WHERE client_id = " );
  317. befehl->append( klientId );
  318. lock();
  319. datenbank->befehl( befehl->getText() );
  320. Result res = datenbank->getResult();
  321. unlock();
  322. befehl->release();
  323. if( res.zeilenAnzahl )
  324. {
  325. int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  326. res.destroy();
  327. return ret;
  328. }
  329. res.destroy();
  330. return 0;
  331. }
  332. int CSDatenbank::getAccountFreunde( int accountId, Array< int > *fAccountId )
  333. {
  334. Text *befehl = new Text( "SELECT account_id AS freundId FROM freund WHERE freund_account_id = " );
  335. befehl->append( accountId );
  336. befehl->append( " UNION SELECT freund_account_id AS freundId FROM freund WHERE account_id = " );
  337. befehl->append( accountId );
  338. lock();
  339. datenbank->befehl( befehl->getText() );
  340. Result res = datenbank->getResult();
  341. unlock();
  342. befehl->release();
  343. int anzahl = res.zeilenAnzahl;
  344. for( int i = 0; i < anzahl; i++ )
  345. fAccountId->add( TextZuInt( res.values[ i ].getText(), 10 ), i );
  346. res.destroy();
  347. return anzahl;
  348. }
  349. int CSDatenbank::getAccountOnlineFreunde( int accountId, Array< int > *fAccountId )
  350. {
  351. Text *befehl = new Text( "SELECT freund.account_id AS freundId FROM freund, account_clients WHERE freund.freund_account_id = " );
  352. befehl->append( accountId );
  353. befehl->append( " AND freund.account_id = account_clients.account_id "
  354. "UNION SELECT freund.freund_account_id AS freundId FROM freund, account_clients WHERE freund.account_id = " );
  355. befehl->append( accountId );
  356. befehl->append( " AND freund.freund_account_id = account_clients.account_id " );
  357. lock();
  358. datenbank->befehl( befehl->getText() );
  359. Result res = datenbank->getResult();
  360. unlock();
  361. befehl->release();
  362. int anzahl = res.zeilenAnzahl;
  363. for( int i = 0; i < anzahl; i++ )
  364. fAccountId->add( TextZuInt( res.values[ i ].getText(), 10 ), i );
  365. res.destroy();
  366. return anzahl;
  367. }
  368. bool CSDatenbank::accountNameChange( int accountId, const char *name )
  369. {
  370. if( !name )
  371. return 1;
  372. Text *befehl = new Text( "UPDATE account SET ruf_name = '" );
  373. Text n( name );
  374. n.ersetzen( "'", "''" );
  375. befehl->append( (char*)n );
  376. befehl->append( "' WHERE id = " );
  377. befehl->append( accountId );
  378. bool ret = 0;
  379. lock();
  380. ret = datenbank->befehl( befehl->getText() );
  381. unlock();
  382. befehl->release();
  383. return ret;
  384. }
  385. bool CSDatenbank::beendeFreundschaft( int accountId1, int accountId2 )
  386. {
  387. Text *befehl = new Text( "DELETE FROM freund WHERE ( account_id = " );
  388. befehl->append( accountId1 );
  389. befehl->append( " AND freund_account_id = " );
  390. befehl->append( accountId2 );
  391. befehl->append( " ) OR ( freund_account_id = " );
  392. befehl->append( accountId1 );
  393. befehl->append( " AND account_id = " );
  394. befehl->append( accountId2 );
  395. befehl->append( " )" );
  396. lock();
  397. if( !datenbank->befehl( befehl->getText() ) )
  398. {
  399. unlock();
  400. return 0;
  401. }
  402. unlock();
  403. befehl->release();
  404. return 1;
  405. }
  406. bool CSDatenbank::proveFreundschaftsAnfrage( int vonAccountId, int zuAccountId )
  407. {
  408. if( vonAccountId == zuAccountId )
  409. return 0;
  410. Text *befehl = new Text( "SELECT account_id AS freundId FROM freund WHERE freund_account_id = " );
  411. befehl->append( vonAccountId );
  412. befehl->append( " AND freund.account_id = " );
  413. befehl->append( zuAccountId );
  414. befehl->append( "UNION SELECT freund_account_id AS freundId FROM freund WHERE freund_account_id = " );
  415. befehl->append( zuAccountId );
  416. befehl->append( " AND freund.account_id = " );
  417. befehl->append( vonAccountId );
  418. lock();
  419. datenbank->befehl( befehl->getText() );
  420. Result res = datenbank->getResult();
  421. unlock();
  422. befehl->release();
  423. bool ret = res.zeilenAnzahl == 0;
  424. res.destroy();
  425. return ret;
  426. }
  427. bool CSDatenbank::saveFreundschaftsAnfrage( int vonAccountId, int zuAccountId )
  428. {
  429. Text *befehl = new Text( "SELECT account_id FROM freund_anfrage WHERE ( account_id = " );
  430. befehl->append( vonAccountId );
  431. befehl->append( " AND freund_id = " );
  432. befehl->append( zuAccountId );
  433. befehl->append( " ) OR ( freund_id = " );
  434. befehl->append( vonAccountId );
  435. befehl->append( " AND account_id = " );
  436. befehl->append( zuAccountId );
  437. befehl->append( " )" );
  438. lock();
  439. if( !datenbank->befehl( befehl->getText() ) )
  440. {
  441. unlock();
  442. befehl->release();
  443. return 0;
  444. }
  445. Result res = datenbank->getResult();
  446. unlock();
  447. if( res.zeilenAnzahl )
  448. {
  449. res.destroy();
  450. befehl->release();
  451. return 1;
  452. }
  453. res.destroy();
  454. befehl->setText( "INSERT INTO freund_anfrage( account_id, freund_id ) VALUES( " );
  455. befehl->append( vonAccountId );
  456. befehl->append( ", " );
  457. befehl->append( zuAccountId );
  458. befehl->append( " )" );
  459. lock();
  460. if( !datenbank->befehl( befehl->getText() ) )
  461. {
  462. unlock();
  463. befehl->release();
  464. return 0;
  465. }
  466. unlock();
  467. befehl->release();
  468. return 1;
  469. }
  470. int CSDatenbank::getFreundschaftsAnfragen( int accountId, Array< int > *vonAccountIds )
  471. {
  472. Text *befehl = new Text( "SELECT account_id FROM freund_anfrage WHERE freund_id = " );
  473. befehl->append( accountId );
  474. lock();
  475. if( !datenbank->befehl( befehl->getText() ) )
  476. {
  477. unlock();
  478. befehl->release();
  479. return 0;
  480. }
  481. Result res = datenbank->getResult();
  482. unlock();
  483. befehl->setText( "DELETE FROM freund_anfrage WHERE freund_id = " );
  484. befehl->append( accountId ); lock();
  485. if( !datenbank->befehl( befehl->getText() ) )
  486. {
  487. unlock();
  488. befehl->release();
  489. res.destroy();
  490. return 0;
  491. }
  492. unlock();
  493. befehl->release();
  494. int anzahl = res.zeilenAnzahl;
  495. for( int i = 0; i < anzahl; i++ )
  496. vonAccountIds->set( res.values[ i ], i );
  497. res.destroy();
  498. return anzahl;
  499. }
  500. bool CSDatenbank::neueFreundschaft( int accountId1, int accountId2 )
  501. {
  502. Text *befehl = new Text( "INSERT INTO freund VALUES( " );
  503. befehl->append( accountId1 );
  504. befehl->append( ", " );
  505. befehl->append( accountId2 );
  506. befehl->append( " )" );
  507. lock();
  508. bool ret = datenbank->befehl( befehl->getText() );
  509. unlock();
  510. befehl->release();
  511. return ret;
  512. }
  513. Text *CSDatenbank::getAccountRufName( int accountId )
  514. {
  515. Text *befehl = new Text( "SELECT ruf_name FROM account WHERE id = " );
  516. befehl->append( accountId );
  517. lock();
  518. datenbank->befehl( befehl->getText() );
  519. Result res = datenbank->getResult();
  520. unlock();
  521. befehl->release();
  522. Text *ret = new Text( res.values[ 0 ].getText() );
  523. res.destroy();
  524. return ret;
  525. }
  526. bool CSDatenbank::accountIstOnline( int accountId )
  527. {
  528. Text *befehl = new Text( "SELECT * FROM account_clients WHERE account_id = " );
  529. befehl->append( accountId );
  530. lock();
  531. datenbank->befehl( befehl->getText() );
  532. int res = datenbank->getZeilenAnzahl();
  533. unlock();
  534. befehl->release();
  535. return res != 0;
  536. }
  537. bool CSDatenbank::accountIstImSpiel( int accountId )
  538. {
  539. Text *befehl = new Text( "SELECT b.id FROM spiel_spieler a, spiel b WHERE b.spiel_status_id = 2 "
  540. "AND a.spiel_spieler_status_id = 5 AND a.spiel_id = b.id AND a.account_id = " );
  541. befehl->append( accountId );
  542. lock();
  543. datenbank->befehl( befehl->getText() );
  544. int res = datenbank->getZeilenAnzahl();
  545. unlock();
  546. befehl->release();
  547. return res != 0;
  548. }
  549. int CSDatenbank::getChatServerId( int accountId )
  550. {
  551. Text *befehl = new Text( "SELECT server_chat_clients.server_chat_id FROM server_chat_clients, account_clients "
  552. "WHERE server_chat_clients.client_id = account_clients.client_id AND account_clients.account_id = " );
  553. befehl->append( accountId );
  554. lock();
  555. datenbank->befehl( befehl->getText() );
  556. Result res = datenbank->getResult();
  557. unlock();
  558. befehl->release();
  559. if( !res.zeilenAnzahl )
  560. {
  561. res.destroy();
  562. return 0;
  563. }
  564. int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  565. res.destroy();
  566. return ret;
  567. }
  568. bool CSDatenbank::getChatServerIpPort( int serverId, unsigned short *port, char **ip )
  569. {
  570. Text *befehl = new Text( "SELECT port, ip FROM server_chat WHERE id = " );
  571. befehl->append( serverId );
  572. lock();
  573. datenbank->befehl( befehl->getText() );
  574. Result res = datenbank->getResult();
  575. unlock();
  576. befehl->release();
  577. if( !res.zeilenAnzahl )
  578. {
  579. res.destroy();
  580. return 0;
  581. }
  582. *port = (unsigned short)TextZuInt( res.values[ 0 ].getText(), 10 );
  583. int len = res.values[ 1 ].getLength();
  584. char *ipTmp = new char[ len + 1 ];
  585. ipTmp[ len ] = 0;
  586. int i = 0;
  587. for( char *c = res.values[ 1 ].getText(); i < len; c++ )
  588. {
  589. ipTmp[ i ] = *c;
  590. i++;
  591. }
  592. res.destroy();
  593. return 1;
  594. }
  595. int CSDatenbank::getChatNachrichten( int accountId, Array< int > *vonAccount, RCArray< Text > *nachricht )
  596. {
  597. Text *befehl = new Text( "SELECT von_account_id, nachricht FROM chat_nachricht WHERE zu_account_id = " );
  598. befehl->append( accountId );
  599. lock();
  600. datenbank->befehl( befehl->getText() );
  601. Result res = datenbank->getResult();
  602. unlock();
  603. if( !res.zeilenAnzahl )
  604. {
  605. befehl->release();
  606. res.destroy();
  607. return 0;
  608. }
  609. int ret = res.zeilenAnzahl;
  610. for( int i = 0; i < ret; i++ )
  611. {
  612. vonAccount->add( TextZuInt( res.values[ i * 2 ].getText(), 10 ), i );
  613. nachricht->add( new Text( res.values[ i * 2 + 1 ].getText() ), i );
  614. }
  615. res.destroy();
  616. befehl->setText( "DELETE FROM chat_nachricht WHERE zu_account_id = " );
  617. befehl->append( accountId );
  618. lock();
  619. datenbank->befehl( befehl->getText() );
  620. unlock();
  621. befehl->release();
  622. return ret;
  623. }
  624. bool CSDatenbank::speicherChatNachricht( int vonAccount, int zuAccount, const char *nachricht )
  625. {
  626. if( !nachricht )
  627. return 1;
  628. Text *befehl = new Text( "INSERT INTO chat_nachricht VALUES( " );
  629. befehl->append( vonAccount );
  630. befehl->append( ", " );
  631. befehl->append( zuAccount );
  632. befehl->append( ", '" );
  633. Text n( nachricht );
  634. n.ersetzen( "'", "''" );
  635. befehl->append( (char*)n );
  636. befehl->append( "' )" );
  637. lock();
  638. bool ret = datenbank->befehl( befehl->getText() );
  639. unlock();
  640. befehl->release();
  641. return ret;
  642. }
  643. int CSDatenbank::getChatroomAccount( int chatroomId, Array< int > *accountId )
  644. {
  645. Text *befehl = new Text( "SELECT account_id FROM chatroom_spieler WHERE chatroom_id = " );
  646. befehl->append( chatroomId );
  647. lock();
  648. datenbank->befehl( befehl->getText() );
  649. Result res = datenbank->getResult();
  650. unlock();
  651. befehl->release();
  652. if( !res.zeilenAnzahl )
  653. {
  654. res.destroy();
  655. return 0;
  656. }
  657. int ret = res.zeilenAnzahl;
  658. for( int i = 0; i < ret; i++ )
  659. accountId->add( TextZuInt( res.values[ i ].getText(), 10 ), i );
  660. res.destroy();
  661. return ret;
  662. }
  663. int CSDatenbank::chatroomErstellen( int accountId, const char *name )
  664. {
  665. Text *befehl = new Text( "INSERT INTO chatroom( admin_account_id, name ) VALUES( " );
  666. befehl->append( accountId );
  667. befehl->append( ", '" );
  668. Text n( name );
  669. n.ersetzen( "'", "''" );
  670. befehl->append( (char*)n );
  671. befehl->append( "' ) RETURNING id" );
  672. lock();
  673. if( !datenbank->befehl( befehl->getText() ) )
  674. {
  675. unlock();
  676. befehl->release();
  677. return 0;
  678. }
  679. Result res = datenbank->getResult();
  680. unlock();
  681. befehl->setText( "INSERT INTO chatroom_spieler VALUES( ( SELECT id FROM chatroom WHERE name = '" );
  682. befehl->append( (char*)n );
  683. befehl->append( "' ), " );
  684. befehl->append( accountId );
  685. befehl->append( " )" );
  686. lock();
  687. datenbank->befehl( befehl->getText() );
  688. unlock();
  689. befehl->release();
  690. if( !res.zeilenAnzahl )
  691. {
  692. res.destroy();
  693. return 0;
  694. }
  695. int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  696. res.destroy();
  697. return ret;
  698. }
  699. bool CSDatenbank::proveChatroomEinladung( int vonAccount, int zuAccount, int chatroomId )
  700. {
  701. Text *befehl = new Text( "SELECT pruef_chatroom_einladung( " );
  702. befehl->append( chatroomId );
  703. befehl->append( ", " );
  704. befehl->append( vonAccount );
  705. befehl->append( ", " );
  706. befehl->append( zuAccount );
  707. befehl->append( " )" );
  708. lock();
  709. datenbank->befehl( befehl->getText() );
  710. Result res = datenbank->getResult();
  711. unlock();
  712. befehl->release();
  713. if( !res.zeilenAnzahl )
  714. {
  715. res.destroy();
  716. return 0;
  717. }
  718. bool ret = res.values[ 0 ].istGleich( "t" );
  719. res.destroy();
  720. return ret;
  721. }
  722. bool CSDatenbank::chatroomBeitreten( int accountId, int chatroomId )
  723. {
  724. Text *befehl = new Text( "INSERT INTO chatroom_spieler values( " );
  725. befehl->append( chatroomId );
  726. befehl->append( " , " );
  727. befehl->append( accountId );
  728. befehl->append( " )" );
  729. lock();
  730. bool ret = datenbank->befehl( befehl->getText() );
  731. unlock();
  732. befehl->release();
  733. return ret;
  734. }
  735. int CSDatenbank::chatroomVerlassen( int accountId, int chatroomId )
  736. {
  737. Text *befehl = new Text( "SELECT chatroom_verlassen( " );
  738. befehl->append( chatroomId );
  739. befehl->append( ", " );
  740. befehl->append( accountId );
  741. befehl->append( " )" );
  742. lock();
  743. datenbank->befehl( befehl->getText() );
  744. Result res = datenbank->getResult();
  745. unlock();
  746. befehl->release();
  747. if( !res.zeilenAnzahl )
  748. {
  749. res.destroy();
  750. return 0;
  751. }
  752. int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  753. res.destroy();
  754. return ret;
  755. }
  756. int CSDatenbank::getChatroomAdmin( int chatroomId )
  757. {
  758. Text *befehl = new Text( "SELECT admin_account_id FROM chatroom WHERE id = " );
  759. befehl->append( chatroomId );
  760. lock();
  761. datenbank->befehl( befehl->getText() );
  762. Result res = datenbank->getResult();
  763. unlock();
  764. befehl->release();
  765. if( !res.zeilenAnzahl )
  766. {
  767. res.destroy();
  768. return 0;
  769. }
  770. int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
  771. res.destroy();
  772. return ret;
  773. }
  774. // constant
  775. Text *CSDatenbank::getLetzterFehler() const
  776. {
  777. return datenbank->getLetzterFehler();
  778. }
  779. // Reference Counting
  780. CSDatenbank *CSDatenbank::getThis()
  781. {
  782. ref++;
  783. return this;
  784. }
  785. CSDatenbank *CSDatenbank::release()
  786. {
  787. ref--;
  788. if( !ref )
  789. delete this;
  790. return 0;
  791. }