/** request : 'db -> sql:string -> 'result <doc>Executes the SQL request and returns its result</doc> **/ HL_PRIM sqlite_result *HL_NAME(request)(sqlite_database *db, vbyte *sql ) { sqlite_result *r; const char *tl; int i,j; r = (sqlite_result*)hl_gc_alloc_finalizer(sizeof(sqlite_result)); r->finalize = HL_NAME(finalize_result); r->db = db; if( sqlite3_prepare16_v2(db->db, sql, -1, &r->r, &tl) != SQLITE_OK ) { HL_NAME(error)(db->db, false); } if( *tl ) { sqlite3_finalize(r->r); hl_error("SQLite error: Cannot execute several SQL requests at the same time"); } r->ncols = sqlite3_column_count(r->r); r->names = (int*)hl_gc_alloc(sizeof(int)*r->ncols); r->bools = (int*)hl_gc_alloc(sizeof(int)*r->ncols); r->first = 1; r->done = 0; for(i=0;i<r->ncols;i++) { int id = hl_hash_gen((uchar*)sqlite3_column_name16(r->r,i), true); const char *dtype = sqlite3_column_decltype(r->r,i); for(j=0;j<i;j++) if( r->names[j] == id ) { if( strcmp(sqlite3_column_name16(r->r,i), sqlite3_column_name16(r->r,j)) == 0 ) { sqlite3_finalize(r->r); hl_buffer *b = hl_alloc_buffer(); hl_buffer_str(b, USTR("SQLite error: Same field is two times in the request: ")); hl_buffer_str(b, (uchar*)sql); hl_error_msg(hl_buffer_content(b, NULL)); } else { hl_buffer *b = hl_alloc_buffer(); hl_buffer_str(b, USTR("SQLite error: Same field ids for: ")); hl_buffer_str(b, sqlite3_column_name16(r->r,i)); hl_buffer_str(b, USTR(" and ")); hl_buffer_str(b, sqlite3_column_name16(r->r,j)); sqlite3_finalize(r->r); hl_error_msg(hl_buffer_content(b, NULL)); } } r->names[i] = id; r->bools[i] = dtype?(strcmp(dtype,"BOOL") == 0):0; } // changes in an update/delete if( db->last != NULL ) HL_NAME(finalize_request)(db->last, false); db->last = r; return db->last; }
int SQLite::select(const std::string& query, select_t func) { sqlite3_stmt* stmt; int rc = sqlite3_prepare_v2(this->database, query.c_str(), -1, &stmt, nullptr); if (rc != SQLITE_OK) { printf("SQLite::select() failed: %s\n", sqlite3_errmsg(this->database)); return rc; } while (sqlite3_step(stmt) == SQLITE_ROW) { SQResult result(stmt); int columnCount = sqlite3_column_count(stmt); for (int index = 0; index < columnCount; index++) { std::string type = sqlite3_column_decltype(stmt, index); if (type == "INT") { result.row.emplace_back( SQResult::INTEGER, new int(sqlite3_column_int(stmt, index))); } else if (type == "FLOAT") { result.row.emplace_back( SQResult::FLOAT, new double(sqlite3_column_double(stmt, index))); } else if (type == "TEXT") { result.row.emplace_back( SQResult::TEXT, new std::string((char*) sqlite3_column_text(stmt, index))); } else { printf("SQLite::select(): Unknown column decltype: %s\n", type.c_str()); return SQLITE_IOERR_NOMEM; } } // column index // execute callback to user with result func(result); } // row index sqlite3_finalize(stmt); return SQLITE_OK; } // select()
/** request : 'db -> sql:string -> 'result <doc>Executes the SQL request and returns its result</doc> **/ static value request( value v, value sql ) { database *db; result *r; const char *tl; int i,j; val_check_kind(v,k_db); val_check(sql,string); db = val_db(v); r = (result*)alloc(sizeof(result)); r->db = db; if( sqlite3_prepare(db->db,val_string(sql),val_strlen(sql),&r->r,&tl) != SQLITE_OK ) { buffer b = alloc_buffer("Sqlite error in "); val_buffer(b,sql); buffer_append(b," : "); buffer_append(b,sqlite3_errmsg(db->db)); val_throw(buffer_to_string(b)); } if( *tl ) { sqlite3_finalize(r->r); val_throw(alloc_string("Cannot execute several SQL requests at the same time")); } r->ncols = sqlite3_column_count(r->r); r->names = (field*)alloc_private(sizeof(field)*r->ncols); r->bools = (int*)alloc_private(sizeof(int)*r->ncols); r->first = 1; r->done = 0; for(i=0;i<r->ncols;i++) { field id = val_id(sqlite3_column_name(r->r,i)); const char *dtype = sqlite3_column_decltype(r->r,i); for(j=0;j<i;j++) if( r->names[j] == id ) { if( strcmp(sqlite3_column_name(r->r,i),sqlite3_column_name(r->r,j)) == 0 ) { buffer b = alloc_buffer("Error, same field is two times in the request "); val_buffer(b,sql); sqlite3_finalize(r->r); val_throw(buffer_to_string(b)); } else { buffer b = alloc_buffer("Error, same field ids for : "); buffer_append(b,sqlite3_column_name(r->r,i)); buffer_append(b," and "); buffer_append(b,sqlite3_column_name(r->r,j)); buffer_append_char(b,'.'); sqlite3_finalize(r->r); val_throw(buffer_to_string(b)); } } r->names[i] = id; r->bools[i] = dtype?(strcmp(dtype,"BOOL") == 0):0; } // changes in an update/delete if( db->last != NULL ) finalize_result(val_result(db->last),0); db->last = alloc_abstract(k_result,r); return db->last; }
CString CppSQLite3Query::fieldDeclType(int nCol) { checkVM(); if (nCol < 0 || nCol > mnCols-1) { throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested"); } return sqlite3_column_decltype(mpVM, nCol); }
static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) { pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; const char *str; zval flags; if (!S->stmt) { return FAILURE; } if(colno >= sqlite3_data_count(S->stmt)) { /* error invalid column */ pdo_sqlite_error_stmt(stmt); return FAILURE; } array_init(return_value); array_init(&flags); switch (sqlite3_column_type(S->stmt, colno)) { case SQLITE_NULL: add_assoc_string(return_value, "native_type", "null"); break; case SQLITE_FLOAT: add_assoc_string(return_value, "native_type", "double"); break; case SQLITE_BLOB: add_next_index_string(&flags, "blob"); case SQLITE_TEXT: add_assoc_string(return_value, "native_type", "string"); break; case SQLITE_INTEGER: add_assoc_string(return_value, "native_type", "integer"); break; } str = sqlite3_column_decltype(S->stmt, colno); if (str) { add_assoc_string(return_value, "sqlite:decl_type", (char *)str); } #ifdef SQLITE_ENABLE_COLUMN_METADATA str = sqlite3_column_table_name(S->stmt, colno); if (str) { add_assoc_string(return_value, "table", (char *)str); } #endif add_assoc_zval(return_value, "flags", &flags); return SUCCESS; }
const char* CppSQLite3Query::fieldDeclType(int nCol) { checkVM(); if (nCol < 0 || nCol > mnCols-1) { IwError(("Invalid field index requested")); } return sqlite3_column_decltype(mpVM, nCol); }
/* test if a file is a sqlite.data.frame. returns the internal name of the * sdf if file is an sdf or NULL otherwise */ const char * _is_sdf2(const char *filename) { sqlite3* db = _is_sqlitedb(filename); const char *ret = (db == NULL) ? NULL : filename; if (ret) { sqlite3_stmt *stmt; char *sql = "select * from sdf_attributes where attr='name'"; int res, ncols; res = sqlite3_prepare(db, sql, -1, &stmt, NULL); ret = (((res == SQLITE_OK) && /* no attribute table */ ((ncols = sqlite3_column_count(stmt)) == 2) && (strcmp(sqlite3_column_name(stmt, 0), "attr") == 0) && (strcmp(sqlite3_column_decltype(stmt, 0), "text") == 0) && (strcmp(sqlite3_column_name(stmt, 1), "value") == 0) && (strcmp(sqlite3_column_decltype(stmt, 1), "text") == 0))) ? ret : NULL ; if (ret == NULL) goto _is_sdf_cleanup; /* get internal name. we are assuming here that the 1st row of the attributes * table always contains the column name */ res = sqlite3_step(stmt); ret = (res == SQLITE_ROW) ? ret : NULL; if (ret == NULL) goto _is_sdf_cleanup; /* copy to buf2, because when we finalize stmt, we won't be sure * if sqlite3_column_text()'s ret value will still be there */ strcpy(g_sql_buf[2], (char *)sqlite3_column_text(stmt, 1)); ret = g_sql_buf[2]; sqlite3_finalize(stmt); sql = "select * from sdf_data"; res = sqlite3_prepare(db, sql, -1, &stmt, NULL); ret = (res == SQLITE_OK) ? ret : NULL; /* if not, missing data table */ _is_sdf_cleanup: sqlite3_finalize(stmt); sqlite3_close(db); } return ret; }
Status getQueryColumnsInternal(const std::string& q, TableColumns& columns, sqlite3* db) { // Turn the query into a prepared statement sqlite3_stmt* stmt{nullptr}; auto rc = sqlite3_prepare_v2( db, q.c_str(), static_cast<int>(q.length() + 1), &stmt, nullptr); if (rc != SQLITE_OK || stmt == nullptr) { if (stmt != nullptr) { sqlite3_finalize(stmt); } return Status(1, sqlite3_errmsg(db)); } // Get column count auto num_columns = sqlite3_column_count(stmt); TableColumns results; results.reserve(num_columns); // Get column names and types Status status = Status(); bool unknown_type = false; for (int i = 0; i < num_columns; ++i) { auto col_name = sqlite3_column_name(stmt, i); auto col_type = sqlite3_column_decltype(stmt, i); if (col_name == nullptr) { status = Status(1, "Could not get column type"); break; } if (col_type == nullptr) { // Types are only returned for table columns (not expressions). col_type = "UNKNOWN"; unknown_type = true; } results.push_back(std::make_tuple( col_name, columnTypeName(col_type), ColumnOptions::DEFAULT)); } // An unknown type means we have to parse the plan and SQLite opcodes. if (unknown_type) { QueryPlanner planner(q, db); planner.applyTypes(results); } if (status.ok()) { columns = std::move(results); } sqlite3_finalize(stmt); return status; }
void Sqlite3Connection::GetColumn(int i, Ref f) const { ASSERT(NULL != current_stmt); if(i == -1) { f = Value(sqlite3_last_insert_rowid(db)); return; } ASSERT(got_row_data); String coltype; const char *s = sqlite3_column_decltype(current_stmt,i); if(s) coltype = ToLower(s); switch (sqlite3_column_type(current_stmt,i)) { case SQLITE_INTEGER: f = sqlite3_column_int64(current_stmt,i); break; case SQLITE_FLOAT: f = sqlite3_column_double(current_stmt,i); break; case SQLITE_TEXT: if(coltype == "date" || f.GetType() == DATE_V){ const char *s = (const char *)sqlite3_column_text(current_stmt, i); if(strlen(s) >= 10) f = Value(Date(atoi(s), atoi(s + 5), atoi(s + 8))); else f = Null; } else if(coltype == "datetime" || f.GetType() == TIME_V) { const char *s = (const char *)sqlite3_column_text(current_stmt, i); if(strlen(s) >= 19) f = Value(Time(atoi(s), atoi(s + 5), atoi(s + 8), atoi(s + 11), atoi(s + 14), atoi(s + 17))); else if(strlen(s) >= 10) f = Value(ToTime(Date(atoi(s), atoi(s + 5), atoi(s + 8)))); else f = Null; } else f = Value(WString((const wchar*)sqlite3_column_text16(current_stmt,i))); break; case SQLITE_NULL: f = Null; break; case SQLITE_BLOB: f = Value(String( (const byte*)sqlite3_column_blob(current_stmt,i), sqlite3_column_bytes(current_stmt,i) )); break; default: NEVER(); break; } return; }
/* test if a file is a SQLiteDF workspace */ sqlite3* _is_workspace(char *filename) { sqlite3* db = _is_sqlitedb(filename); if (db != NULL) { sqlite3_stmt *stmt; char *sql = "select * from workspace"; int res = sqlite3_prepare(db, sql, -1, &stmt, 0), ncols; if ((res != SQLITE_OK) || /* no workspace table */ ((ncols = sqlite3_column_count(stmt)) != WORKSPACE_COLUMNS) || /* below also checks the ordering of the columns */ (strcmp(sqlite3_column_name(stmt, 0), "rel_filename") != 0) || (strcmp(sqlite3_column_decltype(stmt, 0), "text") != 0) || (strcmp(sqlite3_column_name(stmt, 1), "full_filename") != 0) || (strcmp(sqlite3_column_decltype(stmt, 1), "text") != 0) || (strcmp(sqlite3_column_name(stmt, 2), "internal_name") != 0) || (strcmp(sqlite3_column_decltype(stmt, 2), "text") != 0) || (strcmp(sqlite3_column_name(stmt, 3), "loaded") != 0) || (strcmp(sqlite3_column_decltype(stmt, 3), "bit") != 0) || (strcmp(sqlite3_column_name(stmt, 4), "uses") != 0) || (strcmp(sqlite3_column_decltype(stmt, 4), "int") != 0) || (strcmp(sqlite3_column_name(stmt, 5), "used") != 0) || (strcmp(sqlite3_column_decltype(stmt, 5), "bit") != 0)) { sqlite3_finalize(stmt); sqlite3_close(db); db = NULL; } else { sqlite3_finalize(stmt); } } return db; }
/* call-seq: stmt.column_decltype(index) * * Get the column type at +index+. 0 based. */ static VALUE column_decltype(VALUE self, VALUE index) { sqlite3StmtRubyPtr ctx; const char * name; Data_Get_Struct(self, sqlite3StmtRuby, ctx); REQUIRE_OPEN_STMT(ctx); name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index)); if(name) return rb_str_new2(name); return Qnil; }
const char* CppSQLite3Query::fieldDeclType(int nCol) { checkVM(); if (nCol < 0 || nCol > mnCols-1) { // throw CppSQLite3Exception(CPPSQLITE_ERROR, // "Invalid field index requested", // DONT_DELETE_MSG); } return sqlite3_column_decltype(mpVM, nCol); }
const char* CppSQLite3Query::FieldDeclType(int nField) { CheckStmt(); if (nField < 0 || nField > mnCols - 1) { throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); } return sqlite3_column_decltype(mpStmt, nField); }
EValueType GetColumnType( sqlite3_stmt* stmt, int index ) { const char* s = sqlite3_column_decltype( stmt, index ); if( s ) { wstring str; FROM_UTF8( s, str ); return ConvertTypeNameToEnum( str ); } return (EValueType) sqlite3_column_type( stmt, index ); }
SWIGEXPORT jstring JNICALL Java_com_almworks_sqlite4java__1SQLiteSwiggedJNI_sqlite3_1column_1decltype(JNIEnv *jenv, jclass jcls, jlong jarg1, jint jarg2) { jstring jresult = 0 ; sqlite3_stmt *arg1 = (sqlite3_stmt *) 0 ; int arg2 ; char *result = 0 ; (void)jenv; (void)jcls; arg1 = *(sqlite3_stmt **)&jarg1; arg2 = (int)jarg2; result = (char *)sqlite3_column_decltype(arg1,arg2); if (result) jresult = (*jenv)->NewStringUTF(jenv, (const char *)result); return jresult; }
static HB_USHORT sqlite3DeclType(sqlite3_stmt * st, HB_USHORT uiIndex ) { const char * szDeclType; szDeclType = sqlite3_column_decltype( st, uiIndex ); /* the order of comparisons below is important to replicate * type precedence used by SQLITE3 */ if( szDeclType != NULL ) { HB_SIZE nLen = strlen( szDeclType ); if( hb_strAtI( "INT", 3, szDeclType, nLen ) != 0 ) return HB_FT_INTEGER; if( hb_strAtI( "CHAR", 4, szDeclType, nLen ) != 0 || hb_strAtI( "TEXT", 4, szDeclType, nLen ) != 0 || hb_strAtI( "CLOB", 4, szDeclType, nLen ) != 0 ) return HB_FT_STRING; if( hb_strAtI( "BLOB", 4, szDeclType, nLen ) != 0 ) return HB_FT_ANY; if( hb_strAtI( "REAL", 4, szDeclType, nLen ) != 0 || hb_strAtI( "FLOA", 4, szDeclType, nLen ) != 0 || hb_strAtI( "DOUB", 4, szDeclType, nLen ) != 0 ) return HB_FT_LONG; } #ifdef HB_SQLT3_MAP_UNDECLARED_TYPES_AS_ANY return HB_FT_ANY; #else switch( sqlite3_column_type( st, uiIndex ) ) { case SQLITE_TEXT: return HB_FT_STRING; case SQLITE_FLOAT: return HB_FT_LONG; case SQLITE_INTEGER: return HB_FT_INTEGER; case SQLITE_BLOB: return HB_FT_BLOB; case SQLITE_NULL: return HB_FT_ANY; } return HB_FT_NONE; #endif }
static gboolean is_date_column(sqlite3_stmt* query, int i) { gboolean res; const char* column_type = sqlite3_column_decltype(query, i); char* column_type_up = (column_type == NULL)? NULL: g_utf8_strup(column_type, -1); if((column_type_up != NULL) && (strcmp(column_type_up, "DATE") == 0)) res = TRUE; else res = FALSE; g_free(column_type_up); return res; }
ColumnType Statement::GetDeclaredColumnType(int col) const { std::string column_type(sqlite3_column_decltype(GetStatement(), col)); std::transform(column_type.begin(), column_type.end(), column_type.begin(), tolower); if (column_type == "integer") return COLUMN_TYPE_INTEGER; else if (column_type == "float") return COLUMN_TYPE_FLOAT; else if (column_type == "text") return COLUMN_TYPE_TEXT; else if (column_type == "blob") return COLUMN_TYPE_BLOB; return COLUMN_TYPE_NULL; }
MetaColumn::ColumnDataType Utility::getColumnType(sqlite3_stmt* pStmt, std::size_t pos) { poco_assert_dbg (pStmt); static Utility u; const char* pc = sqlite3_column_decltype(pStmt, (int) pos); std::string sqliteType = pc ? pc : ""; Poco::toUpperInPlace(sqliteType); sqliteType = sqliteType.substr(0, sqliteType.find_first_of(" (")); TypeMap::const_iterator it = _types.find(Poco::trimInPlace(sqliteType)); if (_types.end() == it) throw Poco::NotFoundException(); return it->second; }
void CSQLiteQuery::ColumnDeclType(int nCol, LPTSTR pszType, DWORD dwLength) { LPCSTR pszTypeA = sqlite3_column_decltype(m_pStmt, nCol); if( NULL != pszTypeA ) { #ifdef _UNICODE MultiByteToWideChar( CP_ACP, 0, pszTypeA, -1, pszType, dwLength ); #else StringCchCopy(pszType, dwLength, pszTypeA); #endif } }
const TCHAR* CppSQLite3Query::fieldDeclType(int nCol) { checkVM(); if (nCol < 0 || nCol > mnCols-1) { throw CppSQLite3Exception(CPPSQLITE_ERROR, _T("Invalid field index requested"), DONT_DELETE_MSG); } #ifdef _UNICODE return (const TCHAR*)sqlite3_column_decltype16(mpVM, nCol); #else return sqlite3_column_decltype(mpVM, nCol); #endif }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ void DatabaseResult::getColumns(I_ColumnVisitor& _visitor) { const int numFields = sqlite3_column_count(m_pResult); _visitor.begin(); for(int i = 0 ; i < numFields ; i++ ) { const char* const pColumnType = sqlite3_column_decltype(m_pResult, i); const char* const pColumnName = sqlite3_column_name(m_pResult, i); DatabaseColumn column(pColumnName, pColumnType); _visitor.visit(column); } _visitor.end(); }
/* static int create_cursor(lua_State *L, int conn, sqlite3_stmt *sql_vm, int numcols, const char **row, const char **col_info)*/ static int create_cursor(lua_State *L, int o, conn_data *conn, sqlite3_stmt *sql_vm, int numcols) { int i; cur_data *cur = (cur_data*)lua_newuserdata(L, sizeof(cur_data)); luasql_setmeta (L, LUASQL_CURSOR_SQLITE); /* increment cursor count for the connection creating this cursor */ conn->cur_counter++; /* fill in structure */ cur->closed = 0; cur->conn = LUA_NOREF; cur->numcols = numcols; cur->colnames = LUA_NOREF; cur->coltypes = LUA_NOREF; cur->sql_vm = sql_vm; cur->conn_data = conn; lua_pushvalue(L, o); cur->conn = luaL_ref(L, LUA_REGISTRYINDEX); /* create table with column names */ lua_newtable(L); for (i = 0; i < numcols;) { lua_pushstring(L, sqlite3_column_name(sql_vm, i)); lua_rawseti(L, -2, ++i); } cur->colnames = luaL_ref(L, LUA_REGISTRYINDEX); /* create table with column types */ lua_newtable(L); for (i = 0; i < numcols;) { lua_pushstring(L, sqlite3_column_decltype(sql_vm, i)); lua_rawseti(L, -2, ++i); } cur->coltypes = luaL_ref(L, LUA_REGISTRYINDEX); return 1; }
void FTNCALL sqlite3_column_name_type_c_( sqlite3_stmt **stmt, int *colidx, char *name, int len_name, char *type, int len_type ) { int rc ; char *pstr ; pstr = sqlite3_column_name(*stmt, *colidx ) ; strncpy( name, pstr, len_name ) ; name[len_name-1] = '\0' ; pstr = sqlite3_column_decltype(*stmt, *colidx ) ; strncpy( type, pstr, len_type ) ; type[len_type-1] = '\0' ; return ; }
jstring JNICALL Java_com_baidu_javalite_PrepareStmt_sqlite3_1column_1decltype( JNIEnv *env, jclass cls, jlong handle, jint column) { if (handle == 0) { throwSqliteException(env, "handle is NULL"); return 0; } sqlite3_stmt* stmt = (sqlite3_stmt*) handle; const char* cn = sqlite3_column_decltype(stmt, column); if (cn == 0) { return (*env)->NewStringUTF(env, ""); } else { return (*env)->NewStringUTF(env, cn); } }
std::string Column::getDecltype() const throw () { return sqlite3_column_decltype(m_Statement.statement.get(), m_index); }
__declspec(dllexport) const char * WINAPI sqlite3_column_decltype_interop(sqlite3_stmt *stmt, int iCol, int *plen) { const char *pval = sqlite3_column_decltype(stmt, iCol); *plen = (pval != 0) ? strlen(pval) : 0; return pval; }
char const* query::column_decltype(int idx) const { return sqlite3_column_decltype(stmt_, idx); }
void OGRGeoPackageLayer::BuildFeatureDefn( const char *pszLayerName, sqlite3_stmt *hStmt ) { m_poFeatureDefn = new OGRSQLiteFeatureDefn( pszLayerName ); SetDescription( m_poFeatureDefn->GetName() ); m_poFeatureDefn->SetGeomType(wkbNone); m_poFeatureDefn->Reference(); int nRawColumns = sqlite3_column_count( hStmt ); panFieldOrdinals = (int *) CPLMalloc( sizeof(int) * nRawColumns ); int iCol; for( iCol = 0; iCol < nRawColumns; iCol++ ) { OGRFieldDefn oField( OGRSQLiteParamsUnquote(sqlite3_column_name( hStmt, iCol )), OFTString ); // In some cases, particularly when there is a real name for // the primary key/_rowid_ column we will end up getting the // primary key column appearing twice. Ignore any repeated names. if( m_poFeatureDefn->GetFieldIndex( oField.GetNameRef() ) != -1 ) continue; if( EQUAL(oField.GetNameRef(), "FID") ) { CPLFree(m_pszFidColumn); m_pszFidColumn = CPLStrdup(oField.GetNameRef()); iFIDCol = iCol; } if( m_pszFidColumn != NULL && EQUAL(m_pszFidColumn, oField.GetNameRef())) continue; // The rowid is for internal use, not a real column. if( EQUAL(oField.GetNameRef(),"_rowid_") ) continue; // this will avoid the old geom field to appear when running something // like "select st_buffer(geom,5) as geom, * from my_layer" if( m_poFeatureDefn->GetGeomFieldCount() && EQUAL(oField.GetNameRef(), m_poFeatureDefn->GetGeomFieldDefn(0)->GetNameRef()) ) continue; int nColType = sqlite3_column_type( hStmt, iCol ); const char * pszDeclType = sqlite3_column_decltype(hStmt, iCol); // Recognize a geometry column from trying to build the geometry // Useful for OGRSQLiteSelectLayer if( nColType == SQLITE_BLOB && m_poFeatureDefn->GetGeomFieldCount() == 0 ) { const int nBytes = sqlite3_column_bytes( hStmt, iCol ); if( nBytes > 4 ) { int iGpkgSize = sqlite3_column_bytes(hStmt, iCol); const GByte* pabyGpkg = (const GByte*)sqlite3_column_blob( hStmt, iCol ); GPkgHeader oHeader; OGRGeometry* poGeom = NULL; int nSRID; if( GPkgHeaderFromWKB(pabyGpkg, &oHeader) == OGRERR_NONE ) { OGRGeomFieldDefn oGeomField(oField.GetNameRef(), wkbUnknown); /* Read the SRS */ OGRSpatialReference *poSRS = m_poDS->GetSpatialRef(oHeader.iSrsId); if ( poSRS ) { oGeomField.SetSpatialRef(poSRS); poSRS->Dereference(); } OGRwkbGeometryType eGeomType = wkbUnknown; if( pszDeclType != NULL ) { eGeomType = GPkgGeometryTypeToWKB(pszDeclType, (oHeader.iDims == 3)); if( eGeomType != wkbNone ) oGeomField.SetType( eGeomType ); } #ifdef SQLITE_HAS_COLUMN_METADATA const char* pszTableName = sqlite3_column_table_name( hStmt, iCol ); if( oGeomField.GetType() == wkbUnknown && pszTableName != NULL ) { OGRGeoPackageLayer* poLayer = (OGRGeoPackageLayer*) m_poDS->GetLayerByName(pszTableName); if( poLayer != NULL && poLayer->GetLayerDefn()->GetGeomFieldCount() > 0) { oGeomField.SetType( poLayer->GetLayerDefn()->GetGeomFieldDefn(0)->GetType() ); } } #endif m_poFeatureDefn->AddGeomFieldDefn(&oGeomField); iGeomCol = iCol; continue; } // Try also spatialite geometry blobs else if( OGRSQLiteLayer::ImportSpatiaLiteGeometry( pabyGpkg, iGpkgSize, &poGeom, &nSRID ) == OGRERR_NONE ) { OGRGeomFieldDefn oGeomField(oField.GetNameRef(), wkbUnknown); /* Read the SRS */ OGRSpatialReference *poSRS = m_poDS->GetSpatialRef(nSRID); if ( poSRS ) { oGeomField.SetSpatialRef(poSRS); poSRS->Dereference(); } delete poGeom; m_poFeatureDefn->AddGeomFieldDefn(&oGeomField); iGeomCol = iCol; continue; } } } switch( nColType ) { case SQLITE_INTEGER: if( CSLTestBoolean(CPLGetConfigOption("OGR_PROMOTE_TO_INTEGER64", "FALSE")) ) oField.SetType( OFTInteger64 ); else { GIntBig nVal = sqlite3_column_int64(hStmt, iCol); if( (GIntBig)(int)nVal == nVal ) oField.SetType( OFTInteger ); else oField.SetType( OFTInteger64 ); } break; case SQLITE_FLOAT: oField.SetType( OFTReal ); break; case SQLITE_BLOB: oField.SetType( OFTBinary ); break; default: /* leave it as OFTString */; } if (pszDeclType != NULL) { OGRFieldSubType eSubType; int nMaxWidth; OGRFieldType eFieldType = GPkgFieldToOGR(pszDeclType, eSubType, nMaxWidth); if( (int)eFieldType <= OFTMaxType ) { oField.SetType(eFieldType); oField.SetSubType(eSubType); oField.SetWidth(nMaxWidth); } } m_poFeatureDefn->AddFieldDefn( &oField ); panFieldOrdinals[m_poFeatureDefn->GetFieldCount() - 1] = iCol; } }
result_t SQLite::execute(const char *sql, int32_t sLen, obj_ptr<DBResult_base> &retVal) { if (!m_db) return CHECK_ERROR(CALL_E_INVALID_CALL); sqlite3_stmt *stmt = 0; const char *pStr1; if (sqlite3_prepare_sleep(m_db, sql, sLen, &stmt, &pStr1, SQLITE_SLEEP_TIME)) { result_t hr = CHECK_ERROR(Runtime::setError(sqlite3_errmsg(m_db))); if (stmt) sqlite3_finalize(stmt); return hr; } if (!stmt) return CHECK_ERROR(Runtime::setError("SQLite: Query was empty")); int32_t columns = sqlite3_column_count(stmt); obj_ptr<DBResult> res; if (columns > 0) { int32_t i; res = new DBResult(columns); for (i = 0; i < columns; i++) { exlib::string s = sqlite3_column_name(stmt, i); res->setField(i, s); } while (true) { int32_t r = sqlite3_step_sleep(stmt, SQLITE_SLEEP_TIME); if (r == SQLITE_ROW) { res->beginRow(); for (i = 0; i < columns; i++) { Variant v; switch (sqlite3_column_type(stmt, i)) { case SQLITE_NULL: break; case SQLITE_INTEGER: v = (int64_t) sqlite3_column_int64(stmt, i); break; case SQLITE_FLOAT: v = sqlite3_column_double(stmt, i); break; default: const char *type = sqlite3_column_decltype(stmt, i); if (type && (!qstricmp(type, "blob", 4) || !qstricmp(type, "tinyblob", 8) || !qstricmp(type, "mediumblob", 10) || !qstricmp(type, "longblob", 8) || !qstricmp(type, "binary", 6) || !qstricmp(type, "varbinary", 9) )) { const char *data = (const char *) sqlite3_column_blob(stmt, i); int32_t size = sqlite3_column_bytes(stmt, i); v = new Buffer(data, size); } else if (type && (!qstricmp(type, "datetime") || !qstricmp(type, "date") || !qstricmp(type, "time"))) { const char *data = (const char *) sqlite3_column_text(stmt, i); int32_t size = sqlite3_column_bytes(stmt, i); v.parseDate(data, size); } else { const char *data = (const char *) sqlite3_column_text(stmt, i); int32_t size = sqlite3_column_bytes(stmt, i); v = exlib::string(data, size); } break; } res->rowValue(i, v); } res->endRow(); } else if (r == SQLITE_DONE) break; else { sqlite3_finalize(stmt); return CHECK_ERROR(Runtime::setError(sqlite3_errmsg(m_db))); } } } else { int32_t r = sqlite3_step_sleep(stmt, SQLITE_SLEEP_TIME); if (r == SQLITE_DONE) res = new DBResult(0, sqlite3_changes(m_db), sqlite3_last_insert_rowid(m_db)); else { sqlite3_finalize(stmt); return CHECK_ERROR(Runtime::setError(sqlite3_errmsg(m_db))); } } sqlite3_finalize(stmt); res->freeze(); retVal = res; return 0; }