Warning, /sdk/cutehmi/extensions/CuteHMI/SharedDatabase.1/README.md is written in an unsupported language. File is not indexed.

0001 # Shared Database
0002 
0003 ![Development status](doc/status-alpha.svg)
0004 
0005 This extension acts as shared database provider. The intent of this extension is to provide database connections, which live in
0006 dedicated database threads, but can be shared across different extensions. Below is a brief description of classes, which help to
0007 accomplish this.
0008 
0009 Class cutehmi::shareddatabase::Database is responsible for configuring and maintaining database connection that lives in dedicated
0010 thread. From QML (`import CuteHMI.SharedDatabase.1`) it is available as @ref CuteHMI::SharedDatabase::Database "Database" component.
0011 
0012 cutehmi::shareddatabase::DatabaseWorker is a convenient class that allows one to run SQL-specific code in the dedicated database
0013 thread.
0014 
0015 In order to make this extension work you need Qt SQL module and appropriate client library as explained in Qt documentation on
0016 [SQL Database Drivers](https://doc.qt.io/qt-5/sql-driver.html). For development purposes you can copy library files to `deploy/lib`
0017 subdirectory of [external](../../../external/) folder. This is handy especially on Windows.
0018 
0019 ## Console
0020 
0021 Extension provides cutehmi::shareddatabase::Console component, which can be used with
0022 [cutehmi.console.0](../../../tools/cutehmi.console.0/) tool.
0023 ```
0024 cutehmi.console.0 CuteHMI.SharedDatabase.1
0025 ```
0026 
0027 This component allows for some very basic database operations. You can check out whether connection works by setting properties on
0028 `db` object. For example below is a typical default PostgreSQL configuration.
0029 ```
0030 # db.type = "QPSQL"
0031 # db.name = "postgres"
0032 # db.host = "localhost"
0033 # db.port = 5432
0034 # db.user = "postgres"
0035 # db.password = "postgres"
0036 ```
0037 
0038 Connection can be tested by starting the service.
0039 ```
0040 # service.start()
0041 CuteHMI.2: [NOTIFICATION] Service 'Database Service' has started.
0042 ```
0043 
0044 In case of error different notification is shown.
0045 ```
0046 CuteHMI.2: [NOTIFICATION] Service 'Database Service' broke.
0047 ```
0048 
0049 Once connection is established one can create PostgreSQL database.
0050 ```
0051 # postgres.createDatabase("some_database")
0052 CuteHMI.2: [NOTIFICATION] Created 'some_database' database.
0053 ```
0054 
0055 One can use JavaScript object to specify parameters after `WITH` keyword.
0056 ```
0057 # postgres.createDatabase("some_database", {"OWNER":"postgres", "TEMPLATE":"default"})
0058 ```
0059 
0060 Database can be dropped in a similar way.
0061 ```
0062 # postgres.dropDatabase("some_database")
0063 CuteHMI.2: [NOTIFICATION] Dropped 'some_database' database.
0064 ```