TableCreate.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2005-2011, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys.db;
  26. import sys.db.SpodInfos;
  27. class TableCreate {
  28. public static function getTypeSQL( t : SpodType, dbName : String ) {
  29. return switch( t ) {
  30. case DId: "INTEGER "+(dbName == "SQLite" ? "PRIMARY KEY AUTOINCREMENT" : "AUTO_INCREMENT");
  31. case DUId: "INTEGER UNSIGNED "+(dbName == "SQLite" ? "PRIMARY KEY AUTOINCREMENT" : "AUTO_INCREMENT");
  32. case DInt, DEncoded, DFlags(_): "INTEGER";
  33. case DTinyInt: "TINYINT";
  34. case DUInt: "INTEGER UNSIGNED";
  35. case DSingle: "FLOAT";
  36. case DFloat: "DOUBLE";
  37. case DBool: "TINYINT(1)";
  38. case DString(n): "VARCHAR("+n+")";
  39. case DDate: "DATE";
  40. case DDateTime: "DATETIME";
  41. case DTimeStamp: "TIMESTAMP DEFAULT 0";
  42. case DTinyText: "TINYTEXT";
  43. case DSmallText: "TEXT";
  44. case DText, DSerialized: "MEDIUMTEXT";
  45. case DSmallBinary: "BLOB";
  46. case DBinary, DNekoSerialized: "MEDIUMBLOB";
  47. case DLongBinary: "LONGBLOB";
  48. case DBigInt: "BIGINT";
  49. case DBigId: "BIGINT AUTO_INCREMENT";
  50. case DBytes(n): "BINARY(" + n + ")";
  51. case DNull, DInterval: throw "assert";
  52. };
  53. }
  54. public static function create( manager : sys.db.Manager<Dynamic>, ?engine ) {
  55. function quote(v:String):String {
  56. return untyped manager.quoteField(v);
  57. }
  58. var cnx : Connection = untyped manager.getCnx();
  59. if( cnx == null )
  60. throw "SQL Connection not initialized on Manager";
  61. var dbName = cnx.dbName();
  62. var infos = manager.dbInfos();
  63. var sql = "CREATE TABLE "+quote(infos.name)+ " (";
  64. var decls = [];
  65. for( f in infos.fields )
  66. decls.push(quote(f.name)+" "+getTypeSQL(f.t,dbName)+(f.isNull ? "" : " NOT NULL"));
  67. decls.push("PRIMARY KEY ("+Lambda.map(infos.key,quote).join(",")+")");
  68. sql += decls.join(",");
  69. sql += ")";
  70. if( engine != null )
  71. sql += "ENGINE="+engine;
  72. cnx.request(sql);
  73. }
  74. }