DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Add A Concat() Aggregate Function To SQLite

Snippets Manager user avatar by
Snippets Manager
·
Mar. 23, 07 · · Code Snippet
Like (0)
Save
Tweet
2.13K Views

Join the DZone community and get the full member experience.

Join For Free
I often need string concatenation to behave just like an aggregate function.
Once again I find a need to do that in SQLite, and to do that without recompiling
SQLite for every platform we distribute for... 


#include 
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1


typedef struct SCtx SCtx;
struct SCtx {
  int rowCnt;
  int charCnt;
  char *result;
};

static void concat_step(sqlite3_context* ctx, int argc, sqlite3_value**argv) {

  SCtx *p = (SCtx *) sqlite3_aggregate_context(ctx, sizeof(*p));

  char *sep = sqlite3_value_text(argv[1]);

  char *txt = sqlite3_value_text(argv[0]);

  if (p->rowCnt) {
    char *txt2 = malloc(strlen(txt) + strlen(sep) + 1);
    strcpy(txt2,sep);
    strcat(txt2,txt);
    txt = txt2;
  }

  //  printf("%d. Txt: [%s] len %d\n", p->rowCnt, txt, strlen(txt));

  int len = strlen(txt);

  if (!p->result) {
    p->result = malloc(len + 1);
    strcpy(p->result, txt);
  } else {
    p->result = realloc(p->result, strlen(p->result) + len + 1);
    strcat(p->result,txt);
  }
  //  printf ("intermediate [%s]\n", p->result);
  p->rowCnt++;
}

static void concat_final(sqlite3_context* ctx,
                         int argc,
                         sqlite3_value** argv) {

  SCtx *p = (SCtx *) sqlite3_aggregate_context(ctx, sizeof(*p));
  //  printf("Finally: %s\n", p->result);
  sqlite3_result_text(ctx,  p->result, strlen(p->result), NULL);
}

int sqlite3_extension_init(
  sqlite3 *db,
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi)
  sqlite3_create_function(db, "concat", 2, SQLITE_ANY, 0, NULL, concat_step, concat_final);
  return 0;
}


I compiled this with the following (here, ./src is the SQLite code - I used
http://www.sqlite.org/sqlite-source-3_3_13.zip). 


gcc -fpic -c agg.c -I./src
gcc -shared -Wl,-soname,libagg.so -o libagg.so agg.o


And here's how it works:


sqlite> CREATE TABLE test (animals VARCHAR, interjection VARCHAR);

sqlite> insert into test (animals, interjection) values ('lions', 'oh my');

sqlite> insert into test (animals, interjection) values ('tigers', 'oh my'); 

sqlite> insert into test (animals, interjection) values ('bears', 'oh my');

sqlite> select load_extension('./libagg.so');

sqlite> select concat(animals, ' and '), interjection from test group by interjection;

sqlite> lions and tigers and bears|oh my




See also:
1. http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions
2. http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
3. http://www.sqlite.org/capi3ref.html
SQLite

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is URL Rewriting? | Java Servlets
  • Exporting and Importing Projects in Eclipse
  • Flutter vs React Native. How to Cover All Mobile Platforms in 2022 With No Hassle
  • How to Perform Visual Regression Testing Using Cypress

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo