|
| 1 | +/*************************************************************************** |
| 2 | + qgsmysqlprovider.h - Data provider for MySQL 5.0+ |
| 3 | + ------------------- |
| 4 | + begin : 2006-01-07 |
| 5 | + copyright : (C) 2006 by Gary E.Sherman |
| 6 | + email : sherman at mrcc.com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +/* $Id: */ |
| 19 | + |
| 20 | +#include "../../src/qgsvectordataprovider.h" |
| 21 | + |
| 22 | + |
| 23 | +#include <map> |
| 24 | + |
| 25 | +#include "qgsdatasourceuri.h" |
| 26 | +extern "C"{ |
| 27 | +#include <mysql.h> |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class QgsFeature; |
| 32 | +class QgsField; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | +\class QgsMySQLProvider |
| 37 | +\brief Data provider for MySQL |
| 38 | +*/ |
| 39 | +class QgsMySQLProvider : public QgsVectorDataProvider |
| 40 | +{ |
| 41 | +public: |
| 42 | +/* |
| 43 | + * Constructor for the provider |
| 44 | +* The uri for a MySQL connection must include all needed parameters |
| 45 | +* to make the connection: |
| 46 | +* host=localhost user=gsherman dbname=test password=xxx table=test.alaska (the_geom) |
| 47 | +*/ |
| 48 | + QgsMySQLProvider(QString const & uri = ""); |
| 49 | + |
| 50 | + virtual ~ QgsMySQLProvider(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the permanent storage type for this layer as a friendly name. |
| 54 | + */ |
| 55 | + QString storageType(); |
| 56 | + |
| 57 | + /** get the first feature |
| 58 | +
|
| 59 | + mFile should be open with the file pointer at the record of the next |
| 60 | + feature, or EOF. The feature found on the current line is parsed. |
| 61 | +
|
| 62 | + @param feature the feature object to be populated with next feature |
| 63 | + @param fetchAttributes is true if you want to get the feature attributes, too |
| 64 | +
|
| 65 | + The search will be limited to the search region if one is active. |
| 66 | +
|
| 67 | + @return found QgsFeature, or null if not found |
| 68 | + */ |
| 69 | + QgsFeature *getFirstFeature(bool fetchAttributes = false); |
| 70 | + |
| 71 | + /** Get the next feature, if any |
| 72 | +
|
| 73 | + mFile should be open with the file pointer at the record of the next |
| 74 | + feature, or EOF. The feature found on the current line is parsed. |
| 75 | +
|
| 76 | + @param feature the feature object to be populated with next feature |
| 77 | + @param fetchAttributes is true if you want to get the feature attributes, too |
| 78 | +
|
| 79 | + The search will be limited to the search region if one is active. |
| 80 | +
|
| 81 | + * @return found QgsFeature, or null if not found |
| 82 | + */ |
| 83 | + QgsFeature *getNextFeature(bool fetchAttributes = false); |
| 84 | + |
| 85 | + /** get the next feature, if any |
| 86 | +
|
| 87 | + mFile should be open with the file pointer at the record of the next |
| 88 | + feature, or EOF. The feature found on the current line is parsed. |
| 89 | +
|
| 90 | + @param feature the feature object to be populated with next feature |
| 91 | + @param fetchAttributes is true if you want to get the feature attributes, too |
| 92 | +
|
| 93 | + @return false if unable to get the next feature |
| 94 | +
|
| 95 | + @note that feature will also be set invalid if unable to locate the next |
| 96 | + feature |
| 97 | +
|
| 98 | + The search will be limited to the search region if one is active. |
| 99 | +*/ |
| 100 | + /** Used to ask the layer for its projection as a WKT string. Implements |
| 101 | + virtual method of same name in QgsDataProvider. */ |
| 102 | + QString getProjectionWKT() {return QString("Not implemented yet");} ; |
| 103 | + |
| 104 | + /** |
| 105 | + * Get the first feature resulting from a select operation |
| 106 | + * @return QgsFeature |
| 107 | + */ |
| 108 | + bool getNextFeature(QgsFeature & feature, bool fetchAttributes = false); |
| 109 | + |
| 110 | + /** get the next feature, if any |
| 111 | +
|
| 112 | + mFile should be open with the file pointer at the record of the next |
| 113 | + feature, or EOF. The feature found on the current line is parsed. |
| 114 | +
|
| 115 | + @param feature the feature object to be populated with next feature @param |
| 116 | + attlist is list of attributes by their field positions to fetch |
| 117 | + with object |
| 118 | +
|
| 119 | + @return false if unable to get the next feature |
| 120 | +
|
| 121 | + @note That feature will also be set invalid if unable to locate the next |
| 122 | + feature. |
| 123 | +
|
| 124 | + The search will be limited to the search region if one is active. |
| 125 | +
|
| 126 | + */ |
| 127 | + QgsFeature *getNextFeature(std::list<int> const & desiredAttributes, int featureQueueSize = 1); |
| 128 | + |
| 129 | + |
| 130 | + /** Get the feature type. This corresponds to |
| 131 | + WKBPoint, |
| 132 | + WKBLineString, |
| 133 | + WKBPolygon, |
| 134 | + WKBMultiPoint, |
| 135 | + WKBMultiLineString or |
| 136 | + WKBMultiPolygon |
| 137 | + * as defined in qgis.h |
| 138 | + * This provider will always return WKBPoint |
| 139 | + */ |
| 140 | + int geometryType() const; |
| 141 | + |
| 142 | + |
| 143 | + /** return the number of layers for the current data source |
| 144 | +
|
| 145 | + @note |
| 146 | +
|
| 147 | + Should this be subLayerCount() instead? |
| 148 | + */ |
| 149 | + size_t layerCount() const; |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + /** |
| 154 | + * Get the number of features in the layer |
| 155 | + */ |
| 156 | + long featureCount() const; |
| 157 | + /** |
| 158 | + * Get the number of fields in the layer |
| 159 | + */ |
| 160 | + int fieldCount() const; |
| 161 | + /** |
| 162 | + * Select features based on a bounding rectangle. Features can be retrieved |
| 163 | + * with calls to getFirstFeature and getNextFeature. |
| 164 | + * @param mbr QgsRect containing the extent to use in selecting features |
| 165 | + */ |
| 166 | + void select(QgsRect * mbr, bool useIntersect = false); |
| 167 | + |
| 168 | + /** |
| 169 | + * Identify features within the search radius specified by rect |
| 170 | + * @param rect Bounding rectangle of search radius |
| 171 | + * @return std::vector containing QgsFeature objects that intersect rect |
| 172 | + */ |
| 173 | + virtual std::vector < QgsFeature > &identify(QgsRect * rect); |
| 174 | + |
| 175 | + /** Return the extent for this data layer |
| 176 | + */ |
| 177 | + virtual QgsRect *extent(); |
| 178 | + /** |
| 179 | + * Get the attributes associated with a feature |
| 180 | + */ |
| 181 | + void getFeatureAttributes(int key, QgsFeature * f); |
| 182 | + /** |
| 183 | + * Get the field information for the layer |
| 184 | + */ |
| 185 | + std::vector<QgsField> const & fields() const; |
| 186 | + |
| 187 | + /* Reset the layer (ie move the file pointer to the head |
| 188 | + of the file. |
| 189 | + */ |
| 190 | + void reset(); |
| 191 | + |
| 192 | + /**Returns the minimum value of an attribute |
| 193 | + @param position the number of the attribute*/ |
| 194 | + QString minValue(int position); |
| 195 | + |
| 196 | + /**Returns the maximum value of an attribute |
| 197 | + @param position the number of the attribute*/ |
| 198 | + QString maxValue(int position); |
| 199 | + |
| 200 | + /**Returns true if this is a valid delimited file |
| 201 | + */ |
| 202 | + bool isValid(); |
| 203 | + |
| 204 | + /** |
| 205 | + * Check to see if the point is withn the selection |
| 206 | + * rectangle |
| 207 | + * @param x X value of point |
| 208 | + * @param y Y value of point |
| 209 | + * @return True if point is within the rectangle |
| 210 | + */ |
| 211 | + bool boundsCheck(double x, double y); |
| 212 | + |
| 213 | + int capabilities() const; |
| 214 | + |
| 215 | + //! Save the layer as a shapefile |
| 216 | + bool saveAsShapefile(); |
| 217 | + |
| 218 | + QgsDataSourceURI *getURI() |
| 219 | + { |
| 220 | + return 0; |
| 221 | + }; |
| 222 | + /** The MySQL provider does not do its own transforms so we return |
| 223 | + * false for the following three functions to indicate that transforms |
| 224 | + * should be handled by the QgsCoordinateTransform object. See the |
| 225 | + * documentation on QgsVectorDataProvider for details on these functions. |
| 226 | + */ |
| 227 | + bool supportsNativeTransform(){return false;} |
| 228 | + bool usesSrid(){return false;} |
| 229 | + bool usesWKT(){return false;} |
| 230 | + |
| 231 | + /** return a provider name |
| 232 | +
|
| 233 | + Essentially just returns the provider key. Should be used to build file |
| 234 | + dialogs so that providers can be shown with their supported types. Thus |
| 235 | + if more than one provider supports a given format, the user is able to |
| 236 | + select a specific provider to open that file. |
| 237 | +
|
| 238 | + @note |
| 239 | +
|
| 240 | + Instead of being pure virtual, might be better to generalize this |
| 241 | + behavior and presume that none of the sub-classes are going to do |
| 242 | + anything strange with regards to their name or description? |
| 243 | +
|
| 244 | + */ |
| 245 | + QString name() const; |
| 246 | + |
| 247 | + |
| 248 | + /** return description |
| 249 | +
|
| 250 | + Return a terse string describing what the provider is. |
| 251 | +
|
| 252 | + @note |
| 253 | +
|
| 254 | + Instead of being pure virtual, might be better to generalize this |
| 255 | + behavior and presume that none of the sub-classes are going to do |
| 256 | + anything strange with regards to their name or description? |
| 257 | +
|
| 258 | + */ |
| 259 | + QString description() const; |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | +private: |
| 265 | + |
| 266 | + /** get the next feature, if any |
| 267 | +
|
| 268 | + mFile should be open with the file pointer at the record of the next |
| 269 | + feature, or EOF. The feature found on the current line is parsed. |
| 270 | +
|
| 271 | + @param feature the feature object to be populated with next feature |
| 272 | + @param getAttributes is true if you want to get the feature attributes, too |
| 273 | + |
| 274 | + @param desiredAttributes attributes fields to be collected for the feature |
| 275 | + as denoted by their position |
| 276 | +
|
| 277 | + @return false if unable to get the next feature |
| 278 | + */ |
| 279 | + bool getNextFeature_( QgsFeature & feature, |
| 280 | + bool getAttributes, |
| 281 | + std::list<int> const * desiredAttributes = 0 ); |
| 282 | + |
| 283 | + void fillMinMaxCash(); |
| 284 | + |
| 285 | + int *getFieldLengths(); |
| 286 | + |
| 287 | + //! Fields |
| 288 | + std::vector < QgsField > attributeFields; |
| 289 | + |
| 290 | + //! Map to store field position by name |
| 291 | + std::map < QString, int >fieldPositions; |
| 292 | + |
| 293 | + QString mFileName; |
| 294 | + QString mDelimiter; |
| 295 | + QString mXField; |
| 296 | + QString mYField; |
| 297 | + |
| 298 | + //! Layer extent |
| 299 | + QgsRect *mExtent; |
| 300 | + |
| 301 | + //! Current selection rectangle |
| 302 | + |
| 303 | + QgsRect *mSelectionRectangle; |
| 304 | + |
| 305 | + bool mValid; |
| 306 | + |
| 307 | + int mGeomType; |
| 308 | + |
| 309 | + long mNumberFeatures; |
| 310 | + |
| 311 | + //! Feature id |
| 312 | + long mFid; |
| 313 | + |
| 314 | + /**Flag indicating, if the minmaxcache should be renewed (true) or not (false)*/ |
| 315 | + bool mMinMaxCacheDirty; |
| 316 | + |
| 317 | + /**Matrix storing the minimum and maximum values*/ |
| 318 | + double **mMinMaxCache; |
| 319 | + |
| 320 | + /**Fills the cash and sets minmaxcachedirty to false*/ |
| 321 | + void mFillMinMaxCash(); |
| 322 | + |
| 323 | + //! Data source URI struct for this layer |
| 324 | + QgsDataSourceURI mUri; |
| 325 | + |
| 326 | + /** |
| 327 | + * Flag indicating if the layer data source is a valid MySQL layer |
| 328 | + */ |
| 329 | + bool valid; |
| 330 | + /** |
| 331 | + * Name of the table with no schema |
| 332 | + */ |
| 333 | + QString mTableName; |
| 334 | + /** |
| 335 | + * Name of the table with schema included |
| 336 | + */ |
| 337 | + QString mSchemaTableName; |
| 338 | + /** |
| 339 | + * Name of the schema |
| 340 | + */ |
| 341 | + QString mSchemaName; |
| 342 | + /** |
| 343 | + * SQL statement used to limit the features retreived |
| 344 | + */ |
| 345 | + QString sqlWhereClause; |
| 346 | + /** |
| 347 | + * Primary key column for fetching features. If there is no primary key |
| 348 | + * the oid is used to fetch features. |
| 349 | + */ |
| 350 | + QString primaryKey; |
| 351 | + /** |
| 352 | + * Data type for the primary key |
| 353 | + */ |
| 354 | + QString primaryKeyType; |
| 355 | + /** |
| 356 | + * Name of the geometry column in the table |
| 357 | + */ |
| 358 | + QString geometryColumn; |
| 359 | + /** |
| 360 | + * MySQL structure |
| 361 | + */ |
| 362 | + MYSQL mysql; |
| 363 | + |
| 364 | + struct wkbPoint |
| 365 | + { |
| 366 | + unsigned char byteOrder; |
| 367 | + quint32 wkbType; |
| 368 | + double x; |
| 369 | + double y; |
| 370 | + }; |
| 371 | + wkbPoint mWKBpt; |
| 372 | + |
| 373 | +}; |
0 commit comments