Merge remote-tracking branch 'origin/upstream_release_3_0_13' into tr-integ-fr-3...
[freeradius.git] / src / modules / rlm_sql / drivers / rlm_sql_sqlite / rlm_sql_sqlite.c
index e2deae9..7797710 100644 (file)
@@ -45,7 +45,7 @@ RCSID("$Id$")
 #endif
 
 #ifndef HAVE_SQLITE3_INT64
-typedef sqlite3_int64 sqlite_int64
+typedef sqlite_int64 sqlite3_int64;
 #endif
 
 typedef struct rlm_sql_sqlite_conn {
@@ -62,7 +62,7 @@ typedef struct rlm_sql_sqlite_config {
 static const CONF_PARSER driver_config[] = {
        { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, rlm_sql_sqlite_config_t, filename), NULL },
        { "busy_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_sql_sqlite_config_t, busy_timeout), "200" },
-       {NULL, -1, 0, NULL, NULL}
+       CONF_PARSER_TERMINATOR
 };
 
 /** Convert an sqlite status code to an sql_rcode_t
@@ -217,13 +217,13 @@ static void sql_print_error(sqlite3 *db, int status, char const *fmt, ...)
         */
        if ((status != SQLITE_OK) && (status != hstatus)) {
 #ifdef HAVE_SQLITE3_ERRSTR
-               ERROR("rlm_sql_sqlite: %s: code 0x%x (%i): %s", p, status, status, sqlite3_errstr(status));
+               ERROR("rlm_sql_sqlite: %s: Code 0x%04x (%i): %s", p, status, status, sqlite3_errstr(status));
 #else
-               ERROR("rlm_sql_sqlite: %s: code 0x%x (%i)", p, status, status);
+               ERROR("rlm_sql_sqlite: %s: Code 0x%04x (%i)", p, status, status);
 #endif
        }
 
-       if (hstatus != SQLITE_OK) ERROR("rlm_sql_sqlite: %s: code 0x%x (%i): %s",
+       if (hstatus != SQLITE_OK) ERROR("rlm_sql_sqlite: %s: Code 0x%04x (%i): %s",
                                        p, hstatus, hstatus, sqlite3_errmsg(db));
 }
 
@@ -231,7 +231,7 @@ static void sql_print_error(sqlite3 *db, int status, char const *fmt, ...)
 static int sql_loadfile(TALLOC_CTX *ctx, sqlite3 *db, char const *filename)
 {
        ssize_t         len;
-       int             line = 0;
+       int             statement_cnt = 0;
        char            *buffer;
        char            *p, *q, *s;
        int             cl;
@@ -307,7 +307,7 @@ static int sql_loadfile(TALLOC_CTX *ctx, sqlite3 *db, char const *filename)
                        if ((*p != 0x0a) && (*p != 0x0d) && (*p != '\t')) break;
                        cl = 1;
                } else {
-                       cl = fr_utf8_char((uint8_t *) p);
+                       cl = fr_utf8_char((uint8_t *) p, -1);
                        if (!cl) break;
                }
        }
@@ -325,7 +325,7 @@ static int sql_loadfile(TALLOC_CTX *ctx, sqlite3 *db, char const *filename)
        while ((q = strchr(p, ';'))) {
                if (q[1] != '\n') {
                        p = q + 1;
-                       line++;
+                       statement_cnt++;
                        continue;
                }
 
@@ -334,18 +334,18 @@ static int sql_loadfile(TALLOC_CTX *ctx, sqlite3 *db, char const *filename)
 #ifdef HAVE_SQLITE3_PREPARE_V2
                status = sqlite3_prepare_v2(db, s, len, &statement, &z_tail);
 #else
-               status = sqlite3_prepare(db, s, len, &>statement, &z_tail);
+               status = sqlite3_prepare(db, s, len, &statement, &z_tail);
 #endif
 
                if (sql_check_error(db, status) != RLM_SQL_OK) {
-                       sql_print_error(db, status, "[%i] Error preparing statement", line);
+                       sql_print_error(db, status, "Failed preparing statement %i", statement_cnt);
                        talloc_free(buffer);
                        return -1;
                }
 
                status = sqlite3_step(statement);
                if (sql_check_error(db, status) != RLM_SQL_OK) {
-                       sql_print_error(db, status, "[%i] Error executing statement", line);
+                       sql_print_error(db, status, "Failed executing statement %i", statement_cnt);
                        sqlite3_finalize(statement);
                        talloc_free(buffer);
                        return -1;
@@ -353,12 +353,12 @@ static int sql_loadfile(TALLOC_CTX *ctx, sqlite3 *db, char const *filename)
 
                status = sqlite3_finalize(statement);
                if (sql_check_error(db, status) != RLM_SQL_OK) {
-                       sql_print_error(db, status, "[%i] Error finalizing statement", line);
+                       sql_print_error(db, status, "Failed finalizing statement %i", statement_cnt);
                        talloc_free(buffer);
                        return -1;
                }
 
-               line++;
+               statement_cnt++;
                p = s = q + 1;
        }
 
@@ -536,7 +536,9 @@ static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *c
        INFO("rlm_sql_sqlite: Opening SQLite database \"%s\"", driver->filename);
 #ifdef HAVE_SQLITE3_OPEN_V2
        status = sqlite3_open_v2(driver->filename, &(conn->db), SQLITE_OPEN_READWRITE | SQLITE_OPEN_NOMUTEX, NULL);
+       sqlite3_busy_timeout( conn->db, 200); /*wait up to 200 ms for db locks*/
 #else
+       
        status = sqlite3_open(driver->filename, &(conn->db));
 #endif
 
@@ -663,6 +665,8 @@ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *con
 
        char **row;
 
+       TALLOC_FREE(handle->row);
+
        /*
         *      Executes the SQLite query and interates over the results
         */
@@ -676,9 +680,7 @@ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *con
        /*
         *      No more rows to process (were done)
         */
-       if (status == SQLITE_DONE) {
-               return 1;
-       }
+       if (status == SQLITE_DONE) return RLM_SQL_NO_MORE_ROWS;
 
        /*
         *      We only need to do this once per result set, because
@@ -689,11 +691,6 @@ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *con
                if (conn->col_count == 0) return RLM_SQL_ERROR;
        }
 
-       /*
-        *      Free the previous result (also gets called on finish_query)
-        */
-       talloc_free(handle->row);
-
        MEM(row = handle->row = talloc_zero_array(handle->conn, char *, conn->col_count + 1));
 
        for (i = 0; i < conn->col_count; i++) {
@@ -735,7 +732,7 @@ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *con
                }
        }
 
-       return 0;
+       return RLM_SQL_OK;
 }
 
 static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
@@ -757,7 +754,7 @@ static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_conf
         *      It's just the last error that occurred processing the
         *      statement.
         */
-       return 0;
+       return RLM_SQL_OK;
 }
 
 /** Retrieves any errors associated with the connection handle
@@ -798,9 +795,7 @@ static int sql_affected_rows(rlm_sql_handle_t *handle,
 {
        rlm_sql_sqlite_conn_t *conn = handle->conn;
 
-       if (conn->db) {
-               return sqlite3_changes(conn->db);
-       }
+       if (conn->db) return sqlite3_changes(conn->db);
 
        return -1;
 }