<commit_before>// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
	"github.com/gocql/gocql"
	"net"
	"gitlab.cisco.com/ctao/vnf-agent/examples/gocql/sql"
	"github.com/willfaught/gockle"
	"os"
	"github.com/ligato/cn-infra/db/sql/cassandra"
)

// UserTable global variable reused when building queries/statements
var UserTable = &User{}

// User is simple structure used in automated tests
type User struct {
	FirstName string `gocql:"first_name"`
	LastName  string `gocql:"last_name"`
	//NetIP      net.IP //mapped to native cassandra type
	//WrapIP  string //Wrapper01 used for custom (un)marshalling
	WrapIP2 *Wrapper01
	Udt03   *Udt03
	Udt04   Udt04
}

func main() {
	err := example()
	if err != nil {
		fmt.Println("failed ", err)
		os.Exit(1)
	} else {
		fmt.Println("sucessfull")
	}
}

func example() (err error) {
	// connect to the cluster
	cluster := gocql.NewCluster("172.17.0.1")
	//cluster.Keyspace = "demo"
	session, _ := cluster.CreateSession()

	defer session.Close()

	if err := session.Query("CREATE KEYSPACE IF NOT EXISTS demo WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};").
		Exec(); err != nil {
		return err
	}

	session.KeyspaceMetadata("demo")

	if err := session.Query(`CREATE TYPE IF NOT EXISTS udt03 (
		tx text,
		tx2 text)`).Exec(); err != nil {
		return err
	}
	if err := session.Query(`CREATE TYPE IF NOT EXISTS udt04 (
		ahoj text,
		caf frozen<udt03>)`).Exec(); err != nil {
		return err
	}

	if err := session.Query(`CREATE TABLE IF NOT EXISTS user (
			userid text PRIMARY KEY,
				first_name text,
				last_name text,
				Udt03 frozen<Udt03>,
				Udt04 frozen<Udt04>,
				NetIP inet,
				WrapIP text,
				emails set<text>,
				topscores list<int>,
				todo map<timestamp, text>
		);`).

		Exec(); err != nil {
		return err
	}

	if err := session.Query("CREATE INDEX IF NOT EXISTS demo_users_last_name ON user (last_name);").
		Exec(); err != nil {
		return err
	}

	_ /*ip01 */ , ipPrefix01, err := net.ParseCIDR("192.168.1.2/24")
	if err != nil {
		return err
	}
	db := cassandra.NewBrokerUsingSession(gockle.NewSession(session))
	err = db.Put("userid='<NAME>'", &User{"Fero", "Mrkva", /*ip01, */
		//"kkk",
		&Wrapper01{ipPrefix01}, &Udt03{Tx: "tx1", Tx2: "tx2" /*, Inet1: "172.16.58.3"*/ },

		Udt04{"kuk", &Udt03{Tx: "txxxxxxxxx1", Tx2: "txxxxxxxxx2" /*, Inet1: "172.16.58.3"*/ }},
	})
	if err != nil {
		return err
	}

	users := &[]User{}
	err = sql.SliceIt(users, db.ListValues(sql.SelectFrom(users)+sql.Where(sql.FieldEq(&UserTable.LastName, UserTable, "Mrkva"))))
	fmt.Println("users ", err, " ", users)

	return nil
}

// Wrapper01 implements gocql.Marshaller, gocql.Unmarshaller
// it uses string representation of net.IPNet
type Wrapper01 struct {
	ip *net.IPNet
}

// MarshalCQL serializes the string representation of net.IPNet
func (w *Wrapper01) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {

	if w.ip == nil {
		return []byte{}, nil
	}

	return []byte(w.ip.String()), nil
}

// UnmarshalCQL deserializes the string representation of net.IPNet
func (w *Wrapper01) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {

	if len(data) > 0 {
		_, ipPrefix, err := net.ParseCIDR(string(data))

		if err != nil {
			return err
		}<commit_msg>ODPM-419 cassandra simple example uses Expression<commit_after>// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
	"github.com/gocql/gocql"
	"net"
	"github.com/willfaught/gockle"
	"os"
	"github.com/ligato/cn-infra/db/sql"
	"github.com/ligato/cn-infra/db/sql/cassandra"
)

// UserTable global variable reused when building queries/statements
var UserTable = &User{}

// User is simple structure used in automated tests
type User struct {
	FirstName string `gocql:"first_name"`
	LastName  string `gocql:"last_name"`
	//NetIP      net.IP //mapped to native cassandra type
	//WrapIP  string //Wrapper01 used for custom (un)marshalling
	WrapIP2 *Wrapper01
	Udt03   *Udt03
	Udt04   Udt04
}

func main() {
	err := example()
	if err != nil {
		fmt.Println("failed ", err)
		os.Exit(1)
	} else {
		fmt.Println("sucessfull")
	}
}

func example() (err error) {
	// connect to the cluster
	cluster := gocql.NewCluster("172.17.0.1")
	//cluster.Keyspace = "demo"
	session, _ := cluster.CreateSession()

	defer session.Close()

	if err := session.Query("CREATE KEYSPACE IF NOT EXISTS demo WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};").
		Exec(); err != nil {
		return err
	}

	session.KeyspaceMetadata("demo")

	if err := session.Query(`CREATE TYPE IF NOT EXISTS udt03 (
		tx text,
		tx2 text)`).Exec(); err != nil {
		return err
	}
	if err := session.Query(`CREATE TYPE IF NOT EXISTS udt04 (
		ahoj text,
		caf frozen<udt03>)`).Exec(); err != nil {
		return err
	}

	if err := session.Query(`CREATE TABLE IF NOT EXISTS user (
			userid text PRIMARY KEY,
				first_name text,
				last_name text,
				Udt03 frozen<Udt03>,
				Udt04 frozen<Udt04>,
				NetIP inet,
				WrapIP text,
				emails set<text>,
				topscores list<int>,
				todo map<timestamp, text>
		);`).

		Exec(); err != nil {
		return err
	}

	if err := session.Query("CREATE INDEX IF NOT EXISTS demo_users_last_name ON user (last_name);").
		Exec(); err != nil {
		return err
	}

	_ /*ip01 */ , ipPrefix01, err := net.ParseCIDR("192.168.1.2/24")
	if err != nil {
		return err
	}
	db := cassandra.NewBrokerUsingSession(gockle.NewSession(session))
	err = db.Put(sql.Exp("userid='Fero Mrkva'"), &User{"Fero", "Mrkva", /*ip01, */
		//"kkk",
		&Wrapper01{ipPrefix01}, &Udt03{Tx: "tx1", Tx2: "tx2" /*, Inet1: "172.16.58.3"*/ },

		Udt04{"kuk", &Udt03{Tx: "txxxxxxxxx1", Tx2: "txxxxxxxxx2" /*, Inet1: "172.16.58.3"*/ }},
	})
	if err != nil {
		return err
	}

	users := &[]User{}
	err = sql.SliceIt(users, db.ListValues(sql.FROM(UserTable,
		sql.WHERE(sql.Field(&UserTable.LastName, sql.EQ("Mrkva"))))))
	fmt.Println("users ", err, " ", users)

	return nil
}

// Wrapper01 implements gocql.Marshaller, gocql.Unmarshaller
// it uses string representation of net.IPNet
type Wrapper01 struct {
	ip *net.IPNet
}

// MarshalCQL serializes the string representation of net.IPNet
func (w *Wrapper01) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {

	if w.ip == nil {
		return []byte{}, nil
	}

	return []byte(w.ip.String()), nil
}

// UnmarshalCQL deserializes the string representation of net.IPNet
func (w *Wrapper01) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {

	if len(data) > 0 {
		_, ipPrefix, err := net.ParseCIDR(string(data))

		if err != nil {
			return err
		}
{'fraction_non_alphanumeric': 0.14465079780277268, 'fraction_numerical': 0.027596128694742348, 'mean_word_length': 3.623337363966143, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '809971', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2703, 'n_words': 780}