tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / platform / sql / SQLiteFileSystem.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #define __STDC_FORMAT_MACROS
32 #include "config.h"
33 #include "SQLiteFileSystem.h"
34
35 #include "FileSystem.h"
36 #include "SQLiteDatabase.h"
37 #include "SQLiteStatement.h"
38 #include <inttypes.h>
39 #include <sqlite3.h>
40
41 namespace WebCore {
42
43 SQLiteFileSystem::SQLiteFileSystem()
44 {
45 }
46
47 void SQLiteFileSystem::registerSQLiteVFS()
48 {
49 }
50
51 int SQLiteFileSystem::openDatabase(const String& fileName, sqlite3** database, bool)
52 {
53     // SQLite expects a null terminator on its UTF-16 strings.
54     String path = fileName;
55     return sqlite3_open16(path.charactersWithNullTermination(), database);
56 }
57
58 String SQLiteFileSystem::getFileNameForNewDatabase(const String& dbDir, const String&,
59                                                    const String&, SQLiteDatabase* db)
60 {
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)
65         return String();
66     int result = sequenceStatement.step();
67     int64_t seq = 0;
68     if (result == SQLResultRow)
69         seq = sequenceStatement.getColumnInt64(0);
70     else if (result != SQLResultDone)
71         return String();
72     sequenceStatement.finalize();
73
74     // increment the number until we can use it to form a file name that doesn't exist
75     String fileName;
76     do {
77         ++seq;
78         fileName = pathByAppendingComponent(dbDir, String::format("%016"PRIx64".db", seq));
79     } while (fileExists(fileName));
80
81     return String::format("%016"PRIx64".db", seq);
82 }
83
84 String SQLiteFileSystem::appendDatabaseFileNameToPath(const String& path, const String& fileName)
85 {
86     return pathByAppendingComponent(path, fileName);
87 }
88
89 bool SQLiteFileSystem::ensureDatabaseDirectoryExists(const String& path)
90 {
91     if (path.isEmpty())
92         return false;
93     return makeAllDirectories(path);
94 }
95
96 bool SQLiteFileSystem::ensureDatabaseFileExists(const String& fileName, bool checkPathOnly)
97 {
98     if (fileName.isEmpty())
99         return false;
100
101     if (checkPathOnly) {
102         String dir = directoryName(fileName);
103         return ensureDatabaseDirectoryExists(dir);
104     }
105
106     return fileExists(fileName);
107 }
108
109 bool SQLiteFileSystem::deleteEmptyDatabaseDirectory(const String& path)
110 {
111     return deleteEmptyDirectory(path);
112 }
113
114 bool SQLiteFileSystem::deleteDatabaseFile(const String& fileName)
115 {
116     return deleteFile(fileName);
117 }
118
119 long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName)
120 {        
121     long long size;
122     return getFileSize(fileName, size) ? size : 0;
123 }
124
125 } // namespace WebCore