| 1 |
/* |
| 2 |
* Copyright (C)2005-2016 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
package haxelib.server; |
| 23 |
|
| 24 |
import sys.db.*; |
| 25 |
import sys.db.Types; |
| 26 |
import haxelib.server.Paths.*; |
| 27 |
|
| 28 |
class User extends Object { |
| 29 |
|
| 30 |
public var id : SId; |
| 31 |
public var name : String; |
| 32 |
public var fullname : String; |
| 33 |
public var email : String; |
| 34 |
public var pass : String; |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
class Project extends Object { |
| 39 |
|
| 40 |
public var id : SId; |
| 41 |
public var name : String; |
| 42 |
public var description : String; |
| 43 |
public var website : String; |
| 44 |
public var license : String; |
| 45 |
public var downloads : Int = 0; |
| 46 |
@:relation(owner) public var ownerObj : User; |
| 47 |
@:relation(version) public var versionObj : SNull<Version>; |
| 48 |
|
| 49 |
static public function containing( word:String ) : List<{ id: Int, name: String }> { |
| 50 |
var ret = new List(); |
| 51 |
word = '%$word%'; |
| 52 |
for (project in manager.search($name.like(word) || $description.like(word))) |
| 53 |
ret.push( { id: project.id, name: project.name } ); |
| 54 |
return ret; |
| 55 |
} |
| 56 |
|
| 57 |
static public function allByName() { |
| 58 |
//TODO: Propose SPOD patch to support manager.search(true, { orderBy: name.toLowerCase() } ); |
| 59 |
return manager.unsafeObjects('SELECT * FROM Project ORDER BY LOWER(name)', false); |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
class Tag extends Object { |
| 65 |
|
| 66 |
public var id : SId; |
| 67 |
public var tag : String; |
| 68 |
@:relation(project) public var projectObj : Project; |
| 69 |
|
| 70 |
static public function topTags( n : Int ) : List<{ tag:String, count: Int }> { |
| 71 |
return cast Manager.cnx.request("SELECT tag, COUNT(*) as count FROM Tag GROUP BY tag ORDER BY count DESC LIMIT " + n).results(); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
class Version extends Object { |
| 77 |
|
| 78 |
public var id : SId; |
| 79 |
@:relation(project) public var projectObj : Project; |
| 80 |
public var major : Int; |
| 81 |
public var minor : Int; |
| 82 |
public var patch : Int; |
| 83 |
public var preview : SNull<SEnum<SemVer.Preview>>; |
| 84 |
public var previewNum : SNull<Int>; |
| 85 |
@:skip public var name(get, never):String; |
| 86 |
function get_name():String return toSemver(); |
| 87 |
|
| 88 |
public function toSemver():SemVer { |
| 89 |
return { |
| 90 |
major: this.major, |
| 91 |
minor: this.minor, |
| 92 |
patch: this.patch, |
| 93 |
preview: this.preview, |
| 94 |
previewNum: this.previewNum, |
| 95 |
} |
| 96 |
} |
| 97 |
public var date : String; // sqlite does not have a proper 'date' type |
| 98 |
public var comments : String; |
| 99 |
public var downloads : Int; |
| 100 |
public var documentation : SNull<String>; |
| 101 |
|
| 102 |
static public function latest( n : Int ) { |
| 103 |
return manager.search(1 == 1, { orderBy: -date, limit: n } ); |
| 104 |
} |
| 105 |
|
| 106 |
static public function byProject( p : Project ) { |
| 107 |
return manager.search($project == p.id, { orderBy: -date } ); |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
@:id(user,project) |
| 113 |
class Developer extends Object { |
| 114 |
|
| 115 |
@:relation(user) public var userObj : User; |
| 116 |
@:relation(project) public var projectObj : Project; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
class SiteDb { |
| 121 |
static var db : Connection; |
| 122 |
//TODO: this whole configuration business is rather messy to say the least |
| 123 |
|
| 124 |
static public function init() { |
| 125 |
db = |
| 126 |
if (Sys.getEnv("HAXELIB_DB_HOST") != null) |
| 127 |
Mysql.connect({ |
| 128 |
"host": Sys.getEnv("HAXELIB_DB_HOST"), |
| 129 |
"port": Std.parseInt(Sys.getEnv("HAXELIB_DB_PORT")), |
| 130 |
"database": Sys.getEnv("HAXELIB_DB_NAME"), |
| 131 |
"user": Sys.getEnv("HAXELIB_DB_USER"), |
| 132 |
"pass": Sys.getEnv("HAXELIB_DB_PASS"), |
| 133 |
"socket": null |
| 134 |
}); |
| 135 |
else if (sys.FileSystem.exists(DB_CONFIG)) |
| 136 |
Mysql.connect(haxe.Json.parse(sys.io.File.getContent(DB_CONFIG))); |
| 137 |
else |
| 138 |
Sqlite.open(DB_FILE); |
| 139 |
|
| 140 |
Manager.cnx = db; |
| 141 |
Manager.initialize(); |
| 142 |
|
| 143 |
var managers:Array<Manager<Dynamic>> = [ |
| 144 |
User.manager, |
| 145 |
Project.manager, |
| 146 |
Tag.manager, |
| 147 |
Version.manager, |
| 148 |
Developer.manager |
| 149 |
]; |
| 150 |
for (m in managers) |
| 151 |
if (!TableCreate.exists(m)) |
| 152 |
TableCreate.create(m); |
| 153 |
} |
| 154 |
|
| 155 |
static public function cleanup() { |
| 156 |
db.close(); |
| 157 |
Manager.cleanup(); |
| 158 |
} |
| 159 |
} |