summaryrefslogtreecommitdiff
path: root/vendor/thecodingmachine/safe/deprecated/mssql.php
blob: 234ef56b1cbe32eba4c16f8f11d7571a9d6745a3 (plain)
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
<?php

namespace Safe;

use Safe\Exceptions\MssqlException;

/**
 * Binds a parameter to a stored procedure or a remote stored procedure.
 *
 * @param resource $stmt Statement resource, obtained with mssql_init.
 * @param string $param_name The parameter name, as a string.
 *
 * You have to include the @ character, like in the
 * T-SQL syntax. See the explanation included in
 * mssql_execute.
 * @param mixed $var The PHP variable you'll bind the MSSQL parameter to. It is passed by
 * reference, to retrieve OUTPUT and RETVAL values after
 * the procedure execution.
 * @param int $type One of: SQLTEXT,
 * SQLVARCHAR, SQLCHAR,
 * SQLINT1, SQLINT2,
 * SQLINT4, SQLBIT,
 * SQLFLT4, SQLFLT8,
 * SQLFLTN.
 * @param bool $is_output Whether the value is an OUTPUT parameter or not. If it's an OUTPUT
 * parameter and you don't mention it, it will be treated as a normal
 * input parameter and no error will be thrown.
 * @param bool $is_null Whether the parameter is NULL or not. Passing the NULL value as
 * var will not do the job.
 * @param int $maxlen Used with char/varchar values. You have to indicate the length of the
 * data so if the parameter is a varchar(50), the type must be
 * SQLVARCHAR and this value 50.
 * @throws MssqlException
 *
 */
function mssql_bind($stmt, string $param_name, &$var, int $type, bool $is_output = false, bool $is_null = false, int $maxlen = -1): void
{
    error_clear_last();
    $result = \mssql_bind($stmt, $param_name, $var, $type, $is_output, $is_null, $maxlen);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}


/**
 * Closes the link to a MS SQL Server database that's associated with the
 * specified link identifier.  If the link identifier isn't specified, the
 * last opened link is assumed.
 *
 * Note that this isn't usually necessary, as non-persistent open
 * links are automatically closed at the end of the script's
 * execution.
 *
 * @param resource $link_identifier A MS SQL link identifier, returned by
 * mssql_connect.
 *
 * This function will not close persistent links generated by
 * mssql_pconnect.
 * @throws MssqlException
 *
 */
function mssql_close($link_identifier = null): void
{
    error_clear_last();
    if ($link_identifier !== null) {
        $result = \mssql_close($link_identifier);
    } else {
        $result = \mssql_close();
    }
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}


/**
 * mssql_connect establishes a connection to a
 * MS SQL server.
 *
 * The link to the server will be closed as soon as the execution of
 * the script ends, unless it's closed earlier by explicitly calling
 * mssql_close.
 *
 * @param string $servername The MS SQL server. It can also include a port number, e.g.
 * hostname:port (Linux), or
 * hostname,port (Windows).
 * @param string $username The username.
 * @param string $password The password.
 * @param bool $new_link If a second call is made to mssql_connect with the
 * same arguments, no new link will be established, but instead, the link
 * identifier of the already opened link will be returned. This parameter
 * modifies this behavior and makes mssql_connect
 * always open a new link, even if mssql_connect was
 * called before with the same parameters.
 * @return resource Returns a MS SQL link identifier on success.
 * @throws MssqlException
 *
 */
function mssql_connect(string $servername = null, string $username = null, string $password = null, bool $new_link = false)
{
    error_clear_last();
    if ($new_link !== false) {
        $result = \mssql_connect($servername, $username, $password, $new_link);
    } elseif ($password !== null) {
        $result = \mssql_connect($servername, $username, $password);
    } elseif ($username !== null) {
        $result = \mssql_connect($servername, $username);
    } elseif ($servername !== null) {
        $result = \mssql_connect($servername);
    } else {
        $result = \mssql_connect();
    }
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * mssql_data_seek moves the internal row
 * pointer of the MS SQL result associated with the specified result
 * identifier to point to the specified row number, first row being
 * number 0. The next call to mssql_fetch_row
 * would return that row.
 *
 * @param resource $result_identifier The result resource that is being evaluated.
 * @param int $row_number The desired row number of the new result pointer.
 * @throws MssqlException
 *
 */
function mssql_data_seek($result_identifier, int $row_number): void
{
    error_clear_last();
    $result = \mssql_data_seek($result_identifier, $row_number);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}


/**
 * Returns the length of field no. offset in
 * result.
 *
 * @param resource $result The result resource that is being evaluated. This result comes from a
 * call to mssql_query.
 * @param int $offset The field offset, starts at 0. If omitted, the current field is used.
 * @return int The length of the specified field index on success.
 * @throws MssqlException
 *
 */
function mssql_field_length($result, int $offset = -1): int
{
    error_clear_last();
    $result = \mssql_field_length($result, $offset);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * Returns the name of field no. offset in
 * result.
 *
 * @param resource $result The result resource that is being evaluated. This result comes from a
 * call to mssql_query.
 * @param int $offset The field offset, starts at 0. If omitted, the current field is used.
 * @return string The name of the specified field index on success.
 * @throws MssqlException
 *
 */
function mssql_field_name($result, int $offset = -1): string
{
    error_clear_last();
    $result = \mssql_field_name($result, $offset);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * Seeks to the specified field offset.  If the next call to
 * mssql_fetch_field won't include a field
 * offset, this field would be returned.
 *
 * @param resource $result The result resource that is being evaluated. This result comes from a
 * call to mssql_query.
 * @param int $field_offset The field offset, starts at 0.
 * @throws MssqlException
 *
 */
function mssql_field_seek($result, int $field_offset): void
{
    error_clear_last();
    $result = \mssql_field_seek($result, $field_offset);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}


/**
 * Returns the type of field no. offset in
 * result.
 *
 * @param resource $result The result resource that is being evaluated. This result comes from a
 * call to mssql_query.
 * @param int $offset The field offset, starts at 0. If omitted, the current field is used.
 * @return string The type of the specified field index on success.
 * @throws MssqlException
 *
 */
function mssql_field_type($result, int $offset = -1): string
{
    error_clear_last();
    $result = \mssql_field_type($result, $offset);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * mssql_free_result only needs to be called
 * if you are worried about using too much memory while your script
 * is running. All result memory will automatically be freed when
 * the script ends. You may call mssql_free_result
 * with the result identifier as an argument and the associated
 * result memory will be freed.
 *
 * @param resource $result The result resource that is being freed. This result comes from a
 * call to mssql_query.
 * @throws MssqlException
 *
 */
function mssql_free_result($result): void
{
    error_clear_last();
    $result = \mssql_free_result($result);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}


/**
 * mssql_free_statement only needs to be called
 * if you are worried about using too much memory while your script
 * is running. All statement memory will automatically be freed when
 * the script ends. You may call mssql_free_statement
 * with the statement identifier as an argument and the associated
 * statement memory will be freed.
 *
 * @param resource $stmt Statement resource, obtained with mssql_init.
 * @throws MssqlException
 *
 */
function mssql_free_statement($stmt): void
{
    error_clear_last();
    $result = \mssql_free_statement($stmt);
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}


/**
 * Initializes a stored procedure or a remote stored procedure.
 *
 * @param string $sp_name Stored procedure name, like ownew.sp_name or
 * otherdb.owner.sp_name.
 * @param resource $link_identifier A MS SQL link identifier, returned by
 * mssql_connect.
 * @return resource Returns a resource identifier "statement", used in subsequent calls to
 * mssql_bind and mssql_executes.
 * @throws MssqlException
 *
 */
function mssql_init(string $sp_name, $link_identifier = null)
{
    error_clear_last();
    if ($link_identifier !== null) {
        $result = \mssql_init($sp_name, $link_identifier);
    } else {
        $result = \mssql_init($sp_name);
    }
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * mssql_pconnect acts very much like
 * mssql_connect with two major differences.
 *
 * First, when connecting, the function would first try to find a
 * (persistent) link that's already open with the same host,
 * username and password.  If one is found, an identifier for it
 * will be returned instead of opening a new connection.
 *
 * Second, the connection to the SQL server will not be closed when
 * the execution of the script ends.  Instead, the link will remain
 * open for future use (mssql_close will not
 * close links established by mssql_pconnect).
 *
 * This type of links is therefore called 'persistent'.
 *
 * @param string $servername The MS SQL server. It can also include a port number. e.g.
 * hostname:port.
 * @param string $username The username.
 * @param string $password The password.
 * @param bool $new_link If a second call is made to mssql_pconnect with
 * the same arguments, no new link will be established, but instead, the
 * link identifier of the already opened link will be returned. This
 * parameter modifies this behavior and makes
 * mssql_pconnect always open a new link, even if
 * mssql_pconnect was called before with the same
 * parameters.
 * @return resource Returns a positive MS SQL persistent link identifier on success.
 * @throws MssqlException
 *
 */
function mssql_pconnect(string $servername = null, string $username = null, string $password = null, bool $new_link = false)
{
    error_clear_last();
    if ($new_link !== false) {
        $result = \mssql_pconnect($servername, $username, $password, $new_link);
    } elseif ($password !== null) {
        $result = \mssql_pconnect($servername, $username, $password);
    } elseif ($username !== null) {
        $result = \mssql_pconnect($servername, $username);
    } elseif ($servername !== null) {
        $result = \mssql_pconnect($servername);
    } else {
        $result = \mssql_pconnect();
    }
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * mssql_query sends a query to the currently active
 * database on the server that's associated with the specified link
 * identifier.
 *
 * @param string $query An SQL query.
 * @param resource $link_identifier A MS SQL link identifier, returned by
 * mssql_connect or
 * mssql_pconnect.
 *
 * If the link identifier isn't specified, the last opened link is
 * assumed.  If no link is open, the function tries to establish a link
 * as if mssql_connect was called, and use it.
 * @param int $batch_size The number of records to batch in the buffer.
 * @return mixed Returns a MS SQL result resource on success, TRUE if no rows were
 * returned.
 * @throws MssqlException
 *
 */
function mssql_query(string $query, $link_identifier = null, int $batch_size = 0)
{
    error_clear_last();
    if ($batch_size !== 0) {
        $result = \mssql_query($query, $link_identifier, $batch_size);
    } elseif ($link_identifier !== null) {
        $result = \mssql_query($query, $link_identifier);
    } else {
        $result = \mssql_query($query);
    }
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
    return $result;
}


/**
 * mssql_select_db sets the current active
 * database on the server that's associated with the specified link
 * identifier.
 *
 * Every subsequent call to mssql_query will be
 * made on the active database.
 *
 * @param string $database_name The database name.
 *
 * To escape the name of a database that contains spaces, hyphens ("-"),
 * or any other exceptional characters, the database name must be
 * enclosed in brackets, as is shown in the example, below. This
 * technique must also be applied when selecting a database name that is
 * also a reserved word (such as primary).
 * @param resource $link_identifier A MS SQL link identifier, returned by
 * mssql_connect or
 * mssql_pconnect.
 *
 * If no link identifier is specified, the last opened link is assumed.
 * If no link is open, the function will try to establish a link as if
 * mssql_connect was called, and use it.
 * @throws MssqlException
 *
 */
function mssql_select_db(string $database_name, $link_identifier = null): void
{
    error_clear_last();
    if ($link_identifier !== null) {
        $result = \mssql_select_db($database_name, $link_identifier);
    } else {
        $result = \mssql_select_db($database_name);
    }
    if ($result === false) {
        throw MssqlException::createFromPhpError();
    }
}