equal
deleted
inserted
replaced
53 def bytes(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] = |
53 def bytes(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] = |
54 new Column_Bytes(name, strict, primary_key) |
54 new Column_Bytes(name, strict, primary_key) |
55 } |
55 } |
56 |
56 |
57 abstract class Column[+A] private[SQL]( |
57 abstract class Column[+A] private[SQL]( |
58 val name: String, val strict: Boolean, val primary_key: Boolean) |
58 val name: String, val strict: Boolean, val primary_key: Boolean) |
|
59 extends Function[ResultSet, A] |
59 { |
60 { |
60 def sql_name: String = quote_ident(name) |
61 def sql_name: String = quote_ident(name) |
61 def sql_type: String |
62 def sql_type: String |
62 def sql_decl: String = |
63 def sql_decl: String = |
63 sql_name + " " + sql_type + |
64 sql_name + " " + sql_type + |
127 |
128 |
128 def table(name: String, columns: Column[Any]*): Table = new Table(name, columns.toList) |
129 def table(name: String, columns: Column[Any]*): Table = new Table(name, columns.toList) |
129 |
130 |
130 class Table private[SQL](name: String, columns: List[Column[Any]]) |
131 class Table private[SQL](name: String, columns: List[Column[Any]]) |
131 { |
132 { |
|
133 private val columns_index: Map[String, Int] = |
|
134 columns.iterator.map(_.name).zipWithIndex.toMap |
|
135 |
132 Library.duplicates(columns.map(_.name)) match { |
136 Library.duplicates(columns.map(_.name)) match { |
133 case Nil => |
137 case Nil => |
134 case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
138 case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
135 } |
139 } |
136 |
140 |
146 (if (rowid) "" else " WITHOUT ROWID") |
150 (if (rowid) "" else " WITHOUT ROWID") |
147 |
151 |
148 def sql_drop(strict: Boolean): String = |
152 def sql_drop(strict: Boolean): String = |
149 "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name) |
153 "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name) |
150 |
154 |
|
155 def sql_insert: String = |
|
156 "INSERT INTO " + quote_ident(name) + |
|
157 " VALUES " + columns.map(_ => "?").mkString("(", ", ", ")") |
|
158 |
151 override def toString: String = |
159 override def toString: String = |
152 "TABLE " + quote_ident(name) + " " + columns.map(_.toString).mkString("(", ", ", ")") |
160 "TABLE " + quote_ident(name) + " " + columns.map(_.toString).mkString("(", ", ", ")") |
153 } |
161 } |
|
162 |
|
163 |
|
164 /* results */ |
|
165 |
|
166 def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A] |
|
167 { |
|
168 private var _next: Boolean = rs.next() |
|
169 def hasNext: Boolean = _next |
|
170 def next: A = { val x = get(rs); _next = rs.next(); x } |
|
171 } |
154 } |
172 } |