62
62
/* Note: the code below relies on there being twelve (or less) varargs, all of which are the table name */
63
const char* trigger_stmts[] = {
64
"CREATE TRIGGER \"%s_zoom_insert\"\n"
65
"BEFORE INSERT ON \"%s\"\n"
66
"FOR EACH ROW BEGIN\n"
67
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: zoom_level not specified for table in tile_matrix_metadata')\n"
68
"WHERE NOT (NEW.zoom_level IN (SELECT zoom_level FROM tile_matrix_metadata WHERE t_table_name = \"%s\"));\n"
63
const char *trigger_stmts[] = {
64
"CREATE TRIGGER \"%s_zoom_insert\"\n"
65
"BEFORE INSERT ON \"%s\"\n"
66
"FOR EACH ROW BEGIN\n"
67
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: zoom_level not specified for table in gpkg_tile_matrix')\n"
68
"WHERE NOT (NEW.zoom_level IN (SELECT zoom_level FROM gpkg_tile_matrix WHERE table_name = \"%s\"));\n"
71
71
"CREATE TRIGGER \"%s_zoom_update\"\n"
72
"BEFORE UPDATE OF zoom_level ON \"%s\"\n"
73
"FOR EACH ROW BEGIN\n"
74
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: zoom_level not specified for table in tile_matrix_metadata')\n"
75
"WHERE NOT (NEW.zoom_level IN (SELECT zoom_level FROM tile_matrix_metadata WHERE t_table_name = \"%s\"));\n"
72
"BEFORE UPDATE OF zoom_level ON \"%s\"\n"
73
"FOR EACH ROW BEGIN\n"
74
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: zoom_level not specified for table in gpkg_tile_matrix')\n"
75
"WHERE NOT (NEW.zoom_level IN (SELECT zoom_level FROM gpkg_tile_matrix WHERE table_name = \"%s\"));\n"
78
78
"CREATE TRIGGER \"%s_tile_column_insert\"\n"
79
"BEFORE INSERT ON \"%s\"\n"
80
"FOR EACH ROW BEGIN\n"
81
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_column cannot be < 0')\n"
82
"WHERE (NEW.tile_column < 0) ;\n"
83
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_column must be < matrix_width specified for table and zoom level in tile_matrix_metadata')\n"
84
"WHERE NOT (NEW.tile_column < (SELECT matrix_width FROM tile_matrix_metadata WHERE t_table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
79
"BEFORE INSERT ON \"%s\"\n"
80
"FOR EACH ROW BEGIN\n"
81
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_column cannot be < 0')\n"
82
"WHERE (NEW.tile_column < 0) ;\n"
83
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_column must be < matrix_width specified for table and zoom level in gpkg_tile_matrix')\n"
84
"WHERE NOT (NEW.tile_column < (SELECT matrix_width FROM gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
87
87
"CREATE TRIGGER \"%s_tile_column_update\"\n"
88
"BEFORE UPDATE OF tile_column ON \"%s\"\n"
89
"FOR EACH ROW BEGIN\n"
90
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_column cannot be < 0')\n"
91
"WHERE (NEW.tile_column < 0) ;\n"
92
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_column must be < matrix_width specified for table and zoom level in tile_matrix_metadata')\n"
93
"WHERE NOT (NEW.tile_column < (SELECT matrix_width FROM tile_matrix_metadata WHERE t_table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
88
"BEFORE UPDATE OF tile_column ON \"%s\"\n"
89
"FOR EACH ROW BEGIN\n"
90
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_column cannot be < 0')\n"
91
"WHERE (NEW.tile_column < 0) ;\n"
92
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_column must be < matrix_width specified for table and zoom level in gpkg_tile_matrix')\n"
93
"WHERE NOT (NEW.tile_column < (SELECT matrix_width FROM gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
96
96
"CREATE TRIGGER \"%s_tile_row_insert\"\n"
97
"BEFORE INSERT ON \"%s\"\n"
98
"FOR EACH ROW BEGIN\n"
99
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_row cannot be < 0')\n"
100
"WHERE (NEW.tile_row < 0) ;\n"
101
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_row must be < matrix_height specified for table and zoom level in tile_matrix_metadata')\n"
102
"WHERE NOT (NEW.tile_row < (SELECT matrix_height FROM tile_matrix_metadata WHERE t_table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
97
"BEFORE INSERT ON \"%s\"\n"
98
"FOR EACH ROW BEGIN\n"
99
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_row cannot be < 0')\n"
100
"WHERE (NEW.tile_row < 0) ;\n"
101
"SELECT RAISE(ABORT, 'insert on table ''%s'' violates constraint: tile_row must be < matrix_height specified for table and zoom level in gpkg_tile_matrix')\n"
102
"WHERE NOT (NEW.tile_row < (SELECT matrix_height FROM gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
105
105
"CREATE TRIGGER \"%s_tile_row_update\"\n"
106
"BEFORE UPDATE OF tile_row ON \"%s\"\n"
107
"FOR EACH ROW BEGIN\n"
108
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_row cannot be < 0')\n"
109
"WHERE (NEW.tile_row < 0) ;\n"
110
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_row must be < matrix_height specified for table and zoom level in tile_matrix_metadata')\n"
111
"WHERE NOT (NEW.tile_row < (SELECT matrix_height FROM tile_matrix_metadata WHERE t_table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
106
"BEFORE UPDATE OF tile_row ON \"%s\"\n"
107
"FOR EACH ROW BEGIN\n"
108
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_row cannot be < 0')\n"
109
"WHERE (NEW.tile_row < 0) ;\n"
110
"SELECT RAISE(ABORT, 'update on table ''%s'' violates constraint: tile_row must be < matrix_height specified for table and zoom level in gpkg_tile_matrix')\n"
111
"WHERE NOT (NEW.tile_row < (SELECT matrix_height FROM gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = NEW.zoom_level));\n"
117
117
if (sqlite3_value_type (argv[0]) != SQLITE_TEXT)
119
sqlite3_result_error(context, "gpkgAddTileTriggers() error: argument 1 [table] is not of the String type", -1);
119
sqlite3_result_error (context,
120
"gpkgAddTileTriggers() error: argument 1 [table] is not of the String type",
122
124
table = sqlite3_value_text (argv[0]);
124
126
for (i = 0; trigger_stmts[i] != NULL; ++i)
126
sql_stmt = sqlite3_mprintf(trigger_stmts[i], table, table, table, table, table, table, table, table, table, table, table, table);
127
sqlite = sqlite3_context_db_handle (context);
128
ret = sqlite3_exec (sqlite, sql_stmt, NULL, NULL, &errMsg);
129
sqlite3_free(sql_stmt);
130
if (ret != SQLITE_OK)
132
sqlite3_result_error(context, errMsg, -1);
133
sqlite3_free(errMsg);
129
sqlite3_mprintf (trigger_stmts[i], table, table, table, table,
130
table, table, table, table, table, table, table,
132
sqlite = sqlite3_context_db_handle (context);
133
ret = sqlite3_exec (sqlite, sql_stmt, NULL, NULL, &errMsg);
134
sqlite3_free (sql_stmt);
135
if (ret != SQLITE_OK)
137
sqlite3_result_error (context, errMsg, -1);
138
sqlite3_free (errMsg);