124 } |
126 } |
125 |
127 |
126 |
128 |
127 /* tables */ |
129 /* tables */ |
128 |
130 |
129 def table(name: String, columns: Column[Any]*): Table = new Table(name, columns.toList) |
131 def table(name: String, columns: List[Column[Any]]): Table = new Table(name, columns) |
130 |
132 |
131 class Table private[SQL](name: String, columns: List[Column[Any]]) |
133 class Table private[SQL](name: String, columns: List[Column[Any]]) |
132 { |
134 { |
133 private val columns_index: Map[String, Int] = |
135 private val columns_index: Map[String, Int] = |
134 columns.iterator.map(_.name).zipWithIndex.toMap |
136 columns.iterator.map(_.name).zipWithIndex.toMap |
144 case _ => |
146 case _ => |
145 } |
147 } |
146 |
148 |
147 def sql_create(strict: Boolean, rowid: Boolean): String = |
149 def sql_create(strict: Boolean, rowid: Boolean): String = |
148 "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
150 "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
149 quote_ident(name) + " " + columns.map(_.sql_decl).mkString("(", ", ", ")") + |
151 quote_ident(name) + " " + enclosure(columns.map(_.sql_decl)) + |
150 (if (rowid) "" else " WITHOUT ROWID") |
152 (if (rowid) "" else " WITHOUT ROWID") |
151 |
153 |
152 def sql_drop(strict: Boolean): String = |
154 def sql_drop(strict: Boolean): String = |
153 "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name) |
155 "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name) |
154 |
156 |
|
157 def sql_create_index( |
|
158 index_name: String, index_columns: List[Column[Any]], |
|
159 strict: Boolean, unique: Boolean): String = |
|
160 "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
|
161 (if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " + |
|
162 quote_ident(name) + " " + enclosure(index_columns.map(_.name)) |
|
163 |
|
164 def sql_drop_index(index_name: String, strict: Boolean): String = |
|
165 "DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name) |
|
166 |
155 def sql_insert: String = |
167 def sql_insert: String = |
156 "INSERT INTO " + quote_ident(name) + |
168 "INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?")) |
157 " VALUES " + columns.map(_ => "?").mkString("(", ", ", ")") |
169 |
|
170 def sql_select(select_columns: List[Column[Any]], distinct: Boolean): String = |
|
171 "SELECT " + (if (distinct) "DISTINCT " else "") + |
|
172 commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name) |
158 |
173 |
159 override def toString: String = |
174 override def toString: String = |
160 "TABLE " + quote_ident(name) + " " + columns.map(_.toString).mkString("(", ", ", ")") |
175 "TABLE " + quote_ident(name) + " " + enclosure(columns.map(_.toString)) |
161 } |
176 } |
162 |
177 |
163 |
178 |
164 /* results */ |
179 /* results */ |
165 |
180 |