Changeset View
Changeset View
Standalone View
Standalone View
src/migration/KexiSqlMigrate.cpp
Show First 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | |||||
57 | bool KexiSqlMigrate::drv_readTableSchema( | 57 | bool KexiSqlMigrate::drv_readTableSchema( | ||
58 | const QString& originalName, KDbTableSchema *tableSchema) | 58 | const QString& originalName, KDbTableSchema *tableSchema) | ||
59 | { | 59 | { | ||
60 | //! @todo IDEA: ask for user input for captions | 60 | //! @todo IDEA: ask for user input for captions | ||
61 | 61 | | |||
62 | //Perform a query on the table to get some data | 62 | //Perform a query on the table to get some data | ||
63 | KDbEscapedString sql = KDbEscapedString("SELECT * FROM %1 LIMIT 0") | 63 | KDbEscapedString sql = KDbEscapedString("SELECT * FROM %1 LIMIT 0") | ||
64 | .arg(sourceConnection()->escapeIdentifier(tableSchema->name())); | 64 | .arg(sourceConnection()->escapeIdentifier(tableSchema->name())); | ||
65 | QScopedPointer<KDbSqlResult> result(sourceConnection()->executeSQL(sql)); | 65 | QSharedPointer<KDbSqlResult> result = sourceConnection()->prepareSql(sql); | ||
66 | if (!result) { | 66 | if (!result) { | ||
67 | return false; | 67 | return false; | ||
68 | } | 68 | } | ||
69 | 69 | | |||
70 | bool ok = true; | 70 | bool ok = true; | ||
71 | const int fieldsCount = result->fieldsCount(); | 71 | const int fieldsCount = result->fieldsCount(); | ||
72 | for (int i = 0; i < fieldsCount; i++) { | 72 | for (int i = 0; i < fieldsCount; i++) { | ||
73 | KDbField *field = result->createField(originalName, i); | 73 | KDbField *field = result->createField(originalName, i); | ||
74 | if (field->type() == KDbField::InvalidType) { | 74 | if (field->type() == KDbField::InvalidType) { | ||
75 | field->setType(userType(originalName + '.' + field->name())); | 75 | field->setType(userType(originalName + '.' + field->name())); | ||
76 | } | 76 | } | ||
77 | if (!tableSchema->addField(field)) { | 77 | if (!tableSchema->addField(field)) { | ||
78 | delete field; | 78 | delete field; | ||
79 | tableSchema->clear(); | 79 | tableSchema->clear(); | ||
80 | ok = false; | 80 | ok = false; | ||
81 | break; | 81 | break; | ||
82 | } | 82 | } | ||
83 | } | 83 | } | ||
84 | return ok; | 84 | return ok; | ||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | bool KexiSqlMigrate::drv_tableNames(QStringList *tableNames) | 87 | bool KexiSqlMigrate::drv_tableNames(QStringList *tableNames) | ||
88 | { | 88 | { | ||
89 | QScopedPointer<KDbSqlResult> result(sourceConnection()->executeSQL(m_tableNamesSql)); | 89 | QSharedPointer<KDbSqlResult> result = sourceConnection()->prepareSql(m_tableNamesSql); | ||
90 | if (!result || result->fieldsCount() < 1) { | 90 | if (!result || result->fieldsCount() < 1) { | ||
91 | return false; | 91 | return false; | ||
92 | } | 92 | } | ||
93 | Q_FOREVER { | 93 | Q_FOREVER { | ||
94 | QScopedPointer<KDbSqlRecord> record(result->fetchRecord()); | 94 | QSharedPointer<KDbSqlRecord> record = result->fetchRecord(); | ||
95 | if (!record) { | 95 | if (!record) { | ||
96 | if (result->lastResult().isError()) { | 96 | if (result->lastResult().isError()) { | ||
97 | return false; | 97 | return false; | ||
98 | } | 98 | } | ||
99 | break; | 99 | break; | ||
100 | } | 100 | } | ||
101 | tableNames->append(record->stringValue(0)); | 101 | tableNames->append(record->stringValue(0)); | ||
102 | } | 102 | } | ||
103 | return true; | 103 | return true; | ||
104 | } | 104 | } | ||
105 | 105 | | |||
106 | tristate KexiSqlMigrate::drv_queryStringListFromSQL( | 106 | tristate KexiSqlMigrate::drv_queryStringListFromSQL( | ||
107 | const KDbEscapedString& sqlStatement, int fieldIndex, QStringList *stringList, int numRecords) | 107 | const KDbEscapedString& sqlStatement, int fieldIndex, QStringList *stringList, int numRecords) | ||
108 | { | 108 | { | ||
109 | QScopedPointer<KDbSqlResult> result(sourceConnection()->executeSQL(sqlStatement)); | 109 | QSharedPointer<KDbSqlResult> result= sourceConnection()->prepareSql(sqlStatement); | ||
110 | if (!result) { | 110 | if (!result) { | ||
111 | return true; | 111 | return true; | ||
112 | } | 112 | } | ||
113 | if (result->fieldsCount() < (fieldIndex+1)) { | 113 | if (result->fieldsCount() < (fieldIndex+1)) { | ||
114 | qWarning() << sqlStatement << ": fieldIndex too large (" | 114 | qWarning() << sqlStatement << ": fieldIndex too large (" | ||
115 | << fieldIndex << "), expected 0.." << result->fieldsCount() - 1; | 115 | << fieldIndex << "), expected 0.." << result->fieldsCount() - 1; | ||
116 | return false; | 116 | return false; | ||
117 | } | 117 | } | ||
118 | for (int i = 0; numRecords == -1 || i < numRecords; i++) { | 118 | for (int i = 0; numRecords == -1 || i < numRecords; i++) { | ||
119 | QScopedPointer<KDbSqlRecord> record(result->fetchRecord()); | 119 | QSharedPointer<KDbSqlRecord> record = result->fetchRecord(); | ||
120 | if (!record) { | 120 | if (!record) { | ||
121 | if (numRecords != -1 || result->lastResult().isError()) { | 121 | if (numRecords != -1 || result->lastResult().isError()) { | ||
122 | return false; | 122 | return false; | ||
123 | } | 123 | } | ||
124 | return true; | 124 | return true; | ||
125 | } | 125 | } | ||
126 | stringList->append(record->stringValue(fieldIndex)); | 126 | stringList->append(record->stringValue(fieldIndex)); | ||
127 | } | 127 | } | ||
128 | return true; | 128 | return true; | ||
129 | } | 129 | } | ||
130 | 130 | | |||
131 | bool KexiSqlMigrate::drv_copyTable(const QString& srcTable, KDbConnection *destConn, | 131 | bool KexiSqlMigrate::drv_copyTable(const QString& srcTable, KDbConnection *destConn, | ||
132 | KDbTableSchema* dstTable, | 132 | KDbTableSchema* dstTable, | ||
133 | const RecordFilter *recordFilter) | 133 | const RecordFilter *recordFilter) | ||
134 | { | 134 | { | ||
135 | QScopedPointer<KDbSqlResult> result( | 135 | QSharedPointer<KDbSqlResult> result = sourceConnection()->prepareSql( | ||
136 | sourceConnection()->executeSQL(KDbEscapedString("SELECT * FROM %1") | 136 | KDbEscapedString("SELECT * FROM %1").arg(sourceConnection()->escapeIdentifier(srcTable))); | ||
137 | .arg(sourceConnection()->escapeIdentifier(srcTable)))); | | |||
138 | if (!result) { | 137 | if (!result) { | ||
139 | return false; | 138 | return false; | ||
140 | } | 139 | } | ||
141 | const KDbQueryColumnInfo::Vector fieldsExpanded(dstTable->query()->fieldsExpanded()); | 140 | const KDbQueryColumnInfo::Vector fieldsExpanded(dstTable->query()->fieldsExpanded()); | ||
142 | const int numFields = qMin(fieldsExpanded.count(), result->fieldsCount()); | 141 | const int numFields = qMin(fieldsExpanded.count(), result->fieldsCount()); | ||
143 | Q_FOREVER { | 142 | Q_FOREVER { | ||
144 | QScopedPointer<KDbSqlRecord> record(result->fetchRecord()); | 143 | QSharedPointer<KDbSqlRecord> record = result->fetchRecord(); | ||
145 | if (!record) { | 144 | if (!record) { | ||
146 | if (!result->lastResult().isError()) { | 145 | if (!result->lastResult().isError()) { | ||
147 | break; | 146 | break; | ||
148 | } | 147 | } | ||
149 | return false; | 148 | return false; | ||
150 | } | 149 | } | ||
151 | if (recordFilter) { | 150 | if (recordFilter) { | ||
152 | if (!(*recordFilter)(*record)) { | 151 | if (!(*recordFilter)(record)) { | ||
153 | continue; | 152 | continue; | ||
154 | } | 153 | } | ||
155 | } | 154 | } | ||
156 | QList<QVariant> vals; | 155 | QList<QVariant> vals; | ||
157 | for(int i = 0; i < numFields; ++i) { | 156 | for(int i = 0; i < numFields; ++i) { | ||
158 | const KDbSqlString s(record->cstringValue(i)); | 157 | const KDbSqlString s(record->cstringValue(i)); | ||
159 | vals.append(KDb::cstringToVariant( | 158 | vals.append(KDb::cstringToVariant( | ||
160 | s.string, fieldsExpanded.at(i)->field()->type(), 0, s.length)); | 159 | s.string, fieldsExpanded.at(i)->field()->type(), 0, s.length)); | ||
Show All 10 Lines | |||||
171 | } | 170 | } | ||
172 | /*! @todo Check that wasn't an error, rather than end of result set */ | 171 | /*! @todo Check that wasn't an error, rather than end of result set */ | ||
173 | return true; | 172 | return true; | ||
174 | } | 173 | } | ||
175 | 174 | | |||
176 | bool KexiSqlMigrate::drv_getTableSize(const QString& table, quint64 *size) | 175 | bool KexiSqlMigrate::drv_getTableSize(const QString& table, quint64 *size) | ||
177 | { | 176 | { | ||
178 | Q_ASSERT(size); | 177 | Q_ASSERT(size); | ||
179 | QScopedPointer<KDbSqlResult> result( | 178 | QSharedPointer<KDbSqlResult> result | ||
180 | sourceConnection()->executeSQL(KDbEscapedString("SELECT COUNT(*) FROM %1") | 179 | = sourceConnection()->prepareSql(KDbEscapedString("SELECT COUNT(*) FROM %1") | ||
181 | .arg(sourceConnection()->escapeIdentifier(table)))); | 180 | .arg(sourceConnection()->escapeIdentifier(table))); | ||
182 | if (!result) { | 181 | if (!result) { | ||
183 | return false; | 182 | return false; | ||
184 | } | 183 | } | ||
185 | QScopedPointer<KDbSqlRecord> record(result->fetchRecord()); | 184 | QSharedPointer<KDbSqlRecord> record = result->fetchRecord(); | ||
186 | if (!result || result->fieldsCount() == 0) { | 185 | if (!result || result->fieldsCount() == 0) { | ||
187 | return false; | 186 | return false; | ||
188 | } | 187 | } | ||
189 | bool ok; | 188 | bool ok; | ||
190 | quint64 value = record->toByteArray(0).toULongLong(&ok); | 189 | quint64 value = record->toByteArray(0).toULongLong(&ok); | ||
191 | if (!ok) { | 190 | if (!ok) { | ||
192 | value = -1; | 191 | value = -1; | ||
193 | } | 192 | } | ||
194 | *size = value; | 193 | *size = value; | ||
195 | return ok; | 194 | return ok; | ||
196 | } | 195 | } | ||
197 | 196 | | |||
198 | KDbSqlResult* KexiSqlMigrate::drv_readFromTable(const QString& tableName) | 197 | QSharedPointer<KDbSqlResult> KexiSqlMigrate::drv_readFromTable(const QString& tableName) | ||
199 | { | 198 | { | ||
200 | QScopedPointer<KDbSqlResult> result(sourceConnection()->executeSQL(KDbEscapedString("SELECT * FROM %1") | 199 | QSharedPointer<KDbSqlResult> result = sourceConnection()->prepareSql( | ||
201 | .arg(sourceConnection()->escapeIdentifier(tableName)))); | 200 | KDbEscapedString("SELECT * FROM %1").arg(sourceConnection()->escapeIdentifier(tableName))); | ||
202 | if (!result || result->lastResult().isError()) { | 201 | if (!result || result->lastResult().isError()) { | ||
203 | m_result = sourceConnection()->result(); | 202 | m_result = sourceConnection()->result(); | ||
204 | qWarning() << m_result; | 203 | qWarning() << m_result; | ||
205 | return nullptr; | 204 | result.clear(); | ||
206 | } | 205 | } | ||
207 | return result.take(); | 206 | return result; | ||
208 | } | 207 | } |