1// Copyright (c) 2012-today The upper.io/db authors. All rights reserved. 2// 3// Permission is hereby granted, free of charge, to any person obtaining 4// a copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to 8// permit persons to whom the Software is furnished to do so, subject to 9// the following conditions: 10// 11// The above copyright notice and this permission notice shall be 12// included in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22package mssql 23 24import ( 25 "database/sql" 26 "os" 27 28 db "upper.io/db.v3" 29 "upper.io/db.v3/lib/sqlbuilder" 30 "upper.io/db.v3/internal/testsuite" 31) 32 33var settings = ConnectionURL{ 34 Database: os.Getenv("DB_NAME"), 35 User: os.Getenv("DB_USERNAME"), 36 Password: os.Getenv("DB_PASSWORD"), 37 Host: os.Getenv("DB_HOST") + ":" + os.Getenv("DB_PORT"), 38 Options: map[string]string{}, 39} 40 41type Helper struct { 42 sess sqlbuilder.Database 43} 44 45func (h *Helper) Session() db.Database { 46 return h.sess 47} 48 49func (h *Helper) SQLBuilder() sqlbuilder.Database { 50 return h.sess 51} 52 53func (h *Helper) Adapter() string { 54 return "mssql" 55} 56 57func (h *Helper) TearDown() error { 58 return h.sess.Close() 59} 60 61func (h *Helper) TearUp() error { 62 var err error 63 64 h.sess, err = Open(settings) 65 if err != nil { 66 return err 67 } 68 69 batch := []string{ 70 `DROP TABLE IF EXISTS artist`, 71 72 `CREATE TABLE artist ( 73 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 74 name VARCHAR(60) 75 )`, 76 77 `DROP TABLE IF EXISTS publication`, 78 79 `CREATE TABLE publication ( 80 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 81 title VARCHAR(80), 82 author_id BIGINT 83 )`, 84 85 `DROP TABLE IF EXISTS review`, 86 `CREATE TABLE review ( 87 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 88 publication_id BIGINT, 89 name VARCHAR(80), 90 comments TEXT, 91 created DATETIME NOT NULL 92 )`, 93 94 `DROP TABLE IF EXISTS data_types`, 95 `CREATE TABLE data_types ( 96 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 97 _uint INT DEFAULT 0, 98 _uint8 INT DEFAULT 0, 99 _uint16 INT DEFAULT 0, 100 _uint32 INT DEFAULT 0, 101 _uint64 INT DEFAULT 0, 102 _int INT DEFAULT 0, 103 _int8 INT DEFAULT 0, 104 _int16 INT DEFAULT 0, 105 _int32 INT DEFAULT 0, 106 _int64 INT DEFAULT 0, 107 _float32 DECIMAL(10,6), 108 _float64 DECIMAL(10,6), 109 _bool TINYINT, 110 _string TEXT, 111 _blob BINARY(12), 112 _date DATETIMEOFFSET(4) NULL, 113 _nildate DATETIME NULL, 114 _ptrdate DATETIME NULL, 115 _defaultdate DATETIME NOT NULL DEFAULT(GETDATE()), 116 _time BIGINT NOT NULL DEFAULT 0 117 )`, 118 119 `DROP TABLE IF EXISTS stats_test`, 120 `CREATE TABLE stats_test ( 121 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 122 [numeric] INT, 123 [value] INT 124 )`, 125 126 `DROP TABLE IF EXISTS composite_keys`, 127 `CREATE TABLE composite_keys ( 128 code VARCHAR(255) default '', 129 user_id VARCHAR(255) default '', 130 some_val VARCHAR(255) default '', 131 PRIMARY KEY (code, user_id) 132 )`, 133 134 `DROP TABLE IF EXISTS [birthdays]`, 135 `CREATE TABLE [birthdays] ( 136 id BIGINT IDENTITY(1, 1) PRIMARY KEY NOT NULL, 137 name NVARCHAR(50), 138 born DATETIMEOFFSET, 139 born_ut BIGINT 140 )`, 141 142 `DROP TABLE IF EXISTS [fibonacci]`, 143 `CREATE TABLE [fibonacci] ( 144 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 145 input BIGINT NOT NULL, 146 output BIGINT NOT NULL 147 )`, 148 149 `DROP TABLE IF EXISTS [is_even]`, 150 `CREATE TABLE [is_even] ( 151 input BIGINT NOT NULL, 152 is_even TINYINT 153 )`, 154 155 `DROP TABLE IF EXISTS [CaSe_TesT]`, 156 `CREATE TABLE [CaSe_TesT] ( 157 id BIGINT PRIMARY KEY NOT NULL IDENTITY(1,1), 158 case_test NVARCHAR(60) 159 )`, 160 } 161 162 for _, query := range batch { 163 driver := h.sess.Driver().(*sql.DB) 164 if _, err := driver.Exec(query); err != nil { 165 return err 166 } 167 } 168 169 return nil 170} 171 172var _ testsuite.Helper = &Helper{} 173