├── .classpath
├── .gitignore
├── .project
├── build.xml
├── examples
├── MySQL_example1
│ ├── MySQL_example1.pde
│ └── sketch.properties
├── MySQL_example2
│ └── MySQL_example2.pde
├── MySQL_example3
│ ├── MySQL_example3.pde
│ └── data
│ │ └── test.db
├── MySQL_localhost_example
│ └── MySQL_localhost_example.pde
├── MySQL_timestamp
│ └── MySQL_timestamp.pde
├── PostgreSQL_example1
│ └── PostgreSQL_example1.pde
├── SQLite_example1
│ ├── SQLite_example1.pde
│ └── data
│ │ └── test.db
└── SQLite_example2
│ ├── SQLite_example2.pde
│ └── data
│ └── test.db
├── lib
├── ant-contrib-1.0b3.jar
├── build.number
├── mysql-connector-j-8.0.31.jar
├── postgresql-9.2-1002.jdbc3.jar
└── sqlite-jdbc-3.7.2.jar
├── readme.md
├── release
├── BezierSQLib.txt
└── BezierSQLib.zip
├── resources
├── build.number
├── library.properties
├── stylesheet.css
└── web
│ ├── index.html
│ └── stylesheet.css
└── src
├── DeBezierDataSQL.java
└── de
└── bezier
└── data
└── sql
├── MySQL.java
├── PostgreSQL.java
├── SQL.java
├── SQLite.java
└── mapper
├── NameMapper.java
└── UnderScoreToCamelCaseMapper.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | *~
3 |
4 | local.properties
5 | /BezierSQLib
6 |
7 | # ant-javac build directory
8 | /bin/
9 |
10 | # hide some results
11 | documentation/*
12 | distribution/*
13 |
14 | # XCode
15 | build/
16 | *.mode1
17 | *.mode1v3
18 | *.mode2v3
19 | *.perspective
20 | *.perspectivev3
21 | *.pbxuser
22 |
23 | # old stuff i can't seem to be able to delete
24 | releases/
25 |
26 | website/BezierSQLib/*
27 | web-export/*
28 | */web-export/*
29 | web-export
30 | .idea/
31 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | sql-library-processing
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | BezierSQLib (formerly SQLibrary) build file, based on Processing libraries build file.
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | Properties initialized.
66 | src path ${src}
67 | bin path ${bin}
68 | Processing dir ${processing.dir}
69 | processing Libraries ${libraries.dir}
70 | java version ${javaVersion}
71 |
72 |
73 |
74 |
75 |
76 |
77 | Start to build the library ... this is vers. ${versionNumber}, build #${build.number} on ${date}
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
163 |
165 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
226 |
227 |
231 |
232 |
236 |
237 |
241 |
242 |
246 |
247 |
251 |
252 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
292 |
296 |
297 |
298 |
301 |
302 |
303 |
304 |
307 |
308 |
312 |
313 |
314 |
315 |
--------------------------------------------------------------------------------
/examples/MySQL_example1/MySQL_example1.pde:
--------------------------------------------------------------------------------
1 | /**
2 | * MySQL example 1
3 | * https://github.com/fjenett/sql-library-processing
4 | *
5 | * created 2005-05-10 by fjenett
6 | * updated fjenett 20130718
7 | * tested with Processing 2.0.x
8 | */
9 |
10 | import de.bezier.data.sql.*;
11 |
12 | MySQL msql;
13 |
14 |
15 | void setup()
16 | {
17 | size( 100, 100 );
18 |
19 | // this example assumes that you are running the
20 | // mysql server locally (on "localhost")
21 |
22 | // 1
23 | // replace your mysql login:
24 |
25 | String user = "sql_user";
26 | String pass = "sql_pass";
27 |
28 | // 2
29 | // name of the database to use
30 |
31 | String database = "sql_library_test_db";
32 |
33 | // add additional parameters like this:
34 | // sql_library_test_db?useUnicode=true&characterEncoding=UTF-8
35 |
36 | // 3
37 | // connect to database of server "localhost"
38 |
39 | msql = new MySQL( this, "localhost", database, user, pass );
40 |
41 | if ( msql.connect() )
42 | {
43 | msql.query( "SELECT COUNT(*) FROM %s", "example1" );
44 | msql.next();
45 | println( "this table has " + msql.getInt(1) + " number of rows" );
46 | }
47 | else
48 | {
49 | // connection failed !
50 |
51 | // - check your login, password
52 | // - check that your server runs on localhost and that the port is set right
53 | // - try connecting through other means (terminal or console / MySQL workbench / ...)
54 | }
55 | }
56 |
57 | void draw()
58 | {
59 | // i know this is not really a visual sketch ...
60 | }
61 |
--------------------------------------------------------------------------------
/examples/MySQL_example1/sketch.properties:
--------------------------------------------------------------------------------
1 | mode.id=processing.mode.java.JavaMode
2 | mode=Java
3 |
--------------------------------------------------------------------------------
/examples/MySQL_example2/MySQL_example2.pde:
--------------------------------------------------------------------------------
1 | import de.bezier.data.sql.*;
2 |
3 | // created 2005-05-10 by fjenett
4 | // updated fjenett 20080605
5 |
6 |
7 | MySQL dbconnection;
8 |
9 |
10 | void setup()
11 | {
12 | size( 100, 100 );
13 |
14 | // this example assumes that you are running the
15 | // mysql server locally (on "localhost").
16 | //
17 |
18 | // replace --username--, --password-- with your mysql-account.
19 | //
20 | String user = "ffu";
21 | String pass = "ffu";
22 |
23 | // name of the database to use
24 | //
25 | String database = "ffu";
26 |
27 | // name of the table that will be created
28 | //
29 | String table = "";
30 |
31 | // connect to database of server "localhost"
32 | //
33 | dbconnection = new MySQL( this, "localhost", database, user, pass );
34 |
35 | if ( dbconnection.connect() )
36 | {
37 | // now read it back out
38 | //
39 | dbconnection.query( "SELECT * FROM file_uploads" );
40 |
41 | while (dbconnection.next())
42 | {
43 | String s = dbconnection.getString("name");
44 | int n = dbconnection.getInt("fuid");
45 | println(s + " " + n);
46 | }
47 | }
48 | else
49 | {
50 | // connection failed !
51 | }
52 | }
53 |
54 | void draw()
55 | {
56 | // i know this is not really a visual sketch ...
57 | }
58 |
--------------------------------------------------------------------------------
/examples/MySQL_example3/MySQL_example3.pde:
--------------------------------------------------------------------------------
1 | // fjenett 20120226
2 |
3 | import de.bezier.data.sql.*;
4 |
5 | MySQL db;
6 |
7 | void setup()
8 | {
9 | size( 100, 100 );
10 |
11 | db = new MySQL( this, "localhost", "x", "x", "x" ); // open database file
12 | db.setDebug(false);
13 |
14 | if ( db.connect() )
15 | {
16 | String[] tableNames = db.getTableNames();
17 |
18 | db.query( "SELECT * FROM %s", tableNames[0] );
19 |
20 | while (db.next())
21 | {
22 | TableOne t = new TableOne();
23 | db.setFromRow( t );
24 | println( t );
25 | }
26 |
27 | TableOne t1 = new TableOne();
28 | t1.fieldOne = "one, two, three";
29 | t1.fieldTwo = 123;
30 | t1.id = 101;
31 | db.saveToDatabase(t1);
32 | }
33 | }
34 |
35 | class TableOne
36 | {
37 | int id;
38 | public String fieldOne;
39 | public int fieldTwo;
40 |
41 | public String toString ()
42 | {
43 | return String.format("id: %d, fieldOne: %s fieldTwo: %d", id, fieldOne, fieldTwo);
44 | }
45 |
46 | public void setId ( int id ) {
47 | this.id = id;
48 | }
49 |
50 | public int getId () {
51 | return id;
52 | }
53 | }
54 |
55 |
--------------------------------------------------------------------------------
/examples/MySQL_example3/data/test.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/examples/MySQL_example3/data/test.db
--------------------------------------------------------------------------------
/examples/MySQL_localhost_example/MySQL_localhost_example.pde:
--------------------------------------------------------------------------------
1 | // Library by fjenett
2 | // Example created on 2014-03-12 by Powder
3 | // Tested with Processing 3.0a5
4 |
5 | import de.bezier.data.sql.*;
6 |
7 | String dbHost = "localhost"; // if you are using a using a local database, this should be fine
8 | String dbPort = "8889"; // replace with database port, MAMP standard is 8889
9 | String dbUser = "root"; // replace with database username, MAMP standard is "root"
10 | String dbPass = "root"; // replace with database password, MAMP standard is "root"
11 | String dbName = "database_name"; // replace with database name
12 | String tableName = "table_name"; // replace with table name
13 |
14 | MySQL msql;
15 |
16 | void setup() {
17 | msql = new MySQL( this, dbHost + ":" + dbPort, dbName, dbUser, dbPass );
18 |
19 | if (msql.connect()) {
20 | // get number of rows
21 | msql.query("SELECT COUNT(*) FROM " + tableName);
22 | msql.next();
23 | println("Number of rows: " + msql.getInt(1));
24 | println();
25 |
26 | // access table
27 | msql.query("SELECT * FROM " + tableName);
28 | while (msql.next()){
29 | // replace "first_name" and "last_name" with column names from your table
30 | String s1 = msql.getString("first_name");
31 | String s2 = msql.getString("last_name");
32 | println(s1 + " " + s2);
33 | }
34 | } else {
35 | println("Yikes, there was an error!");
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/examples/MySQL_timestamp/MySQL_timestamp.pde:
--------------------------------------------------------------------------------
1 | import de.bezier.data.sql.*;
2 |
3 | /*
4 | CREATE TABLE `test` (
5 | `id` int(10) NOT NULL auto_increment,
6 | `time` timestamp NOT NULL default CURRENT_TIMESTAMP,
7 | PRIMARY KEY (`id`)
8 | )
9 | */
10 |
11 | MySQL ms;
12 | java.sql.Timestamp last_ts;
13 |
14 | String database = "ddd", user = "", pass = "";
15 |
16 | void setup ()
17 | {
18 | ms = new MySQL( this, "localhost", database, user, pass );
19 | last_ts = new java.sql.Timestamp( 0 );
20 |
21 | if ( ms.connect() )
22 | {
23 | ms.query( "SELECT * FROM test ORDER BY time ASC LIMIT 1" );
24 | ms.next();
25 | last_ts = ms.getTimestamp("time");
26 | }
27 | }
28 |
29 | void draw ()
30 | {
31 | if ( ms.connect() )
32 | {
33 | ms.query( "SELECT * FROM test WHERE time > '"+last_ts+"' ORDER BY time ASC" );
34 | while( ms.next() )
35 | {
36 | println( ms.getInt( "id" ) );
37 | last_ts = ms.getTimestamp( "time" );
38 | }
39 | }
40 | }
41 |
42 |
--------------------------------------------------------------------------------
/examples/PostgreSQL_example1/PostgreSQL_example1.pde:
--------------------------------------------------------------------------------
1 | // updated fjenett 20081129
2 |
3 | import de.bezier.data.sql.*;
4 |
5 |
6 | PostgreSQL pgsql;
7 |
8 |
9 | void setup()
10 | {
11 | size( 100, 100 );
12 |
13 | // this example assumes that you are running the
14 | // postgresql server locally (on "localhost").
15 | //
16 |
17 | // replace with your own postgresql-account.
18 | //
19 | String user = "fjenett";
20 | String pass = "fjenett";
21 |
22 | // name of the database to use
23 | //
24 | String database = "test";
25 |
26 | // connect to database on "localhost"
27 | //
28 | pgsql = new PostgreSQL( this, "localhost", database, user, pass );
29 |
30 | // connected?
31 | if ( pgsql.connect() )
32 | {
33 | // query the number of entries in table "weather"
34 | pgsql.query( "SELECT COUNT(*) FROM weather" );
35 |
36 | // results found?
37 | if ( pgsql.next() )
38 | {
39 | // nice, then let's report them back
40 | println( "number of rows in table weather: " + pgsql.getInt(1) );
41 | }
42 |
43 | // now let's query for last 10 entries in "weather"
44 | pgsql.query( "SELECT * FROM weather LIMIT 10" );
45 |
46 | // anything found?
47 | while( pgsql.next() )
48 | {
49 | // splendid, here's what we've found ..
50 | println( pgsql.getString("city") );
51 | }
52 | }
53 | else
54 | {
55 | // yay, connection failed !
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/examples/SQLite_example1/SQLite_example1.pde:
--------------------------------------------------------------------------------
1 | // fjenett 20081129
2 |
3 | import de.bezier.data.sql.*;
4 |
5 | SQLite db;
6 |
7 | void setup()
8 | {
9 | size( 100, 100 );
10 |
11 | db = new SQLite( this, "test.db" ); // open database file
12 |
13 | if ( db.connect() )
14 | {
15 | // list table names
16 | db.query( "SELECT name as \"Name\" FROM SQLITE_MASTER where type=\"table\"" );
17 |
18 | while (db.next())
19 | {
20 | println( db.getString("Name") );
21 | }
22 |
23 | // read all in table "table_one"
24 | db.query( "SELECT * FROM table_one" );
25 |
26 | while (db.next())
27 | {
28 | println( db.getString("field_one") );
29 | println( db.getInt("field_two") );
30 | }
31 | }
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/examples/SQLite_example1/data/test.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/examples/SQLite_example1/data/test.db
--------------------------------------------------------------------------------
/examples/SQLite_example2/SQLite_example2.pde:
--------------------------------------------------------------------------------
1 | // fjenett 20120226
2 |
3 | import de.bezier.data.sql.*;
4 |
5 | SQLite db;
6 |
7 | void setup()
8 | {
9 | size( 100, 100 );
10 |
11 | db = new SQLite( this, "test.db" ); // open database file
12 |
13 | if ( db.connect() )
14 | {
15 | String[] tableNames = db.getTableNames();
16 |
17 | db.query( "SELECT * FROM %s", tableNames[0] );
18 |
19 | while (db.next())
20 | {
21 | TableOne t = new TableOne();
22 | db.setFromRow( t );
23 | println( t );
24 | }
25 | }
26 | }
27 |
28 | class TableOne
29 | {
30 | public String fieldOne;
31 | public int fieldTwo;
32 |
33 | public String toString ()
34 | {
35 | return String.format("fieldOne: %s fieldTwo: %d", fieldOne, fieldTwo);
36 | }
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/examples/SQLite_example2/data/test.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/examples/SQLite_example2/data/test.db
--------------------------------------------------------------------------------
/lib/ant-contrib-1.0b3.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/lib/ant-contrib-1.0b3.jar
--------------------------------------------------------------------------------
/lib/build.number:
--------------------------------------------------------------------------------
1 | #Build Number for ANT. Do not edit!
2 | #Sun Jan 08 14:58:26 CET 2023
3 | build.number=599
4 |
--------------------------------------------------------------------------------
/lib/mysql-connector-j-8.0.31.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/lib/mysql-connector-j-8.0.31.jar
--------------------------------------------------------------------------------
/lib/postgresql-9.2-1002.jdbc3.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/lib/postgresql-9.2-1002.jdbc3.jar
--------------------------------------------------------------------------------
/lib/sqlite-jdbc-3.7.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/lib/sqlite-jdbc-3.7.2.jar
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | This library wraps around JDBC drivers to let you access MySQL, SQLite and PostgreSQL databases.
2 |
3 | ## Latest Release (0.3.2)
4 |
5 | This fork has been modified slightly for Processing 3. It's now available in the Contribution Manager in the PDE, or you can install manually by unzipping this folder into your libraries folder:
6 | https://github.com/fjenett/sql-library-processing/raw/latest/release/BezierSQLib.zip
7 |
8 | ## Features
9 |
10 | It's not an ORM (and will not become one) but it has some handy features
11 | since v0.2.0 that makes it easier to load and save objects to a database.
12 |
13 | setFromRow(object)
14 | saveToDatabase(object)
15 | insertUpdateIntoDatabase(table, columns, values)
16 |
17 | See:
18 | https://raw.github.com/fjenett/sql-library-processing/master/src/de/bezier/data/sql/SQL.java
19 |
20 | ## Database Drivers
21 |
22 | Parts of this library are available under their own licenses. These are redistributed as downloaded from the original sources:
23 |
24 | MySQL Connector/J (JDBC driver)
25 | mysql-connector-java-3.1.8-bin.jar
26 | http://dev.mysql.com/downloads/connector/j/
27 | http://dev.mysql.com/downloads/connector/j/3.1.html
28 | GPL, http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
29 |
30 | PostgreSQL JDBC driver
31 | postgresql-8.3-604.jdbc3.jar
32 | http://jdbc.postgresql.org/
33 | http://jdbc.postgresql.org/download.html
34 | BSD, http://jdbc.postgresql.org/license.html
35 |
36 | SQLite JDBC driver
37 | sqlite-jdbc-3.7.2.jar
38 | http://code.google.com/p/sqlite-jdbc/
39 | Apache License 2.0, http://www.apache.org/licenses/LICENSE-2.0
40 |
--------------------------------------------------------------------------------
/release/BezierSQLib.txt:
--------------------------------------------------------------------------------
1 | name = BezierSQLib
2 | authorList = [Florian Jenett](http://www.bezier.de/)
3 | url = https://github.com/fjenett/sql-library-processing
4 | category = Data
5 | sentence = A library to facilitate communication with SQL-based databases
6 | paragraph = SQLibrary is an interface to MySQL, SQLite and PostgreSQL databases
7 | version = 1192
8 | prettyVersion = 0.3.2
--------------------------------------------------------------------------------
/release/BezierSQLib.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fjenett/sql-library-processing/25d3478e868bafc4ffab4f0124a1cefb3130e634/release/BezierSQLib.zip
--------------------------------------------------------------------------------
/resources/build.number:
--------------------------------------------------------------------------------
1 | #Build Number for ANT. Do not edit!
2 | #Sun Jan 08 14:58:26 CET 2023
3 | build.number=1194
4 |
--------------------------------------------------------------------------------
/resources/library.properties:
--------------------------------------------------------------------------------
1 | name = @@LIBRARY_NAME@@
2 | authorList = [Florian Jenett](http://www.bezier.de/)
3 | url = https://github.com/fjenett/sql-library-processing
4 | category = Data
5 | sentence = A library to facilitate communication with SQL-based databases
6 | paragraph = SQLibrary is an interface to MySQL, SQLite and PostgreSQL databases
7 | version = @@VERSION@@
8 | prettyVersion = @@PRETTY_VERSION@@
--------------------------------------------------------------------------------
/resources/stylesheet.css:
--------------------------------------------------------------------------------
1 | /* Javadoc style sheet */
2 | /* Define colors, fonts and other style attributes here to override the defaults */
3 | /* processingLibs style by andreas schlegel, sojamo */
4 |
5 |
6 | body {
7 | margin : 0;
8 | padding : 0;
9 | padding-left : 10px;
10 | padding-right : 8px;
11 | background-color : #FFFFFF;
12 | font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
13 | font-size : 100%;
14 | font-size : 0.7em;
15 | font-weight : normal;
16 | line-height : normal;
17 | margin-bottom:30px;
18 | }
19 |
20 |
21 |
22 |
23 | /* Headings */
24 | h1, h2, h3, h4, h5, th {
25 | font-family :Arial, Helvetica, sans-serif;
26 | font-size:1.2em;
27 | }
28 |
29 |
30 | p {
31 | font-size : 1em;
32 | width:80%;
33 | }
34 |
35 | pre, code {
36 | font-family : "Courier New", Courier, monospace;
37 | font-size : 12px;
38 | line-height : normal;
39 | }
40 |
41 |
42 |
43 | table {
44 | border:0;
45 | margin-bottom:10px;
46 | margin-top:10px;
47 | }
48 |
49 |
50 | tr, td {
51 | border-top: 0px solid;
52 | border-left: 0px solid;
53 | padding-top:8px;
54 | padding-bottom:8px;
55 | }
56 |
57 |
58 |
59 | hr {
60 | border:0;
61 | height:1px;
62 | padding:0;
63 | margin:0;
64 |
65 | }
66 |
67 |
68 |
69 | dd, th, td, font {
70 | font-size:1.0em;
71 | line-height:1.0em;
72 | }
73 |
74 |
75 |
76 | dt {
77 | margin-bottom:4px;
78 | }
79 |
80 |
81 |
82 | dd {
83 | margin-bottom:20px;
84 | }
85 |
86 |
87 |
88 | a {
89 | text-decoration: underline;
90 | font-weight: normal;
91 | }
92 |
93 | a:hover,
94 | a:active {
95 | text-decoration: underline;
96 | font-weight: normal;
97 | }
98 |
99 | a:visited,
100 | a:link:visited {
101 | text-decoration: underline;
102 | font-weight: normal;
103 | }
104 |
105 |
106 | img {
107 | border: 0px solid #000000;
108 | }
109 |
110 |
111 |
112 | /* Navigation bar fonts */
113 | .NavBarCell1 {
114 | border:0;
115 | }
116 |
117 | .NavBarCell1Rev {
118 | border:0;
119 | }
120 |
121 | .NavBarFont1 {
122 | font-family: Arial, Helvetica, sans-serif;
123 | font-size:1.1em;
124 | }
125 |
126 |
127 | .NavBarFont1 b {
128 | font-weight:normal;
129 | }
130 |
131 |
132 |
133 | .NavBarFont1:after, .NavBarFont1Rev:after {
134 | font-weight:normal;
135 | content: " \\";
136 | }
137 |
138 |
139 | .NavBarFont1Rev {
140 | font-family: Arial, Helvetica, sans-serif;
141 | font-size:1.1em;
142 | }
143 |
144 | .NavBarFont1Rev b {
145 | font-family: Arial, Helvetica, sans-serif;
146 | font-size:1.1em;
147 | font-weight:normal;
148 | }
149 |
150 | .NavBarCell2 {
151 | font-family: Arial, Helvetica, sans-serif;
152 | }
153 |
154 | .NavBarCell3 {
155 | font-family: Arial, Helvetica, sans-serif;
156 | }
157 |
158 |
159 |
160 | font.FrameItemFont {
161 | font-family: Helvetica, Arial, sans-serif;
162 | }
163 |
164 | font.FrameHeadingFont {
165 | font-family: Helvetica, Arial, sans-serif;
166 | line-height:32px;
167 | }
168 |
169 | /* Font used in left-hand frame lists */
170 | .FrameTitleFont {
171 | font-family: Helvetica, Arial, sans-serif
172 | }
173 |
174 |
175 |
176 |
177 | /* COLORS */
178 |
179 | pre, code {
180 | color: #000000;
181 | }
182 |
183 |
184 | body {
185 | color : #333333;
186 | background-color :#FFFFFF;
187 | }
188 |
189 |
190 | h1, h2, h3, h4, h5, h6 {
191 | color:#5A5A46;
192 | }
193 |
194 | a {
195 | color: #3399CC;
196 | }
197 |
198 | a:hover,
199 | a:active {
200 | color: #3399CC;
201 | }
202 |
203 | a:visited,
204 | a:link:visited {
205 | color: #3399CC;
206 | }
207 |
208 | td,tr {
209 | border-color: #999999;
210 | }
211 |
212 | hr {
213 | color:#999999;
214 | background:#999999;
215 | }
216 |
217 |
218 | .TableHeadingColor {
219 | background: #CCCCBE;
220 | color: #5A5A46;
221 | }
222 |
223 |
224 | .TableSubHeadingColor {
225 | background: #EEEEFF
226 | }
227 |
228 | .TableRowColor {
229 | background: #FFFFFF
230 | }
231 |
232 |
233 | .NavBarCell1 {
234 | background-color:#CCCCBE;
235 | color:#000;
236 | }
237 |
238 | .NavBarCell1 a {
239 | color:#5A5A46;
240 | }
241 |
242 |
243 | .NavBarCell1Rev {
244 | background-color:transparent;
245 | }
246 |
247 | .NavBarFont1 {
248 | color:#5A5A46;
249 | }
250 |
251 |
252 | .NavBarFont1Rev {
253 | color:#fff;
254 | }
255 |
256 | .NavBarCell2 {
257 | background-color:#999983;
258 | }
259 |
260 | .NavBarCell2 a {
261 | color:#fff;
262 | }
263 |
264 |
265 |
266 | .NavBarCell3 {
267 | background-color:#DCDCC3;
268 | }
269 |
270 |
--------------------------------------------------------------------------------
/resources/web/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ##yourLibrary## version ##versionNumber## - by ##author##
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
##yourLibrary##
47 |
48 | a library for the programming environment processing .
49 | last update, ##date##.
50 |
51 |
52 | SQLibrary lets you communicate with a MySQL , PostgreSQL or SQLite databases.
53 |
54 |
55 | please note: working with a remote database in a plublicly accessible applet is highly insecure.
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
Download
64 |
65 |
66 | Get ##yourLibrary## version ##versionNumber## in .zip format
67 |
68 |
69 |
Installation
70 |
71 |
72 | Before trying to use this library with any database make sure you have that up and running.
73 | the library is just a wrapper to help connect to already installed and running databases (MySQL, SQLite, PostgreSQL).
74 |
75 |
76 | Installation in Processing 2.0 should be as easy as opening the "Library Manager" from the menu at:
77 | Sketch > Import Library ... > Add Library ... > (select ##yourLibrary## and click "install")
78 |
79 |
80 | Manual installation (Processing 1.5 and earlier):
81 |
82 | Quit processing
83 | Find your sketchbook folder, see second paragraph here
84 | Add a folder called libraries (lowercase) to your sketchbook if its not already there
85 | Move the ##yourLibrary##-##versionNumber##.zip into that libraries folder
86 | Unzip it in place
87 | Rename it to ##yourLibrary##
88 | Start processing and run one of the included examples to see if the lib is working
89 |
90 |
91 |
92 |
93 |
94 |
95 |
Keywords: ##keywords##
96 |
97 |
Documentation is online here and included in the download.
98 |
99 |
Source is located here and included in the download.
100 |
101 |
Bugs, issues, requests can be added at located Github.
102 |
103 |
104 |
105 |
106 |
107 |
108 |
Examples
109 |
110 | find a list of examples in the current distribution of ##yourLibrary##, or have a look at them by following the links below.
111 |
114 |
115 |
116 |
117 |
118 |
Tested on ...
119 |
120 | Platform osx
121 |
122 |
123 | Processing 2.0b
124 |
125 |
126 | Dependencies mysql, sqlite and postgresql jdbc drivers (all included)
127 |
128 |
129 |
130 |
131 |
132 |
133 |
Used in:
134 |
135 |
139 |
140 | Have an example this library in use? ...
contact me!
141 |
142 |
143 |
144 |
145 |
146 |
147 |
150 |
151 |
152 |
153 |
154 |
155 |
159 |
160 |
161 |
162 |
163 |
164 |
--------------------------------------------------------------------------------
/resources/web/stylesheet.css:
--------------------------------------------------------------------------------
1 | /* processingLibs style by andreas schlegel, sojamo. */
2 | body, div, h1, h2, h3, h4, h5, h6, p, a, strong {
3 | margin:0;
4 | padding:0;
5 | border:0;
6 | }
7 | h1, h2, h3, h4
8 | {
9 | margin-bottom: 1em;
10 | }
11 | body {
12 | font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
13 | font-size : 100%;
14 | font-size : 0.7em;
15 | font-weight : normal;
16 | line-height : normal;
17 | }
18 | div {
19 | margin-bottom:40px;
20 | }
21 | #container {
22 | background-color:#fff;
23 | width:750px;
24 | }
25 | #header,
26 | #menu,
27 | #about,
28 | #download,
29 | #examples,
30 | #demos,
31 | #misc {
32 | padding-left:60px;
33 | }
34 | #header {
35 | float:left;
36 | padding-top:20px;
37 | width:690px;
38 | margin-bottom:0px;
39 | }
40 | #menu {
41 | padding-top:6px;
42 | float:left;
43 | margin-bottom:40px;
44 | height:20px;
45 | width:690px;
46 | }
47 | #about,
48 | #download,
49 | #examples,
50 | #demos,
51 | #misc {
52 | width:400px;
53 | float:left;
54 | }
55 | #resources, #info {
56 | width:200px;
57 | margin-right: 20px;
58 | float:right;
59 | }
60 | .clear {
61 | clear:both;
62 | }
63 | #footer {
64 | margin-top:300px;
65 | height:20px;
66 | padding-left:60px;
67 | padding-top:6px;
68 | }
69 | #menu ul {
70 | list-style:none;
71 | padding:0;
72 | margin:0;
73 | }
74 | #menu li {
75 | float:left;
76 | padding-right:6px;
77 | }
78 | /* Headings */
79 | h1 {
80 | font-size:1.5em;
81 | }
82 | h2, h3, h4, h5, th {
83 | font-size:1.0em;
84 | }
85 | p {
86 | font-size:1em;
87 | width:90%;
88 | margin-bottom:20px;
89 | }
90 | pre, code {
91 | font-family:"Courier New", Courier, monospace;
92 | font-size:1em;
93 | line-height:normal;
94 | }
95 | strong {
96 | font-weight:normal;
97 | }
98 | hr {
99 | border:0;
100 | height:1px;
101 | margin-bottom:24px;
102 | }
103 | a {
104 | text-decoration: underline;
105 | font-weight: normal;
106 | }
107 | a:hover,
108 | a:active {
109 | text-decoration: underline;
110 | font-weight: normal;
111 | }
112 | a:visited,
113 | a:link:visited {
114 | text-decoration: underline;
115 | font-weight: normal;
116 | }
117 | img {
118 | border: 0px solid #000000;
119 | }
120 | /* COLORS */
121 | body {
122 | color : #333333;
123 | background-color :#E2E2CC;
124 | }
125 | #header {
126 | background-color:#000;
127 | color:#fff;
128 | }
129 | h1, h2, h3, h4, h5, h6 {
130 | color:#5A5A46;
131 | }
132 | pre, code {
133 | color: #000000;
134 | }
135 | a,strong {
136 | color: #3399CC;
137 | }
138 | a:hover,
139 | a:active {
140 | color: #3399CC;
141 | }
142 | a:visited,
143 | a:link:visited {
144 | color: #3399CC;
145 | }
146 | #footer, #menu {
147 | background-color:#5A5A4E;
148 | color:#fff;
149 | }
150 | #footer a, #menu a {
151 | color:#fff;
152 | }
153 |
--------------------------------------------------------------------------------
/src/DeBezierDataSQL.java:
--------------------------------------------------------------------------------
1 | /** UNPACKAGED to be able to access unpackaged classes **/
2 |
3 | import de.bezier.data.sql.*;
4 | import de.bezier.data.sql.mapper.*;
5 | import java.lang.reflect.*;
6 |
7 | /**
8 | * Java classes in a package can not gain access to
9 | * classes outside any package through Java's reflection.
10 | *
11 | * This class is a helper that can access other unpackaged
12 | * classes (as Processing sketches are).
13 | *
14 | * de.bezier.data.SQL <-> DeBezierDataSQL <-> _unpackaged_class_
15 | *
16 | * fjenett 2011
17 | */
18 |
19 | public class DeBezierDataSQL
20 | {
21 | public static void setFromRow ( SQL db, Object object )
22 | {
23 | String[] colNames = db.getColumnNames();
24 | if ( colNames == null || colNames.length == 0 ) return;
25 |
26 | Class klass = object.getClass();
27 | for ( String colName : colNames )
28 | {
29 | // translate the fieldname via mapper
30 |
31 | String fieldName = colName;
32 | NameMapper mapper = db.getNameMapper();
33 | if ( mapper != null )
34 | {
35 | fieldName = mapper.forward( colName );
36 | }
37 |
38 | // fetch value from DB
39 |
40 | Object val = null;
41 | try {
42 | val = db.getObject(colName);
43 | } catch (Exception e) {
44 | if (db.getDebug()) e.printStackTrace();
45 | }
46 | if ( val == null ) {
47 | if (db.getDebug()) {
48 | System.err.println( "setFromRow(): Value is null" );
49 | System.err.println( colName );
50 | }
51 | continue; // TODO warn here?
52 | }
53 |
54 | // // try to make a clone of the value ..?
55 | //
56 | // Class vClass = val.getClass();
57 | // try {
58 | // Method vMeth = vClass.getMethod("clone");
59 | // if ( vMeth != null &&
60 | // Modifier.isPublic(vMeth.getModifiers()) )
61 | // {
62 | // Object tmp = vMeth.invoke(val);
63 | // val = tmp;
64 | // //if (DEBUG) System.out.println("Value cloned.");
65 | // }
66 | // } catch ( Exception ex ) {
67 | // if (DEBUG) ex.printStackTrace();
68 | // }
69 |
70 | //
71 | // try fields first
72 | // obj.fieldName = value
73 |
74 | Field f = null;
75 | try {
76 | f = klass.getField( fieldName );
77 | } catch ( Exception e ) {
78 | if (db.getDebug()) e.printStackTrace();
79 | }
80 | if ( f != null )
81 | {
82 | try {
83 | f.set( object, val );
84 | continue;
85 | } catch (Exception e) {
86 | if (db.getDebug()) e.printStackTrace();
87 | }
88 | }
89 |
90 | //
91 | // try setter next ..
92 | // obj.setFieldName( value )
93 | // obj.fieldName( value )
94 |
95 | Method[] meths = null;
96 | try {
97 | meths = klass.getMethods();
98 | } catch ( Exception e ) {
99 | if (db.getDebug()) e.printStackTrace();
100 | }
101 | if ( meths != null && meths.length > 0 )
102 | {
103 | String setterName = db.nameToSetter(fieldName);
104 | //Class[] paramTypes = new Class[]{val.getClass()};
105 | for ( Method meth : meths )
106 | {
107 | if ( meth.getName().equals(setterName) ||
108 | meth.getName().equals(fieldName)
109 | /*&& meth.getParameterTypes().equals(paramTypes)*/ )
110 | {
111 | //if (db.getDebug()) System.out.println( meth );
112 | try {
113 | meth.invoke( object, new Object[]{val} );
114 | break;
115 | } catch ( Exception e ) {
116 | if (db.getDebug()) e.printStackTrace();
117 | }
118 | }
119 | }
120 | }
121 | }
122 | }
123 |
124 | public static Object[] getValuesFromObject ( SQL db, Field[] fields, Method[] getters, Object object )
125 | {
126 | if ( db == null || object == null ) return null;
127 | if ( fields == null || getters == null ) return null;
128 | if ( fields.length != getters.length ) return null;
129 |
130 | Object[] vals = new Object[fields.length];
131 |
132 | for ( int i = 0; i < vals.length; i++ ) {
133 | try {
134 | if ( fields[i] != null )
135 | vals[i] = fields[i].get(object);
136 | else if ( getters[i] != null )
137 | vals[i] = getters[i].invoke(object);
138 | else
139 | {
140 | System.err.println(String.format(
141 | "getValuesFromObject() : neither field nor getter given for a value."
142 | ));
143 | return null;
144 | }
145 | } catch ( Exception e ) {
146 | e.printStackTrace();
147 | return null;
148 | }
149 | }
150 |
151 | return vals;
152 | }
153 | }
--------------------------------------------------------------------------------
/src/de/bezier/data/sql/MySQL.java:
--------------------------------------------------------------------------------
1 | package de.bezier.data.sql;
2 |
3 | import processing.core.*;
4 | import java.util.ArrayList;
5 |
6 | /**
7 | * MySQL wrapper for SQL library for Processing 2+
8 | *
9 | * A wrapper around some of sun's java.sql.* classes
10 | * and the "com.mysql.jdbc.Driver" driver by mysql.com (GPL).
11 | *
12 | * see:
13 | * - http://www.mysql.com/products/connector/j/
14 | * - http://java.sun.com/products/jdbc/
15 | *
16 | *
17 | * @author Florian Jenett - mail@florianjenett.de
18 | *
19 | * created: 07.05.2005 - 12:46 Uhr
20 | * modified: 2012-02
21 | *
22 | */
23 |
24 | public class MySQL
25 | extends de.bezier.data.sql.SQL
26 | {
27 | public MySQL ( PApplet _papplet, String _database )
28 | {
29 | // should not be used
30 | }
31 |
32 | /**
33 | * Creates a new MySQL connection.
34 | *
35 | * @param _papplet Normally you'd pass "this" in for your sketch
36 | * @param _server The server running the database, try "localhost"
37 | * @param _database Name of the database
38 | * @param _user Username for that database
39 | * @param _pass Password for user
40 | */
41 |
42 | public MySQL ( PApplet _papplet, String _server, String _database, String _user, String _pass)
43 | {
44 | super( _papplet, _server, _database, _user, _pass );
45 | init();
46 | }
47 |
48 | private void init ()
49 | {
50 | this.driver = "com.mysql.cj.jdbc.Driver";
51 | this.type = "mysql";
52 |
53 | this.url = "jdbc:" + type + "://" + server + "/" + database;
54 | }
55 |
56 | public String[] getTableNames ()
57 | {
58 | if ( tableNames == null )
59 | {
60 | tableNames = new ArrayList();
61 | query( "SHOW TABLES" );
62 | while ( next() ) {
63 | tableNames.add( getObject("Tables_in_"+database).toString() );
64 | }
65 | }
66 | return tableNames.toArray(new String[0]);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/de/bezier/data/sql/PostgreSQL.java:
--------------------------------------------------------------------------------
1 | package de.bezier.data.sql;
2 |
3 | import processing.core.*;
4 | import java.util.ArrayList;
5 |
6 | /**
7 | * PostgreSQL wrapper for SQL library for Processing 2+
8 | *
9 | * This is a wrapper around some of sun's java.sql.* classes
10 | * and the "org.postgresql.Driver" driver by postgresql.org (BSD).
11 | *
12 | * see:
13 | * http://jdbc.postgresql.org/download.html
14 | * http://java.sun.com/products/jdbc/
15 | *
16 | *
17 | * PostgreSQL on OS-X (i used a mix of these on 10.5.x):
18 | * http://developer.apple.com/internet/opensource/postgres.html
19 | * http://shifteleven.com/articles/2008/03/21/installing-postgresql-on-leopard-using-macports
20 | * http://systems.takizo.com/2008/03/10/installing-postgresql-82-on-leopard-with-macports/
21 | *
22 | * PostgreSQL documentation is at:
23 | * http://www.postgresql.org/docs/8.3/interactive/index.html
24 | *
25 | *
26 | * @author Florian Jenett - mail@florianjenett.de
27 | *
28 | * created: 2008-11-29 17:49:23 - fjenett
29 | * modified: fjenett 2012-02
30 | *
31 | */
32 |
33 | public class PostgreSQL
34 | extends de.bezier.data.sql.SQL
35 | {
36 | public PostgreSQL ( PApplet _papplet, String _database )
37 | {
38 | // should not be used
39 | }
40 |
41 | /**
42 | * Creates a new PostgreSQL connection.
43 | *
44 | * @param _papplet Normally you'd pass "this" in for your sketch
45 | * @param _server The server running the database, try "localhost"
46 | * @param _database Name of the database
47 | * @param _user Username for that database
48 | * @param _pass Password for user
49 | */
50 |
51 | public PostgreSQL ( PApplet _papplet, String _server, String _database, String _user, String _pass )
52 | {
53 | super( _papplet, _server, _database, _user, _pass );
54 | init();
55 | }
56 |
57 | private void init ()
58 | {
59 | this.driver = "org.postgresql.Driver";
60 | this.type = "postgresql";
61 |
62 | this.url = "jdbc:" + type + "://" + server + "/" + database;
63 | }
64 |
65 | public String[] getTableNames ()
66 | {
67 | if ( tableNames == null )
68 | {
69 | tableNames = new ArrayList();
70 | query( "SELECT relname AS table_name FROM pg_stat_user_tables WHERE schemaname='public'" );
71 | while ( next() ) {
72 | tableNames.add( getObject("table_name").toString() );
73 | }
74 | }
75 | return tableNames.toArray(new String[0]);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/de/bezier/data/sql/SQL.java:
--------------------------------------------------------------------------------
1 | package de.bezier.data.sql;
2 |
3 | import processing.core.*;
4 | import de.bezier.data.sql.mapper.*;
5 |
6 | import java.io.*;
7 | import java.sql.*;
8 | import java.lang.reflect.*;
9 | import java.util.HashMap;
10 | import java.util.Map;
11 | import java.util.ArrayList;
12 |
13 | /**
14 | * SQL library for Processing 2+
15 | *
16 | * Since v 0.2.0 it has some ORM like features, see
17 | *
22 | *
23 | * Links:
24 | * http://www.mysql.com/products/connector/j/
25 | * http://java.sun.com/products/jdbc/
26 | * http://www.toxi.co.uk/blog/2007/07/using-javadb-and-db4o-in-processing.htm
27 | * http://www.tom-carden.co.uk/2007/07/30/a-quick-note-on-using-sqlite-in-processing/
28 | *
29 | *
30 | * @author Florian Jenett - mail@florianjenett.de
31 | *
32 | * created: 07.05.2005 - 12:46 Uhr
33 | * modified: fjenett 20121217
34 | */
35 |
36 | abstract public class SQL
37 | {
38 | PApplet papplet;
39 |
40 | public String server;
41 | public String database;
42 | public String url;
43 | public String user;
44 | protected String pass;
45 | public String driver = "";
46 | public String type = "";
47 |
48 | private int driverMinorVersion = -1;
49 | private int driverMajorVersion = -1;
50 |
51 | public java.sql.Connection connection;
52 | public String previousQuery;
53 |
54 | public java.sql.Statement statement;
55 | public java.sql.ResultSet result;
56 |
57 | private boolean DEBUG = true;
58 | //private HashMap columnNamesCache;
59 | private NameMapper mapper;
60 | private HashMap classToTableMap;
61 | protected ArrayList tableNames;
62 |
63 | /**
64 | * Do not use this contructor.
65 | */
66 | public SQL ()
67 | {
68 | System.out.println(
69 | "SQL(): Please use this constructor\n"+
70 | "\tSQL ( String _serv, String _db, String _u, String _p, PApplet _pa )"
71 | );
72 |
73 | mapper = new de.bezier.data.sql.mapper.UnderScoreToCamelCaseMapper();
74 | }
75 |
76 |
77 | /**
78 | * You should not directly use the SQL.class instead use the classes for your database type.
79 | */
80 | public SQL ( PApplet _pa, String _db )
81 | {
82 | this.user = "";
83 | this.pass = "";
84 | this.server = "";
85 |
86 | String f = _pa.dataPath(_db);
87 | File ff = new File(f);
88 | if ( !ff.exists() || !ff.canRead() )
89 | {
90 | f = _pa.sketchPath( _db );
91 | ff = new File(f);
92 |
93 | if ( !ff.exists() || !ff.canRead() )
94 | {
95 | f = _db;
96 | ff = new File(f);
97 |
98 | if ( !ff.exists() || !ff.canRead() )
99 | {
100 | System.err.println(
101 | "Sorry can't find any file named " + _db +
102 | " make sure it exists and the path is correct."
103 | );
104 | }
105 | }
106 | }
107 |
108 | PApplet.println( "Using this database: " + f );
109 |
110 | this.database = f;
111 |
112 | this.url = "jdbc:" + type + ":" + database;
113 |
114 | this.papplet = _pa;
115 | papplet.registerMethod("dispose", this);
116 | mapper = new de.bezier.data.sql.mapper.UnderScoreToCamelCaseMapper();
117 | }
118 |
119 |
120 | /**
121 | * You should not directly use the SQL.class instead use the classes for your database type.
122 | */
123 | public SQL ( PApplet _pa, String _serv, String _db, String _u, String _p )
124 | {
125 | this.server = _serv;
126 | this.database = _db;
127 |
128 | this.url = "jdbc:" + type + "://" + server + "/" + database;
129 |
130 | this.user = _u;
131 | this.pass = _p;
132 |
133 | this.papplet = _pa;
134 | papplet.registerMethod("dispose", this);
135 | mapper = new de.bezier.data.sql.mapper.UnderScoreToCamelCaseMapper();
136 | }
137 |
138 |
139 | /**
140 | * Turn some debugging on/off.
141 | *
142 | * @param yesNo Turn it on or off
143 | */
144 | public void setDebug ( boolean yesNo )
145 | {
146 | DEBUG = yesNo;
147 | }
148 |
149 |
150 | /**
151 | * Get current debugging setting
152 | *
153 | * @param yesNo Turn it on or off
154 | */
155 | public boolean getDebug ()
156 | {
157 | return DEBUG;
158 | }
159 |
160 |
161 | /**
162 | * Open the database connection with the parameters given in the contructor.
163 | */
164 | public boolean connect()
165 | {
166 | if ( driver == null || driver.equals("") ||
167 | type == null || type.equals("") )
168 | {
169 | System.out.println( "SQL.connect(): You have to set a driver and type first." );
170 | return false;
171 | }
172 |
173 | // TODO: need to add mechanisms for different connection types and parameters, see:
174 | // http://jdbc.postgresql.org/documentation/83/connect.html
175 |
176 | try
177 | {
178 | Class.forName(driver);
179 | connection = java.sql.DriverManager.getConnection(url, user, pass);
180 |
181 | }
182 | catch (ClassNotFoundException e)
183 | {
184 | System.out.println( "SQL.connect(): Could not find the database driver ( "+driver+" ).\r" );
185 | if (DEBUG) e.printStackTrace();
186 | return false;
187 |
188 | }
189 | catch (java.sql.SQLException e)
190 | {
191 | System.out.println( "SQL.connect(): Could not connect to the database ( "+url+" ).\r" );
192 | if (DEBUG) e.printStackTrace();
193 | return false;
194 |
195 | }
196 |
197 | getTableNames();
198 |
199 | try {
200 | Driver jdbcDriver = java.sql.DriverManager.getDriver( url );
201 | if ( jdbcDriver != null ) {
202 | driverMinorVersion = jdbcDriver.getMinorVersion();
203 | driverMajorVersion = jdbcDriver.getMajorVersion();
204 |
205 | if ( DEBUG ) System.out.println( "Using driver " + getDriverVersion() );
206 | }
207 | } catch ( SQLException sqle ) {
208 | sqle.printStackTrace();
209 | }
210 |
211 | return true;
212 | }
213 |
214 |
215 | /**
216 | * Return the version of the currently active JDBC driver
217 | *
218 | * @return String The version of the current driver
219 | */
220 | public String getDriverVersion ()
221 | {
222 | if ( connection == null ) {
223 | System.out.println( "SQL.getDriverVersion(): you need to connect() first" );
224 | return null;
225 | } else if ( driver == null || driver.equals("") ) {
226 | System.out.println( "SQL.getDriverVersion(): no driver specified ... or it is null" );
227 | return null;
228 | }
229 | return driver + " " + driverMajorVersion + "." + driverMinorVersion;
230 | }
231 |
232 | private void preQueryOrExecute ()
233 | {
234 | result = null;
235 | }
236 |
237 |
238 | /**
239 | * Execute a SQL command on the open database connection.
240 | *
241 | * @param _sql The SQL command to execute
242 | */
243 | public void execute ( String _sql )
244 | {
245 | preQueryOrExecute();
246 |
247 | query( _sql, false );
248 | }
249 |
250 |
251 | /**
252 | * Execute a SQL command on the open database connection.
253 | * Arguments are passed to String.format() first.
254 | *
255 | * @param _sql SQL command as pattern for String.format()
256 | * @param args Zero or more objects to be passed to String.format()
257 | *
258 | * @see Format syntax
259 | * @see java.lang.String#format(java.lang.String,java.lang.Object...)
260 | */
261 | public void execute ( String _sql, Object ... args )
262 | {
263 | preQueryOrExecute();
264 |
265 | if ( args == null || args.length == 0 ) queryOrExecute( _sql, false );
266 |
267 | Method meth = null;
268 | try {
269 | meth = String.class.getMethod(
270 | "format",
271 | String.class,
272 | java.lang.reflect.Array.newInstance(Object.class,0).getClass()
273 | );
274 | } catch ( Exception ex ) {
275 | ex.printStackTrace();
276 | return;
277 | }
278 | // Object[] args2 = new Object[args.length+1];
279 | // args2[0] = _sql;
280 | // System.arraycopy( args, 0, args2, 1, args.length );
281 | String sql2 = null;
282 | try {
283 | sql2 = (String)meth.invoke( null, _sql, args );
284 | } catch ( Exception ex ) {
285 | if (DEBUG) ex.printStackTrace();
286 | }
287 |
288 | queryOrExecute( sql2, false );
289 | }
290 |
291 |
292 | /**
293 | * Issue a query on the open database connection
294 | *
295 | * @param _sql SQL command to execute for the query
296 | */
297 | public void query ( String _sql )
298 | {
299 | preQueryOrExecute();
300 |
301 | queryOrExecute( _sql, true );
302 | }
303 |
304 |
305 | /**
306 | * Issue a query on the open database connection.
307 | * Arguments are passed to String.format() first.
308 | *
309 | * @param _sql SQL command as pattern for String.format()
310 | * @param args Zero or more objects to be passed to String.format()
311 | *
312 | * @see Format syntax
313 | * @see java.lang.String#format(java.lang.String,java.lang.Object...)
314 | */
315 | public void query ( String _sql, Object ... args )
316 | {
317 | preQueryOrExecute();
318 |
319 | if ( args == null || args.length == 0 ) queryOrExecute( _sql, true );
320 |
321 | Method meth = null;
322 | try {
323 | meth = String.class.getMethod(
324 | "format",
325 | String.class,
326 | java.lang.reflect.Array.newInstance(Object.class,0).getClass()
327 | );
328 | } catch ( Exception ex ) {
329 | ex.printStackTrace();
330 | return;
331 | }
332 |
333 | String sql2 = null;
334 | try {
335 | sql2 = (String)meth.invoke( null, _sql, args );
336 | } catch ( Exception ex ) {
337 | if (DEBUG) ex.printStackTrace();
338 | }
339 |
340 | queryOrExecute( sql2, true );
341 | }
342 |
343 |
344 | /**
345 | * Query implemenbtation called by execute() / query()
346 | */
347 | private void queryOrExecute ( String _sql, boolean keep )
348 | {
349 | if ( connection == null )
350 | {
351 | System.out.println( "SQL.query(): You need to connect() first." );
352 | return;
353 | }
354 |
355 | previousQuery = _sql;
356 |
357 | try
358 | {
359 | if ( statement == null )
360 | {
361 | statement = connection.createStatement();
362 | }
363 |
364 | boolean hasResults = statement.execute( _sql );
365 |
366 | if ( keep && hasResults )
367 | {
368 | this.result = statement.getResultSet();
369 | }
370 | }
371 | catch ( java.sql.SQLException e )
372 | {
373 | System.out.println( "SQL.query(): java.sql.SQLException.\r" );
374 | if (DEBUG) {
375 | System.out.println( _sql );
376 | e.printStackTrace();
377 | }
378 | }
379 | }
380 |
381 |
382 | /**
383 | * Check if more results (rows) are available. This needs to be called before any results can be retrieved.
384 | *
385 | * @return boolean true if more results are available, false otherwise
386 | */
387 | public boolean next ()
388 | {
389 | if ( result == null )
390 | {
391 | System.out.println( "SQL.next(): You need to query() something first." );
392 | return false;
393 | }
394 |
395 | try
396 | {
397 | return result.next();
398 | }
399 | catch ( java.sql.SQLException e )
400 | {
401 | System.out.println( "SQL.next(): java.sql.SQLException.\r" );
402 | if ( DEBUG ) e.printStackTrace();
403 | }
404 | return false;
405 | }
406 |
407 |
408 | /**
409 | * Get names of available tables in active database,
410 | * needs to be implemented per db adapter.
411 | *
412 | * @return String[] The table names
413 | */
414 | abstract public String[] getTableNames ();
415 |
416 |
417 | /**
418 | * Returns an array with the column names of the last request.
419 | *
420 | * @return String[] the column names of last result or null
421 | */
422 | public String[] getColumnNames ()
423 | {
424 | String[] colNames = null;
425 |
426 | if ( result == null )
427 | {
428 | System.out.println( "SQL.getColumnNames(): You need to query() something first." );
429 | return null;
430 | }
431 |
432 | // if ( columnNamesCache == null )
433 | // columnNamesCache = new HashMap();
434 | //
435 | // colNames = columnNamesCache.get( result );
436 | // if ( colNames != null ) return colNames;
437 |
438 | java.sql.ResultSetMetaData meta = null;
439 | try {
440 | meta = result.getMetaData();
441 | } catch ( SQLException sqle ) {
442 | if (DEBUG) sqle.printStackTrace();
443 | return null;
444 | }
445 |
446 | if ( meta != null )
447 | {
448 | try {
449 | colNames = new String[ meta.getColumnCount() ];
450 | for ( int i = 1, k = meta.getColumnCount(); i <= k; i++ )
451 | {
452 | colNames[i-1] = meta.getColumnName( i );
453 | }
454 | } catch ( SQLException sqle ) {
455 | if (DEBUG) sqle.printStackTrace();
456 | return null;
457 | }
458 | }
459 |
460 | // columnNamesCache.clear();
461 | // columnNamesCache.put( result, colNames );
462 |
463 | return colNames;
464 | }
465 |
466 |
467 | /**
468 | * Get connection. ... in case you want to do JDBC stuff directly.
469 | *
470 | * @return java.sql.Connection The connection
471 | */
472 | public java.sql.Connection getConnection ()
473 | {
474 | return connection;
475 | }
476 |
477 |
478 | /**
479 | * Read an integer value from the specified field.
480 | * Represents an INT / INTEGER type:
481 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
482 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
483 | *
484 | * @param _field The name of the field
485 | * @return int Value of the field or 0
486 | */
487 | public int getInt ( String _field )
488 | {
489 | // TODO: 0 does not seem to be a good return value for a numeric field to indicate failure
490 | // same goes for other numeric fields
491 |
492 | if ( result == null )
493 | {
494 | System.out.println( "SQL.getInt(): You need to query() something first." );
495 | return 0;
496 | }
497 |
498 | try
499 | {
500 | return result.getInt( _field );
501 | }
502 | catch ( java.sql.SQLException e )
503 | {
504 | System.out.println( "SQL.getInt(): java.sql.SQLException.\r" );
505 | if (DEBUG) e.printStackTrace();
506 | }
507 | return 0;
508 | }
509 |
510 | /**
511 | * Read an integer value from the specified field.
512 | * Represents an INT / INTEGER type:
513 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
514 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
515 | *
516 | * @param _column The column index of the field to read
517 | * @return int Value of the field or 0
518 | */
519 | public int getInt ( int _column )
520 | {
521 | if ( result == null )
522 | {
523 | System.out.println( "SQL.getInt(): You need to query() something first." );
524 | return 0;
525 | }
526 |
527 | try
528 | {
529 | return result.getInt( _column );
530 | }
531 | catch ( java.sql.SQLException e )
532 | {
533 | System.out.println( "SQL.getInt(): java.sql.SQLException.\r" );
534 | if (DEBUG) e.printStackTrace();
535 | }
536 | return 0;
537 | }
538 |
539 |
540 | /**
541 | * Read a long value from the specified field.
542 | * Represents a BIGINT type:
543 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
544 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
545 | *
546 | * @param _field The name of the field
547 | * @return long Value of the field or 0
548 | */
549 | public long getLong ( String _field )
550 | {
551 | if ( result == null )
552 | {
553 | System.out.println( "SQL.getLong(): You need to query() something first." );
554 | return 0;
555 | }
556 |
557 | try
558 | {
559 | return result.getLong( _field );
560 | }
561 | catch ( java.sql.SQLException e )
562 | {
563 | System.out.println( "SQL.getLong(): java.sql.SQLException.\r" );
564 | if (DEBUG) e.printStackTrace();
565 | }
566 | return 0;
567 | }
568 |
569 |
570 | /**
571 | * Read a long value from the specified field.
572 | * Represents a BIGINT type:
573 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
574 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
575 | *
576 | * @param _column The column index of the field
577 | * @return long Value of the field or 0
578 | */
579 | public long getLong ( int _column )
580 | {
581 | if ( result == null )
582 | {
583 | System.out.println( "SQL.getLong(): You need to query() something first." );
584 | return 0;
585 | }
586 |
587 | try
588 | {
589 | return result.getLong( _column );
590 | }
591 | catch ( java.sql.SQLException e )
592 | {
593 | System.out.println( "SQL.getLong(): java.sql.SQLException.\r" );
594 | if (DEBUG) e.printStackTrace();
595 | }
596 | return 0;
597 | }
598 |
599 |
600 | /**
601 | * Read a float value from the specified field.
602 | * Represents a REAL type:
603 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
604 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
605 | *
606 | * @param _field The name of the field
607 | * @return float Value of the field or 0
608 | */
609 | public float getFloat ( String _field )
610 | {
611 | if ( result == null )
612 | {
613 | System.out.println( "SQL.getFloat(): You need to query() something first." );
614 | return 0.0f;
615 | }
616 |
617 | try
618 | {
619 | return result.getFloat( _field );
620 | }
621 | catch ( java.sql.SQLException e )
622 | {
623 | System.out.println( "SQL.getFloat(): java.sql.SQLException.\r" );
624 | if (DEBUG) e.printStackTrace();
625 | }
626 | return 0.0f;
627 | }
628 |
629 |
630 | /**
631 | * Read a float value from the specified field.
632 | * Represents a REAL type:
633 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
634 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
635 | *
636 | * @param _column The index of the column of the field
637 | * @return float Value of the field or 0
638 | */
639 | public float getFloat ( int _column )
640 | {
641 | if ( result == null )
642 | {
643 | System.out.println( "SQL.getFloat(): You need to query() something first." );
644 | return 0.0f;
645 | }
646 |
647 | try
648 | {
649 | return result.getFloat( _column );
650 | }
651 | catch ( java.sql.SQLException e )
652 | {
653 | System.out.println( "SQL.getFloat(): java.sql.SQLException.\r" );
654 | if (DEBUG) e.printStackTrace();
655 | }
656 | return 0.0f;
657 | }
658 |
659 |
660 | /**
661 | * Read a double value from the specified field.
662 | * Represents FLOAT and DOUBLE types:
663 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
664 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
665 | *
666 | * @param _field The name of the field
667 | * @return double Value of the field or 0
668 | */
669 | public double getDouble ( String _field )
670 | {
671 | if ( result == null )
672 | {
673 | System.out.println( "SQL.getDouble(): You need to query() something first." );
674 | return 0.0;
675 | }
676 |
677 | try
678 | {
679 | return result.getDouble( _field );
680 | }
681 | catch ( java.sql.SQLException e )
682 | {
683 | System.out.println( "SQL.getDouble(): java.sql.SQLException.\r" );
684 | if (DEBUG) e.printStackTrace();
685 | }
686 | return 0.0;
687 | }
688 |
689 |
690 | /**
691 | * Read a double value from the specified field.
692 | * Represents FLOAT and DOUBLE types:
693 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
694 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
695 | *
696 | * @param _column The column index of the field
697 | * @return double Value of the field or 0
698 | */
699 | public double getDouble ( int _column )
700 | {
701 | if ( result == null )
702 | {
703 | System.out.println( "SQL.getDouble(): You need to query() something first." );
704 | return 0.0;
705 | }
706 |
707 | try
708 | {
709 | return result.getDouble( _column );
710 | }
711 | catch ( java.sql.SQLException e )
712 | {
713 | System.out.println( "SQL.getDouble(): java.sql.SQLException.\r" );
714 | if (DEBUG) e.printStackTrace();
715 | }
716 | return 0.0;
717 | }
718 |
719 |
720 | /**
721 | * Read a java.math.BigDecimal value from the specified field.
722 | * Represents DECIMAL and NUMERIC types:
723 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
724 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
725 | *
726 | * @param _field The name of the field
727 | * @return java.math.BigDecimal Value of the field or null
728 | */
729 | public java.math.BigDecimal getBigDecimal ( String _field )
730 | {
731 | if ( result == null )
732 | {
733 | System.out.println( "SQL.getBigDecimal(): You need to query() something first." );
734 | return null;
735 | }
736 |
737 | try
738 | {
739 | return result.getBigDecimal( _field );
740 | }
741 | catch ( java.sql.SQLException e )
742 | {
743 | System.out.println( "SQL.getBigDecimal(): java.sql.SQLException.\r" );
744 | if (DEBUG) e.printStackTrace();
745 | }
746 | return null;
747 | }
748 |
749 |
750 | /**
751 | * Read a java.math.BigDecimal value from the specified field.
752 | * Represents DECIMAL and NUMERIC types:
753 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
754 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
755 | *
756 | * @param _column The column index of the field
757 | * @return java.math.BigDecimal Value of the field or null
758 | */
759 | public java.math.BigDecimal getBigDecimal ( int _column )
760 | {
761 | if ( result == null )
762 | {
763 | System.out.println( "SQL.getBigDecimal(): You need to query() something first." );
764 | return null;
765 | }
766 |
767 | try
768 | {
769 | return result.getBigDecimal( _column );
770 | }
771 | catch ( java.sql.SQLException e )
772 | {
773 | System.out.println( "SQL.getBigDecimal(): java.sql.SQLException.\r" );
774 | if (DEBUG) e.printStackTrace();
775 | }
776 | return null;
777 | }
778 |
779 |
780 | /**
781 | * Read a boolean value from the specified field.
782 | * Represents BIT type:
783 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
784 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
785 | *
786 | * @param _field The name of the field
787 | * @return boolean Value of the field or 0
788 | */
789 | public boolean getBoolean ( String _field )
790 | {
791 | if ( result == null )
792 | {
793 | System.out.println( "SQL.getBoolean(): You need to query() something first." );
794 | return false;
795 | }
796 |
797 | try
798 | {
799 | return result.getBoolean( _field );
800 | }
801 | catch ( java.sql.SQLException e )
802 | {
803 | System.out.println( "SQL.getBoolean(): java.sql.SQLException.\r" );
804 | if (DEBUG) e.printStackTrace();
805 | }
806 | return false;
807 | }
808 |
809 |
810 | /**
811 | * Read a boolean value from the specified field.
812 | * Represents BIT type:
813 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
814 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
815 | *
816 | * @param _column The column index of the field
817 | * @return boolean Value of the field or 0
818 | */
819 | public boolean getBoolean ( int _column )
820 | {
821 | if ( result == null )
822 | {
823 | System.out.println( "SQL.getBoolean(): You need to query() something first." );
824 | return false;
825 | }
826 |
827 | try
828 | {
829 | return result.getBoolean( _column );
830 | }
831 | catch ( java.sql.SQLException e )
832 | {
833 | System.out.println( "SQL.getBoolean(): java.sql.SQLException.\r" );
834 | if (DEBUG) e.printStackTrace();
835 | }
836 | return false;
837 | }
838 |
839 |
840 | /**
841 | * Read a String value from the specified field.
842 | * Represents VARCHAR and CHAR types:
843 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
844 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
845 | *
846 | * @param _field The name of the field
847 | * @return String Value of the field or null
848 | */
849 | public String getString ( String _field )
850 | {
851 | if ( result == null )
852 | {
853 | System.out.println( "SQL.getString(): You need to query() something first." );
854 | return null;
855 | }
856 |
857 | try
858 | {
859 | return result.getString( _field );
860 | }
861 | catch ( java.sql.SQLException e )
862 | {
863 | System.out.println( "SQL.getString(): java.sql.SQLException.\r" );
864 | if (DEBUG) e.printStackTrace();
865 | }
866 | return null;
867 | }
868 |
869 | /**
870 | * Read a String value from the specified field.
871 | * Represents VARCHAR and CHAR types:
872 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
873 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
874 | *
875 | * @param _column The column index of the field
876 | * @return String Value of the field or null
877 | */
878 | public String getString ( int _column )
879 | {
880 | if ( result == null )
881 | {
882 | System.out.println( "SQL.getString(): You need to query() something first." );
883 | return null;
884 | }
885 |
886 | try
887 | {
888 | return result.getString( _column );
889 | }
890 | catch ( java.sql.SQLException e )
891 | {
892 | System.out.println( "SQL.getString(): java.sql.SQLException.\r" );
893 | if (DEBUG) e.printStackTrace();
894 | }
895 | return null;
896 | }
897 |
898 | /**
899 | * Read a java.sql.Date value from the specified field.
900 | * Represents DATE type:
901 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
902 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
903 | *
904 | * @param _field The name of the field
905 | * @return java.sql.Date Value of the field or null
906 | */
907 | public java.sql.Date getDate ( String _field )
908 | {
909 | if ( result == null )
910 | {
911 | System.out.println( "SQL.getDate(): You need to query() something first." );
912 | return null;
913 | }
914 |
915 | try
916 | {
917 | return result.getDate( _field );
918 | }
919 | catch ( java.sql.SQLException e )
920 | {
921 | System.out.println( "SQL.getDate(): java.sql.SQLException.\r" );
922 | if (DEBUG) e.printStackTrace();
923 | }
924 | return null;
925 | }
926 |
927 |
928 | /**
929 | * Read a java.sql.Date value from the specified field.
930 | * Represents DATE type:
931 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
932 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
933 | *
934 | * @param _column The column index of the field
935 | * @return java.sql.Date Value of the field or null
936 | */
937 | public java.sql.Date getDate ( int _column )
938 | {
939 | if ( result == null )
940 | {
941 | System.out.println( "SQL.getDate(): You need to query() something first." );
942 | return null;
943 | }
944 |
945 | try
946 | {
947 | return result.getDate( _column );
948 | }
949 | catch ( java.sql.SQLException e )
950 | {
951 | System.out.println( "SQL.getDate(): java.sql.SQLException.\r" );
952 | if (DEBUG) e.printStackTrace();
953 | }
954 | return null;
955 | }
956 |
957 |
958 | /**
959 | * Read a java.sql.Time value from the specified field.
960 | * Represents TIME type:
961 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
962 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
963 | *
964 | * @param _field The name of the field
965 | * @return java.sql.Time Value of the field or null
966 | */
967 | public java.sql.Time getTime ( String _field )
968 | {
969 | if ( result == null )
970 | {
971 | System.out.println( "SQL.getTime(): You need to query() something first." );
972 | return null;
973 | }
974 |
975 | try
976 | {
977 | return result.getTime( _field );
978 | }
979 | catch ( java.sql.SQLException e )
980 | {
981 | System.out.println( "SQL.getTime(): java.sql.SQLException.\r" );
982 | if (DEBUG) e.printStackTrace();
983 | }
984 | return null;
985 | }
986 |
987 |
988 | /**
989 | * Read a java.sql.Time value from the specified field.
990 | * Represents TIME type:
991 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
992 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
993 | *
994 | * @param _column The column index of the field
995 | * @return java.sql.Time Value of the field or null
996 | */
997 | public java.sql.Time getTime ( int _column )
998 | {
999 | if ( result == null )
1000 | {
1001 | System.out.println( "SQL.getTime(): You need to query() something first." );
1002 | return null;
1003 | }
1004 |
1005 | try
1006 | {
1007 | return result.getTime( _column );
1008 | }
1009 | catch ( java.sql.SQLException e )
1010 | {
1011 | System.out.println( "SQL.getTime(): java.sql.SQLException.\r" );
1012 | if (DEBUG) e.printStackTrace();
1013 | }
1014 | return null;
1015 | }
1016 |
1017 |
1018 | /**
1019 | * Read a java.sql.Timestamp value from the specified field.
1020 | * Represents TIMESTAMP type:
1021 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
1022 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
1023 | *
1024 | * @param _field The name of the field
1025 | * @return java.sql.Timestamp Value of the field or null
1026 | */
1027 | public java.sql.Timestamp getTimestamp ( String _field )
1028 | {
1029 | if ( result == null )
1030 | {
1031 | System.out.println( "SQL.getTimestamp(): You need to query() something first." );
1032 | return null;
1033 | }
1034 |
1035 | try
1036 | {
1037 | return result.getTimestamp( _field );
1038 | }
1039 | catch ( java.sql.SQLException e )
1040 | {
1041 | System.out.println( "SQL.getTimestamp(): java.sql.SQLException.\r" );
1042 | if (DEBUG) e.printStackTrace();
1043 | }
1044 | return null;
1045 | }
1046 |
1047 |
1048 | /**
1049 | * Read a java.sql.Timestamp value from the specified field.
1050 | * Represents TIMESTAMP type:
1051 | * http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
1052 | * "8.9.6 Conversions by ResultSet.getXXX Methods"
1053 | *
1054 | * @param _column The column index of the field
1055 | * @return java.sql.Timestamp Value of the field or null
1056 | */
1057 | public java.sql.Timestamp getTimestamp ( int _column )
1058 | {
1059 | if ( result == null )
1060 | {
1061 | System.out.println( "SQL.getTimestamp(): You need to query() something first." );
1062 | return null;
1063 | }
1064 |
1065 | try
1066 | {
1067 | return result.getTimestamp( _column );
1068 | }
1069 | catch ( java.sql.SQLException e )
1070 | {
1071 | System.out.println( "SQL.getTimestamp(): java.sql.SQLException.\r" );
1072 | if (DEBUG) e.printStackTrace();
1073 | }
1074 | return null;
1075 | }
1076 |
1077 |
1078 | /**
1079 | * Read a value from the specified field to have it returned as an byte[]
1080 | *
1081 | * @param _field The name of the field
1082 | * @return byte[] byte array representing a blob
1083 | */
1084 | public byte[] getBlob ( String _field )
1085 | {
1086 | if ( result == null )
1087 | {
1088 | System.out.println( "SQL.getBlob(): You need to query() something first." );
1089 | return null;
1090 | }
1091 |
1092 | try
1093 | {
1094 | Blob blob = result.getBlob( _field );
1095 | return blob.getBytes(1,(int)blob.length());
1096 | }
1097 | catch ( java.sql.SQLException e )
1098 | {
1099 | System.out.println( "SQL.getObject(): java.sql.SQLException.\r" );
1100 | if (DEBUG) e.printStackTrace();
1101 | }
1102 | return null;
1103 | }
1104 |
1105 |
1106 | /**
1107 | * Read a value from the specified field to have it returned as an byte[]
1108 | *
1109 | * @param _column The column index of the field
1110 | * @return byte[] byte array representing a blob
1111 | */
1112 | public byte[] getBlob ( int _column )
1113 | {
1114 | if ( result == null )
1115 | {
1116 | System.out.println( "SQL.getBlob(): You need to query() something first." );
1117 | return null;
1118 | }
1119 |
1120 | try
1121 | {
1122 | Blob blob = result.getBlob( _column );
1123 | return blob.getBytes(1,(int)blob.length());
1124 | }
1125 | catch ( java.sql.SQLException e )
1126 | {
1127 | System.out.println( "SQL.getObject(): java.sql.SQLException.\r" );
1128 | if (DEBUG) e.printStackTrace();
1129 | }
1130 | return null;
1131 | }
1132 |
1133 |
1134 | /**
1135 | * Read a value from the specified field to have it returned as an object.
1136 | *
1137 | * @param _field The name of the field
1138 | * @return Object Value of the field or null
1139 | */
1140 | public Object getObject ( String _field )
1141 | {
1142 | if ( result == null )
1143 | {
1144 | System.out.println( "SQL.getObject(): You need to query() something first." );
1145 | return null;
1146 | }
1147 |
1148 | try
1149 | {
1150 | return result.getObject( _field );
1151 | }
1152 | catch ( java.sql.SQLException e )
1153 | {
1154 | System.out.println( "SQL.getObject(): java.sql.SQLException.\r" );
1155 | if (DEBUG) e.printStackTrace();
1156 | }
1157 | return null;
1158 | }
1159 |
1160 |
1161 | /**
1162 | * Read a value from the specified field to have it returned as an object.
1163 | *
1164 | * @param _column The column index of the field
1165 | * @return Object Value of the field or null
1166 | */
1167 | public Object getObject ( int _column )
1168 | {
1169 | if ( result == null )
1170 | {
1171 | System.out.println( "SQL.getObject(): You need to query() something first." );
1172 | return null;
1173 | }
1174 |
1175 | try
1176 | {
1177 | return result.getObject( _column );
1178 | }
1179 | catch ( java.sql.SQLException e )
1180 | {
1181 | System.out.println( "SQL.getObject(): java.sql.SQLException.\r" );
1182 | if (DEBUG) e.printStackTrace();
1183 | }
1184 | return null;
1185 | }
1186 |
1187 |
1188 | /**
1189 | * Close the database connection
1190 | */
1191 | public void close()
1192 | {
1193 | dispose();
1194 | }
1195 |
1196 |
1197 | /**
1198 | * Callback function for PApplet.registerDispose()
1199 | *
1200 | * @see processing.core.PApplet.registerDispose(java.lang.Object)
1201 | */
1202 | public void dispose ()
1203 | {
1204 | if ( result != null )
1205 | {
1206 | try
1207 | {
1208 | result.close();
1209 | }
1210 | catch ( java.sql.SQLException e ) { ; }
1211 |
1212 | result = null;
1213 | }
1214 |
1215 | if ( statement != null )
1216 | {
1217 | try
1218 | {
1219 | statement.close();
1220 | }
1221 | catch ( java.sql.SQLException e ) { ; }
1222 |
1223 | statement = null;
1224 | }
1225 |
1226 | if ( connection != null )
1227 | {
1228 | try
1229 | {
1230 | connection.close();
1231 | }
1232 | catch ( java.sql.SQLException e ) { ; }
1233 |
1234 | connection = null;
1235 | }
1236 | }
1237 |
1238 |
1239 | /**
1240 | * Generate an escaped String for a given Object
1241 | *
1242 | * @param object the Object to escape
1243 | * @return String the ecaped String representation of the Object
1244 | */
1245 | public String escape ( Object o )
1246 | {
1247 | return "\"" + o.toString().replaceAll("\"","\\\"") + "\"";
1248 | }
1249 |
1250 |
1251 | /**
1252 | * Set the current NameMapper
1253 | *
1254 | * @param mapper the name mapper
1255 | * @see de.bezier.data.sql.mapper.NameMapper
1256 | */
1257 | public void setNameMapper ( NameMapper mapper )
1258 | {
1259 | this.mapper = mapper;
1260 | }
1261 |
1262 |
1263 | /**
1264 | * Get the current NameMapper
1265 | *
1266 | * @see de.bezier.data.sql.mapper.NameMapper
1267 | */
1268 | public NameMapper getNameMapper ()
1269 | {
1270 | return mapper;
1271 | }
1272 |
1273 |
1274 | /**
1275 | * Highly experimental ...
1276 | * tries to map column names to public fields or setter methods
1277 | * in the given object.
1278 | *
1279 | * Use like so:
1280 | *
1281 | * db.query("SELECT name, id, sometime FROM table");
1282 | *
1283 | * while ( db.next() ) {
1284 | * SomeObject obj = new SomeObject();
1285 | * db.setFromRow(obj);
1286 | * // obj.name is now same as db.getString("name"), etc.
1287 | * }
1288 | *
1289 | *
1290 | * SomeObject might look like:
1291 | *
1292 | * class SomeObject {
1293 | * public String name;
1294 | * public int id;
1295 | * Date sometime;
1296 | * }
1297 | *
1298 | *
1299 | * @param object The object to populate from the currently selected row
1300 | */
1301 | public void setFromRow ( Object object )
1302 | {
1303 | if ( object == null ) {
1304 | System.err.println( "SQL.rowToObject(): Handing in null won't cut it." );
1305 | return;
1306 | }
1307 |
1308 | if ( result == null ) {
1309 | System.err.println( "SQL.rowToObject(): You need to query() something first!" );
1310 | return;
1311 | }
1312 |
1313 | String[] colNames = getColumnNames();
1314 | if ( colNames == null )
1315 | {
1316 | System.err.println(
1317 | "SQL.rowToObject(): uh-oh something went wrong: unable to get column names." );
1318 | return;
1319 | }
1320 |
1321 | if ( colNames.length > 0 )
1322 | {
1323 | Class> klass = null;
1324 | try {
1325 | klass = Class.forName("DeBezierDataSQL");
1326 | } catch ( Exception e ) {
1327 | if (DEBUG) e.printStackTrace();
1328 | }
1329 | if ( klass != null ) {
1330 | Method meth = null;
1331 | try {
1332 | meth = klass.getMethod(
1333 | "setFromRow",
1334 | new Class[]{ SQL.class, Object.class }
1335 | );
1336 | } catch ( Exception e ) {
1337 | if (DEBUG) e.printStackTrace();
1338 | }
1339 | // System.out.println( meth );
1340 | // System.out.println( meth.getParameterTypes() );
1341 | if ( meth != null ) {
1342 | try {
1343 | meth.invoke( null, this, object );
1344 | } catch ( Exception e ) {
1345 | if (DEBUG) e.printStackTrace();
1346 | }
1347 | }
1348 | }
1349 | }
1350 | }
1351 |
1352 |
1353 | /**
1354 | * Convert a field name to a setter name: fieldName -> setFieldName().
1355 | */
1356 | public String nameToSetter ( String name )
1357 | {
1358 | if ( name == null ) return name;
1359 | if ( name.length() == 0 ) return null;
1360 | return "set" + name.substring(0,1).toUpperCase() + name.substring(1);
1361 | }
1362 |
1363 |
1364 | /**
1365 | * Convert a field name to a getter name: fieldName -> getFieldName().
1366 | */
1367 | public String nameToGetter ( String name )
1368 | {
1369 | if ( name == null ) return name;
1370 | if ( name.length() == 0 ) return null;
1371 | return "get" + name.substring(0,1).toUpperCase() + name.substring(1);
1372 | }
1373 |
1374 |
1375 | /**
1376 | * Set a table name for a class.
1377 | */
1378 | public void registerTableNameForClass ( String name, Object classOrObject )
1379 | {
1380 | if ( name == null || name.equals("") || classOrObject == null ) return;
1381 |
1382 | Class klass = null;
1383 | if ( classOrObject.getClass() != Class.class )
1384 | klass = classOrObject.getClass();
1385 | else
1386 | klass = (Class)classOrObject;
1387 |
1388 | if ( classToTableMap == null )
1389 | classToTableMap = new HashMap();
1390 |
1391 | classToTableMap.put( klass, name );
1392 | if (DEBUG) System.out.println( String.format(
1393 | "Class \"%s\" is now mapped to table \"%s\"", klass.getName(), name
1394 | ));
1395 | }
1396 |
1397 |
1398 | /**
1399 | * Take an object, try to find table name from object name (or look it up),
1400 | * get fields and getters from object and pass that on to
1401 | * insertUpdateIntoDatabase(table, columns, values).
1402 | *
1403 | * @param object Object The object to save to db
1404 | *
1405 | * @see #insertUpdateInDatabase(java.lang.String, java.lang.String[], java.lang.Object[])
1406 | */
1407 | public void saveToDatabase ( Object object )
1408 | {
1409 | if ( object == null ) return;
1410 |
1411 | // Find the table name
1412 |
1413 | String tableName = null;
1414 |
1415 | if ( classToTableMap == null )
1416 | classToTableMap = new HashMap();
1417 |
1418 | tableName = classToTableMap.get(object.getClass());
1419 |
1420 | if ( tableName != null )
1421 | saveToDatabase( tableName, object );
1422 | else
1423 | {
1424 | Class klass = object.getClass();
1425 | tableName = klass.getName();
1426 |
1427 | for ( char c : new char[]{'$','.'} )
1428 | {
1429 | int indx = tableName.lastIndexOf(c);
1430 | if ( indx >= 0 ) {
1431 | tableName = tableName.substring(indx+1);
1432 | }
1433 | }
1434 |
1435 | if ( mapper != null ) {
1436 | tableName = mapper.backward(tableName);
1437 | }
1438 |
1439 | registerTableNameForClass( tableName, klass );
1440 |
1441 | saveToDatabase( tableName, object );
1442 | }
1443 | }
1444 |
1445 |
1446 | /**
1447 | * Takes a table name and an object and tries to construct a set of
1448 | * columns names from fields and getters found in the object. After
1449 | * the values are fetched from the object all is passed to
1450 | * insertUpdateIntoDatabase().
1451 | *
1452 | * @param tableName String The name of the table
1453 | * @param object Object The object to look at
1454 | *
1455 | * @see #insertUpdateInDatabase(java.lang.String, java.lang.String[], java.lang.Object[])
1456 | */
1457 | public void saveToDatabase ( String tableName, Object object )
1458 | {
1459 | if ( object == null ) return;
1460 |
1461 | String[] tableNames = getTableNames();
1462 | if ( !java.util.Arrays.asList(tableNames).contains(tableName) ) {
1463 | System.err.println(String.format(
1464 | "saveToDatabase(): table '%s' not found in database '%s'", tableName, database
1465 | ));
1466 | return;
1467 | }
1468 |
1469 | String[] colNames = getColumnNames();
1470 | String[] fieldNames = new String[colNames.length];
1471 |
1472 | if ( mapper != null )
1473 | {
1474 | for ( int i = 0; i < colNames.length; i++ )
1475 | {
1476 | //System.out.println(colNames[i]);
1477 | fieldNames[i] = mapper.forward(colNames[i]);
1478 | //System.out.println(fieldNames[i]);
1479 | }
1480 | }
1481 |
1482 | Class extends Object> klass = object.getClass();
1483 | Field[] fields = new Field[colNames.length];
1484 | Method[] getters = new Method[colNames.length];
1485 |
1486 | for ( int i = 0; i < colNames.length; i++ )
1487 | {
1488 | String fieldName = fieldNames[i];
1489 | String colName = colNames[i];
1490 |
1491 | Field f = null;
1492 | try {
1493 | f = klass.getField(fieldName);
1494 | if ( f == null ) {
1495 | f = klass.getField(colName);
1496 | }
1497 | } catch ( Exception e ) {
1498 | if (DEBUG) e.printStackTrace();
1499 | }
1500 | if ( f != null ) {
1501 | // try {
1502 | // values[i] = f.get(object);
1503 | // } catch ( Exception e ) {
1504 | // if (DEBUG) e.printStackTrace();
1505 | // }
1506 | fields[i] = f;
1507 | }
1508 | else
1509 | {
1510 | if (DEBUG) System.out.println( "Field not found, trying setter method" );
1511 |
1512 | String getterName = nameToGetter(fieldName);
1513 | Method getter = null;
1514 | try {
1515 | getter = klass.getMethod(
1516 | getterName, new Class[0]
1517 | );
1518 | } catch ( Exception e ) {
1519 | if (DEBUG) e.printStackTrace();
1520 | }
1521 | // try {
1522 | // values[i] = getter.invoke(object);
1523 | // } catch ( Exception e ) {
1524 | // if (DEBUG) e.printStackTrace();
1525 | // }
1526 | getters[i] = getter;
1527 | }
1528 | if ( fields[i] == null && getters[i] == null ) {
1529 | System.err.println(String.format(
1530 | "Unable to get a field or getter for column '%s'", colName
1531 | ));
1532 | return;
1533 | }
1534 | }
1535 |
1536 | Object[] values = null;
1537 |
1538 | Class> clazz = null;
1539 | try {
1540 | clazz = Class.forName("DeBezierDataSQL");
1541 | } catch ( Exception e ) {
1542 | if (DEBUG) e.printStackTrace();
1543 | }
1544 |
1545 | if ( klass != null ) {
1546 | Method meth = null;
1547 | try {
1548 | meth = clazz.getMethod(
1549 | "getValuesFromObject",
1550 | new Class[]{ SQL.class, Field[].class, Method[].class, Object.class }
1551 | );
1552 | } catch ( Exception e ) {
1553 | if (DEBUG) e.printStackTrace();
1554 | }
1555 | // System.out.println( meth );
1556 | // System.out.println( meth.getParameterTypes() );
1557 | if ( meth != null ) {
1558 | try {
1559 | values = (Object[])meth.invoke( null, this, fields, getters, object );
1560 | } catch ( Exception e ) {
1561 | if (DEBUG) e.printStackTrace();
1562 | }
1563 | }
1564 | }
1565 |
1566 | if ( values != null )
1567 | {
1568 | insertUpdateInDatabase( tableName, colNames, values );
1569 | }
1570 | else
1571 | {
1572 | System.err.println("saveToDatabase() : trouble, trouble!!");
1573 | }
1574 | }
1575 |
1576 |
1577 | /**
1578 | * Insert or update a bunch of values in the database. If the given table has a
1579 | * primary key the entry will be updated if it already existed.
1580 | *
1581 | * @param tableName String The name of the table
1582 | * @param columnNames String[] The names of the columns to fill or update
1583 | * @param values Object[] The values to instert or update
1584 | */
1585 | public void insertUpdateInDatabase ( String tableName, String[] columnNames, Object[] values )
1586 | {
1587 | HashMap valuesKeys = new HashMap();
1588 | for ( int i = 0; i < values.length; i++ )
1589 | {
1590 | valuesKeys.put(columnNames[i], values[i]);
1591 | }
1592 |
1593 | HashMap primaryKeys = null;
1594 | try {
1595 | DatabaseMetaData meta = connection.getMetaData();
1596 | ResultSet rs = meta.getPrimaryKeys(null, null, tableName);
1597 |
1598 | while ( rs.next() )
1599 | {
1600 | if ( primaryKeys == null )
1601 | primaryKeys = new HashMap();
1602 |
1603 | String columnName = rs.getString("COLUMN_NAME");
1604 | primaryKeys.put(columnName, valuesKeys.get(columnName));
1605 | valuesKeys.remove(columnName);
1606 | }
1607 |
1608 | } catch ( SQLException sqle ) {
1609 | sqle.printStackTrace();
1610 | }
1611 |
1612 | //System.out.println(valuesKeys);
1613 | //System.out.println(primaryKeys);
1614 |
1615 | String cols = "";
1616 | String patt = "";
1617 | HashMap valueIndices = new HashMap();
1618 | int i = 1;
1619 | for ( Map.Entry e : valuesKeys.entrySet() )
1620 | {
1621 | cols += ( i > 1 ? " , " : "" ) + e.getKey();
1622 | patt += ( i > 1 ? " , " : "" ) + "?";
1623 | valueIndices.put( e.getKey(), i );
1624 | i++;
1625 | }
1626 |
1627 | String sql = null, opts = null;
1628 | HashMap primaryIndices = null;
1629 | if ( primaryKeys == null || primaryKeys.size() == 0 ) {
1630 | sql = "INSERT INTO " + tableName + " ( " + cols + " ) VALUES ( " + patt + " )";
1631 | } else {
1632 | primaryIndices = new HashMap();
1633 | opts = " WHERE ";
1634 | int p = 1;
1635 | for ( Map.Entry e : primaryKeys.entrySet() ) {
1636 | opts += (p > 1 ? " , " : "") + e.getKey() + " = ? ";
1637 | primaryIndices.put( e.getKey(), p );
1638 | p++;
1639 | }
1640 | //System.out.println( opts );
1641 | String sqlFind = "SELECT * FROM "+tableName+" "+opts;
1642 | //System.out.println( sqlFind );
1643 | try {
1644 | PreparedStatement psFind = connection.prepareStatement( sqlFind );
1645 | for ( Map.Entry e : primaryKeys.entrySet() ) {
1646 | psFind.setString( primaryIndices.get(e.getKey()), e.getValue().toString() );
1647 | }
1648 | result = psFind.executeQuery();
1649 | boolean found = next();
1650 | psFind.close();
1651 | if ( !found ) {
1652 | if (DEBUG) System.out.println(String.format(
1653 | "No entry with %s found in table '%s'", primaryKeys.toString(), tableName
1654 | ));
1655 | int k = 1, m = valueIndices.size();
1656 | for ( Map.Entry e : primaryKeys.entrySet() ) {
1657 | cols += ( m > 0 ? " , " : "" ) + e.getKey();
1658 | patt += ( m > 0 ? " , " : "" ) + "?";
1659 | valuesKeys.put( e.getKey(), e.getValue() );
1660 | valueIndices.put( e.getKey(), m+k );
1661 | k++;
1662 | }
1663 | sql = "INSERT INTO " + tableName + " ( " + cols + " ) VALUES ( " + patt + " )";
1664 | primaryKeys = null;
1665 | }
1666 | else
1667 | {
1668 | cols = "";
1669 | for ( Map.Entry e : valuesKeys.entrySet() )
1670 | {
1671 | cols += ( cols.equals("") ? "" : " , " ) + e.getKey() + " = " + "?";
1672 | }
1673 | sql = "UPDATE "+tableName+" SET " + cols + " " + opts;
1674 | }
1675 | } catch ( java.sql.SQLException sqle ) {
1676 | sqle.printStackTrace();
1677 | }
1678 | }
1679 |
1680 | if (DEBUG) System.out.println( sql );
1681 |
1682 | try {
1683 | PreparedStatement ps = connection.prepareStatement( sql );
1684 |
1685 | for ( Map.Entry e : valuesKeys.entrySet() )
1686 | {
1687 | ps.setString( valueIndices.get(e.getKey()), e.getValue()+"" );
1688 | }
1689 |
1690 | if ( primaryKeys != null )
1691 | {
1692 | for ( Map.Entry e : primaryKeys.entrySet() )
1693 | {
1694 | ps.setString( valueIndices.size()+primaryIndices.get(e.getKey()), e.getValue()+"" );
1695 | }
1696 | }
1697 |
1698 | ps.executeUpdate();
1699 | ps.close();
1700 | } catch ( java.sql.SQLException sqle ) {
1701 | sqle.printStackTrace();
1702 | }
1703 | }
1704 | }
1705 |
--------------------------------------------------------------------------------
/src/de/bezier/data/sql/SQLite.java:
--------------------------------------------------------------------------------
1 | package de.bezier.data.sql;
2 |
3 | import processing.core.*;
4 | import java.util.ArrayList;
5 |
6 | /**
7 | * SQLite wrapper for SQL library for Processing 2+
8 | *
9 | * A wrapper around some of sun's java.sql.* classes
10 | * and the pure java "org.sqlite.JDBC" driver of the Xerial project (Apache 2 license).
11 | *
12 | * see:
13 | * http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
14 | * http://www.xerial.org/maven/repository/site/xerial-project/sqlite-jdbc/apidocs/index.html?index-all.html
15 | * http://java.sun.com/products/jdbc/
16 | *
17 | *
18 | *
19 | * @author Florian Jenett - mail@florianjenett.de
20 | *
21 | * created: 2008-11-29 12:15:15 - fjenett
22 | * modified: fjenett 20121217
23 | *
24 | */
25 |
26 | public class SQLite
27 | extends de.bezier.data.sql.SQL
28 | {
29 | /**
30 | * Creates a new SQLite connection.
31 | *
32 | * @param _papplet Your sketch, pass "this" in here
33 | * @param _database Path to the database file, if this is just a name the data and sketch folders are searched for the file
34 | */
35 |
36 | public SQLite ( PApplet _papplet, String _database )
37 | {
38 | super( _papplet, _database );
39 | init();
40 | }
41 |
42 | /**
43 | * Creates a new SQLite connection, same as SQLite( PApplet, String )
44 | *
45 | * @param _papplet Your sketch, pass "this" in here
46 | * @param _server Ignored
47 | * @param _database Path to the database file, if this is just a name the data and sketch folders are searched for the file
48 | * @param _user Ignored
49 | * @param _pass Ignored
50 | */
51 |
52 | public SQLite ( PApplet _papplet, String _server, String _database, String _user, String _pass )
53 | {
54 | this( _papplet, _database );
55 | }
56 |
57 |
58 | private void init ()
59 | {
60 | this.driver = "org.sqlite.JDBC";
61 | this.type = "sqlite";
62 |
63 | this.url = "jdbc:" + type + ":" + database;
64 | }
65 |
66 | public String[] getTableNames ()
67 | {
68 | if ( tableNames == null )
69 | {
70 | tableNames = new ArrayList();
71 | query( "SELECT name AS 'table_name' FROM SQLITE_MASTER WHERE type=\"table\"" );
72 | while ( next() ) {
73 | tableNames.add( getObject("table_name").toString() );
74 | }
75 | }
76 | return tableNames.toArray(new String[0]);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/de/bezier/data/sql/mapper/NameMapper.java:
--------------------------------------------------------------------------------
1 | package de.bezier.data.sql.mapper;
2 |
3 | /**
4 | * NameMapper is used to map database names to instance names
5 | * When setting objects from objects with SQL.setFromRow().
6 | *
7 | * This is just an interface and only one implementation is
8 | * provided in form of the default UnderScoreToCamelCaseMapper
9 | * which does: field_name -> fieldName and vv.
10 | */
11 |
12 | public interface NameMapper
13 | {
14 | /**
15 | * Maps a database name to an object name, typically
16 | * this might look like: field_name -> fieldName.
17 | */
18 | public String forward ( String name );
19 |
20 | /**
21 | * Reverse of forward, maps object names to database
22 | * names like: fieldName -> field_name.
23 | */
24 | public String backward ( String name );
25 | }
--------------------------------------------------------------------------------
/src/de/bezier/data/sql/mapper/UnderScoreToCamelCaseMapper.java:
--------------------------------------------------------------------------------
1 | package de.bezier.data.sql.mapper;
2 |
3 | /**
4 | * UnderScoreToCamelCaseMapper, does as it says.
5 | */
6 | public class UnderScoreToCamelCaseMapper implements NameMapper
7 | {
8 | public String backward ( String name )
9 | {
10 | String newName = "";
11 | for ( int i = 0, k = name.length(); i < k; i++ )
12 | {
13 | String c = name.charAt(i) + "";
14 | if ( c.toUpperCase().equals(c) && !c.toLowerCase().equals(c) ) {
15 | if ( i > 0 && i < k-1 )
16 | c = "_" + c.toLowerCase();
17 | else
18 | c = c.toLowerCase();
19 | }
20 | newName += c;
21 | }
22 | return newName;
23 | }
24 |
25 | public String forward ( String name )
26 | {
27 | String[] pieces = name.split("_");
28 | String newName = pieces[0];
29 | for ( int i = 1; i < pieces.length; i++ )
30 | {
31 | if ( pieces[i] != null && pieces[1].length() > 0 )
32 | {
33 | newName += pieces[i].substring(0,1).toUpperCase() +
34 | pieces[i].substring(1);
35 | }
36 | }
37 | return newName;
38 | }
39 |
40 | public static void main ( String ... args )
41 | {
42 | String[] test = new String[]{
43 | "created_at",
44 | "created_at_and_else",
45 | "rank_1",
46 | "_rank_abc",
47 | "rank_abc_",
48 | "camelCase_and_more"
49 | };
50 | UnderScoreToCamelCaseMapper mapper = new UnderScoreToCamelCaseMapper();
51 | for ( String t : test )
52 | {
53 | String fwd = mapper.forward(t);
54 | System.out.println( t + " -> " + fwd );
55 | String back = mapper.backward(fwd);
56 | System.out.println( fwd + " -> " + back );
57 | System.out.println( t.equals(back) );
58 | System.out.println( "" );
59 | }
60 | }
61 | }
--------------------------------------------------------------------------------