-
Notifications
You must be signed in to change notification settings - Fork 44
/
mysql.prg
229 lines (158 loc) · 7.26 KB
/
mysql.prg
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
#ifdef __PLATFORM__WINDOWS
#include "c:\harbour\include\hbdyn.ch"
#else
#include "/usr/include/harbour/hbdyn.ch"
#endif
#define HB_VERSION_BITWIDTH 17
#define NULL 0
static pLib, hMySQL := 0, hConnection := 0, hMyRes := 0
//----------------------------------------------------------------//
function Main()
local nRetVal, n, m, hField, hRow
// ShowConsole()
// SetMode( 40, 120 )
pLib = hb_LibLoad( hb_SysMySQL() )
?? "pLib = " + ValType( pLib ) + ;
If( ValType( pLib ) == "P", " (MySQL library properly loaded)", " (MySQL library not found)" )
if ValType( pLib ) == "P"
hMySQL = mysql_init()
? "hMySQL = " + Str( hMySQL ) + " (MySQL library " + ;
If( hMySQL != 0, "properly initalized)", "failed to initialize)" )
endif
? If( hMySQL != 0, "MySQL version: " + mysql_get_server_info( hMySQL ), "" )
if hMySQL != 0
? "Connection = "
?? hConnection := mysql_real_connect( "localhost", "harbour", "password", "dbHarbour", 3306 )
?? If( hConnection != hMySQL, " (Failed connection)", " (Successfull connection)" )
? If( hConnection != hMySQL, "Error: " + mysql_error( hMySQL ), "" )
endif
if hConnection != 0
nRetVal = mysql_query( hConnection, "select * from users" )
? "MySQL query " + If( nRetVal == 0, "succeeded", "failed" )
if nRetVal != 0
? "error: " + Str( nRetVal )
endif
endif
if hConnection != 0
hMyRes = mysql_store_result( hConnection )
? "MySQL store result " + If( hMyRes != 0, "succeeded", "failed" )
?
endif
if hMyRes != 0
? "Number of rows: " + Str( mysql_num_rows( hMyRes ) )
endif
if hMyRes != 0
? "Number of fields: " + Str( mysql_num_fields( hMyRes ) )
? "<table border=1 cellspacing=0>"
?? "<tr>"
for n = 1 to mysql_num_fields( hMyRes )
hField = mysql_fetch_field( hMyRes )
if hField != 0
?? "<td>" + PtrToStr( hField, 0 ) + "</td>"
endif
next
?? "</tr>"
for n = 1 to mysql_num_rows( hMyRes )
if ( hRow := mysql_fetch_row( hMyRes ) ) != 0
?? "<tr>"
for m = 1 to mysql_num_fields( hMyRes )
?? "<td>" + PtrToStr( hRow, m - 1 ) + "</td>"
next
?? "</tr>"
endif
next
?? "</table>"
endif
if hMyRes != 0
mysql_free_result( hMyRes )
endif
if hMySQL != 0
mysql_close( hMySQL )
endif
if ValType( pLib ) == "P"
? "MySQL library properly freed: ", HB_LibFree( pLib )
endif
return nil
//----------------------------------------------------------------//
function mysql_init()
return hb_DynCall( { "mysql_init", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ) }, NULL )
//----------------------------------------------------------------//
function mysql_close( hMySQL )
return hb_DynCall( { "mysql_close", pLib,;
hb_SysCallConv(), hb_SysLong() }, hMySQL )
//----------------------------------------------------------------//
function mysql_real_connect( cServer, cUserName, cPassword, cDataBaseName, nPort )
if nPort == nil
nPort = 3306
endif
return hb_DynCall( { "mysql_real_connect", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ), hb_SysLong(),;
HB_DYN_CTYPE_CHAR_PTR, HB_DYN_CTYPE_CHAR_PTR, HB_DYN_CTYPE_CHAR_PTR, HB_DYN_CTYPE_CHAR_PTR,;
HB_DYN_CTYPE_LONG, HB_DYN_CTYPE_LONG, HB_DYN_CTYPE_LONG },;
hMySQL, cServer, cUserName, cPassword, cDataBaseName, nPort, 0, 0 )
//----------------------------------------------------------------//
function mysql_query( hConnect, cQuery )
return hb_DynCall( { "mysql_query", pLib, hb_bitOr( HB_DYN_CTYPE_INT,;
hb_SysCallConv() ), hb_SysLong(), HB_DYN_CTYPE_CHAR_PTR },;
hConnect, cQuery )
//----------------------------------------------------------------//
function mysql_use_result( hMySQL )
return hb_DynCall( { "mysql_use_result", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ), hb_SysLong() }, hMySQL )
//----------------------------------------------------------------//
function mysql_store_result( hMySQL )
return hb_DynCall( { "mysql_store_result", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ), hb_SysLong() }, hMySQL )
//----------------------------------------------------------------//
function mysql_free_result( hMyRes)
return hb_DynCall( { "mysql_free_result", pLib,;
hb_SysCallConv(), hb_SysLong() }, hMyRes )
//----------------------------------------------------------------//
function mysql_fetch_row( hMyRes )
return hb_DynCall( { "mysql_fetch_row", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ), hb_SysLong() }, hMyRes )
//----------------------------------------------------------------//
function mysql_num_rows( hMyRes )
return hb_DynCall( { "mysql_num_rows", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ), hb_SysLong() }, hMyRes )
//----------------------------------------------------------------//
function mysql_num_fields( hMyRes )
return hb_DynCall( { "mysql_num_fields", pLib, hb_bitOr( HB_DYN_CTYPE_LONG_UNSIGNED,;
hb_SysCallConv() ), hb_SysLong() }, hMyRes )
//----------------------------------------------------------------//
function mysql_fetch_field( hMyRes )
return hb_DynCall( { "mysql_fetch_field", pLib, hb_bitOr( hb_SysLong(),;
hb_SysCallConv() ), hb_SysLong() }, hMyRes )
//----------------------------------------------------------------//
function mysql_get_server_info( hMySQL )
return hb_DynCall( { "mysql_get_server_info", pLib, hb_bitOr( HB_DYN_CTYPE_CHAR_PTR,;
hb_SysCallConv() ), hb_SysLong() }, hMySql )
//----------------------------------------------------------------//
function mysql_error( hMySQL )
return hb_DynCall( { "mysql_error", pLib, hb_bitOr( HB_DYN_CTYPE_CHAR_PTR,;
hb_SysCallConv() ), hb_SysLong() }, hMySql )
//----------------------------------------------------------------//
function hb_SysLong()
return If( hb_OSIS64BIT(), HB_DYN_CTYPE_LLONG_UNSIGNED, HB_DYN_CTYPE_LONG_UNSIGNED )
//----------------------------------------------------------------//
function hb_SysCallConv()
return If( ! "Windows" $ OS(), HB_DYN_CALLCONV_CDECL, HB_DYN_CALLCONV_STDCALL )
//----------------------------------------------------------------//
function hb_SysMySQL()
local cLibName
if ! "Windows" $ OS()
if "Darwin" $ OS()
cLibName = "/usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.dylib"
else
cLibName = If( hb_version( HB_VERSION_BITWIDTH ) == 64,;
"/usr/lib/x86_64-linux-gnu/libmariadb.so.3",; // libmysqlclient.so.20 for mariaDB
"/usr/lib/x86-linux-gnu/libmariadbclient.so" )
endif
else
cLibName = If( hb_version( HB_VERSION_BITWIDTH ) == 64,;
"c:/Apache24/htdocs/libmysql64.dll",;
"c:/Apache24/htdocs/libmysql.dll" )
endif
return cLibName
//----------------------------------------------------------------//