63778
|
1 |
/* Title: Pure/Tools/sql.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
63779
|
4 |
Generic support for SQL.
|
63778
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
63779
|
10 |
import java.sql.ResultSet
|
|
11 |
|
|
12 |
|
63778
|
13 |
object SQL
|
|
14 |
{
|
|
15 |
/* concrete syntax */
|
|
16 |
|
|
17 |
def quote_char(c: Char): String =
|
|
18 |
c match {
|
|
19 |
case '\u0000' => "\\0"
|
|
20 |
case '\'' => "\\'"
|
|
21 |
case '\"' => "\\\""
|
|
22 |
case '\b' => "\\b"
|
|
23 |
case '\n' => "\\n"
|
|
24 |
case '\r' => "\\r"
|
|
25 |
case '\t' => "\\t"
|
|
26 |
case '\u001a' => "\\Z"
|
|
27 |
case '\\' => "\\\\"
|
|
28 |
case _ => c.toString
|
|
29 |
}
|
|
30 |
|
|
31 |
def quote_string(s: String): String =
|
|
32 |
quote(s.map(quote_char(_)).mkString)
|
|
33 |
|
63779
|
34 |
def quote_ident(s: String): String =
|
|
35 |
{
|
|
36 |
require(!s.contains('`'))
|
|
37 |
"`" + s + "`"
|
|
38 |
}
|
|
39 |
|
|
40 |
|
|
41 |
/* columns */
|
|
42 |
|
|
43 |
object Column
|
|
44 |
{
|
|
45 |
def int(name: String, strict: Boolean = true): Column[Int] = new Column_Int(name, strict)
|
|
46 |
def long(name: String, strict: Boolean = true): Column[Long] = new Column_Long(name, strict)
|
|
47 |
def double(name: String, strict: Boolean = true): Column[Double] = new Column_Double(name, strict)
|
|
48 |
def string(name: String, strict: Boolean = true): Column[String] = new Column_String(name, strict)
|
|
49 |
def bytes(name: String, strict: Boolean = true): Column[Bytes] = new Column_Bytes(name, strict)
|
|
50 |
}
|
|
51 |
|
|
52 |
abstract class Column[A] private[SQL](val name: String, val strict: Boolean)
|
|
53 |
{
|
|
54 |
def sql_name: String = quote_ident(name)
|
|
55 |
def sql_type: String
|
|
56 |
def sql_decl: String = sql_name + " " + sql_type + (if (strict) " NOT NULL" else "")
|
|
57 |
def result(rs: ResultSet): A
|
|
58 |
def result_string(rs: ResultSet): String = rs.getString(name)
|
|
59 |
|
|
60 |
override def toString: String = sql_decl
|
|
61 |
}
|
|
62 |
|
|
63 |
class Column_Int private[SQL](name: String, strict: Boolean)
|
|
64 |
extends Column[Int](name, strict)
|
|
65 |
{
|
|
66 |
def sql_type: String = "INTEGER"
|
|
67 |
def result(rs: ResultSet): Int = rs.getInt(name)
|
|
68 |
}
|
|
69 |
|
|
70 |
class Column_Long private[SQL](name: String, strict: Boolean)
|
|
71 |
extends Column[Long](name, strict)
|
|
72 |
{
|
|
73 |
def sql_type: String = "INTEGER"
|
|
74 |
def result(rs: ResultSet): Long = rs.getLong(name)
|
|
75 |
}
|
|
76 |
|
|
77 |
class Column_Double private[SQL](name: String, strict: Boolean)
|
|
78 |
extends Column[Double](name, strict)
|
|
79 |
{
|
|
80 |
def sql_type: String = "REAL"
|
|
81 |
def result(rs: ResultSet): Double = rs.getDouble(name)
|
|
82 |
}
|
|
83 |
|
|
84 |
class Column_String private[SQL](name: String, strict: Boolean)
|
|
85 |
extends Column[String](name, strict)
|
|
86 |
{
|
|
87 |
def sql_type: String = "TEXT"
|
|
88 |
def result(rs: ResultSet): String = rs.getString(name)
|
|
89 |
}
|
|
90 |
|
|
91 |
class Column_Bytes private[SQL](name: String, strict: Boolean)
|
|
92 |
extends Column[Bytes](name, strict)
|
|
93 |
{
|
|
94 |
def sql_type: String = "BLOB"
|
|
95 |
def result(rs: ResultSet): Bytes =
|
|
96 |
{
|
|
97 |
val bs = rs.getBytes(name)
|
|
98 |
if (bs == null) null else Bytes(bs)
|
|
99 |
}
|
|
100 |
}
|
63778
|
101 |
}
|