How to configure SQLite in Kohana 3? - sql

I'm struggling to find any information on how to configure SQLite in Kohana 3.2. I mainly need to know:
What should I set hostname, database, username and password to (with default user and no password)?
Also, how can I set the path to the SQLite database file?
What should the "type" be? I tried "sqlite" but I get an error Class 'Database_Sqlite' not found.
This is my current configuration options:
'exportedDatabase' => array
(
'type' => 'sqlite',
'connection' => array(
/**
* The following options are available for MySQL:
*
* string hostname server hostname, or socket
* string database database name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => $hostname,
'database' => $database,
'username' => $username,
'password' => $password,
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),

You can use PDO through Database module. The proper way of configuring it looks like this:
'exportedDatabase' => array(
'type' => 'pdo',
'connection' => array(
'dsn' => 'sqlite:/path/to/file.sqlite',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => NULL, /* IMPORTANT- charset 'utf8' breaks sqlite(?) */
'caching' => FALSE,
'profiling' => TRUE,
),
One disadvantage of using PDO in Kohana is that in ORM you have to specify all fields by hand in your model (you should do it anyway for performance reasons) because of how different database systems handle listing of table fields.
There is also real database module created by banditron. You have to remember that's it is NOT a drop-in replacement for Database module and therefore Kohana's ORM will not work with it. Other than that it's pretty neat and has wide support for database systems other than SQLite.

As I found out, Kohana 3.x doesn't actually support SQLite. There's an unsupported module for it and, as far as I can tell, it's not working.
It's easy enough to use PDO though and the syntax is pretty much the same as Kohana's ORM:
$db = new PDO('sqlite:' . $dbFilePath);
$query = $db->prepare('CREATE TABLE example (id INTEGER PRIMARY KEY, something TEXT)');
$query->execute();
$query = $db->prepare("INSERT INTO example (id, something) VALUES (:id, :something)");
$query->bindParam(':id', $id);
$query->bindParam(':something', $something);
$query->execute();

I don't use Kohana, but this should work:
'hostname' => /path/to/your/sql/lite/file.sqlite
'database' => ''
'username' => ''
'password' => ''

Related

How to change database

I am currently on a vps from ovh with prestashop 1.7.5. I ordered a CloudDB database.
I would like to know the handling to follow to be able to change the database because I do not find a solution to my problem.
thank you so much
PS: I'm a beginner with PrestaShop.
Using your FTP client or file manager open this file:
/app/config/parameters.php
Find the lines with database_host, database_name, database_user and
database_password parameters.
Replace existing values of that params with the new ones.
Save the file and upload it back to the server.
All done! Open your site in the browser and check whether it works.
<?php return array (
'parameters' =>
array (
'database_host' => '127.0.0.1',
'database_port' => '',
'database_name' => 'your_database_name',
'database_user' => 'your_database_user',
'database_password' => 'your_database_password',
'database_prefix' => 'ps_',
'database_engine' => 'InnoDB',
'mailer_transport' => 'smtp',
'mailer_host' => '127.0.0.1',
'mailer_user' => NULL,
'mailer_password' => NULL,
'secret' => 'Vz6rVXVYNWYt7E6Fvfvdfvg34MCdc97h3IttReewVIxNOu7wHAoS',
'ps_caching' => 'CacheMemcache',
'ps_cache_enable' => false,
'ps_creation_date' => '2017-05-22',
'locale' => 'en-US',
'cookie_key' => 'xduROM3yvjDSym43CvfdvFDVD47YG8zCrVruNFjjZ3NUb7Ut9',
'cookie_iv' => '7kBvdfvgh',
'new_cookie_key' => 'def00000766ffa61aae49b279evfsdvsdfvsfdvfdvwerr324r3frdfveb1afedc8702a964f0a1f6828bf1623ca54efad10ed4bbfb289beec62b01a3062d4c3ce78254157',
),
);

INSERT data from database into another database, running on different hosts

I'm facing the following problem:
I have two MariaDB databases, running on two different hosts. Both of them are used to run two different websites, each of them having Drupal and CiviCRM installed and running.
Some of the data stored in the contacts table of CiviCRM from website 1 needs to be kept in sync with these same contacts on website 2.
Keeping in sync means : inserting new contacts, and updating existing contacts.
I was wondering if this coud be done via trigger?
I know I can activate remote sql on my cPanel, as I use this to work with Mysql Workbench or similar software.
Any ideas? Would a trigger work? Do I rather need to write some code in another language than SQL?
You can add multiple databases at the same time for your Drupal to connect to in your settings.php:
$databases = [
'HOST1.DATABASE' => [
'default' => [
'driver' => 'mysql',
'username' => '',
'password' => '',
'host' => '127.0.0.1',
'port' => '3306',
'prefix' => '',
'database' => 'contacts',
'collation' => 'utf8mb4_general_ci',
],
],
'HOST2.DATABASE' => [
'default' => [
'driver' => 'mysql',
'username' => '',
'password' => '',
'host' => '127.0.0.1',
'port' => '3306',
'prefix' => '',
'database' => 'contacts_audit',
'collation' => 'utf8mb4_general_ci',
],
],
];
After this you can define in the getConnection() method, which key of the $database array you want to connect.
\Drupal\Core\Database\Database::getConnection('HOST1.DATABASE')
->query('CREATE TRIGGER contacts_after_update AFTER UPDATE ON contacts FOR EACH ROW BEGIN')
->execute();
and
\Drupal\Core\Database\Database::getConnection('HOST2.DATABASE')
->query('INSERT INTO contacts_audit ( contact_id, updated_date, updated_by) VALUES ( NEW.contact_id, SYSDATE(), ); END;')
->execute();
(If you leave the parameter of getConnection() empty, it would connect to the database on $databases['default'] key. Also, you can use setActiveConnection() if you want to work more with the database, which as its name says, sets the active connection to the desired key of $databases)
Hope this helps some way.

Laravel SQL Server connection with ENCRYPT=yes trustServerCertificate=true

I got a ubuntu docker container which runs php 5.5.9, laraverl 5.2 which can connect successfully to SQL Server and get results back.
The docker image I am using is https://hub.docker.com/r/h2labs/laravel-mssql/
The problem I got is that the server uses encryption and I cant find how to pass the following parameters to the laravel connection string for mssql
ENCRYPT=yes;trustServerCertificate=true
My SQL Server connection string at present looks like this
DB_CONNECTION=sqlsrv
DB_HOST=sql.mydomain.com
DB_PORT=1433
DB_DATABASE=mydbname
DB_USERNAME=mysusername
DB_PASSWORD=mypass
My laravel database config looks like this
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
The SQL Server error log entry is
Encryption is required to connect to this server but the client library does not support encryption; the connection has been closed. Please upgrade your client library. [CLIENT: 103.31.114.56]
Support for either option was not introduced until Laravel 5.4; Specifically, v5.4.11
So you would first need to upgrade to laravel/framework:>=5.4.11,<5.5
Then, to configure your application, you will need to modify your config/database.php file as follows:
// ...
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'encrypt' => 'yes', // alternatively, defer to an env variable
'trust_server_certificate' => 'true', // alternatively, defer to an env variable
],
// ...
DatabaseServiceProvider, via ConnectionFactory and SqlServerConnector will use this to build the underlying PDO connection with those options set in the DSN.

Don't use prepared statements in Laravel Eloquent ORM?

Can I have Eloquent ORM run a query without using prepared statements? Or do I have to use whereRaw()?
I need to use a raw query because I'm trying to interact with InfiniDB, which lacks support for prepared statements from PHP. At any rate, all queries will be using internally generated data, not user input so it should not be a security issue.
For anything other than SELECT you can use unprepared()
DB::unprepared($sql);
For an unprepared SELECT you can use plain PDO query() by getting access to active PDO connection through getPdo()
$pdo = DB::getPdo();
$query = $pdo->query($sql);
$result = $query->fetchAll();
There's an easy way to do it. In the file config/database.php you can specify options for php PDO like so:
'mysql_unprepared' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PROXY_PORT', '6033'),
'username' => env('DB_CACHED_USERNAME', 'forge'),
'password' => env('DB_CACHED_PASSWORD', ''),
'database' => env('DB_DATABASE', 'forge'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? [
PDO::ATTR_EMULATE_PREPARES => true,
] : [],
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],
As you can see, there is an option PDO::ATTR_EMULATE_PREPARES which, when set to true, will do a prepare-like action on application level and send the query unprepared instead. I didn't figure PDO had this option until I had already created an extension for Laravel's mysql driver just to intercept select queries and do unprepared mysqli queries instead so that ProxySql could cache them.
So this answer could have been a lot more complicated. Cheers.

How to use Amazon RDS with Kohana's ORM

Is there any way to use Kohana's ORM with Amazon RDS?
I found the Amazon PHP SDK but I'm not sure how to plug it into Kohana so that the ORM uses it. I also couldn't find any Kohana module for Amazon RDS. Any suggestion?
Yes, this is absolutely possible. I have this exact configuration for my website.
In your AWS management console, you will need to get the "endpoint" of your RDS server. The name is quite long and begins with the name of your DB instance. (See the code below for example)
Next, open your database configuration file: application/config/database.php
In the 'default' configuration, change your hostname to the endpoint. Also change the database, username and password to whatever yours is set up with:
'default' => array
(
'type' => 'mysql',
'connection' => array(
/**
* The following options are available for MySQL:
*
* string hostname server hostname, or socket
* string database database name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => 'your-db-instance.njgo7sn43.us-east-1.rds.amazonaws.com',
'database' => 'db_name',
'username' => 'username',
'password' => 'SuperCaliFrajilisticExpiAliDocious',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
Also, in your application/bootstrap.php file, make sure to UN-comment the database module:
Kohana::modules(array(
'database' => MODPATH.'database', // Database access
'orm' => MODPATH.'orm', // Object Relationship Mapping
));
The ORM module is optional but very nice to use.
Hope this helps!