UnQL

Check-in [c9b9b54e59]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Get INSERT working. Improved error messages.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c9b9b54e59a328ca5f9f2d67a6574f5d261c8277
User & Date: drh 2011-06-14 18:47:36
Context
2011-06-21
17:10
Begin adding code to perform queries. Compiles and links, but probably does not work. This is code from a few days ago that I am just now coming back to work on again. check-in: fb25f79d0f user: drh tags: trunk
2011-06-14
18:47
Get INSERT working. Improved error messages. check-in: c9b9b54e59 user: drh tags: trunk
17:16
Begin adding error message handling logic and code to implement statements. Incremental check-in because I have to stop to work on something else. check-in: 6299c43154 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/conn.c.

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/*
** Change the error message and error code.
*/
PRIVATE void xjd1Error(xjd1 *pConn, int errCode, const char *zFormat, ...){
  va_list ap;
  if( pConn==0 ) return;
  if( pConn->appendErr ){
    xjd1StringTruncate(&pConn->errMsg);
  }else if( xjd1StringLen(&pConn->errMsg) ){
    xjd1StringAppend(&pConn->errMsg, "\n", 1);
  }
  pConn->errCode = errCode;
  va_start(ap, zFormat);
  xjd1StringVAppendF(&pConn->errMsg, zFormat, ap);
  va_end(ap);
}







|









96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/*
** Change the error message and error code.
*/
PRIVATE void xjd1Error(xjd1 *pConn, int errCode, const char *zFormat, ...){
  va_list ap;
  if( pConn==0 ) return;
  if( !pConn->appendErr ){
    xjd1StringTruncate(&pConn->errMsg);
  }else if( xjd1StringLen(&pConn->errMsg) ){
    xjd1StringAppend(&pConn->errMsg, "\n", 1);
  }
  pConn->errCode = errCode;
  va_start(ap, zFormat);
  xjd1StringVAppendF(&pConn->errMsg, zFormat, ap);
  va_end(ap);
}

Changes to src/json.c.

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
      case XJD1_STRUCT: {
        char cSep = '{';
        JsonStructElem *pElem;
        for(pElem=p->u.pStruct; pElem; pElem=pElem->pNext){
          xjd1StringAppend(pOut, &cSep, 1);
          cSep = ',';
          renderString(pOut, pElem->zLabel);
          xjd1StringAppend(pOut, ",", 1);
          xjd1JsonRender(pOut, pElem->pValue);
        }
        xjd1StringAppend(pOut, "}", 1);
        break;
      }
    }
  }







|







118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
      case XJD1_STRUCT: {
        char cSep = '{';
        JsonStructElem *pElem;
        for(pElem=p->u.pStruct; pElem; pElem=pElem->pNext){
          xjd1StringAppend(pOut, &cSep, 1);
          cSep = ',';
          renderString(pOut, pElem->zLabel);
          xjd1StringAppend(pOut, ":", 1);
          xjd1JsonRender(pOut, pElem->pValue);
        }
        xjd1StringAppend(pOut, "}", 1);
        break;
      }
    }
  }

Changes to src/shell.c.

148
149
150
151
152
153
154

155
156
157
158
159
160
161
162



163
164
165
166
167
168
169
170
171
172
173
    nStmt += n;
    if( xjd1_complete(zStmt) ){
      xjd1_stmt *pStmt;
      rc = xjd1_stmt_new(pDb, zStmt, &pStmt, &n);
      if( rc!=XJD1_OK ){
        fprintf(stderr, "ERROR: %s\n", xjd1_errmsg(pDb));
      }else{

        char *zTrace = xjd1_stmt_debug_listing(pStmt);
        if( zTrace ) printf("%s", zTrace);
        free(zTrace);
        while( xjd1_stmt_step(pStmt)==XJD1_ROW ){
          const char *zVal;
          xjd1_stmt_value(pStmt, &zVal);
          printf("%s\n", zVal);
        }



      }
      xjd1_stmt_delete(pStmt);
      zPrompt = "xjd1> ";
      nStmt = 0;
    }else{
      zPrompt = " ...> ";
    }
  }
  xjd1_close(pDb);
  return 0;
}







>



|




>
>
>











148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    nStmt += n;
    if( xjd1_complete(zStmt) ){
      xjd1_stmt *pStmt;
      rc = xjd1_stmt_new(pDb, zStmt, &pStmt, &n);
      if( rc!=XJD1_OK ){
        fprintf(stderr, "ERROR: %s\n", xjd1_errmsg(pDb));
      }else{
        int res;
        char *zTrace = xjd1_stmt_debug_listing(pStmt);
        if( zTrace ) printf("%s", zTrace);
        free(zTrace);
        while( (res = xjd1_stmt_step(pStmt))==XJD1_ROW ){
          const char *zVal;
          xjd1_stmt_value(pStmt, &zVal);
          printf("%s\n", zVal);
        }
        if( res!=XJD1_DONE ){
          fprintf(stderr, "ERROR: %s\n", xjd1_errmsg(pDb));
        }
      }
      xjd1_stmt_delete(pStmt);
      zPrompt = "xjd1> ";
      nStmt = 0;
    }else{
      zPrompt = " ...> ";
    }
  }
  xjd1_close(pDb);
  return 0;
}

Changes to src/stmt.c.

78
79
80
81
82
83
84

85
86
87
88





89
90
91
92
93
94

95
96
97
98






































99
100
101
102
103
104
105
  if( pStmt==0 ) return rc;
  pCmd = pStmt->pCmd;
  if( pCmd==0 ) return rc;
  switch( pCmd->eCmdType ){
    case TK_CREATETABLE: {
      char *zSql;
      int res;

      zSql = sqlite3_mprintf("CREATE TABLE %s \"%!.*w\"(x)",
                 pCmd->u.crtab.ifExists ? "IF NOT EXISTS" : "",
                 pCmd->u.crtab.name.n, pCmd->u.crtab.name.z);
      res = sqlite3_exec(pStmt->pConn->db, zSql, 0, 0, 0);





      sqlite3_free(zSql);
      break;
    }
    case TK_DROPTABLE: {
      char *zSql;
      int res;

      zSql = sqlite3_mprintf("DROP TABLE %s \"%!.*w\"",
                 pCmd->u.crtab.ifExists ? "IF EXISTS" : "",
                 pCmd->u.crtab.name.n, pCmd->u.crtab.name.z);
      res = sqlite3_exec(pStmt->pConn->db, zSql, 0, 0, 0);






































      sqlite3_free(zSql);
      break;
    }
  }
  return rc;
}








>



|
>
>
>
>
>






>



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  if( pStmt==0 ) return rc;
  pCmd = pStmt->pCmd;
  if( pCmd==0 ) return rc;
  switch( pCmd->eCmdType ){
    case TK_CREATETABLE: {
      char *zSql;
      int res;
      char *zErr = 0;
      zSql = sqlite3_mprintf("CREATE TABLE %s \"%!.*w\"(x)",
                 pCmd->u.crtab.ifExists ? "IF NOT EXISTS" : "",
                 pCmd->u.crtab.name.n, pCmd->u.crtab.name.z);
      res = sqlite3_exec(pStmt->pConn->db, zSql, 0, 0, &zErr);
      if( zErr ){
        xjd1Error(pStmt->pConn, XJD1_ERROR, "%s", zErr);
        sqlite3_free(zErr);
        rc = XJD1_ERROR;
      }
      sqlite3_free(zSql);
      break;
    }
    case TK_DROPTABLE: {
      char *zSql;
      int res;
      char *zErr = 0;
      zSql = sqlite3_mprintf("DROP TABLE %s \"%!.*w\"",
                 pCmd->u.crtab.ifExists ? "IF EXISTS" : "",
                 pCmd->u.crtab.name.n, pCmd->u.crtab.name.z);
      res = sqlite3_exec(pStmt->pConn->db, zSql, 0, 0, &zErr);
      if( zErr ){
        xjd1Error(pStmt->pConn, XJD1_ERROR, "%s", zErr);
        sqlite3_free(zErr);
        rc = XJD1_ERROR;
      }
      sqlite3_free(zSql);
      break;
    }
    case TK_INSERT: {
      JsonNode *pNode;
      String json;
      int res;
      char *zErr;
      char *zSql;
      if( pCmd->u.ins.pQuery ){
        xjd1Error(pStmt->pConn, XJD1_ERROR, 
                 "INSERT INTO ... SELECT not yet implemented");
        break;
      }
      pNode = xjd1JsonParse(pCmd->u.ins.jvalue.z);
      if( pNode==0 ){
        xjd1Error(pStmt->pConn, XJD1_ERROR,
                  "malformed JSON");
        break;
      }
      xjd1StringInit(&json,0,0);
      xjd1JsonRender(&json, pNode);
      xjd1JsonFree(pNode);
      zSql = sqlite3_mprintf("INSERT INTO \"%.*w\" VALUES('%q')",
                  pCmd->u.ins.name.n, pCmd->u.ins.name.z,
                  xjd1StringText(&json));
      xjd1StringClear(&json);
      res = sqlite3_exec(pStmt->pConn->db, zSql, 0, 0, &zErr);
      if( zErr ){
        xjd1Error(pStmt->pConn, XJD1_ERROR, "%s", zErr);
        sqlite3_free(zErr);
        rc = XJD1_ERROR;
      }
      sqlite3_free(zSql);
      break;
    }
  }
  return rc;
}

Changes to src/tokenize.c.

408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  *pN = 0;
  pEngine = xjd1ParserAlloc(malloc);
  if( pEngine==0 ) return XJD1_NOMEM;
  memset(&sParse, 0, sizeof(sParse));
  sParse.pConn = pConn;
  sParse.pPool = &pStmt->sPool;
  xjd1StringInit(&sParse.errMsg, &pStmt->sPool, 0);
  while( zCode[i] && sParse.errCode ){
    assert( i>=0 );
    sParse.sTok.z = &zCode[i];
    sParse.sTok.n = xjd1GetToken((unsigned char*)&zCode[i], &tokenType);
    i += sParse.sTok.n;
    switch( tokenType ){
      case TK_SPACE: {
        break;







|







408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  *pN = 0;
  pEngine = xjd1ParserAlloc(malloc);
  if( pEngine==0 ) return XJD1_NOMEM;
  memset(&sParse, 0, sizeof(sParse));
  sParse.pConn = pConn;
  sParse.pPool = &pStmt->sPool;
  xjd1StringInit(&sParse.errMsg, &pStmt->sPool, 0);
  while( zCode[i] && sParse.errCode==0 ){
    assert( i>=0 );
    sParse.sTok.z = &zCode[i];
    sParse.sTok.n = xjd1GetToken((unsigned char*)&zCode[i], &tokenType);
    i += sParse.sTok.n;
    switch( tokenType ){
      case TK_SPACE: {
        break;