LCOV - code coverage report
Current view: top level - src/tds/unittests - toodynamic.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 30 45 66.7 %
Date: 2025-07-02 09:40:27 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2008 Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : #include "common.h"
      20             : 
      21             : /*
      22             :  * Test creating a lot of dynamics. This can cause some problems cause
      23             :  * generated IDs are reused on a base of 2^16
      24             :  */
      25             : 
      26             : static void
      27           0 : fatal_error(const char *msg)
      28             : {
      29           0 :         fprintf(stderr, "%s\n", msg);
      30           0 :         exit(1);
      31             : }
      32             : 
      33          10 : TEST_MAIN()
      34             : {
      35             :         TDSLOGIN *login;
      36             :         TDSSOCKET *tds;
      37          10 :         int verbose = 0;
      38          10 :         TDSDYNAMIC *dyn = NULL;
      39             :         int rc;
      40             :         unsigned int n;
      41             : 
      42          10 :         printf("%s: Test creating a lot of dynamic queries\n", __FILE__);
      43          10 :         rc = try_tds_login(&login, &tds, __FILE__, verbose);
      44          10 :         if (rc != TDS_SUCCESS)
      45           0 :                 fatal_error("try_tds_login() failed");
      46             : 
      47          10 :         run_query(tds, "DROP TABLE #test");
      48          10 :         if (run_query(tds, "CREATE TABLE #test (i INT, c VARCHAR(40))") != TDS_SUCCESS)
      49           0 :                 fatal_error("creating table error");
      50             : 
      51          10 :         if (tds->cur_dyn)
      52           0 :                 fatal_error("already a dynamic query??");
      53             : 
      54             :         /* prepare to insert */
      55          10 :         if (tds_submit_prepare(tds, "UPDATE #test SET c = 'test' WHERE i = ?", NULL, &dyn, NULL) != TDS_SUCCESS)
      56           0 :                 fatal_error("tds_submit_prepare() error");
      57          10 :         if (tds_process_simple_query(tds) != TDS_SUCCESS)
      58           0 :                 fatal_error("tds_process_simple_query() error");
      59          10 :         if (!dyn)
      60           0 :                 fatal_error("dynamic not present??");
      61             : 
      62             :         /* waste some ids */
      63      655250 :         for (n = 0; n < 65525; ++n) {
      64             :                 TDSDYNAMIC *dyn;
      65             : 
      66      655250 :                 dyn = tds_alloc_dynamic(tds->conn, NULL);
      67      655250 :                 if (!dyn)
      68           0 :                         fatal_error("create dynamic");
      69             : 
      70      655250 :                 tds_dynamic_deallocated(tds->conn, dyn);
      71      655250 :                 tds_release_dynamic(&dyn);
      72             :         }
      73             : 
      74             :         /* this should not cause duplicate IDs or erros*/
      75         200 :         for (n = 0; n < 20; ++n) {
      76         200 :                 TDSDYNAMIC *dyn2 = NULL;
      77             : 
      78         200 :                 if (tds_submit_prepare(tds, "INSERT INTO #test(i,c) VALUES(?,?)", NULL, &dyn2, NULL) != TDS_SUCCESS)
      79           0 :                         fatal_error("tds_submit_prepare() error");
      80         200 :                 if (dyn == dyn2)
      81           0 :                         fatal_error("got duplicated dynamic");
      82         200 :                 if (tds_process_simple_query(tds) != TDS_SUCCESS)
      83           0 :                         fatal_error("tds_process_simple_query() error");
      84         200 :                 if (!dyn2)
      85           0 :                         fatal_error("dynamic not present??");
      86         200 :                 if (tds_submit_unprepare(tds, dyn2) != TDS_SUCCESS || tds_process_simple_query(tds) != TDS_SUCCESS)
      87           0 :                         fatal_error("unprepare error");
      88         200 :                 tds_dynamic_deallocated(tds->conn, dyn2);
      89         200 :                 tds_release_dynamic(&dyn2);
      90             :         }
      91             : 
      92          10 :         tds_dynamic_deallocated(tds->conn, dyn);
      93          10 :         tds_release_dynamic(&dyn);
      94             : 
      95          10 :         try_tds_logout(login, tds, verbose);
      96          10 :         return 0;
      97             : }
      98             : 

Generated by: LCOV version 1.13