src/Pure/General/sql.scala
changeset 63791 c6cbdfaae19e
parent 63790 3d723062dc70
child 65003 4b4ccf86755c
equal deleted inserted replaced
63790:3d723062dc70 63791:c6cbdfaae19e
    34   def quote_ident(s: String): String =
    34   def quote_ident(s: String): String =
    35   {
    35   {
    36     require(!s.contains('`'))
    36     require(!s.contains('`'))
    37     "`" + s + "`"
    37     "`" + s + "`"
    38   }
    38   }
       
    39 
       
    40   def enclosure(ss: Iterable[String]): String = ss.mkString("(", ", ", ")")
    39 
    41 
    40 
    42 
    41   /* columns */
    43   /* columns */
    42 
    44 
    43   object Column
    45   object Column
   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