In one of my applications I have the need to know what is the declared default value for any given column. I have patched my SQLite copy but I think this would be something worth putting in the official code. I'm attaching the changes, which are very simple.
Best, - Cesar
--- a/sqlite3.c +++ b/sqlite3.c @@ -4712,7 +4712,8 @@ SQLITE_API int sqlite3_table_column_metadata( char const **pzCollSeq, /* OUTPUT: Collation sequence name */ int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ + int *pAutoinc, /* OUTPUT: True if column is auto-increment */ + char const **pzDefault /* OUTPUT: Declared default value */ ); /* @@ -82517,7 +82518,7 @@ struct sqlite3_api_routines { void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); char * (*snprintf)(int,char*,const char*,...); int (*step)(sqlite3_stmt*); - int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*); + int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*,char const **); void (*thread_cleanup)(void); int (*total_changes)(sqlite3*); void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); @@ -104528,7 +104529,8 @@ SQLITE_API int sqlite3_table_column_metadata( char const **pzCollSeq, /* OUTPUT: Collation sequence name */ int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ + int *pAutoinc, /* OUTPUT: True if column is auto-increment */ + char const **pzDefault /* OUTPUT: Declared default value */ ){ int rc; char *zErrMsg = 0; @@ -104538,6 +104540,7 @@ SQLITE_API int sqlite3_table_column_metadata( char const *zDataType = 0; char const *zCollSeq = 0; + char const *zDefault = 0; int notnull = 0; int primarykey = 0; int autoinc = 0; @@ -104589,6 +104592,7 @@ SQLITE_API int sqlite3_table_column_metadata( if( pCol ){ zDataType = pCol->zType; zCollSeq = pCol->zColl; + zDefault = pCol->zDflt; notnull = pCol->notNull!=0; primarykey = pCol->isPrimKey!=0; autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; @@ -104609,6 +104613,7 @@ error_out: */ if( pzDataType ) *pzDataType = zDataType; if( pzCollSeq ) *pzCollSeq = zCollSeq; + if( pzDefault ) *pzDefault = zDefault; if( pNotNull ) *pNotNull = notnull; if( pPrimaryKey ) *pPrimaryKey = primarykey; if( pAutoinc ) *pAutoinc = autoinc; diff --git a/c/util/sqlite/sqlite3.h b/c/util/sqlite/sqlite3.h index 3d02c7c..63be8c4 100644 --- a/sqlite3.h +++ b/sqlite3.h @@ -4176,7 +4176,8 @@ SQLITE_API int sqlite3_table_column_metadata( char const **pzCollSeq, /* OUTPUT: Collation sequence name */ int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ + int *pAutoinc, /* OUTPUT: True if column is auto-increment */ + char const **pzDefault /* OUTPUT: Declared default value */ ); /*
_______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users