~ubuntu-branches/ubuntu/wily/spatialite/wily-proposed

« back to all changes in this revision

Viewing changes to src/geopackage/gpkg_add_tile_triggers.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-07-14 11:57:46 UTC
  • mfrom: (16.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150714115746-e2iljfmb5sq7o5hh
Tags: 4.3.0-1
Move from experimental to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
The Initial Developer of the Original Code is Brad Hards (bradh@frogmouth.net)
20
20
 
21
 
Portions created by the Initial Developer are Copyright (C) 2012
 
21
Portions created by the Initial Developer are Copyright (C) 2012-2015
22
22
the Initial Developer. All Rights Reserved.
23
23
 
24
24
Contributor(s):
42
42
#include "geopackage_internal.h"
43
43
 
44
44
#ifdef ENABLE_GEOPACKAGE
45
 
GEOPACKAGE_DECLARE void
46
 
fnct_gpkgAddTileTriggers (sqlite3_context * context, int argc __attribute__ ((unused)),
47
 
                          sqlite3_value ** argv)
 
45
GEOPACKAGE_PRIVATE void
 
46
fnct_gpkgAddTileTriggers (sqlite3_context * context, int argc
 
47
                          __attribute__ ((unused)), sqlite3_value ** argv)
48
48
{
49
49
/* SQL function:
50
50
/ gpkgAddTileTriggers(table)
60
60
    int ret = 0;
61
61
    int i = 0;
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"
69
 
        "END",
70
 
        
 
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"
 
69
            "END",
 
70
 
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"
76
 
        "END",
 
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"
 
76
            "END",
77
77
 
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"
85
 
        "END",
 
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"
 
85
            "END",
86
86
 
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"
94
 
        "END",
95
 
        
 
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"
 
94
            "END",
 
95
 
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"
103
 
        "END",
104
 
        
 
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"
 
103
            "END",
 
104
 
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"
112
 
        "END",
 
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"
 
112
            "END",
113
113
 
114
114
        NULL
115
115
    };
116
 
    
 
116
 
117
117
    if (sqlite3_value_type (argv[0]) != SQLITE_TEXT)
118
 
    {
119
 
        sqlite3_result_error(context, "gpkgAddTileTriggers() error: argument 1 [table] is not of the String type", -1);
120
 
        return;
121
 
    }
 
118
      {
 
119
          sqlite3_result_error (context,
 
120
                                "gpkgAddTileTriggers() error: argument 1 [table] is not of the String type",
 
121
                                -1);
 
122
          return;
 
123
      }
122
124
    table = sqlite3_value_text (argv[0]);
123
125
 
124
126
    for (i = 0; trigger_stmts[i] != NULL; ++i)
125
 
    {
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)
131
 
        {
132
 
            sqlite3_result_error(context, errMsg, -1);
133
 
            sqlite3_free(errMsg);
134
 
            return;
135
 
        }
136
 
    }    
 
127
      {
 
128
          sql_stmt =
 
129
              sqlite3_mprintf (trigger_stmts[i], table, table, table, table,
 
130
                               table, table, table, table, table, table, table,
 
131
                               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)
 
136
            {
 
137
                sqlite3_result_error (context, errMsg, -1);
 
138
                sqlite3_free (errMsg);
 
139
                return;
 
140
            }
 
141
      }
137
142
}
138
143
#endif