/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.ignite.examples.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.examples.ExampleNodeStartup;

/**
 *
 */
public class SqlLikeTest {
    /**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
    public static void main(String[] args) throws Exception {
        Ignite ignite = Ignition.start();
        print("JDBC example started.");

        // Open JDBC connection
        try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/")) {
            print("Connected to server.");

         /*   try (Statement stmt2 = conn.createStatement()) {
                stmt2.executeUpdate("DROP TABLE City");
            }*/

            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("CREATE TABLE city (id LONG PRIMARY KEY, name VARCHAR) " +
                    "WITH \"template=partitioned\"");
            }

            print("Created database objects.");
            PreparedStatement stmt = conn.prepareStatement("INSERT INTO city (id, name) VALUES (?, ?)");
            for (int i = 1; i <= 10; i ++) {
                stmt.setLong(1, i);
                stmt.setString(2, "test" + i);
                stmt.execute();
            }

            print("Populated data.");

            int count = 0;
            try (Statement stmt2 = conn.createStatement()) {
                // Create reference City table based on REPLICATED template.
                ResultSet rs = stmt2.executeQuery("SELECT * FROM city WHERE name LIKE ('%st1') " +
                    "ORDER BY id asc OFFSET 0 ROWS FETCH NEXT 1012 ROWS ONLY");

                while (rs.next())
                    count++;

                print("SIZE FOR LIKE: " + count);
            }

            // Drop database objects.
            try (Statement stmt3 = conn.createStatement()) {
                stmt3.executeUpdate("DROP TABLE City");
            }

            print("Dropped database objects.");
        }

        ignite.close();

        print("JDBC example finished.");
    }

    /**
     * Prints message.
     *
     * @param msg Message to print before all objects are printed.
     */
    private static void print(String msg) {
        System.out.println();
        System.out.println(">>> " + msg);
    }
}