101 def apply(table: Table): Column = |
101 def apply(table: Table): Column = |
102 Column(Long_Name.qualify(table.name, name), T, strict = strict, primary_key = primary_key) |
102 Column(Long_Name.qualify(table.name, name), T, strict = strict, primary_key = primary_key) |
103 |
103 |
104 def ident: String = SQL.ident(name) |
104 def ident: String = SQL.ident(name) |
105 |
105 |
106 def sql_decl(sql_type: Type.Value => String): String = |
106 def decl(sql_type: Type.Value => String): String = |
107 ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
107 ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
108 |
108 |
109 def where_equal(s: String): String = "WHERE " + ident + " = " + string(s) |
109 def where_equal(s: String): String = "WHERE " + ident + " = " + string(s) |
110 |
110 |
111 override def toString: String = sql_decl(sql_type_default) |
111 override def toString: String = ident |
112 } |
112 } |
113 |
113 |
114 |
114 |
115 /* tables */ |
115 /* tables */ |
116 |
116 |
128 |
128 |
129 def expr: String = |
129 def expr: String = |
130 if (body == "") error("Missing SQL body for table " + quote(name)) |
130 if (body == "") error("Missing SQL body for table " + quote(name)) |
131 else SQL.enclose(body) |
131 else SQL.enclose(body) |
132 |
132 |
133 def sql_columns(sql_type: Type.Value => String): String = |
133 def create(strict: Boolean = false, sql_type: Type.Value => String): String = |
134 { |
134 { |
135 val primary_key = |
135 val primary_key = |
136 columns.filter(_.primary_key).map(_.name) match { |
136 columns.filter(_.primary_key).map(_.name) match { |
137 case Nil => Nil |
137 case Nil => Nil |
138 case keys => List("PRIMARY KEY " + enclosure(keys)) |
138 case keys => List("PRIMARY KEY " + enclosure(keys)) |
139 } |
139 } |
140 enclosure(columns.map(_.sql_decl(sql_type)) ::: primary_key) |
|
141 } |
|
142 |
|
143 def create(strict: Boolean = false, sql_type: Type.Value => String): String = |
|
144 "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
140 "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
145 ident + " " + sql_columns(sql_type) |
141 ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
|
142 } |
146 |
143 |
147 def create_index(index_name: String, index_columns: List[Column], |
144 def create_index(index_name: String, index_columns: List[Column], |
148 strict: Boolean = false, unique: Boolean = false): String = |
145 strict: Boolean = false, unique: Boolean = false): String = |
149 "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
146 "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
150 (if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
147 (if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
160 |
157 |
161 def select(select_columns: List[Column], sql: String = "", distinct: Boolean = false): String = |
158 def select(select_columns: List[Column], sql: String = "", distinct: Boolean = false): String = |
162 SQL.select(select_columns, distinct = distinct) + ident + |
159 SQL.select(select_columns, distinct = distinct) + ident + |
163 (if (sql == "") "" else " " + sql) |
160 (if (sql == "") "" else " " + sql) |
164 |
161 |
165 override def toString: String = |
162 override def toString: String = ident |
166 "TABLE " + ident + " " + sql_columns(sql_type_default) |
|
167 } |
163 } |
168 |
164 |
169 |
165 |
170 |
166 |
171 /** SQL database operations **/ |
167 /** SQL database operations **/ |