** SQL function: shell_add_schema(S,X) ** ** Add the schema name X to the CREATE statement in S and return the result. ** Examples: ** ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); ** ** Also works on ** ** CREATE INDEX ** CREATE UNIQUE INDEX ** CREATE VIEW ** CREATE TRIGGER ** CREATE VIRTUAL TABLE ** ** This UDF is used by the .schema command to insert the schema name of
| 969 | ** attached databases into the middle of the sqlite_schema.sql field. |
| 970 | */ |
| 971 | static void shellAddSchemaName( |
| 972 | sqlite3_context *pCtx, |
| 973 | int nVal, |
| 974 | sqlite3_value **apVal |
| 975 | ){ |
| 976 | static const char *aPrefix[] = { |
| 977 | "TABLE", |
| 978 | "INDEX", |
| 979 | "UNIQUE INDEX", |
| 980 | "VIEW", |
| 981 | "TRIGGER", |
| 982 | "VIRTUAL TABLE" |
| 983 | }; |
| 984 | int i = 0; |
| 985 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 986 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
| 987 | const char *zName = (const char*)sqlite3_value_text(apVal[2]); |
| 988 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 989 | UNUSED_PARAMETER(nVal); |
| 990 | if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ |
| 991 | for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){ |
| 992 | int n = strlen30(aPrefix[i]); |
| 993 | if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
| 994 | char *z = 0; |
| 995 | char *zFake = 0; |
| 996 | if( zSchema ){ |
| 997 | char cQuote = quoteChar(zSchema); |
| 998 | if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){ |
| 999 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 1000 | }else{ |
| 1001 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 1002 | } |
| 1003 | } |
| 1004 | if( zName |
| 1005 | && aPrefix[i][0]=='V' |
| 1006 | && (zFake = shellFakeSchema(db, zSchema, zName))!=0 |
| 1007 | ){ |
| 1008 | if( z==0 ){ |
| 1009 | z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake); |
| 1010 | }else{ |
| 1011 | z = sqlite3_mprintf("%z\n/* %s */", z, zFake); |
| 1012 | } |
| 1013 | free(zFake); |
| 1014 | } |
| 1015 | if( z ){ |
| 1016 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 1017 | return; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | sqlite3_result_value(pCtx, apVal[0]); |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | ** The source code for several run-time loadable extensions is inserted |
nothing calls this directly
no test coverage detected