| 11. Database Access |
| Module Sql |
The SQL module is a unified interface between pike and all its supported databases. The parts of this module that is usuable for all normal uses is the Sql.Sql class and the Sql.sql_result class.
string people_in_group(string group) { Sql.Sql db = Sql.Sql("mysql://localhost/testdb"); return db->query("SELECT name FROM users WHERE " "group=%s", group)->name * ","; }
| CLASS Sql.Sql |
Implements those functions that need not be present in all SQL-modules.
object Sql.Sql()->master_sql
Object to use for the actual SQL-queries.
int Sql.Sql()->case_convert
Convert all field names in mappings to lower_case. Only relevant to databases which only implement big_query(), and use upper/mixed-case fieldnames (eg Oracle).
|
string Sql.Sql()->quote(string s)
Quote a string s so that it can safely be put in a query.
string Sql.Sql()->encode_time(int t, int|void is_utc)
Converts a system time value to an appropriately formatted time spec for the database. t Time to encode. is_utc If nonzero then time is taken as a "full" unix time spec (where the date part is ignored), otherwise it's converted as a seconds-since-midnight value.
int Sql.Sql()->decode_time(string t, int|void want_utc)
Converts a database time spec to a system time value. t Time spec to decode. want_utc Take the date part from this system time value. If zero, a seconds-since-midnight value is returned.
string Sql.Sql()->encode_date(int t)
Converts a system time value to an appropriately formatted date-only spec for the database. t Time to encode.
int Sql.Sql()->decode_date(string d)
Converts a database date-only spec to a system time value. d Date spec to decode.
string Sql.Sql()->encode_datetime(int t)
Converts a system time value to an appropriately formatted date and time spec for the database. t Time to encode.
int Sql.Sql()->decode_datetime(string datetime)
Converts a database date and time spec to a system time value. datetime Date and time spec to decode.
void Sql.Sql()->create()
void Sql.Sql()->create(string host)
void Sql.Sql()->create(string host, string db)
void Sql.Sql()->create(string host, mapping(string:int|string) options)
void Sql.Sql()->create(string host, string db, string user)
void Sql.Sql()->create(string host, string db, string user, string password)
void Sql.Sql()->create(string host, string db, string user, string password, mapping(string:int|string) options)
void Sql.Sql()->create(object host)
void Sql.Sql()->create(object host, string db)
Create a new generic SQL object.
|
Select this database.
User name to access the database as.
Password to access the database.
Optional mapping of options. See the SQL-database documentation for the supported options. (eg Mysql.mysql).
In versions of Pike prior to 7.2 it was possible to leave out the dbtype, but that has been deprecated, since it never worked well.
Support for options was added in Pike 7.3.
int|string Sql.Sql()->error()
Return last error message.
void Sql.Sql()->select_db(string db)
Select database to access.
string|object Sql.Sql()->compile_query(string q)
Compiles the query (if possible). Otherwise returns it as is. The resulting object can be used multiple times in query() and big_query().
SQL-query to compile.
array(string|mapping(string|int:mixed)) Sql.Sql()->handle_extraargs(string query, array(mixed) extraargs)
Handle sprintf-based quoted arguments
array(mapping(string:mixed)) Sql.Sql()->query(object|string q, mixed ... extraargs)
Send an SQL query to the underlying SQL-server. The result is returned as an array of mappings indexed on the name of the columns. Returns 0 if the query didn't return any result (e.g. INSERT or similar).
Query to send to the SQL-server. This can either be a string with the query, or a previously compiled query (see compile_query()).
This parameter, if specified, can be in two forms:
A mapping containing bindings of variables used in the query. A variable is identified by a colon (:) followed by a name or number. Each index in the mapping corresponds to one such variable, and the value for that index is substituted (quoted) into the query wherever the variable is used.
query("select foo from bar where gazonk=:baz",
([":baz":"value"])) )
Binary values (BLOBs) may need to be placed in multisets.
Arguments as you would use in sprintf. They are automatically quoted.
query("select foo from bar where gazonk=%s","value") )
int|object Sql.Sql()->big_query(object|string q, mixed ... extraargs)
Send an SQL query to the underlying SQL-server. The result is returned as a Sql.sql_result object. This allows for having results larger than the available memory, and returning some more info about the result. Returns 0 if the query didn't return any result (e.g. INSERT or similar). For the other arguments, they are the same as for the Sql.Sql.query function.
void Sql.Sql()->create_db(string db)
Create a new database.
Name of database to create.
void Sql.Sql()->drop_db(string db)
Drop database
Name of database to drop.
void Sql.Sql()->shutdown()
Shutdown a database server.
void Sql.Sql()->reload()
Reload the tables.
string Sql.Sql()->server_info()
Return info about the current SQL-server.
string Sql.Sql()->host_info()
Return info about the connection to the SQL-server.
array(string) Sql.Sql()->list_dbs(string|void wild)
List available databases on this SQL-server.
Optional wildcard to match against.
array(string) Sql.Sql()->list_tables(string|void wild)
List tables available in the current database.
Optional wildcard to match against.
array(mapping(string:mixed)) Sql.Sql()->list_fields(string table, string|void wild)
List fields available in the specified table
Table to list the fields of.
Optional wildcard to match against.
| CLASS Sql.postgres_result |
Sql.postgres_result contains the result of a Postgres-query. See Sql.Sql.postgres for a description of this program's functions.
| CLASS Sql.postgres |
This is an interface to the Postgres (Postgres95, pgsql) database server. This module may or may not be availible on your Pike, depending whether the appropriate include and library files could be found at compile-time. Note that you do not need to have a Postgres server running on your host to use this module: you can connect to the database over a TCP/IP socket.
Also note that this module uses blocking I/O I/O to connect to the server. Postgres is quite slow, and so you might want to consider this particular aspect. It is (at least should be) thread-safe, and so it can be used in a multithread environment.
The behavior of the Postgres C API also depends on certain environment variables defined in the environment of the pike interpreter.
|
Refer to the Postgres documentation for further details.
Sql.Sql, Postgres.postgres, Sql.postgres_result
void Sql.postgres()->select_db(string dbname)
This function allows you to connect to a database. Due to restrictions of the Postgres frontend-backend protocol, you always have to be connected to a database, so in fact this function just allows you to connect to a different database on the same server.
This function can raise exceptions if something goes wrong (backend process not running, not enough permissions..)
create
string Sql.postgres()->error()
This function returns the textual description of the last server-related error. Returns 0 if no error has occurred yet. It is not cleared upon reading (can be invoked multiple times, will return the same result until a new error occurs).
big_query
string Sql.postgres()->host_info()
This function returns a string describing what host are we talking to, and how (TCP/IP or UNIX sockets).
void Sql.postgres()->reset()
This function resets the connection to the backend. Can be used for a variety of reasons, for example to detect the status of a connection.
This function is Postgres-specific, and thus it is not availible through the generic SQL-interface.
string Sql.postgres()->version
Should you need to report a bug to the author, please submit along with the report the driver version number, as returned by this call.
void Sql.postgres()->create()
void Sql.postgres()->create(string host, void|string database, void|string user, void|string password)
With no arguments, this function initializes (reinitializes if a connection had been previously set up) a connection to the Postgres backend. Since Postgres requires a database to be selected, it will try to connect to the default database. The connection may fail however for a variety of reasons, in this case the most likely of all is because you don't have enough authority to connect to that database. So use of this particular syntax is discouraged.
The host argument can have the syntax "hostname" or "hostname:portname". This allows to specify the TCP/IP port to connect to. If it is 0 or "", it will try to connect to localhost, default port.
The database argument specifies the database to connect to. If 0 or "", it will try to connect to the specified database.
The username and password arguments are silently ignored, since the Postgres C API doesn't allow to connect to the server as any user different than the user running the interface.
You need to have a database selected before using the sql-object, otherwise you'll get exceptions when you try to query it. Also notice that this function can raise exceptions if the db server doesn't respond, if the database doesn't exist or is not accessible by you.
You don't need bothering about syncronizing the connection to the database: it is automatically closed (and the database is sync-ed) when the object is destroyed.
Postgres.postgres, Sql.Sql, postgres->select_db
void Sql.postgres()->set_notify_callback()
void Sql.postgres()->set_notify_callback(function f)
void Sql.postgres()->set_notify_callback(function f, int|float poll_delay)
With Postgres you can associate events and notifications to tables. This function allows you to detect and handle such events.
With no arguments, resets and removes any callback you might have put previously, and any polling cycle.
With one argument, sets the notification callback (there can be only one for each sqlobject).
With two arguments, sets a notification callback and sets a polling cycle.
The polling cycle is necessary because of the way notifications are delivered, that is piggyback with a query result. This means that if you don't do any query, you'll receive no notification. The polling cycle starts a call_out cycle which will do an empty query when the specified interval expires, so that pending notifications may be delivered.
The callback function must return no value, and takes a string argument, which will be the name of the table on which the notification event has occured. In future versions, support for user-specified arguments will be added.
The polling cycle can be run only if your process is in "event-driven mode" (that is, if 'main' has returned a negative number).
This function is Postgres-specific, and thus it is not availible through the generic SQL-interface.
An integer can be passed as first argument, but it's effect is not documented.
void Sql.postgres()->create_db(string db)
This function creates a new database with the given name (assuming we have enough permissions to do this).
drop_db
void Sql.postgres()->drop_db(string db)
This function destroys a database and all the data it contains (assuming we have enough permissions to do so).
create_db
string Sql.postgres()->server_info()
This function returns a string describing the server we are talking to. It has the form "servername/serverversion" (like the HTTP protocol description) and is most useful in conjunction with the generic SQL-server module.
array(string) Sql.postgres()->list_dbs(void|string glob)
Lists all the databases availible on the server. If glob is specified, lists only those databases matching it.
array(string) Sql.postgres()->list_tables(void|string glob)
Returns an array containing the names of all the tables in the currently selected database. If a glob is specified, it will return only those tables whose name matches it.
array(mapping(string:mixed)) Sql.postgres()->list_fields(string table, void|string wild)
Returns a mapping, indexed on the column name, of mappings describing the attributes of a table of the current database. If a glob is specified, will return descriptions only of the columns matching it.
The current fields are:
|
int|object Sql.postgres()->big_query(object|string q, mapping(string|int:mixed)|void bindings)
This is the only provided interface which allows you to query the database. If you wish to use the simpler "query" function, you need to use the Sql.sql generic sql-object.
It returns a postgres_result object (which conforms to the Sql.sql_result standard interface for accessing data). I recommend using query() for simpler queries (because it is easier to handle, but stores all the result in memory), and big_query for queries you expect to return huge amounts of data (it's harder to handle, but fectches results on demand).
This function can raise exceptions.
Sql.Sql, Sql.sql_result
| CLASS Sql.mysql |
Implements the glue needed to access the Mysql-module from the generic SQL module.
string Sql.mysql()->quote(string s)
Quote a string so that it can safely be put in a query.
String to quote.
string Sql.mysql()->encode_time(int time, void|int date)
Converts a system time value to an appropriately formatted time spec for the database.
Time to encode.
If nonzero then time is taken as a "full" unix time spec (where the date part is ignored), otherwise it's converted as a seconds-since-midnight value.
string Sql.mysql()->encode_date(int time)
Converts a system time value to an appropriately formatted date-only spec for the database.
Time to encode.
string Sql.mysql()->encode_datetime(int time)
Converts a system time value to an appropriately formatted date and time spec for the database.
Time to encode.
int Sql.mysql()->decode_time(string timestr, void|int date)
Converts a database time spec to a system time value.
Time spec to decode.
Take the date part from this system time value. If zero, a seconds-since-midnight value is returned.
int Sql.mysql()->decode_date(string datestr)
Converts a database date-only spec to a system time value. Assumes 4-digit years.
Date spec to decode.
int Sql.mysql()->decode_datetime(string timestr)
Converts a database date and time spec to a system time value. Can decode strings missing the time part.
Date and time spec to decode.
int|object Sql.mysql()->big_query(string q, mapping(string|int:mixed)|void bindings)
int(0..1) Sql.mysql()->is_keyword(string name)
Return 1 if the argument name is a mysql keyword.
| CLASS Sql.sql_result |
Implements the generic result of the SQL-interface. Used for return results from SQL.sql->big_query().
object|array Sql.sql_result()->master_res
The actual result.
int Sql.sql_result()->index
This is the number of the current row. (0 <= Sql.sql_result.index < Sql.sql_result.num_rows)
void Sql.sql_result()->create(object|array res)
Create a new Sql.sql_result object
Result to use as base.
int Sql.sql_result()->num_rows()
Returns the number of rows in the result.
int Sql.sql_result()->num_fields()
Returns the number of fields in the result.
int Sql.sql_result()->eof()
Returns non-zero if there are no more rows.
array(mapping(string:mixed)) Sql.sql_result()->fetch_fields()
Return information about the available fields.
void Sql.sql_result()->seek(int skip)
Skip past a number of rows.
Number of rows to skip.
int|array(string|int) Sql.sql_result()->fetch_row()
Fetch the next row from the result.
| Module Sql.sql_util |
Some SQL utility functions
string Sql.sql_util.quote(string s)
Quote a string so that it can safely be put in a query.
String to quote.
void Sql.sql_util.fallback()
Throw an error in case an unimplemented function is called.
string Sql.sql_util.emulate_bindings(string query, mapping(string|int:mixed)|void bindings, void|object driver)
Build a raw SQL query, given the cooked query and the variable bindings It's meant to be used as an emulation engine for those drivers not providing such a behaviour directly (i.e. Oracle). The raw query can contain some variables (identified by prefixing a colon to a name or a number (i.e. ":var" or ":2"). They will be replaced by the corresponding value in the mapping.
The query.
Optional mapping containing the variable bindings. Make sure that no confusion is possible in the query. If necessary, change the variables' names.
| Module Mysql |
This module enables access to the Mysql database from within Pike. Use Sql.sql for general database access.
Mysql is available from http://www.mysql.com.
Mysql.mysql, Mysql.mysql_result, Sql.sql
| CLASS Mysql.mysql_result |
Objects of this class contain the result from Mysql queries.
Mysql.mysql, Mysql.mysql.big_query
void Mysql.mysql_result()->create(object(Mysql.mysql) connection)
Make a new Mysql.mysql_result object.
Mysql.mysql.big_query, Mysql.mysql.list_dbs, Mysql.mysql.list_tables, Mysql.mysql.list_processes, Mysql.mysql
int Mysql.mysql_result()->num_rows()
Number of rows in the result.
Mysql.mysql_result.num_fields
int Mysql.mysql_result()->num_fields()
Number of fields in the result.
Mysql.mysql_result.num_rows
void Mysql.mysql_result()->field_seek(int field_no)
Skip to specified field.
Places the field cursor at the specified position. This affects which field mysql_result->fetch_field() will return next.
Fields are numbered starting with 0.
This function is usually not enabled. To enable it SUPPORT_FIELD_SEEK must be defined when compiling the mysql-module.
Mysql.mysql_result.fetch_field, Mysql.mysql_result.fetch_fields
int(0..1) Mysql.mysql_result()->eof()
Sense end of result table.
Returns 1 when all rows have been read, and 0 (zero) otherwise.
Mysql.mysql_result.fetch_row
int|mapping(string:mixed) Mysql.mysql_result()->fetch_field()
Return specification of the current field.
Returns a mapping with information about the current field, and advances the field cursor one step. Returns 0 (zero) if there are no more fields.
The mapping contains the same entries as those returned by Mysql.mysql.list_fields, except that the entry "default" is missing.
This function is usually not enabled. To enable it SUPPORT_FIELD_SEEK must be defined when compiling the mysql-module.
Mysql.mysql_result.fetch_fields, Mysql.mysql_result.field_seek, Mysql.mysql.list_fields
array(int|mapping(string:mixed)) Mysql.mysql_result()->fetch_fields()
Get specification of all remaining fields.
Returns an array with one mapping for every remaining field in the result table.
The returned data is similar to the data returned by Mysql.mysql.list_fields, except for that the entry "default" is missing.
Resets the field cursor to 0 (zero).
This function always exists even when Mysql.mysql_result.fetch_field and Mysql.mysql_result.field_seek don't.
Mysql.mysql_result.fetch_field, Mysql.mysql_result.field_seek, Mysql.mysql.list_fields
void Mysql.mysql_result()->seek(int rows)
Skip ahead rows rows.
Can only seek forward.
Mysql.mysql_result.fetch_row
int|array(string) Mysql.mysql_result()->fetch_row()
Fetch the next row from the result.
Returns an array with the contents of the next row in the result. Advances the row cursor to the next now.
Returns 0 (zero) at the end of the table.
Mysql.mysql_result.seek
| CLASS Mysql.mysql |
Interface to the Mysql database.
This class enables access to the Mysql database from within Pike.
Mysql is available from http://www.mysql.com.
Mysql.result, Sql.sql
void Mysql.mysql()->create()
void Mysql.mysql()->create(string host)
void Mysql.mysql()->create(string host, string database)
void Mysql.mysql()->create(string host, string database, string user)
void Mysql.mysql()->create(string host, string database, string user, string password)
void Mysql.mysql()->create(string host, string database, string user, string password, mapping(string:string|int) options)
Connect to a Mysql database.
To access the Mysql database, you must first connect to it. This is done with this function.
If you give no argument, or give "" as host it will connect with a UNIX-domain socket, which can be a big performance gain.
This optional mapping can contain zero or more of the following parameters:
|
Some options may not be implemented. Unimplemented options are silently ignored.
string Mysql.mysql()->_sprintf(int type, void|mapping flags)
int Mysql.mysql()->affected_rows()
Returns the number of rows affected by the last query.
int Mysql.mysql()->insert_id()
Returns the id of the last INSERT query into a table with an AUTO INCREMENT field.
string Mysql.mysql()->error()
Returns a string describing the last error from the Mysql-server.
Returns 0 (zero) if there was no error.
void Mysql.mysql()->select_db(string database)
Select database.
The Mysql-server can hold several databases. You select which one you want to access with this function.
Mysql.mysql.create, Mysql.mysql.create_db, Mysql.mysql.drop_db
object(Mysql.mysql_result) Mysql.mysql()->big_query(string query)
Make an SQL query.
This function sends the SQL query query to the Mysql-server. The result of the query is returned as a Mysql.mysql_result object.
Returns 0 (zero) if the query didn't return any result (e.g. INSERT or similar).
Mysql.mysql_result
void Mysql.mysql()->create_db(string database)
Create a new database
This function creates a new database named database in the Mysql-server.
Mysql.mysql.select_db, Mysql.mysql.drop_db
void Mysql.mysql()->drop_db(string database)
Drop a database
This function drops the database named database from the Mysql-server.
Mysql.mysql.create_db, Mysql.mysql.select_db
void Mysql.mysql()->shutdown()
Shutdown the Mysql-server
This function shuts down a running Mysql-server.
Mysql.mysql.reload
void Mysql.mysql()->reload()
Reload security tables
This function causes the Mysql-server to reload its access tables.
Mysql.mysql.shutdown
string Mysql.mysql()->statistics()
Some Mysql-server statistics
This function returns some server statistics.
Mysql.mysql.server_info, Mysql.mysql.host_info, Mysql.mysql.protocol_info
string Mysql.mysql()->server_info()
Get the version number of the Mysql-server.
Mysql.mysql.statistics, Mysql.mysql.host_info, Mysql.mysql.protocol_info
string Mysql.mysql()->host_info()
Get information about the Mysql-server connection
Mysql.mysql.statistics, Mysql.mysql.server_info, Mysql.mysql.protocol_info
int Mysql.mysql()->protocol_info()
Give the Mysql protocol version
This function returns the version number of the protocol the Mysql-server uses.
Mysql.mysql.statistics, Mysql.mysql.server_info, Mysql.mysql.host_info
object(Mysql.mysql_result) Mysql.mysql()->list_dbs()
object(Mysql.mysql_result) Mysql.mysql()->list_dbs(string wild)
List databases
Returns a table containing the names of all databases in the Mysql-server. If the argument wild is specified, only those matching it will be returned.
Mysql.mysql.list_tables, Mysql.mysql.list_fields, Mysql.mysql.list_processes, Mysql.mysql_result
object(Mysql.mysql_result) Mysql.mysql()->list_tables()
object(Mysql.mysql_result) Mysql.mysql()->list_tables(string wild)
List tables in the current database
Returns a table containing the names of all tables in the current database. If the argument wild is given, only those matching it will be returned.
Mysql.mysql.list_dbs, Mysql.mysql.list_fields, Mysql.mysql.list_processes, Mysql.mysql_result
array(int|mapping(string:mixed)) Mysql.mysql()->list_fields(string table)
array(int|mapping(string:mixed)) Mysql.mysql()->list_fields(string table, string wild)
List all fields.
Returns an array of mappings with information about the fields in the table named table. If the argument wild is given, only those fields matching it will be returned
The mappings contain the following entries:
|
The type of the field can be any of: "decimal", "char", "short", "long", "float", "double", "null", "time", "longlong", "int24", "tiny blob", "medium blob", "long blob", "var string", "string" or "unknown".
The flags multiset can contain any of:
|
Michael Widenius recomends use of the following query instead: show fields in 'table' like "wild".
Mysql.mysql.list_dbs, Mysql.mysql.list_tables, Mysql.mysql.list_processes, Mysql.mysql_result.fetch_field
object(Mysql.mysql_result) Mysql.mysql()->list_processes()
List all processes in the Mysql-server
Returns a table containing the names of all processes in the Mysql-server.
Mysql.mysql.list_dbs, Mysql.mysql.list_tables, Mysql.mysql.list_fields, Mysql.mysql_result
int Mysql.mysql()->binary_data()
Inform if this version of Mysql.mysql supports binary data
This function returns non-zero if binary data can be reliably stored and retreived with this version of the mysql-module.
Usually, there is no problem storing binary data in mysql-tables, but data containing '\0' (NUL) couldn't be fetched with old versions (prior to 3.20.5) of the mysql-library.
| Module Postgres |
| CLASS Postgres.postgres |
This is an interface to the Postgres (Postgres95, pgsql) database server. This module may or may not be availible on your Pike, depending whether the appropriate include and library files could be found at compile-time. Note that you do not need to have a Postgres server running on your host to use this module: you can connect to the database over a TCP/IP socket.
Please notice that unless you wish to specifically connect to a Postgres server, you'd better use the Sql.Sql, which is a server-independent sql-server-class. The interfaces to all existing sql-classes are consistent. Using Sql.Sql ensures that your Pike applications will run with any supported SQL server without changing a single line of code, at least for most common (and simple) operations.
The program Postgres.postgres provides the raw interface to the database. Many functions are not availible for this program. Therefore, its use is DEPRECATED. It is included in this documentation only for completeness' sake. Use Sql.postgres instead, or even better Sql.Sql
There is no testsuite for this module, since to test anything would require a working Postgres server. You can try to use the included scripts in the "pike/src/modules/Postgres/extras" directory but you'll probably have to patch them to reflect your site's settings.
Also note that this module uses blocking I/O I/O to connect to the server. Postgres is quite slow, and so you might want to consider this particular aspect. It is (at least should be) thread-safe, and so it can be used in a multithread environment.
The behavior of the Postgres C API also depends on certain environment variables defined in the environment of the pike interpreter.
|
Refer to the Postgres documentation for further details.
Sql.Sql, Sql.postgres, Sql.postgres_result
string Postgres.postgres()->version
Should you need to report a bug to the author, please submit along with the report the driver version number, as returned by this call.
void Postgres.postgres()->create()
void Postgres.postgres()->create(string host, void|string database, void|int port)
With no arguments, this function initializes (reinitializes if a connection had been previously set up) a connection to the Postgres backend. Since Postgres requires a database to be selected, it will try to connect to the default database. The connection may fail however for a variety of reasons, in this case the most likely of all is because you don't have enough authority to connect to that database. So use of this particular syntax is discouraged.
The host argument allows you to connect to databases residing on different hosts. If it is 0 or "", it will try to connect to localhost.
The database argument specifies the database to connect to. If 0 or "", it will try to connect to the default database.
You need to have a database selected before using the sql-object, otherwise you'll get exceptions when you try to query it. Also notice that this function can raise exceptions if the db server doesn't respond, if the database doesn't exist or is not accessible by you.
You don't need bothering about syncronizing the connection to the database: it is automatically closed (and the database is sync-ed) when the object is destroyed.
Sql.postgres, Sql.Sql, select_db
void Postgres.postgres()->select_db(string dbname)
This function allows you to connect to a database. Due to restrictions of the Postgres frontend-backend protocol, you always have to be connected to a database, so in fact this function just allows you to connect to a different database on the same server.
This function can raise exceptions if something goes wrong (backend process not running, not enough permissions..)
create
object(Sql.postgres_result) Postgres.postgres()->big_query(string sqlquery)
This is the only provided interface which allows you to query the database. If you wish to use the simpler "query" function, you need to use the Sql.sql generic sql-object.
It returns a postgres_result object (which conforms to the Sql.sql_result standard interface for accessing data). I recommend using query() for simpler queries (because it is easier to handle, but stores all the result in memory), and big_query for queries you expect to return huge amounts of data (it's harder to handle, but fectches results on demand).
This function can raise exceptions.
The program Sql.postgres_result is exactly the same as Postgres.postgres_result.
Sql.Sql, Sql.sql_result
string Postgres.postgres()->error()
This function returns the textual description of the last server-related error. Returns 0 if no error has occurred yet. It is not cleared upon reading (can be invoked multiple times, will return the same result until a new error occurs).
big_query
void Postgres.postgres()->reset()
This function resets the connection to the backend. Can be used for a variety of reasons, for example to detect the status of a connection.
This function is Postgres-specific, and thus it is not availible through the generic SQL-interface.
void Postgres.postgres()->_set_notify_callback()
void Postgres.postgres()->_set_notify_callback(function f)
With Postgres you can associate events and notifications to tables. This function allows you to detect and handle such events.
With no arguments, resets and removes any callback you might have put previously, and any polling cycle.
With one argument, sets the notification callback (there can be only one for each sqlobject).
The callback function must return no value, and takes a string argument, which will be the name of the table on which the notification event has occured. In future versions, support for user-specified arguments will be added.
The Sql.postgres program adds support for automatic delivery of messages (see it for explanation on the inner workings of this feature).
This function is Postgres-specific, and thus it is not availible through the generic SQL-interface
Sql.postgres
string Postgres.postgres()->host_info()
This function returns a string describing what host are we talking to, and how (TCP/IP or UNIX sockets).
| CLASS Postgres.postgres_result |
Contains the result of a Postgres-query.
Sql.postgres, Postgres.postgres, Sql.Sql, Sql.sql_result
void Postgres.postgres_result()->create(object o)
You can't create istances of this object yourself. The only way to create it is via a big_query to a Postgres database.
int Postgres.postgres_result()->num_rows()
Returns the number of rows in the result.
int Postgres.postgres_result()->num_fields()
Returns the number of fields in the result.
array(mapping(string:mixed)) Postgres.postgres_result()->fetch_fields()
Returns an array with an entry for each field, each entry is a mapping with the following fields:
|
For char() fields, length is to be intended as the MAXIMUM length of the field. This is not part of the interface specifications in fact, but a driver-choice. In fact char() fields are for Postgres _FIXED_ length fields, and are space-padded. If CUT_TRAILING_SPACES is defined when the driver is compiled (default behavior) it will cut such spaces.
void Postgres.postgres_result()->seek()
Moves the result cursor (ahead or backwards) the specified number of rows. Notice that when you fetch a row, the cursor is automatically moved forward one slot.
array(string) Postgres.postgres_result()->fetch_row()
Returns an array with the contents of the next row in the result. Advances the row cursor to the next row. Returns 0 at end of table.
Since there's no generic way to know whether a type is numeric or not in Postgres, all results are returned as strings. You can typecast them in Pike to get the numeric value.
seek
| Module Mird |
| CLASS Mird.Mird |
The Mird module doubles as the Mird database class.
void Mird.Mird()->create(string filename, void|mapping options)
Opens the database using the given filename. Note that the file filename+".journal" will be created too.
Possible options:
|
"flags" is a string with any of these characters:
|
object(Table) Mird.Mird()->table(string name)
Gives back the table object for the specified table in the database.
object(vTable) Mird.Mird()->vtable(string name)
object(vzTable) Mird.Mird()->vtable(string name)
A vTable is just like a Mird.Mird.Table, except that the values can be anything, not just strings. The data is encode_value'ed before storage.
A vzTable is just like a Mird.Mird.vTable, except that the values are both encoded and then packed using Gz.
string Mird.Mird()->table_name(int id)
Returns the name of the table with id id.
object(Table) Mird.Mird()->new_stringkey_table(string name)
object(Table) Mird.Mird()->new_hashkey_table(string name)
Create a new table in the database.
A transaction will be created when calling these on the Database level.
object(Transaction) Mird.Mird()->transaction()
Creates a new transaction
array(string) Mird.Mird()->tables()
Gives a list of all tables in the database.
object Mird.Mird()->sync()
object Mird.Mird()->sync_please()
Syncs the database (syncs against disc and starts to reuse any space that is freed), sync_please() does this when the last living transaction is finished.
object Mird.Mird()->sync_loop(int seconds)
Starts a call_out loop that will call sync_please once every given interval. 0 or fewer seconds will shut down the loop. Default is 1 minute (60 seconds).
void Mird.Mird()->close()
void Mird.Mird()->destroy()
Syncs and closes the database
| CLASS Mird.Mird.Table |
A Mird table has approximately the same characteristics as a Pike mapping. You store key:value pairs in it, you get values from keys, you delete keys.
For convinience, the Table object overloads `[](), `[]=(), _indices() and _values().
void Mird.Mird.Table()->create(object parent, int table_id)
object(Mird.Glue.Scanner) Mird.Mird.Table()->scanner()
object(Mird.Glue.Scanner) Mird.Mird.Table()->scanner(int key)
Creates a scanner over the called table; if key is given, continue at that key (as returned from Mird.Mird.Glue.Scanner.next_key).
A scanner can be used to loop over all elements in a table.
string Mird.Mird.Table()->`[](int|string key)
string Mird.Mird.Table()->`[]=(int|string key, string value)
array(int|string) Mird.Mird.Table()->_indices()
array(int|string) Mird.Mird.Table()->_values()
Get and store key:value pairs, list the table.
All storage in the table will create a transaction, if we're not already in a transaction.
mapping Mird.Mird.Table()->cast("mapping")
array Mird.Mird.Table()->cast("array")
It is possible to cast the Table to a mapping or array; this will read all contents of the table, so beware if you have a big table.
| CLASS Mird.Mird.vTable |
A Mird.Mird.vTable is just like a Mird.Mird.Table, except that the values can be anything, not just strings. The data is encode_value'ed before storage.
Missing content (
inherits Table
| CLASS Mird.Mird.vzTable |
This is just like a normal table, but all data is encode_value()ed, and gzipped, and unpacked if reading, for convinience.
Missing content (
inherits Table
| CLASS Mird.Mird.Transaction |
A Transaction is the environment which in a change to the database can be made. Simplified, the database is not changed until the transaction is closed. More then one change to the database can be performed within one transaction.
If the same entries in the database were changed during the lifespan of the transaction, it cannot be closed but has to be cancelled.
object(Table) Mird.Mird.Transaction()->table(string name)
object(vTable) Mird.Mird.Transaction()->vtable(string name)
object(vzTable) Mird.Mird.Transaction()->vztable(string name)
Creates a Mird.Mird.Table, Mird.Mird.vTable or Mird.Mird.vzTable object for the specified table, which emulates a mapping and in which you can make changes to the database or do lookups.
object(Table) Mird.Mird.Transaction()->new_stringkey_table(string name)
object(Table) Mird.Mird.Transaction()->new_hashkey_table(string name)
Creates a new table in the database. A stringkey table is a mapping from string to string, and a hashkey table is mapping from int to string.
void Mird.Mird.Transaction()->delete_table(string name)
Removes a table and all items in it
array(string) Mird.Mird.Transaction()->tables()
Returns the names of the tables in the database.