MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / shellAddSchemaName

Function shellAddSchemaName

modules/dasSQLITE/sqlite/shell.c:1102–1154  ·  view source on GitHub ↗

** 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

Source from the content-addressed store, hash-verified

1100** attached databases into the middle of the sqlite_schema.sql field.
1101*/
1102static void shellAddSchemaName(
1103 sqlite3_context *pCtx,
1104 int nVal,
1105 sqlite3_value **apVal
1106){
1107 static const char *aPrefix[] = {
1108 "TABLE",
1109 "INDEX",
1110 "UNIQUE INDEX",
1111 "VIEW",
1112 "TRIGGER",
1113 "VIRTUAL TABLE"
1114 };
1115 int i = 0;
1116 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1117 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1118 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1119 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1120 UNUSED_PARAMETER(nVal);
1121 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
1122 for(i=0; i<ArraySize(aPrefix); i++){
1123 int n = strlen30(aPrefix[i]);
1124 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1125 char *z = 0;
1126 char *zFake = 0;
1127 if( zSchema ){
1128 char cQuote = quoteChar(zSchema);
1129 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1130 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1131 }else{
1132 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1133 }
1134 }
1135 if( zName
1136 && aPrefix[i][0]=='V'
1137 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1138 ){
1139 if( z==0 ){
1140 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1141 }else{
1142 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1143 }
1144 free(zFake);
1145 }
1146 if( z ){
1147 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1148 return;
1149 }
1150 }
1151 }
1152 }
1153 sqlite3_result_value(pCtx, apVal[0]);
1154}
1155
1156/*
1157** The source code for several run-time loadable extensions is inserted

Callers

nothing calls this directly

Calls 5

cli_strncmpFunction · 0.85
strlen30Function · 0.85
quoteCharFunction · 0.85
shellFakeSchemaFunction · 0.85
freeFunction · 0.50

Tested by

no test coverage detected