Artifact 96bce2d7b30c3d03fb5b7b13c345689763d47384:
File
src/admin_sql_page.c
part of check-in
[6f05b4292a]
- Added /admin/sql page. Added a 'const' to db_generic_query_view()
by
stephan on
2008-02-05 00:01:44.
0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 20 43 6f 70 79 ard Hipp.** Copy
0030: 72 69 67 68 74 20 28 63 29 20 32 30 30 38 20 53 right (c) 2008 S
0040: 74 65 70 68 61 6e 20 42 65 61 6c 0a 2a 2a 0a 2a tephan Beal.**.*
0050: 2a 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 * This program i
0060: 73 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3b s free software;
0070: 20 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 you can redistr
0080: 69 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a ibute it and/or.
0090: 2a 2a 20 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 ** modify it und
00a0: 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 er the terms of
00b0: 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 the GNU General
00c0: 50 75 62 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 Public.** Licens
00d0: 65 20 61 73 20 70 75 62 6c 69 73 68 65 64 20 62 e as published b
00e0: 79 20 74 68 65 20 46 72 65 65 20 53 6f 66 74 77 y the Free Softw
00f0: 61 72 65 20 46 6f 75 6e 64 61 74 69 6f 6e 3b 20 are Foundation;
0100: 65 69 74 68 65 72 0a 2a 2a 20 76 65 72 73 69 6f either.** versio
0110: 6e 20 32 20 6f 66 20 74 68 65 20 4c 69 63 65 6e n 2 of the Licen
0120: 73 65 2c 20 6f 72 20 28 61 74 20 79 6f 75 72 20 se, or (at your
0130: 6f 70 74 69 6f 6e 29 20 61 6e 79 20 6c 61 74 65 option) any late
0140: 72 20 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a r version..**.**
0150: 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 This program is
0160: 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 distributed in
0170: 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 69 74 the hope that it
0180: 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2c will be useful,
0190: 0a 2a 2a 20 62 75 74 20 57 49 54 48 4f 55 54 20 .** but WITHOUT
01a0: 41 4e 59 20 57 41 52 52 41 4e 54 59 3b 20 77 69 ANY WARRANTY; wi
01b0: 74 68 6f 75 74 20 65 76 65 6e 20 74 68 65 20 69 thout even the i
01c0: 6d 70 6c 69 65 64 20 77 61 72 72 61 6e 74 79 20 mplied warranty
01d0: 6f 66 0a 2a 2a 20 4d 45 52 43 48 41 4e 54 41 42 of.** MERCHANTAB
01e0: 49 4c 49 54 59 20 6f 72 20 46 49 54 4e 45 53 53 ILITY or FITNESS
01f0: 20 46 4f 52 20 41 20 50 41 52 54 49 43 55 4c 41 FOR A PARTICULA
0200: 52 20 50 55 52 50 4f 53 45 2e 20 20 53 65 65 20 R PURPOSE. See
0210: 74 68 65 20 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 the GNU.** Gener
0220: 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 al Public Licens
0230: 65 20 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 e for more detai
0240: 6c 73 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 73 68 ls..**.** You sh
0250: 6f 75 6c 64 20 68 61 76 65 20 72 65 63 65 69 76 ould have receiv
0260: 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ed a copy of the
0270: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
0280: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 lic.** License a
0290: 6c 6f 6e 67 20 77 69 74 68 20 74 68 69 73 20 6c long with this l
02a0: 69 62 72 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 ibrary; if not,
02b0: 77 72 69 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 write to the.**
02c0: 46 72 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f Free Software Fo
02d0: 75 6e 64 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 undation, Inc.,
02e0: 35 39 20 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 59 Temple Place
02f0: 2d 20 53 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 - Suite 330,.**
0300: 42 6f 73 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 Boston, MA 0211
0310: 31 2d 31 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 1-1307, USA..**.
0320: 2a 2a 20 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 ** Author contac
0330: 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a t information:.*
0340: 2a 20 20 20 64 72 68 40 68 77 61 63 69 2e 63 6f * drh@hwaci.co
0350: 6d 0a 2a 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 m.** http://ww
0360: 77 2e 68 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f w.hwaci.com/drh/
0370: 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
0380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0390: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03c0: 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d ***.**.** Implem
03d0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
03e0: 41 64 6d 69 6e 20 53 51 4c 0a 2a 2f 0a 23 69 6e Admin SQL.*/.#in
03f0: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
0400: 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 .#include "confi
0410: 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 61 g.h".#include "a
0420: 64 6d 69 6e 5f 73 71 6c 5f 70 61 67 65 2e 68 22 dmin_sql_page.h"
0430: 0a 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 ...static void a
0440: 64 6d 69 6e 5f 73 71 6c 5f 70 61 67 65 5f 66 6f dmin_sql_page_fo
0450: 72 6d 28 29 0a 7b 0a 20 20 63 68 61 72 20 63 6f rm().{. char co
0460: 6e 73 74 20 2a 20 73 71 6c 20 3d 20 50 28 22 73 nst * sql = P("s
0470: 71 6c 22 29 3b 0a 20 20 40 20 3c 68 72 2f 3e 3c ql");. @ <hr/><
0480: 68 32 3e 53 51 4c 3a 3c 2f 68 32 3e 0a 20 20 40 h2>SQL:</h2>. @
0490: 20 3c 73 70 61 6e 20 63 6c 61 73 73 3d 27 61 63 <span class='ac
04a0: 68 74 75 6e 67 27 3e 59 6f 75 20 63 61 6e 20 65 htung'>You can e
04b0: 6e 74 65 72 20 61 72 62 69 74 72 61 72 79 20 53 nter arbitrary S
04c0: 51 4c 20 68 65 72 65 2c 20 74 6f 20 65 78 65 63 QL here, to exec
04d0: 75 74 65 0a 20 20 40 20 61 67 61 69 6e 73 74 20 ute. @ against
04e0: 74 68 65 20 72 65 70 6f 20 64 61 74 61 62 61 73 the repo databas
04f0: 65 2e 0a 20 20 40 20 57 69 74 68 20 67 72 65 61 e.. @ With grea
0500: 74 20 70 6f 77 65 72 20 63 6f 6d 65 73 20 67 72 t power comes gr
0510: 65 61 74 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 eat responsibili
0520: 74 79 2e 2e 2e 3c 2f 73 70 61 6e 3e 3c 62 72 2f ty...</span><br/
0530: 3e 0a 20 20 40 20 3c 66 6f 72 6d 20 61 63 74 69 >. @ <form acti
0540: 6f 6e 3d 27 27 20 6d 65 74 68 6f 64 3d 27 70 6f on='' method='po
0550: 73 74 27 3e 0a 20 20 40 20 3c 74 65 78 74 61 72 st'>. @ <textar
0560: 65 61 20 73 74 79 6c 65 3d 27 62 6f 72 64 65 72 ea style='border
0570: 3a 32 70 78 20 73 6f 6c 69 64 20 62 6c 61 63 6b :2px solid black
0580: 27 20 6e 61 6d 65 3d 27 73 71 6c 27 20 63 6f 6c ' name='sql' col
0590: 73 3d 27 38 30 27 20 72 6f 77 73 3d 27 35 27 3e s='80' rows='5'>
05a0: 25 73 28 73 71 6c 3f 73 71 6c 3a 22 22 29 3c 2f %s(sql?sql:"")</
05b0: 74 65 78 74 61 72 65 61 3e 0a 20 20 40 20 3c 62 textarea>. @ <b
05c0: 72 2f 3e 3c 69 6e 70 75 74 20 74 79 70 65 3d 27 r/><input type='
05d0: 73 75 62 6d 69 74 27 20 6e 61 6d 65 3d 27 73 71 submit' name='sq
05e0: 6c 5f 73 75 62 6d 69 74 27 2f 3e 20 3c 69 6e 70 l_submit'/> <inp
05f0: 75 74 20 74 79 70 65 3d 27 72 65 73 65 74 27 2f ut type='reset'/
0600: 3e 0a 20 20 40 20 3c 2f 66 6f 72 6d 3e 0a 7d 0a >. @ </form>.}.
0610: 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20 ./*.** WEBPAGE:
0620: 2f 61 64 6d 69 6e 2f 73 71 6c 0a 2a 2f 0a 76 6f /admin/sql.*/.vo
0630: 69 64 20 61 64 6d 69 6e 5f 73 71 6c 5f 70 61 67 id admin_sql_pag
0640: 65 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 2a 0a 20 e(void){. /**.
0650: 20 20 20 20 54 4f 44 4f 73 3a 20 72 65 66 61 63 TODOs: refac
0660: 74 6f 72 20 64 62 5f 67 65 6e 65 72 69 63 5f 71 tor db_generic_q
0670: 75 65 72 79 5f 76 69 65 77 28 29 20 74 6f 20 75 uery_view() to u
0680: 73 65 20 73 71 6c 69 74 65 33 20 64 69 72 65 63 se sqlite3 direc
0690: 74 6c 79 2c 0a 20 20 20 20 20 69 6e 73 74 65 61 tly,. instea
06a0: 64 20 6f 66 20 74 68 65 20 64 62 5f 70 72 65 70 d of the db_prep
06b0: 61 72 65 28 29 2f 64 62 5f 73 74 65 70 28 29 20 are()/db_step()
06c0: 41 50 49 2c 20 73 6f 20 77 65 20 63 61 6e 20 64 API, so we can d
06d0: 6f 20 6f 75 72 20 6f 77 6e 0a 20 20 20 20 20 68 o our own. h
06e0: 61 6e 64 6c 69 6e 67 2e 20 43 75 72 72 65 6e 74 andling. Current
06f0: 6c 79 20 53 51 4c 2d 6c 65 76 65 6c 20 66 61 69 ly SQL-level fai
0700: 6c 75 72 65 73 20 77 69 6c 6c 20 63 61 75 73 65 lures will cause
0710: 20 74 68 65 20 70 61 67 65 20 74 6f 0a 20 20 20 the page to.
0720: 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 73 74 immediately st
0730: 6f 70 20 28 65 2e 67 2e 20 70 61 67 65 20 66 6f op (e.g. page fo
0740: 6f 74 65 72 20 77 6f 6e 27 74 20 67 65 74 20 61 oter won't get a
0750: 20 63 68 61 6e 63 65 20 74 6f 20 72 65 6e 64 65 chance to rende
0760: 72 29 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 67 69 6e r).. */. login
0770: 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 61 _check_credentia
0780: 6c 73 28 29 3b 0a 20 20 73 74 79 6c 65 5f 68 65 ls();. style_he
0790: 61 64 65 72 28 22 41 64 6d 69 6e 20 53 51 4c 22 ader("Admin SQL"
07a0: 29 3b 0a 20 20 69 66 28 20 21 67 2e 6f 6b 41 64 );. if( !g.okAd
07b0: 6d 69 6e 20 29 7b 0a 20 20 20 20 40 20 3c 73 74 min ){. @ <st
07c0: 72 6f 6e 67 3e 41 63 63 65 73 73 20 44 65 6e 69 rong>Access Deni
07d0: 65 64 21 3c 2f 73 74 72 6f 6e 67 3e 20 59 6f 75 ed!</strong> You
07e0: 20 6d 75 73 74 20 62 65 20 61 6e 20 41 64 6d 69 must be an Admi
07f0: 6e 20 74 6f 20 75 73 65 20 74 68 69 73 20 74 6f n to use this to
0800: 6f 6c 2e 0a 20 20 20 20 73 74 79 6c 65 5f 66 6f ol.. style_fo
0810: 6f 74 65 72 28 29 3b 0a 20 20 20 20 72 65 74 75 oter();. retu
0820: 72 6e 3b 0a 20 20 7d 0a 20 20 61 64 6d 69 6e 5f rn;. }. admin_
0830: 73 71 6c 5f 70 61 67 65 5f 66 6f 72 6d 28 29 3b sql_page_form();
0840: 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 20 . char const *
0850: 73 71 6c 20 3d 20 50 28 22 73 71 6c 22 29 3b 0a sql = P("sql");.
0860: 20 20 69 66 28 20 73 71 6c 20 29 0a 20 20 7b 0a if( sql ). {.
0870: 20 20 20 20 64 62 5f 67 65 6e 65 72 69 63 5f 71 db_generic_q
0880: 75 65 72 79 5f 76 69 65 77 28 20 73 71 6c 2c 20 uery_view( sql,
0890: 30 2c 20 30 20 29 3b 0a 20 20 7d 0a 20 20 73 74 0, 0 );. }. st
08a0: 79 6c 65 5f 66 6f 6f 74 65 72 28 29 3b 0a 7d 0a yle_footer();.}.