2013-02-25 07:19:49 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 10:37:37 +00:00
|
|
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
2013-02-25 07:19:49 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
|
|
|
|
2013-02-25 07:19:49 +00:00
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
class AdapterSqlite extends Adapter {
|
2013-02-25 21:49:55 +00:00
|
|
|
public function fixupStatement($statement) {
|
2014-09-17 11:47:55 +00:00
|
|
|
$statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement);
|
2013-02-25 21:49:55 +00:00
|
|
|
$statement = str_replace( '`', '"', $statement );
|
|
|
|
$statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
|
|
|
|
$statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
|
|
|
|
return $statement;
|
|
|
|
}
|
2013-02-26 07:30:42 +00:00
|
|
|
|
2015-03-09 21:12:31 +00:00
|
|
|
/**
|
|
|
|
* @param string $table
|
|
|
|
* @param array $input
|
|
|
|
* @param null $compare
|
|
|
|
* @return int
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
*/
|
2015-03-09 16:25:02 +00:00
|
|
|
public function insertIfNotExist($table, $input, $compare = null) {
|
|
|
|
if ($compare === null) {
|
|
|
|
$compare = array_keys($input);
|
|
|
|
}
|
2015-03-06 13:50:51 +00:00
|
|
|
$fieldList = '`' . implode('`,`', array_keys($input)) . '`';
|
|
|
|
$query = "INSERT INTO `$table` ($fieldList) SELECT "
|
|
|
|
. str_repeat('?,', count($input)-1).'? '
|
|
|
|
. " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE ";
|
|
|
|
|
|
|
|
$inserts = array_values($input);
|
2015-03-09 16:25:02 +00:00
|
|
|
foreach($compare as $key) {
|
2014-07-02 13:27:27 +00:00
|
|
|
$query .= '`' . $key . '`';
|
2015-03-09 16:25:02 +00:00
|
|
|
if (is_null($input[$key])) {
|
2014-07-02 13:27:27 +00:00
|
|
|
$query .= ' IS NULL AND ';
|
|
|
|
} else {
|
2015-03-09 16:25:02 +00:00
|
|
|
$inserts[] = $input[$key];
|
2014-07-02 13:27:27 +00:00
|
|
|
$query .= ' = ? AND ';
|
|
|
|
}
|
2013-02-26 07:30:42 +00:00
|
|
|
}
|
|
|
|
$query = substr($query, 0, strlen($query) - 5);
|
2015-03-06 13:50:51 +00:00
|
|
|
$query .= ')';
|
2014-07-02 13:27:27 +00:00
|
|
|
|
2015-03-09 21:12:31 +00:00
|
|
|
return $this->conn->executeUpdate($query, $inserts);
|
2013-02-26 07:30:42 +00:00
|
|
|
}
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|