File: init.c

package info (click to toggle)
libodbc-ruby 0.9993-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 432 kB
  • ctags: 533
  • sloc: ansic: 6,815; ruby: 785; makefile: 49
file content (186 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download
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
/*
 * Part of ODBC-Ruby binding
 * Copyright (c) 2006 Christian Werner <chw@ch-werner.de>
 *
 * See the file "COPYING" for information on usage
 * and redistribution of this file and for a
 * DISCLAIMER OF ALL WARRANTIES.
 *
 * $Id: init.c,v 1.4 2006/08/16 05:44:23 chw Exp chw $
 */

#include "ruby.h"

#ifdef USE_DLOPEN_FOR_ODBC_LIBS

/*
 * This module acts as a drop-in replacement for linking with
 * "-lodbc -lodbcinst" or "-liodbc -liodbcinst" when dlopen()
 * is supported.
 *
 * Setting the environment variable RUBY_ODBC_DM can be used
 * to force loading a specific driver manager shared library.
 * Same logic is used with RUBY_ODBC_INST for the ODBC installer
 * shared library.
 */

#include <dlfcn.h>

/* Create weak alias and function declarations. */

#define WEAKFUNC(name) \
    int __attribute__((weak, alias("__"#name))) name (void); \
    static int __attribute__((unused)) __ ## name (void) \
	{ return -1; /* == SQL_ERROR */ }

#define WEAKFUNC_BOOL(name) \
    int __attribute__((weak, alias("__"#name))) name (void); \
    static int __attribute__((unused)) __ ## name (void) \
	{ return 0; /* == BOOL/FALSE */ }

WEAKFUNC(SQLAllocConnect)
WEAKFUNC(SQLAllocEnv)
WEAKFUNC(SQLAllocStmt)
WEAKFUNC(SQLBindParameter)
WEAKFUNC(SQLCancel)
WEAKFUNC(SQLDescribeParam)
WEAKFUNC(SQLDisconnect)
WEAKFUNC(SQLExecute)
WEAKFUNC(SQLFetch)
WEAKFUNC(SQLFetchScroll)
WEAKFUNC(SQLFreeConnect)
WEAKFUNC(SQLFreeEnv)
WEAKFUNC(SQLFreeStmt)
WEAKFUNC(SQLGetData)
WEAKFUNC(SQLGetEnvAttr)
WEAKFUNC(SQLGetStmtOption)
WEAKFUNC(SQLMoreResults)
WEAKFUNC(SQLNumParams)
WEAKFUNC(SQLNumResultCols)
WEAKFUNC(SQLRowCount)
WEAKFUNC(SQLSetEnvAttr)
WEAKFUNC(SQLSetStmtOption)
WEAKFUNC(SQLTransact)
WEAKFUNC(SQLEndTran)

WEAKFUNC(SQLColAttributes)
WEAKFUNC(SQLColAttributesW)
WEAKFUNC(SQLColumns)
WEAKFUNC(SQLColumnsW)
WEAKFUNC(SQLConnect)
WEAKFUNC(SQLConnectW)
WEAKFUNC(SQLDataSources)
WEAKFUNC(SQLDataSourcesW)
WEAKFUNC(SQLDriverConnect)
WEAKFUNC(SQLDriverConnectW)
WEAKFUNC(SQLDrivers)
WEAKFUNC(SQLDriversW)
WEAKFUNC(SQLError)
WEAKFUNC(SQLErrorW)
WEAKFUNC(SQLExecDirect)
WEAKFUNC(SQLExecDirectW)
WEAKFUNC(SQLForeignKeys)
WEAKFUNC(SQLForeignKeysW)
WEAKFUNC(SQLGetConnectOption)
WEAKFUNC(SQLGetConnectOptionW)
WEAKFUNC(SQLGetCursorName)
WEAKFUNC(SQLGetCursorNameW)
WEAKFUNC(SQLGetInfo)
WEAKFUNC(SQLGetInfoW)
WEAKFUNC(SQLGetTypeInfo)
WEAKFUNC(SQLGetTypeInfoW)
WEAKFUNC(SQLPrepare)
WEAKFUNC(SQLPrepareW)
WEAKFUNC(SQLPrimaryKeys)
WEAKFUNC(SQLPrimaryKeysW)
WEAKFUNC(SQLProcedureColumns)
WEAKFUNC(SQLProcedureColumnsW)
WEAKFUNC(SQLProcedures)
WEAKFUNC(SQLProceduresW)
WEAKFUNC(SQLSetConnectOption)
WEAKFUNC(SQLSetConnectOptionW)
WEAKFUNC(SQLSetCursorName)
WEAKFUNC(SQLSetCursorNameW)
WEAKFUNC(SQLSpecialColumns)
WEAKFUNC(SQLSpecialColumnsW)
WEAKFUNC(SQLStatistics)
WEAKFUNC(SQLStatisticsW)
WEAKFUNC(SQLTablePrivileges)
WEAKFUNC(SQLTablePrivilegesW)
WEAKFUNC(SQLTables)
WEAKFUNC(SQLTablesW)
WEAKFUNC(SQLInstallerError)
WEAKFUNC(SQLInstallerErrorW)

WEAKFUNC_BOOL(SQLConfigDataSource)
WEAKFUNC_BOOL(SQLConfigDataSourceW)
WEAKFUNC_BOOL(SQLReadFileDSN)
WEAKFUNC_BOOL(SQLReadFileDSNW)
WEAKFUNC_BOOL(SQLWriteFileDSN)
WEAKFUNC_BOOL(SQLWriteFileDSNW)

/* Library initializer and finalizer. */

static void *lib_odbc = 0;
static void *lib_odbcinst = 0;

#define warn(msg) fputs(msg, stderr)

void
ruby_odbc_init()
{
    int useiodbc = 0;
    char *dm_name = getenv("RUBY_ODBC_DM");
    char *inst_name = getenv("RUBY_ODBC_INST");

    if (dm_name) {
	lib_odbc = dlopen(dm_name, RTLD_NOW | RTLD_GLOBAL);
	if (!lib_odbc) {
	    warn("WARNING: $RUBY_ODBC_DM not loaded.\n");
	} else {
	    if (inst_name) {
		lib_odbcinst = dlopen(inst_name, RTLD_NOW | RTLD_GLOBAL);
	    }
	    if (!lib_odbcinst) {
		warn("WARNING: $RUBY_ODBC_INST not loaded.\n");
	    }
	    return;
	}
    }
    lib_odbc = dlopen("libodbc" DLEXT, RTLD_NOW | RTLD_GLOBAL);
    if (!lib_odbc) {
	lib_odbc = dlopen("libiodbc" DLEXT, RTLD_NOW | RTLD_GLOBAL);
	if (!lib_odbc) {
	    warn("WARNING: no ODBC driver manager found.\n");
	    return;
	}
	useiodbc = 1;
    }
    lib_odbcinst = dlopen(useiodbc ?
			  "libiodbcinst" DLEXT : "libodbcinst" DLEXT,
			  RTLD_NOW | RTLD_GLOBAL);
    if (!lib_odbcinst) {
	warn("WARNING: no ODBC installer library found.\n");
    }
}

void
ruby_odbc_fini()
{
    if (lib_odbcinst) {
	dlclose(lib_odbcinst);
	lib_odbcinst = 0;
    }
    if (lib_odbc) {
	dlclose(lib_odbc);
	lib_odbc = 0;
    }
}

int
ruby_odbc_have_func(char *name, void *addr)
{
    return name && addr && (dlsym(NULL, name) != addr);
}

#endif