1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
|
/*********************************************************************
*
* This is based on code created by Peter Harvey,
* (pharvey@codebydesign.com).
*
* Modified and extended by Nick Gorham
* (nick@lurcher.org).
*
* Any bugs or problems should be considered the fault of Nick and not
* Peter.
*
* copyright (c) 1999 Nick Gorham
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
**********************************************************************
*
* $Id: __handles.c,v 1.13 2009/05/15 15:23:56 lurcher Exp $
*
* $Log: __handles.c,v $
* Revision 1.13 2009/05/15 15:23:56 lurcher
* Fix pooled connection thread problems
*
* Revision 1.12 2009/02/18 17:59:08 lurcher
* Shift to using config.h, the compile lines were making it hard to spot warnings
*
* Revision 1.11 2009/02/17 09:47:44 lurcher
* Clear up a number of bugs
*
* Revision 1.10 2007/02/28 15:37:49 lurcher
* deal with drivers that call internal W functions and end up in the driver manager. controlled by the --enable-handlemap configure arg
*
* Revision 1.9 2006/05/31 17:35:34 lurcher
* Add unicode ODBCINST entry points
*
* Revision 1.8 2004/09/28 08:44:46 lurcher
* Fix memory leak in pthread descriptor code
*
* Revision 1.7 2004/07/24 17:55:37 lurcher
* Sync up CVS
*
* Revision 1.6 2003/06/04 12:49:45 lurcher
*
* Further PID logging tweeks
*
* Revision 1.5 2003/06/02 16:51:36 lurcher
*
* Add TracePid option
*
* Revision 1.4 2002/08/12 16:20:44 lurcher
*
* Make it try and find a working iconv set of encodings
*
* Revision 1.3 2002/08/12 13:17:52 lurcher
*
* Replicate the way the MS DM handles loading of driver libs, and allocating
* handles in the driver. usage counting in the driver means that dlopen is
* only called for the first use, and dlclose for the last. AllocHandle for
* the driver environment is only called for the first time per driver
* per application environment.
*
* Revision 1.2 2002/02/22 10:23:22 lurcher
*
* s/Trace File/TraceFile
*
* Revision 1.1.1.1 2001/10/17 16:40:07 lurcher
*
* First upload to SourceForge
*
* Revision 1.14 2001/06/25 12:55:15 nick
*
* Fix threading problem with multiple ENV's
*
* Revision 1.13 2001/06/04 15:24:49 nick
*
* Add port to MAC OSX and QT3 changes
*
* Revision 1.12 2001/05/15 13:33:44 jason
*
* Wrapped calls to stats with COLLECT_STATS
*
* Revision 1.11 2001/04/12 17:43:36 nick
*
* Change logging and added autotest to odbctest
*
* Revision 1.10 2001/03/02 14:24:23 nick
*
* Fix thread detection for Solaris
*
* Revision 1.9 2001/01/04 13:16:25 nick
*
* Add support for GNU portable threads and tidy up some UNICODE compile
* warnings
*
* Revision 1.8 2000/12/18 11:51:59 martin
*
* stats specific mode to uodbc_open_stats.
*
* Revision 1.7 2000/12/18 11:03:58 martin
*
* Add support for the collection and retrieval of handle statistics.
*
* Revision 1.6 2000/12/17 11:17:22 nick
*
* Remove typo
*
* Revision 1.5 2000/12/17 11:00:32 nick
*
* Add thread safe bits to pooling
*
* Revision 1.4 2000/11/29 17:53:59 nick
*
* Fix race condition
*
* Revision 1.3 2000/10/25 09:39:42 nick
*
* Clear handles out, to avoid reuse
*
* Revision 1.2 2000/09/08 08:58:17 nick
*
* Add SQL_DRIVER_HDESC to SQLGetinfo
*
* Revision 1.1.1.1 2000/09/04 16:42:52 nick
* Imported Sources
*
* Revision 1.16 2000/06/29 17:27:52 ngorham
*
* Add fast validate option
*
* Revision 1.15 2000/06/27 17:34:12 ngorham
*
* Fix a problem when the second part of the connect failed a seg fault
* was generated in the error reporting
*
* Revision 1.14 2001/03/28 23:09:57 ngorham
*
* Fix logging
*
* Revision 1.13 2000/03/11 15:55:47 ngorham
*
* A few more changes and bug fixes (see NEWS)
*
* Revision 1.12 2000/02/25 00:02:00 ngorham
*
* Add a patch to support IBM DB2, and Solaris threads
*
* Revision 1.11 2000/02/22 22:14:45 ngorham
*
* Added support for solaris threads
* Added check to overcome bug in PHP4
* Fixed bug in descriptors and ODBC 3 drivers
*
* Revision 1.10 1999/12/11 13:01:57 ngorham
*
* Add some fixes to the Postgres driver for long types
*
* Revision 1.9 1999/12/01 09:20:07 ngorham
*
* Fix some threading problems
*
* Revision 1.8 1999/11/13 23:41:01 ngorham
*
* Alter the way DM logging works
* Upgrade the Postgres driver to 6.4.6
*
* Revision 1.7 1999/11/10 03:51:34 ngorham
*
* Update the error reporting in the DM to enable ODBC 3 and 2 calls to
* work at the same time
*
* Revision 1.6 1999/08/05 18:59:49 ngorham
*
* Typo error found by Greg Bentz
*
* Revision 1.5 1999/08/03 21:47:39 shandyb
* Moving to automake: changed files in DriverManager
*
* Revision 1.4 1999/07/10 21:10:17 ngorham
*
* Adjust error sqlstate from driver manager, depending on requested
* version (ODBC2/3)
*
* Revision 1.3 1999/07/04 21:05:08 ngorham
*
* Add LGPL Headers to code
*
* Revision 1.2 1999/06/30 23:56:56 ngorham
*
* Add initial thread safety code
*
* Revision 1.1.1.1 1999/05/29 13:41:09 sShandyb
* first go at it
*
* Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
* Imported sources
*
* Revision 1.3 1999/05/09 23:27:11 nick
* All the API done now
*
* Revision 1.2 1999/05/03 19:50:43 nick
* Another check point
*
* Revision 1.1 1999/04/25 23:06:11 nick
* Initial revision
*
*
**********************************************************************/
#include <config.h>
#include <ctype.h>
#include "drivermanager.h"
#if defined ( COLLECT_STATS ) && defined( HAVE_SYS_SEM_H )
#include "__stats.h"
#include <uodbc_stats.h>
#endif
static char const rcsid[]= "$RCSfile: __handles.c,v $ $Revision: 1.13 $";
/*
* these are used to enable us to check if a handle is
* valid without the danger of a seg-vio.
*/
static DMHENV enviroment_root;
static DMHDBC connection_root;
static DMHSTMT statement_root;
static DMHDESC descriptor_root;
/*
* use just one mutex for all the lists, this avoids any issues
* with deadlocks, the performance issue should be minimal, if it
* turns out to be a problem, we can readdress this
*
* We also have a mutex to protect the connection pooling code
*
* If compiled with thread support the DM allows four different
* thread strategies
*
* Level 0 - Only the DM internal structures are protected
* the driver is assumed to take care of it's self
*
* Level 1 - The driver is protected down to the statement level
* each statement will be protected, and the same for the connect
* level for connect functions, note that descriptors are considered
* equal to statements when it comes to thread protection.
*
* Level 2 - The driver is protected at the connection level. only
* one thread can be in a particular driver at one time
*
* Level 3 - The driver is protected at the env level, only one thing
* at a time.
*
* By default the driver open connections with a lock level of 0,
* drivers should be expecetd to be thread safe now.
* this can be changed by adding the line
*
* Threading = N
*
* to the driver entry in odbcinst.ini, where N is the locking level
* (0-3)
*
*/
#ifdef HAVE_LIBPTH
#include <pth.h>
static pth_mutex_t mutex_lists = PTH_MUTEX_INIT;
static pth_mutex_t mutex_env = PTH_MUTEX_INIT;
static pth_mutex_t mutex_pool = PTH_MUTEX_INIT;
static pth_mutex_t mutex_iconv = PTH_MUTEX_INIT;
static int pth_init_called = 0;
static int mutex_entry( pth_mutex_t *mutex )
{
if ( !pth_init_called )
{
pth_init();
pth_init_called = 1;
}
return pth_mutex_acquire( mutex, 0, NULL );
}
static int mutex_exit( pth_mutex_t *mutex )
{
return pth_mutex_release( mutex );
}
#elif HAVE_LIBPTHREAD
#include <pthread.h>
static pthread_mutex_t mutex_lists = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex_env = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex_pool = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex_iconv = PTHREAD_MUTEX_INITIALIZER;
static int mutex_entry( pthread_mutex_t *mutex )
{
return pthread_mutex_lock( mutex );
}
static int mutex_exit( pthread_mutex_t *mutex )
{
return pthread_mutex_unlock( mutex );
}
#elif HAVE_LIBTHREAD
#include <thread.h>
static mutex_t mutex_lists;
static mutex_t mutex_env;
static mutex_t mutex_pool;
static mutex_t mutex_iconv;
static int mutex_entry( mutex_t *mutex )
{
return mutex_lock( mutex );
}
static int mutex_exit( mutex_t *mutex )
{
return mutex_unlock( mutex );
}
#else
#define mutex_entry(x)
#define mutex_exit(x)
#endif
/*
* protection for connection pooling
*/
void mutex_pool_entry( void )
{
mutex_entry( &mutex_pool );
}
void mutex_pool_exit( void )
{
mutex_exit( &mutex_pool );
}
/*
* protection for iconv
*/
void mutex_iconv_entry( void )
{
mutex_entry( &mutex_iconv );
}
void mutex_iconv_exit( void )
{
mutex_exit( &mutex_iconv );
}
/*
* protection for lib loading and counting, reuse the lists mutex as this
* is the lowest level protection the DM uses
*/
void mutex_lib_entry( void )
{
mutex_entry( &mutex_lists );
}
void mutex_lib_exit( void )
{
mutex_exit( &mutex_lists );
}
/*
* allocate and register a environment handle
*/
DMHENV __alloc_env( void )
{
DMHENV environment = NULL;
mutex_entry( &mutex_lists );
environment = calloc( sizeof( *environment ), 1 );
if ( environment )
{
char tracing_string[ 64 ];
char tracing_file[ 64 ];
#if defined ( COLLECT_STATS ) && defined( HAVE_SYS_SEM_H )
if (uodbc_open_stats(&environment->sh, UODBC_STATS_WRITE) != 0)
{
;
}
uodbc_update_stats(environment->sh, UODBC_STATS_TYPE_HENV, (void *)1);
#endif
/*
* add to list of env handles
*/
environment -> next_class_list = enviroment_root;
enviroment_root = environment;
environment -> type = HENV_MAGIC;
SQLGetPrivateProfileString( "ODBC", "Trace", "No",
tracing_string, sizeof( tracing_string ),
"odbcinst.ini" );
if ( tracing_string[ 0 ] == '1' ||
toupper( tracing_string[ 0 ] ) == 'Y' ||
( toupper( tracing_string[ 0 ] ) == 'O' &&
toupper( tracing_string[ 1 ] ) == 'N' ))
{
SQLGetPrivateProfileString( "ODBC", "TraceFile", "/tmp/sql.log",
tracing_file, sizeof( tracing_file ),
"odbcinst.ini" );
/*
* start logging
*/
SQLGetPrivateProfileString( "ODBC", "TracePid", "No",
tracing_string, sizeof( tracing_string ),
"odbcinst.ini" );
if ( tracing_string[ 0 ] == '1' ||
toupper( tracing_string[ 0 ] ) == 'Y' ||
( toupper( tracing_string[ 0 ] ) == 'O' &&
toupper( tracing_string[ 1 ] ) == 'N' ))
{
dm_log_open( "ODBC", tracing_file, 1 );
}
else
{
dm_log_open( "ODBC", tracing_file, 0 );
}
sprintf( environment -> msg,
"\n\t\tExit:[SQL_SUCCESS]\n\t\t\tEnvironment = %p", environment );
dm_log_write( __FILE__,
__LINE__,
LOG_INFO,
LOG_INFO, environment -> msg );
}
}
setup_error_head( &environment -> error, environment,
SQL_HANDLE_ENV );
mutex_exit( &mutex_lists );
return environment;
}
/*
* check that a env is real
*/
int __validate_env( DMHENV env )
{
#ifdef FAST_HANDLE_VALIDATE
if ( *(( int * ) env ) == HENV_MAGIC )
return 1;
else
return 0;
#else
DMHENV ptr;
int ret = 0;
mutex_entry( &mutex_lists );
ptr = enviroment_root;
while( ptr )
{
if ( ptr == env )
{
ret = 1;
break;
}
ptr = ptr -> next_class_list;
}
mutex_exit( &mutex_lists );
return ret;
#endif
}
/*
* remove from list
*/
void __release_env( DMHENV environment )
{
DMHENV last = NULL;
DMHENV ptr;
mutex_entry( &mutex_lists );
ptr = enviroment_root;
while( ptr )
{
if ( environment == ptr )
{
break;
}
last = ptr;
ptr = ptr -> next_class_list;
}
if ( ptr )
{
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
}
else
{
enviroment_root = ptr -> next_class_list;
}
}
clear_error_head( &environment -> error );
/*
* free log
*/
dm_log_close();
#if defined ( COLLECT_STATS ) && defined( HAVE_SYS_SEM_H )
if (environment->sh)
uodbc_close_stats(environment->sh);
#endif
/*
* clear just to make sure
*/
memset( environment, 0, sizeof( *environment ));
free( environment );
mutex_exit( &mutex_lists );
}
/*
* get the root, for use in SQLEndTran and SQLTransact
*/
DMHDBC __get_dbc_root( void )
{
return connection_root;
}
/*
* allocate and register a connection handle
*/
DMHDBC __alloc_dbc( void )
{
DMHDBC connection = NULL;
mutex_entry( &mutex_lists );
connection = calloc( sizeof( *connection ), 1 );
if ( connection )
{
/*
* add to list of connection handles
*/
connection -> next_class_list = connection_root;
connection_root = connection;
connection -> type = HDBC_MAGIC;
}
setup_error_head( &connection -> error, connection,
SQL_HANDLE_DBC );
#ifdef HAVE_LIBPTH
pth_mutex_init( &connection -> mutex );
/*
* for the moment protect on a environment level
*/
connection -> protection_level = TS_LEVEL3;
#elif HAVE_LIBPTHREAD
pthread_mutex_init( &connection -> mutex, NULL );
/*
* for the moment protect on a environment level
*/
connection -> protection_level = TS_LEVEL3;
#elif HAVE_LIBTHREAD
mutex_init( &connection -> mutex, USYNC_THREAD, NULL );
connection -> protection_level = TS_LEVEL3;
#endif
mutex_exit( &mutex_lists );
return connection;
}
/*
* adjust the threading level
*/
void dbc_change_thread_support( DMHDBC connection, int level )
{
#if defined ( HAVE_LIBPTHREAD ) || defined( HAVE_LIBTHREAD ) || defined( HAVE_LIBPTH )
int old_level;
if ( connection -> protection_level == level )
return;
old_level = connection -> protection_level;
connection -> protection_level = level;
if ( level == TS_LEVEL3 )
{
/*
* if we are moving from level 3 we may have to release the existing
* connection lock, and create the env lock
*/
if(old_level != TS_LEVEL0)
mutex_exit( &connection -> mutex );
mutex_entry( &mutex_env );
}
else if ( old_level == TS_LEVEL3 )
{
/*
* if we are moving from level 3 we may have to create the new
* connection lock, and remove the env lock
*/
if(level != TS_LEVEL0)
mutex_entry( &connection -> mutex );
mutex_exit( &mutex_env );
}
#endif
}
/*
* check that a connection is real
*/
int __validate_dbc( DMHDBC connection )
{
#ifdef FAST_HANDLE_VALIDATE
if ( *(( int * ) connection ) == HDBC_MAGIC )
return 1;
else
return 0;
#else
DMHDBC ptr;
int ret = 0;
mutex_entry( &mutex_lists );
ptr = connection_root;
while( ptr )
{
if ( ptr == connection )
{
ret = 1;
break;
}
ptr = ptr -> next_class_list;
}
mutex_exit( &mutex_lists );
return ret;
#endif
}
/*
* remove from list
*/
void __release_dbc( DMHDBC connection )
{
DMHDBC last = NULL;
DMHDBC ptr;
mutex_entry( &mutex_lists );
ptr = connection_root;
while( ptr )
{
if ( connection == ptr )
{
break;
}
last = ptr;
ptr = ptr -> next_class_list;
}
if ( ptr )
{
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
}
else
{
connection_root = ptr -> next_class_list;
}
}
clear_error_head( &connection -> error );
#ifdef HAVE_LIBPTH
#elif HAVE_LIBPTHREAD
pthread_mutex_destroy( &connection -> mutex );
#elif HAVE_LIBTHREAD
mutex_destroy( &connection -> mutex );
#endif
/*
* clear just to make sure
*/
memset( connection, 0, sizeof( *connection ));
free( connection );
mutex_exit( &mutex_lists );
}
/*
* allocate and register a statement handle
*/
DMHSTMT __alloc_stmt( void )
{
DMHSTMT statement = NULL;
mutex_entry( &mutex_lists );
statement = calloc( sizeof( *statement ), 1 );
if ( statement )
{
/*
* add to list of statement handles
*/
statement -> next_class_list = statement_root;
#ifdef FAST_HANDLE_VALIDATE
if ( statement_root )
{
statement_root -> prev_class_list = statement;
}
#endif
statement_root = statement;
statement -> type = HSTMT_MAGIC;
}
setup_error_head( &statement -> error, statement,
SQL_HANDLE_STMT );
#ifdef HAVE_LIBPTH
pth_mutex_init( &statement -> mutex );
#elif HAVE_LIBPTHREAD
pthread_mutex_init( &statement -> mutex, NULL );
#elif HAVE_LIBTHREAD
mutex_init( &statement -> mutex, USYNC_THREAD, NULL );
#endif
mutex_exit( &mutex_lists );
return statement;
}
/*
* assigns a statements to the connection
*/
void __register_stmt ( DMHDBC connection, DMHSTMT statement )
{
mutex_entry( &mutex_lists );
connection -> statement_count ++;
statement -> connection = connection;
#ifdef FAST_HANDLE_VALIDATE
statement -> next_conn_list = connection -> statements;
connection -> statements = statement;
#endif
mutex_exit( &mutex_lists );
}
/*
* Sets statement state after commit or rollback transaction
*/
void __set_stmt_state ( DMHDBC connection, SQLSMALLINT cb_value )
{
DMHSTMT statement;
SQLINTEGER stmt_remaining;
mutex_entry( &mutex_lists );
#ifdef FAST_HANDLE_VALIDATE
statement = connection -> statements;
while ( statement )
{
if ( (statement -> state == STATE_S2 ||
statement -> state == STATE_S3) &&
cb_value == SQL_CB_DELETE )
{
statement -> state = STATE_S1;
statement -> prepared = 0;
}
else if ( statement -> state == STATE_S4 ||
statement -> state == STATE_S5 ||
statement -> state == STATE_S6 ||
statement -> state == STATE_S7 )
{
if( !statement -> prepared &&
(cb_value == SQL_CB_DELETE ||
cb_value == SQL_CB_CLOSE) )
{
statement -> state = STATE_S1;
}
else if( statement -> prepared )
{
if( cb_value == SQL_CB_DELETE )
{
statement -> state = STATE_S1;
statement -> prepared = 0;
}
else if( cb_value == SQL_CB_CLOSE )
{
if ( statement -> state == STATE_S4 )
statement -> state = STATE_S2;
else
statement -> state = STATE_S3;
}
}
}
statement = statement -> next_conn_list;
}
#else
statement = statement_root;
stmt_remaining = connection -> statement_count;
while ( statement && stmt_remaining > 0 )
{
if ( statement -> connection == connection )
{
if ( (statement -> state == STATE_S2 ||
statement -> state == STATE_S3) &&
cb_value == SQL_CB_DELETE )
{
statement -> state = STATE_S1;
statement -> prepared = 0;
}
else if ( statement -> state == STATE_S4 ||
statement -> state == STATE_S5 ||
statement -> state == STATE_S6 ||
statement -> state == STATE_S7 )
{
if( !statement -> prepared &&
(cb_value == SQL_CB_DELETE ||
cb_value == SQL_CB_CLOSE) )
{
statement -> state = STATE_S1;
}
else if( statement -> prepared )
{
if( cb_value == SQL_CB_DELETE )
{
statement -> state = STATE_S1;
statement -> prepared = 0;
}
else if( cb_value == SQL_CB_CLOSE )
{
if ( statement -> state == STATE_S4 )
statement -> state = STATE_S2;
else
statement -> state = STATE_S3;
}
}
}
stmt_remaining --;
}
statement = statement -> next_class_list;
}
#endif
mutex_exit( &mutex_lists );
}
/*
* clear all statements on a DBC
*/
int __clean_stmt_from_dbc( DMHDBC connection )
{
DMHSTMT ptr, last;
int ret = 0;
mutex_entry( &mutex_lists );
#ifdef FAST_HANDLE_VALIDATE
while ( connection -> statements )
{
ptr = connection -> statements;
last = connection -> statements -> prev_class_list;
connection -> statements = ptr -> next_conn_list;
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
if ( last -> next_class_list )
{
last -> next_class_list -> prev_class_list = last;
}
}
else
{
statement_root = ptr -> next_class_list;
if ( statement_root )
{
statement_root -> prev_class_list = NULL;
}
}
clear_error_head( &ptr -> error );
#ifdef HAVE_LIBPTH
#elif HAVE_LIBPTHREAD
pthread_mutex_destroy( &ptr -> mutex );
#elif HAVE_LIBTHREAD
mutex_destroy( &ptr -> mutex );
#endif
free( ptr );
}
#else
last = NULL;
ptr = statement_root;
while( ptr )
{
if ( ptr -> connection == connection )
{
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
}
else
{
statement_root = ptr -> next_class_list;
}
clear_error_head( &ptr -> error );
#ifdef HAVE_LIBPTH
#elif HAVE_LIBPTHREAD
pthread_mutex_destroy( &ptr -> mutex );
#elif HAVE_LIBTHREAD
mutex_destroy( &ptr -> mutex );
#endif
free( ptr );
/*
* go back to the start
*/
last = NULL;
ptr = statement_root;
}
else
{
last = ptr;
ptr = ptr -> next_class_list;
}
}
#endif
mutex_exit( &mutex_lists );
return ret;
}
/*
* check if any statements on this connection are in a given state
*/
int __check_stmt_from_dbc( DMHDBC connection, int state )
{
DMHSTMT ptr;
int found = 0;
mutex_entry( &mutex_lists );
#ifdef FAST_HANDLE_VALIDATE
ptr = connection -> statements;
while( ptr )
{
if ( ptr -> state == state )
{
found = 1;
break;
}
ptr = ptr -> next_conn_list;
}
#else
ptr = statement_root;
while( ptr )
{
if ( ptr -> connection == connection )
{
if ( ptr -> state == state )
{
found = 1;
break;
}
}
ptr = ptr -> next_class_list;
}
#endif
mutex_exit( &mutex_lists );
return found;
}
int __check_stmt_from_desc( DMHDESC desc, int state )
{
DMHDBC connection;
DMHSTMT ptr;
int found = 0;
mutex_entry( &mutex_lists );
connection = desc -> connection;
#ifdef FAST_HANDLE_VALIDATE
ptr = connection -> statements;
while( ptr )
{
if ( ptr -> ipd == desc || ptr -> ird == desc || ptr -> apd == desc || ptr -> ard == desc )
{
if ( ptr -> state == state )
{
found = 1;
break;
}
}
ptr = ptr -> next_conn_list;
}
#else
ptr = statement_root;
while( ptr )
{
if ( ptr -> connection == connection )
{
if ( ptr -> ipd == desc || ptr -> ird == desc || ptr -> apd == desc || ptr -> ard == desc )
{
if ( ptr -> state == state )
{
found = 1;
break;
}
}
}
ptr = ptr -> next_class_list;
}
#endif
mutex_exit( &mutex_lists );
return found;
}
int __check_stmt_from_desc_ird( DMHDESC desc, int state )
{
DMHDBC connection;
DMHSTMT ptr;
int found = 0;
mutex_entry( &mutex_lists );
connection = desc -> connection;
#ifdef FAST_HANDLE_VALIDATE
ptr = connection -> statements;
while( ptr )
{
if ( ptr -> ird == desc )
{
if ( ptr -> state == state )
{
found = 1;
break;
}
}
ptr = ptr -> next_conn_list;
}
#else
ptr = statement_root;
while( ptr )
{
if ( ptr -> connection == connection )
{
if ( ptr -> ird == desc )
{
if ( ptr -> state == state )
{
found = 1;
break;
}
}
}
ptr = ptr -> next_class_list;
}
#endif
mutex_exit( &mutex_lists );
return found;
}
/*
* check any statements that are associated with a descriptor
*/
/*
* check that a statement is real
*/
int __validate_stmt( DMHSTMT statement )
{
#ifdef FAST_HANDLE_VALIDATE
if ( statement && *(( int * ) statement ) == HSTMT_MAGIC )
return 1;
else
return 0;
#else
DMHSTMT ptr;
int ret = 0;
mutex_entry( &mutex_lists );
ptr = statement_root;
while( ptr )
{
if ( ptr == statement )
{
ret = 1;
break;
}
ptr = ptr -> next_class_list;
}
mutex_exit( &mutex_lists );
return ret;
#endif
}
/*
* remove from list
*/
void __release_stmt( DMHSTMT statement )
{
DMHSTMT last = NULL;
DMHSTMT ptr;
mutex_entry( &mutex_lists );
#ifdef FAST_HANDLE_VALIDATE
/*
* A check never mind
*/
if ( statement && ( *(( int * ) statement ) == HSTMT_MAGIC ))
{
ptr = statement;
last = statement->prev_class_list;
if ( statement -> connection )
{
DMHDBC connection = statement -> connection;
DMHSTMT conn_last = NULL;
DMHSTMT conn_ptr = connection -> statements;
while ( conn_ptr )
{
if ( statement == conn_ptr )
{
break;
}
conn_last = conn_ptr;
conn_ptr = conn_ptr -> next_conn_list;
}
if ( conn_ptr )
{
if ( conn_last )
{
conn_last -> next_conn_list = conn_ptr -> next_conn_list;
}
else
{
connection -> statements = conn_ptr -> next_conn_list;
}
}
}
}
else
{
ptr = NULL;
last = NULL;
}
#else
ptr = statement_root;
while( ptr )
{
if ( statement == ptr )
{
break;
}
last = ptr;
ptr = ptr -> next_class_list;
}
#endif
if ( ptr )
{
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
#ifdef FAST_HANDLE_VALIDATE
if ( last -> next_class_list )
{
last -> next_class_list -> prev_class_list = last;
}
#endif
}
else
{
statement_root = ptr -> next_class_list;
#ifdef FAST_HANDLE_VALIDATE
if ( statement_root )
{
statement_root -> prev_class_list = NULL;
}
#endif
}
}
clear_error_head( &statement -> error );
#ifdef HAVE_LIBPTH
#elif HAVE_LIBPTHREAD
pthread_mutex_destroy( &statement -> mutex );
#elif HAVE_LIBTHREAD
mutex_destroy( &statement -> mutex );
#endif
/*
* clear just to make sure
*/
memset( statement, 0, sizeof( *statement ));
free( statement );
mutex_exit( &mutex_lists );
}
/*
* allocate and register a descriptor handle
*/
DMHDESC __alloc_desc( void )
{
DMHDESC descriptor = NULL;
mutex_entry( &mutex_lists );
descriptor = calloc( sizeof( *descriptor ), 1 );
if ( descriptor )
{
/*
* add to list of descriptor handles
*/
descriptor -> next_class_list = descriptor_root;
#ifdef FAST_HANDLE_VALIDATE
if ( descriptor_root )
{
descriptor_root -> prev_class_list = descriptor;
}
#endif
descriptor_root = descriptor;
descriptor -> type = HDESC_MAGIC;
}
setup_error_head( &descriptor -> error, descriptor,
SQL_HANDLE_DESC );
#ifdef HAVE_LIBPTH
pth_mutex_init( &descriptor -> mutex );
#elif HAVE_LIBPTHREAD
pthread_mutex_init( &descriptor -> mutex, NULL );
#elif HAVE_LIBTHREAD
mutex_init( &descriptor -> mutex, USYNC_THREAD, NULL );
#endif
mutex_exit( &mutex_lists );
return descriptor;
}
/*
* check that a descriptor is real
*/
int __validate_desc( DMHDESC descriptor )
{
#ifdef FAST_HANDLE_VALIDATE
if ( *(( int * ) descriptor ) == HDESC_MAGIC )
return 1;
else
return 0;
#else
DMHDESC ptr;
int ret = 0;
mutex_entry( &mutex_lists );
ptr = descriptor_root;
while( ptr )
{
if ( ptr == descriptor )
{
ret = 1;
break;
}
ptr = ptr -> next_class_list;
}
mutex_exit( &mutex_lists );
return ret;
#endif
}
/*
* clear all descriptors on a DBC
*/
int __clean_desc_from_dbc( DMHDBC connection )
{
DMHDESC ptr, last;
int ret = 0;
mutex_entry( &mutex_lists );
last = NULL;
ptr = descriptor_root;
while( ptr )
{
if ( ptr -> connection == connection )
{
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
#ifdef FAST_HANDLE_VALIDATE
if ( last -> next_class_list )
{
last -> next_class_list -> prev_class_list = last;
}
#endif
}
else
{
descriptor_root = ptr -> next_class_list;
#ifdef FAST_HANDLE_VALIDATE
if ( descriptor_root )
{
descriptor_root -> prev_class_list = NULL;
}
#endif
}
clear_error_head( &ptr -> error );
#ifdef HAVE_LIBPTH
#elif HAVE_LIBPTHREAD
pthread_mutex_destroy( &ptr -> mutex );
#elif HAVE_LIBTHREAD
mutex_destroy( &ptr -> mutex );
#endif
free( ptr );
/*
* go back to the start
*/
last = NULL;
ptr = descriptor_root;
}
else
{
last = ptr;
ptr = ptr -> next_class_list;
}
}
mutex_exit( &mutex_lists );
return ret;
}
/*
* remove from list
*/
void __release_desc( DMHDESC descriptor )
{
DMHDESC last = NULL;
DMHDESC ptr;
mutex_entry( &mutex_lists );
#ifdef FAST_HANDLE_VALIDATE
/*
* A check never mind
*/
if ( descriptor && ( *(( int * ) descriptor ) == HDESC_MAGIC ))
{
ptr = descriptor;
last = descriptor->prev_class_list;
}
else
{
ptr = NULL;
last = NULL;
}
#else
ptr = descriptor_root;
while( ptr )
{
if ( descriptor == ptr )
{
break;
}
last = ptr;
ptr = ptr -> next_class_list;
}
#endif
if ( ptr )
{
if ( last )
{
last -> next_class_list = ptr -> next_class_list;
#ifdef FAST_HANDLE_VALIDATE
if ( last -> next_class_list )
{
last -> next_class_list -> prev_class_list = last;
}
#endif
}
else
{
descriptor_root = ptr -> next_class_list;
#ifdef FAST_HANDLE_VALIDATE
if ( descriptor_root )
{
descriptor_root -> prev_class_list = NULL;
}
#endif
}
}
clear_error_head( &descriptor -> error );
#ifdef HAVE_LIBPTH
#elif HAVE_LIBPTHREAD
pthread_mutex_destroy( &descriptor -> mutex );
#elif HAVE_LIBTHREAD
mutex_destroy( &descriptor -> mutex );
#endif
/*
* clear just to make sure
*/
memset( descriptor, 0, sizeof( *descriptor ));
free( descriptor );
mutex_exit( &mutex_lists );
}
#if defined ( HAVE_LIBPTHREAD ) || defined ( HAVE_LIBTHREAD ) || defined( HAVE_LIBPTH )
void thread_protect( int type, void *handle )
{
DMHDBC connection;
DMHSTMT statement;
DMHDESC descriptor;
switch( type )
{
case SQL_HANDLE_ENV:
mutex_entry( &mutex_env );
break;
case SQL_HANDLE_DBC:
connection = handle;
if ( connection -> protection_level == TS_LEVEL3 )
{
mutex_entry( &mutex_env );
}
else if ( connection -> protection_level == TS_LEVEL2 ||
connection -> protection_level == TS_LEVEL1 )
{
mutex_entry( &connection -> mutex );
}
break;
case SQL_HANDLE_STMT:
statement = handle;
if ( statement -> connection -> protection_level == TS_LEVEL3 )
{
mutex_entry( &mutex_env );
}
else if ( statement -> connection -> protection_level == TS_LEVEL2 )
{
mutex_entry( &statement -> connection -> mutex );
}
else if ( statement -> connection -> protection_level == TS_LEVEL1 )
{
mutex_entry( &statement -> mutex );
}
break;
case SQL_HANDLE_DESC:
descriptor = handle;
if ( descriptor -> connection -> protection_level == TS_LEVEL3 )
{
mutex_entry( &mutex_env );
}
if ( descriptor -> connection -> protection_level == TS_LEVEL2 )
{
mutex_entry( &descriptor -> connection -> mutex );
}
if ( descriptor -> connection -> protection_level == TS_LEVEL1 )
{
mutex_entry( &descriptor -> mutex );
}
break;
}
}
void thread_release( int type, void *handle )
{
DMHDBC connection;
DMHSTMT statement;
DMHDESC descriptor;
switch( type )
{
case SQL_HANDLE_ENV:
mutex_exit( &mutex_env );
break;
case SQL_HANDLE_DBC:
connection = handle;
if ( connection -> protection_level == TS_LEVEL3 )
{
mutex_exit( &mutex_env );
}
else if ( connection -> protection_level == TS_LEVEL2 ||
connection -> protection_level == TS_LEVEL1 )
{
mutex_exit( &connection -> mutex );
}
break;
case SQL_HANDLE_STMT:
statement = handle;
if ( statement -> connection -> protection_level == TS_LEVEL3 )
{
mutex_exit( &mutex_env );
}
else if ( statement -> connection -> protection_level == TS_LEVEL2 )
{
mutex_exit( &statement -> connection -> mutex );
}
else if ( statement -> connection -> protection_level == TS_LEVEL1 )
{
mutex_exit( &statement -> mutex );
}
break;
case SQL_HANDLE_DESC:
descriptor = handle;
if ( descriptor -> connection -> protection_level == TS_LEVEL3 )
{
mutex_exit( &mutex_env );
}
else if ( descriptor -> connection -> protection_level == TS_LEVEL2 )
{
mutex_exit( &descriptor -> connection -> mutex );
}
else if ( descriptor -> connection -> protection_level == TS_LEVEL1 )
{
mutex_exit( &descriptor -> mutex );
}
break;
}
}
#endif
#ifdef WITH_HANDLE_REDIRECT
/*
* try and find a handle that has the suplied handle as the driver handle
* there will be threading issues with this, so be carefull.
* However it will normally only get used with "broken" drivers.
*/
void *find_parent_handle( DRV_SQLHANDLE drv_hand, int type )
{
void *found_handle = NULL;
mutex_entry( &mutex_lists );
switch( type ) {
case SQL_HANDLE_DBC:
{
DMHDBC hand = connection_root;
while( hand ) {
if ( hand -> driver_dbc == drv_hand ) {
found_handle = hand;
break;
}
hand = hand -> next_class_list;
}
}
break;
case SQL_HANDLE_STMT:
{
DMHSTMT hand = statement_root;
while( hand ) {
if ( hand -> driver_stmt == drv_hand ) {
found_handle = hand;
break;
}
hand = hand -> next_class_list;
}
}
break;
case SQL_HANDLE_DESC:
{
DMHDESC hand = descriptor_root;
while( hand ) {
if ( hand -> driver_desc == drv_hand ) {
found_handle = hand;
break;
}
hand = hand -> next_class_list;
}
}
break;
default:
break;
}
mutex_exit( &mutex_lists );
return found_handle;
}
#endif
|