Skip to content

Commit 79848fa

Browse files
author
Ilia Alshanetsky
committed
Added sqlite_fetch_column_types() function.
1 parent 3203957 commit 79848fa

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ PHP NEWS
2929
. stream_socket_sendto() and stream_socket_recvfrom(). (Wez)
3030
. iconv_mime_decode_headers(). (Moriyoshi)
3131
. get_declared_interfaces(). (Andrey, Marcus)
32+
. sqlite_fetch_column_types(). (Ilia)
3233
- Added proxy support to http:// wrapper. (Sara)
3334
- Added rename(), rmdir() and mkdir() support to userstreams. (Sara)
3435
- Added rename(), rmdir() and mkdir() support to ftp:// wrapper. (Sara)

ext/sqlite/php_sqlite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ PHP_FUNCTION(sqlite_udf_encode_binary);
8888

8989
PHP_FUNCTION(sqlite_factory);
9090

91+
PHP_FUNCTION(sqlite_fetch_column_types);
92+
9193
ZEND_BEGIN_MODULE_GLOBALS(sqlite)
9294
int assoc_case;
9395
ZEND_END_MODULE_GLOBALS(sqlite)

ext/sqlite/sqlite.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ function_entry sqlite_functions[] = {
192192
PHP_FE(sqlite_factory, third_arg_force_ref)
193193
PHP_FE(sqlite_udf_encode_binary, NULL)
194194
PHP_FE(sqlite_udf_decode_binary, NULL)
195+
PHP_FE(sqlite_fetch_column_types, NULL)
195196
{NULL, NULL, NULL}
196197
};
197198

@@ -211,6 +212,7 @@ function_entry sqlite_funcs_db[] = {
211212
PHP_ME_MAPPING(create_function, sqlite_create_function, NULL)
212213
PHP_ME_MAPPING(busy_timeout, sqlite_busy_timeout, NULL)
213214
PHP_ME_MAPPING(last_error, sqlite_last_error, NULL)
215+
PHP_ME_MAPPING(fetch_column_types, sqlite_fetch_column_types, NULL)
214216
/* PHP_ME_MAPPING(error_string, sqlite_error_string, NULL) static */
215217
/* PHP_ME_MAPPING(escape_string, sqlite_escape_string, NULL) static */
216218
{NULL, NULL, NULL}
@@ -1552,6 +1554,72 @@ PHP_FUNCTION(sqlite_unbuffered_query)
15521554
}
15531555
/* }}} */
15541556

1557+
/* {{{ proto resource sqlite_fetch_column_types(string table_name, resource db)
1558+
Return an array of column types from a particular table. */
1559+
PHP_FUNCTION(sqlite_fetch_column_types)
1560+
{
1561+
zval *zdb;
1562+
struct php_sqlite_db *db;
1563+
char *tbl, *sql;
1564+
long tbl_len;
1565+
char *errtext = NULL;
1566+
zval *object = getThis();
1567+
struct php_sqlite_result res;
1568+
const char **rowdata, **colnames, *tail;
1569+
int i, ncols;
1570+
1571+
if (object) {
1572+
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &tbl, &tbl_len)) {
1573+
return;
1574+
}
1575+
DB_FROM_OBJECT(db, object);
1576+
} else {
1577+
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
1578+
ZEND_NUM_ARGS() TSRMLS_CC, "sr", &tbl, &tbl_len, &zdb) &&
1579+
FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zdb, &tbl, &tbl_len)) {
1580+
return;
1581+
}
1582+
DB_FROM_ZVAL(db, &zdb);
1583+
}
1584+
1585+
if (!(sql = sqlite_mprintf("SELECT * FROM %q LIMIT 1", tbl))) {
1586+
RETURN_FALSE;
1587+
}
1588+
1589+
sqlite_exec(db->db, "PRAGMA show_datatypes = ON", NULL, NULL, &errtext);
1590+
1591+
db->last_err_code = sqlite_compile(db->db, sql, &tail, &res.vm, &errtext);
1592+
1593+
sqlite_freemem(sql);
1594+
1595+
if (db->last_err_code != SQLITE_OK) {
1596+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
1597+
sqlite_freemem(errtext);
1598+
RETVAL_FALSE;
1599+
goto done;
1600+
}
1601+
1602+
sqlite_step(res.vm, &ncols, &rowdata, &colnames);
1603+
1604+
array_init(return_value);
1605+
1606+
for (i = 0; i < ncols; i++) {
1607+
char *colname = (char *)colnames[i];
1608+
1609+
if (SQLITE_G(assoc_case) == 1) {
1610+
php_sqlite_strtoupper(colname);
1611+
} else if (SQLITE_G(assoc_case) == 2) {
1612+
php_sqlite_strtolower(colname);
1613+
}
1614+
1615+
add_assoc_string(return_value, colname, colnames[ncols + i] ? (char *)colnames[ncols + i] : "", 1);
1616+
}
1617+
1618+
done:
1619+
sqlite_exec(db->db, "PRAGMA show_datatypes = OFF", NULL, NULL, &errtext);
1620+
}
1621+
/* }}} */
1622+
15551623
/* {{{ proto resource sqlite_query(string query, resource db [, int result_type ])
15561624
Executes a query against a given database and returns a result handle. */
15571625
PHP_FUNCTION(sqlite_query)

ext/sqlite/tests/sqlite_026.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
sqlite: sqlite_fetch_column_types
3+
--SKIPIF--
4+
<?php # vim:ft=php
5+
if (!extension_loaded("sqlite")) print "skip"; ?>
6+
--FILE--
7+
<?php
8+
include "blankdb.inc";
9+
10+
sqlite_query($db, "CREATE TABLE strings(a, b INTEGER, c VARCHAR(10), d)");
11+
sqlite_query($db, "INSERT INTO strings VALUES('1', '2', '3', 'abc')");
12+
13+
var_dump(sqlite_fetch_column_types($db, "strings"));
14+
15+
sqlite_close($db);
16+
?>
17+
--EXPECT--
18+
array(4) {
19+
["a"]=>
20+
string(0) ""
21+
["b"]=>
22+
string(7) "INTEGER"
23+
["c"]=>
24+
string(11) "VARCHAR(10)"
25+
["d"]=>
26+
string(0) ""
27+
}

ext/sqlite/tests/sqlite_oo_028.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
sqlite-oo: sqlite_fetch_column_types
3+
--SKIPIF--
4+
<?php # vim:ft=php
5+
if (!extension_loaded("sqlite")) print "skip"; ?>
6+
--FILE--
7+
<?php
8+
include "blankdb_oo.inc";
9+
10+
$db->query("CREATE TABLE strings(a, b INTEGER, c VARCHAR(10), d)");
11+
$db->query("INSERT INTO strings VALUES('1', '2', '3', 'abc')");
12+
13+
var_dump($db->fetch_column_types("strings"));
14+
?>
15+
--EXPECT--
16+
array(4) {
17+
["a"]=>
18+
string(0) ""
19+
["b"]=>
20+
string(7) "INTEGER"
21+
["c"]=>
22+
string(11) "VARCHAR(10)"
23+
["d"]=>
24+
string(0) ""
25+
}

0 commit comments

Comments
 (0)