Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(40)

Side by Side Diff: gears/base/common/database2_versions_table.cc

Issue 800: Database2Versions table, get/set version operations implemented (Closed) SVN Base: http://google-gears.googlecode.com/svn/contrib/dimitri.glazkov/database2/
Patch Set: Created 3 months, 1 week ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2008, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #include "gears/base/common/database2_versions_table.h"
27
28 bool Database2VersionsTable::GetVersion(const std::string16 &origin,
29 const std::string16 &name,
30 std::string16 *version,
31 bool *found) {
32 assert(db_);
33
34 const char16 *sql = STRING16(
35 L"SELECT Version FROM DatabaseVersions "
36 L"WHERE Origin = ? AND Name = ?");
37
38 SQLStatement statement;
39 if (SQLITE_OK != statement.prepare16(db_, sql)) {
40 LOG(("Database2VersionsTable::GetVersion unable to prepare: %d\n",
41 db_->GetErrorCode()));
42 return false;
43 }
44
45 if (SQLITE_OK != statement.bind_text16(0, origin.c_str())) {
46 LOG(("Database2VersionsTable::GetVersion unable to bind origin: %d\n",
47 db_->GetErrorCode()));
48 return false;
49 }
50
51 if (SQLITE_OK != statement.bind_text16(1, name.c_str())) {
52 LOG(("Database2VersionsTable::GetVersion unable to bind name: %d\n",
53 db_->GetErrorCode()));
54 return false;
55 }
56
57 int rc = statement.step();
58 if (SQLITE_DONE == rc) {
59 *found = false;
60 return true;
61 }
62
63 if (SQLITE_ROW != rc) {
64 LOG(("Database2VersionsTable::GetVersion unable to step: %d\n",
65 db_->GetErrorCode()));
66 return false;
67 }
68
69 *version = statement.column_text16_safe(0);
70 *found = true;
71 return true;
72 }
73
74 bool Database2VersionsTable::SetVersion(const std::string16 &origin,
75 const std::string16 &name,
76 const std::string16 &version) {
77 assert(db_);
78
79 const char16 *sql = STRING16(
80 L"REPLACE INTO DatabaseVersions(Origin, Name, Version) "
81 L"VALUES(?,?,?) ");
82
83 SQLStatement statement;
84 if (SQLITE_OK != statement.prepare16(db_, sql)) {
85 LOG(("Database2VersionsTable::SetVersion unable to prepare: %d\n",
86 db_->GetErrorCode()));
87 return false;
88 }
89
90 if (SQLITE_OK != statement.bind_text16(0, origin.c_str())) {
91 LOG(("Database2VersionsTable::SetVersion unable to bind origin: %d\n",
92 db_->GetErrorCode()));
93 return false;
94 }
95
96 if (SQLITE_OK != statement.bind_text16(1, name.c_str())) {
97 LOG(("Database2VersionsTable::SetVersion unable to bind name: %d\n",
98 db_->GetErrorCode()));
99 return false;
100 }
101
102 if (SQLITE_OK != statement.bind_text16(2, version.c_str())) {
103 LOG(("Database2VersionsTable::SetVersion unable to bind version: %d\n",
104 db_->GetErrorCode()));
105 return false;
106 }
107
108 if (SQLITE_DONE != statement.step()) {
109 LOG(("Database2VersionsTable::GetDatabaseVersion unable to step: %d\n",
110 db_->GetErrorCode()));
111 return false;
112 }
113
114 return true;
115 }
116
117 bool Database2VersionsTable::MaybeCreateTableLatestVersion() {
Aaron 2008/05/19 15:33:50 Let's just call this CreateTableVersion8() and not
Dimitri 2008/05/20 03:07:08 On 2008/05/19 15:33:50, Aaron wrote: > Let's just
118 const char *sql = "CREATE TABLE IF NOT EXISTS DatabaseVersions ("
119 " DatabaseID INTEGER PRIMARY KEY AUTOINCREMENT,"
120 " Origin TEXT NOT NULL,"
121 " Name TEXT NOT NULL,"
122 " Version TEXT NOT NULL,"
123 " UNIQUE (Origin, Name)"
124 ")";
125 if (SQLITE_OK != db_->Execute(sql)) {
126 LOG(("Database2VersionsTabel::MaybeCreateTableLatestVersion create"
127 " unable to execute: %d", db_->GetErrorCode()));
128 return false;
129 }
130 return true;
131 }
132
OLDNEW

Powered by Google App Engine
This is Rietveld r292