@@ -60,7 +60,8 @@ typedef enum {
6060 cmd_column_types ,
6161 cmd_close ,
6262 cmd_stop ,
63- cmd_insert
63+ cmd_insert ,
64+ cmd_enable_load_extension
6465} command_type ;
6566
6667typedef struct {
@@ -625,6 +626,14 @@ do_column_types(ErlNifEnv *env, sqlite3_stmt *stmt)
625626 enif_free (array );
626627 return column_types ;
627628}
629+ static ERL_NIF_TERM
630+ do_enable_extension (ErlNifEnv * env , esqlite_connection * conn , const ERL_NIF_TERM arg )
631+ {
632+ int rc = sqlite3_enable_load_extension (conn -> db , 1 );
633+ if (rc != SQLITE_OK )
634+ return make_sqlite3_error_tuple (env , rc , conn -> db );
635+ return make_atom (env , "ok" );
636+ }
628637
629638static ERL_NIF_TERM
630639do_close (ErlNifEnv * env , esqlite_connection * conn , const ERL_NIF_TERM arg )
@@ -673,6 +682,8 @@ evaluate_command(esqlite_command *cmd, esqlite_connection *conn)
673682 return do_close (cmd -> env , conn , cmd -> arg );
674683 case cmd_insert :
675684 return do_insert (cmd -> env , conn , cmd -> arg );
685+ case cmd_enable_load_extension :
686+ return do_enable_extension (cmd -> env , conn , cmd -> arg );
676687 default :
677688 return make_error_tuple (cmd -> env , "invalid_command" );
678689 }
@@ -1144,6 +1155,34 @@ esqlite_column_types(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
11441155 return push_command (env , conn , cmd );
11451156}
11461157
1158+ /*
1159+ * Enable extension loading
1160+ */
1161+ static ERL_NIF_TERM
1162+ esqlite_enable_load_extension (ErlNifEnv * env , int argc , const ERL_NIF_TERM argv [])
1163+ {
1164+ esqlite_connection * conn ;
1165+ esqlite_command * cmd = NULL ;
1166+ ErlNifPid pid ;
1167+
1168+ if (!enif_get_resource (env , argv [0 ], esqlite_connection_type , (void * * ) & conn ))
1169+ return enif_make_badarg (env );
1170+ if (!enif_is_ref (env , argv [1 ]))
1171+ return make_error_tuple (env , "invalid_ref" );
1172+ if (!enif_get_local_pid (env , argv [2 ], & pid ))
1173+ return make_error_tuple (env , "invalid_pid" );
1174+
1175+ cmd = command_create ();
1176+ if (!cmd )
1177+ return make_error_tuple (env , "command_create_failed" );
1178+
1179+ cmd -> type = cmd_enable_load_extension ;
1180+ cmd -> ref = enif_make_copy (cmd -> env , argv [1 ]);
1181+ cmd -> pid = pid ;
1182+
1183+ return push_command (env , conn , cmd );
1184+ }
1185+
11471186/*
11481187 * Close the database
11491188 */
@@ -1220,7 +1259,8 @@ static ErlNifFunc nif_funcs[] = {
12201259 {"bind" , 5 , esqlite_bind },
12211260 {"column_names" , 4 , esqlite_column_names },
12221261 {"column_types" , 4 , esqlite_column_types },
1223- {"close" , 3 , esqlite_close }
1262+ {"close" , 3 , esqlite_close },
1263+ {"enable_load_extension" , 3 , esqlite_enable_load_extension }
12241264};
12251265
12261266ERL_NIF_INIT (Elixir .Esqlite3Nif , nif_funcs , on_load , on_reload , on_upgrade , NULL );
0 commit comments