2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #define __STDC_FORMAT_MACROS
33 #include "SQLiteFileSystem.h"
35 #include "FileSystem.h"
36 #include "SQLiteDatabase.h"
37 #include "SQLiteStatement.h"
43 SQLiteFileSystem::SQLiteFileSystem()
47 void SQLiteFileSystem::registerSQLiteVFS()
51 int SQLiteFileSystem::openDatabase(const String& fileName, sqlite3** database, bool)
53 // SQLite expects a null terminator on its UTF-16 strings.
54 String path = fileName;
55 return sqlite3_open16(path.charactersWithNullTermination(), database);
58 String SQLiteFileSystem::getFileNameForNewDatabase(const String& dbDir, const String&,
59 const String&, SQLiteDatabase* db)
61 // try to get the next sequence number from the given database
62 // if we can't get a number, return an empty string
63 SQLiteStatement sequenceStatement(*db, "SELECT seq FROM sqlite_sequence WHERE name='Databases';");
64 if (sequenceStatement.prepare() != SQLResultOk)
66 int result = sequenceStatement.step();
68 if (result == SQLResultRow)
69 seq = sequenceStatement.getColumnInt64(0);
70 else if (result != SQLResultDone)
72 sequenceStatement.finalize();
74 // increment the number until we can use it to form a file name that doesn't exist
78 fileName = pathByAppendingComponent(dbDir, String::format("%016"PRIx64".db", seq));
79 } while (fileExists(fileName));
81 return String::format("%016"PRIx64".db", seq);
84 String SQLiteFileSystem::appendDatabaseFileNameToPath(const String& path, const String& fileName)
86 return pathByAppendingComponent(path, fileName);
89 bool SQLiteFileSystem::ensureDatabaseDirectoryExists(const String& path)
93 return makeAllDirectories(path);
96 bool SQLiteFileSystem::ensureDatabaseFileExists(const String& fileName, bool checkPathOnly)
98 if (fileName.isEmpty())
102 String dir = directoryName(fileName);
103 return ensureDatabaseDirectoryExists(dir);
106 return fileExists(fileName);
109 bool SQLiteFileSystem::deleteEmptyDatabaseDirectory(const String& path)
111 return deleteEmptyDirectory(path);
114 bool SQLiteFileSystem::deleteDatabaseFile(const String& fileName)
116 return deleteFile(fileName);
119 long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName)
122 return getFileSize(fileName, size) ? size : 0;
125 } // namespace WebCore