Well, first you imagine you're back at the office in Santa Clara or Redmond
in the early 1990's.
Then take a belt of whisky, cross your eyes, and paste from doc to
clipboard.... a few edits and voila:

#include "sqlite3ext.h"
#include <string.h>
SQLITE_EXTENSION_INIT1
static struct metadata {
  char const *zDataType; /* OUTPUT: Declared data type */
  char const *zCollSeq; /* OUTPUT: Collation sequence name */
  int NotNull; /* OUTPUT: True if NOT NULL constraint exists */
  int PrimaryKey; /* OUTPUT: True if column part of PK */
  int Autoinc;/* OUTPUT: True if column is auto-increment */
} md;
static void initmd(sqlite3_context *context, int argc, sqlite3_value
**argv) {
  sqlite3_table_column_metadata(
    sqlite3_context_db_handle(context), /* Connection handle */
    0, /* Database name or NULL */
    (char const*)sqlite3_value_text(argv[0]), /* Table name */
    (char const*)sqlite3_value_text(argv[1]), /* Column name */
    &md.zDataType, /* OUTPUT: Declared data type */
    &md.zCollSeq, /* OUTPUT: Collation sequence name */
    &md.NotNull, /* OUTPUT: True if NOT NULL constraint exists */
    &md.PrimaryKey, /* OUTPUT: True if column part of PK */
    &md.Autoinc/* OUTPUT: True if column is auto-increment */
    );
}
static void collseq(sqlite3_context *context, int argc, sqlite3_value
**argv) {
  initmd(context,argc,argv);
  if (!md.zCollSeq) return;
  sqlite3_result_text(context, md.zCollSeq, strlen(md.zCollSeq),
SQLITE_TRANSIENT);
}
static void autoinc(sqlite3_context *context, int argc, sqlite3_value
**argv) {
  initmd(context,argc,argv);
  sqlite3_result_int(context, md.Autoinc);
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_colmetadata_init(sqlite3 *db, char **pzErrMsg, const
sqlite3_api_routines *pApi) {
  SQLITE_EXTENSION_INIT2(pApi);
  int rc = sqlite3_create_function(db, "collseq", 2, SQLITE_UTF8, 0,
collseq, 0, 0);
  if (SQLITE_OK == rc) sqlite3_create_function(db, "autoinc", 2,
SQLITE_UTF8, 0, autoinc, 0, 0);
  return rc;
}

Linux box compile is something like this:

gcc -I<sqlite build dir> -fPIC -lm -shared colmetadata.c -o colmetadata.so

Then test it out:

sqlite> .load colmetadata.so
sqlite> SELECT
*,collseq('sqlite_master',name)collseq,autoinc('sqlite_master',name)autoinc
FROM pragma_table_info('sqlite_master');
cid,name,type,notnull,dflt_value,pk,collseq,autoinc
0,type,text,0,,0,BINARY,0
1,name,text,0,,0,BINARY,0
2,tbl_name,text,0,,0,BINARY,0
3,rootpage,integer,0,,0,BINARY,0
4,sql,text,0,,0,BINARY,0

https://www.sqlite.org/c3ref/table_column_metadata.html

So armed with the above document and newly exposed pk,collseq, and autoinc
info you can deduce the rowid aliases.
Feel free to add more functions and clean up the error handling to suit
your needs.










On Fri, Nov 24, 2017 at 5:57 PM, Peter Halasz <pe...@becauseofgames.com>
wrote:

> > sqlite> .load column-meta-data.so
>
> Sorry I'm at a loss to find this extension? Google gives me nothing related
> to SQLite for "isRowId", "column-meta-data.so", "column-meta-data.c", etc.
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to