86 | | '''$emdb''' uses the {{{emSQL}}} helper module to construct common SQL statements. It can be found in www/js/modules/sql.js. |
87 | | |
88 | | Examples: |
89 | | {{{ |
90 | | #!js |
91 | | |
92 | | // Schema definition |
93 | | var schema = { |
94 | | id: { |
95 | | type: 'id' |
96 | | }, |
97 | | name: { |
98 | | type: 'string', |
99 | | label: 'Name', |
100 | | notnull: true |
101 | | }, |
102 | | number: { |
103 | | type: 'integer', |
104 | | }, |
105 | | }; |
106 | | |
107 | | // Create an emSQL helper for this schema |
108 | | var table = emSQL.Table('testtable', schema); |
109 | | |
110 | | // Produce SQL to create the table |
111 | | // sql == 'CREATE IF NOT EXISTS "testtable" (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,number INTEGER)' |
112 | | var sql = table.create(); |
113 | | |
114 | | // Produce SQL to insert data |
115 | | // => produces a tuple: an SQL string with placeholders and an array with values (for use with db.executeSql) |
116 | | // sql == ['INSERT INTO "testtable" (name,number) VALUES (?,?)', ['testing',4]] |
117 | | sql = table.insert({name: 'testing', number: 4}); |
118 | | |
119 | | // Produce SQL to select data |
120 | | // sql == 'SELECT testtable.id,testtable.name FROM "testtable" WHERE number>2' |
121 | | sql = table.select(['id', 'name'], 'number>2'); |
122 | | |
123 | | // Produce SQL to drop the table |
124 | | // sql == 'DROP TABLE IF EXISTS "testtable"' |
125 | | sql = table.drop(); |
126 | | |
127 | | }}} |
| 86 | '''$emDB''' uses the {{{emSQL}}} service to construct common SQL statements. It can be found in www/services/sql.js. |