func (w *PostgresDbConnectionTestSuite) Test_CreateTable(c *gocheck.C) {
	dbConnNew := CreateTestDB()

	tableDefinition := baseDbConnection.TableDefinition{}
	tableDefinition.Name = "test_createtable"
	tableDefinition.Columns = []baseDbConnection.ColumnDefinition{
		baseDbConnection.ColumnDefinition{"id", "integer", true, "0"},
	}
	dbConnNew.CreateTable(tableDefinition)

	createdTableDefinition, _ := dbConnNew.GetTableDefinition(tableDefinition.Name)

	c.Assert(true, gocheck.Equals, reflect.DeepEqual(createdTableDefinition, tableDefinition))
	c.Assert("Table already exists.", gocheck.Equals, dbConnNew.CreateTable(tableDefinition).Error())

	dbConnNew.Exec("INSERT INTO test_createtable (id) VALUES(42)")
	res, _ := dbConnNew.Query("SELECT * FROM test_createtable")
	parsedResults := baseDbConnection.ParseResults(res)

	c.Assert(len(parsedResults), gocheck.Equals, 1)
	c.Assert(parsedResults[0]["id"].(int64), gocheck.Equals, int64(42))

	dbConnNew.Close()
	dbConnTest.DropDatabase(dbConnNew.DBName)
}
예제 #2
0
func (w *DbConnectionTestSuite) Test_CreateTable(c *gocheck.C) {
	expectedErr := "The CreateTable function is not implemented for TestDbConnection."
	tableDefinition := baseDbConnection.TableDefinition{}
	tableDefinition.Columns = []baseDbConnection.ColumnDefinition{
		baseDbConnection.ColumnDefinition{"id", "integer", true, ""},
	}
	dbConn, _ := New("testdb://")
	c.Assert(dbConn.CreateTable(tableDefinition).Error(), gocheck.Equals, expectedErr)
}
func (pgDbc PostgresDbConnection) GetTableDefinition(tableName string, dbConn *baseDbConnection.DbConnection) (baseDbConnection.TableDefinition, error) {
	// In PostgreSQL, we query the system tables in information_schema to get the list
	// of tables and their columns.
	sqlGetTable := `
SELECT c.table_name, c.column_name, c.ordinal_position,
c.data_type, CASE WHEN primary_key = 1 THEN TRUE ELSE FALSE END AS primary_key,
c.column_default
FROM information_schema.columns c LEFT OUTER JOIN
(SELECT kcu.table_name, kcu.column_name, 1 AS primary_key
FROM information_schema.key_column_usage kcu INNER JOIN
information_schema.table_constraints tc ON
tc.table_catalog = kcu.table_catalog AND
tc.table_schema = kcu.table_schema AND
tc.table_name = kcu.table_name
WHERE tc.constraint_type = 'PRIMARY KEY') pk
ON pk.table_name = c.table_name AND
pk.column_name = c.column_name
WHERE c.table_catalog = '` + dbConn.DBName + `' AND
c.table_schema = '` + dbConn.Schema + `' AND c.table_name = '` + tableName + `'
ORDER BY c.table_name, c.ordinal_position, c.column_name`

	tableDefinition := baseDbConnection.TableDefinition{}
	tableDefinition.Columns = make([]baseDbConnection.ColumnDefinition, 0)

	queryResults, err := dbConn.Query(sqlGetTable)
	tableColumns := baseDbConnection.ParseResults(queryResults)

	if err != nil {
		return baseDbConnection.TableDefinition{}, err
	}

	for _, tableColumn := range tableColumns {
		tableDefinition.Name = tableColumn["table_name"].(string)
		newColumn :=
			baseDbConnection.ColumnDefinition{
				tableColumn["column_name"].(string),
				tableColumn["data_type"].(string),
				false,
				tableColumn["column_default"].(string),
			}
		if tableColumn["primary_key"].(bool) {
			newColumn.PrimaryKey = true
		}
		tableDefinition.Columns = append(tableDefinition.Columns, newColumn)
	}

	return tableDefinition, nil
}