Download the PHP package tbolner/monetdb-php without Composer
On this page you can find all versions of the php package tbolner/monetdb-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package monetdb-php
MonetDB-PHP
The official PHP client library for accessing MonetDB. For PHP 8.x and 7.2 or above (see instructions below).
Main features:
- Parameterized queries, using cached prepared statements.
- Extensively tested with Japanese characters for the UTF-8 compliance.
- Multiple, concurrent connections.
- Allows access to response stats, like execution time and affected row count, etc.
- The thrown
MonetException
exception objects contain user-friendly error messages. - Provides information about the columns of the response data, like name, SQL type and length.
If you wish to implement your own client library either for PHP or for another language, then please read the guide about the client-server protocol.
Table of contents
- MonetDB-PHP
- Table of contents
- Installation with Composer (PHP 8.x)
- Usage without installation
- Installation for PHP 7.2 or above.
- Examples
- Example 1: Simple query
- Example 2: Get execution stats
- Example 3: Parameterized query with prepared statement
- Example 4: Using escaping
- Example 5: Renaming fields and using column info
- Example 6: Query the first record only
- Example 7: Transactions
- Example 8: Importing data the fastest way
- Example 9: Using multiple connections
- API Reference
- Connection Class
- Response Class
- StatusRecord Class
- ColumnInfo Class
- Development setup through the Docker image
- Running the integration tests
- IDE setup
Installation with Composer (PHP 8.x)
This library is available on Packagist at:
First install Composer, then execute the following in your project's directory:
Usage without installation
You don't need to use Composer in your project. You can just copy all files in the 'src' folder, and include them in your project through the include.php file, which was created just for this purpose.
Then either reference the classes by a combination of a use
statement and the short class name
(as it is done in the example projects):
Or just use the fully qualified class name (if your project doesn't use namespaces):
Please make sure that the php-mbstring
(multi-byte string) extension is installed and enabled,
and the character encoding for your project is set to UTF-8: (This is required for preventing SQL injection attacks)
Installation for PHP 7.2 or above.
Only the 1.1.x versions support PHP 7.2
or above.
Examples
Example projects:
- Data modification
- Web query
- Japanese test
Example 1: Simple query
The returned values are always in string representation except the null, which
is always returned as null
.
Example 2: Get execution stats
Example 3: Parameterized query with prepared statement
In MonetDB the placeholders of prepared statements have specific types. This library auto-converts some of the PHP types to the corresponding MonetDB types.
MonetDB type | Accepted PHP types | Value examples |
---|---|---|
timestamp | string , DateTime |
"2020-12-20 11:14:26.123456" |
date | string , DateTime |
"2020-12-20" |
boolean | boolean , string , integer |
true , false , "true" , 0 , "0" , 1 , "t" , "f" , "yes" , "no" , "enabled" , "disabled" |
Numeric values | integer , float , string |
12.34 , "12.34" (use string for huge numbers) |
Character types | string |
"Hello World!" |
Binary | string |
"0f44ba12" (always interpreted as hexadecimal) |
time | string , DateTime |
"11:28" , "12:28:34" |
Always pass the null values as null
, and not as a string.
Example 4: Using escaping
Example 5: Renaming fields and using column info
Example 6: Query the first record only
Example 7: Transactions
Or:
Example 8: Importing data the fastest way
Example 9: Using multiple connections
API Reference
Class | Summary |
---|---|
Connection | Class for encapsulating a connection to a MonetDB server. |
Response | This class represents a response for an SQL query or for a command. In case of a 'select' query, this class can be iterated through, using a 'foreach' loop. The records are returned as associative arrays, indexed by the column names. |
StatusRecord | This class shares the information returned by MonetDB about the executed queries. Like execution time, number of rows affected, etc. Note that only specific fields are populated for specific queries, the others remain NULL. |
ColumnInfo | This class contains information about the columns of a table response to a 'select' query. |
Connection Class
Class for encapsulating a connection to a MonetDB server.
Method | Documentation |
---|---|
__construct | Create a new connection to a MonetDB database. br>@paramint $maxReplySize = 200 : The maximal number of tuples returned in a response. A higher value results in smaller number of memory allocations and string operations, but also in higher memory footprint. |
Close | Close the connection |
Query | Execute an SQL query and return its response. For 'select' queries the response can be iterated using a 'foreach' statement. You can pass an array as second parameter to execute the query as prepared statement, where the array contains the parameter values. SECURITY WARNING: For prepared statements in MonetDB, the parameter values are passed in a regular 'EXECUTE' command, using escaping. Therefore the same security considerations apply here as for using the Connection->Escape(...) method. Please read the comments for that method. br>@returnResponse |
QueryFirst | Execute an SQL query and return only the first row as an associative array. If there is more data on the stream, then discard all. Returns null if the query has empty result. You can pass an array as second parameter to execute the query as prepared statement, where the array contains the parameter values. br>@returnstring[] -or- null |
Command | Send a 'command' to MonetDB. Commands are used for configuring the database, for example setting the maximal response size, or for requesting unread parts of a query response ('export').br>@returnResponse -or- null |
Escape | Escape a string value, to be inserted into a query, inside single quotes. The following characters are escaped by this method: backslash, single quote, carriage return, line feed, tabulator, null character, CTRL+Z. As a security measure this library forces the use of multi-byte support and UTF-8 encoding, which is also used by MonetDB, avoiding the SQL-injection attacks, which play with differences between character encodings. strong>$value @returnstring |
ClearPsCache | Clears the in-memory cache of prepared statements. This is called automatically when an error is received from MonetDB, because that also purges the prepared statements and all session state in this case. |
GetMaxReplySize | The maximal number of tuples returned in a response.br> @returnint |
Response Class
This class represents a response for an SQL query or for a command. In case of a 'select' query, this class can be iterated through, using a 'foreach' loop. The records are returned as associative arrays, indexed by the column names.
Method | Documentation |
---|---|
Discard | Read through all of the data and discard it. Use this method when you don't want to iterate through a long query, but you would like to start a new one instead. |
IsDiscarded | Returns true if this response is no longer connected to an input TCP stream.br> @returnboolean |
GetColumnNames | Returns the names of columns for the table. If you would like to have more information about the columns than just their names, then use the 'GetColumnInfo()' method.br> @returnstring[] |
Fetch | Returns the next row as an associative array, or null if the query ended.br> @returnarray -or- null |
GetStatusRecords | Returns one or more Status records that tell information about the queries executed through a single request.br> @returnStatusRecord[] |
GetColumnInfo | Returns an array of ColumnInfo objects that contain information about the columns of a table response to a 'select' query.br> @returnColumnInfo[] |
StatusRecord Class
This class shares the information returned by MonetDB about the executed queries. Like execution time, number of rows affected, etc. Note that only specific fields are populated for specific queries, the others remain NULL.
Method | Documentation |
---|---|
GetQueryType | Returns a short string which identifies the type of the query.br> @returnstring |
GetDescription | Returns a user-friendly text which describes the effect of the query.br> @returnstring |
GetQueryTime | The time the server spent on executing the query. In milliseconds.br> @returnfloat -or- null |
GetSqlOptimizerTime | SQL optimizer time in milliseconds.br> @returnfloat -or- null |
GetMalOptimizerTime | MAL optimizer time in milliseconds.br> @returnfloat -or- null |
GetAffectedRows | The number of rows updated or inserted.br> @returninteger -or- null |
GetTotalRowCount | The total number of rows in the result set. This includes those rows too, which are not in the current response.br> @returninteger -or- null |
GetAsText | Get a description of the status response in a human-readable format.br> @returnstring |
GetPreparedStatementID | Get the ID of a created prepared statement. This ID can be used in an 'EXECUTE' statement, but only in the same session.br> @returninteger -or- null |
GetResultID | Returns the ID of the result set that is returned for a query. It is stored on the server for this session, and parts of it can be queried using the "export" command.br> @returninteger -or- null |
GetAutoCommitState | Available after "start transaction", "commit" or "rollback". Tells whether the current session is in auto-commit mode or not.br> @returnboolean -or- null |
GetRowCount | The number of rows (tuples) in the current response only.br> @returninteger -or- null |
GetColumnCount | Column count. If the response contains tabular data, then this tells the number of columns.br> @returninteger -or- null |
GetQueryID | Query ID. A global ID which is also used in functions such as sys.querylog_catalog().br> @returninteger -or- null |
GetLastInsertID | The last automatically generated ID by an insert statement. (Usually auto_increment) NULL if none.br> @returninteger -or- null |
GetExportOffset | The index (offset) of the first row in a block response. (For an "export" command.)br> @returninteger -or- null |
ColumnInfo Class
This class contains information about the columns of a table response to a 'select' query.
Method | Documentation |
---|---|
GetTableName | The name of the table the field belongs to, or the name of a temporary resource if the value is the result of an expression.br> @returnstring |
GetColumnName | Column name.br> @returnstring |
GetType | The SQL data type of the field.br> @returnstring |
GetLength | A length value that can be used for deciding the width of the columns when rendering the response.br> @returninteger |
Development setup through the Docker image
-
Build the Docker image:
docker/build.sh
-
Create the Docker container with Apache listening on port 9292:
docker/create.sh
-
Login into the container as the host user or as root:
docker/login.sh docker/root_login.sh
-
When you don't need the MonetDB-PHP container anymore, you can get rid of it easily: (this also removes the unused images)
docker/cleanup.sh
Running the integration tests
- Login into the running container with
docker/login.sh
. - Execute the tests with:
The output should be similar to:
IDE setup
- IDE: Visual Studio Code
- Plugins:
- Plugins for the Monet-Explorer project:
All versions of monetdb-php with dependencies
ext-mbstring Version *