1<?php
2/**
3 * @copyright Copyright (c) 2016, ownCloud, Inc.
4 *
5 * @author Bart Visscher <bartv@thisnet.nl>
6 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
7 * @author Daniel Kesselberg <mail@danielkesselberg.de>
8 *
9 * @license AGPL-3.0
10 *
11 * This code is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License, version 3,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License, version 3,
21 * along with this program. If not, see <http://www.gnu.org/licenses/>
22 *
23 */
24namespace OC\Setup;
25
26use OC\DB\ConnectionFactory;
27
28class Sqlite extends AbstractDatabase {
29	public $dbprettyname = 'Sqlite';
30
31	public function validate($config) {
32		return [];
33	}
34
35	public function initialize($config) {
36		/*
37		 * Web: When using web based installer its not possible to set dbname
38		 * or dbtableprefix. Defaults used from ConnectionFactory and dbtype = 'sqlite'
39		 * is written to config.php.
40		 *
41		 * Cli: When --database-name or --database-table-prefix empty or default
42		 * dbtype = 'sqlite' is written to config.php. If you choose a value different
43		 * from default these values are written to config.php. This is required because
44		 * in connection factory configuration is obtained from config.php.
45		 */
46
47		$this->dbName = empty($config['dbname'])
48			? ConnectionFactory::DEFAULT_DBNAME
49			: $config['dbname'];
50
51		$this->tablePrefix = empty($config['dbtableprefix'])
52			? ConnectionFactory::DEFAULT_DBTABLEPREFIX
53			: $config['dbtableprefix'];
54
55		if ($this->dbName !== ConnectionFactory::DEFAULT_DBNAME) {
56			$this->config->setValue('dbname', $this->dbName);
57		}
58
59		if ($this->tablePrefix !== ConnectionFactory::DEFAULT_DBTABLEPREFIX) {
60			$this->config->setValue('dbtableprefix', $this->tablePrefix);
61		}
62	}
63
64	public function setupDatabase($username) {
65		$datadir = $this->config->getValue(
66			'datadirectory',
67			\OC::$SERVERROOT . '/data'
68		);
69
70		$sqliteFile = $datadir . '/' . $this->dbName . 'db';
71		if (file_exists($sqliteFile)) {
72			unlink($sqliteFile);
73		}
74	}
75}
76