|
| 1 | +package me.zort.sqllib; |
| 2 | + |
| 3 | +import me.zort.sqllib.api.data.QueryResult; |
| 4 | +import me.zort.sqllib.api.data.QueryRowsResult; |
| 5 | +import me.zort.sqllib.api.data.Row; |
| 6 | +import me.zort.sqllib.internal.annotation.PrimaryKey; |
| 7 | +import me.zort.sqllib.mapping.annotation.*; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | + |
| 12 | +public class QuickStart { |
| 13 | + |
| 14 | + private SQLDatabaseConnection connection; |
| 15 | + |
| 16 | + public void quickStart() { |
| 17 | + |
| 18 | + String address = "localhost"; |
| 19 | + int port = 3306; |
| 20 | + String database = "database"; |
| 21 | + String username = "username"; |
| 22 | + String password = "password"; |
| 23 | + |
| 24 | + connection = new SQLConnectionBuilder(address, port, database, username, password).build(); |
| 25 | + |
| 26 | + if (!connection.connect()) { |
| 27 | + System.out.println("Failed to connect to the database!"); |
| 28 | + System.exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + QueryResult result = connection.exec(() -> |
| 32 | + "CREATE TABLE IF NOT EXISTS users(" + |
| 33 | + "firstname VARCHAR(32) PRIMARY KEY NOT NULL," + |
| 34 | + "lastname VARCHAR(32) NOT NULL);"); |
| 35 | + |
| 36 | + if (!result.isSuccessful()) { |
| 37 | + System.out.println("Failed to create the table!"); |
| 38 | + System.out.println(result.getRejectMessage()); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + |
| 42 | + QueryResult result2 = connection.insert() |
| 43 | + .into("users", "firstname", "lastname") |
| 44 | + .values("John", "Doe") |
| 45 | + .execute(); |
| 46 | + |
| 47 | + Optional<Row> result3 = connection.select() |
| 48 | + .from("users") |
| 49 | + .where().isEqual("firstname", "John") |
| 50 | + .obtainOne(); |
| 51 | + |
| 52 | + if (!result3.isPresent()) { |
| 53 | + System.out.println("Where did John go?"); |
| 54 | + System.exit(1); |
| 55 | + } |
| 56 | + |
| 57 | + connection.select(); |
| 58 | + connection.update(); |
| 59 | + connection.delete(); |
| 60 | + connection.upsert(); |
| 61 | + |
| 62 | + connection.disconnect(); |
| 63 | + } |
| 64 | + |
| 65 | + class User { |
| 66 | + @PrimaryKey |
| 67 | + private String firstname; |
| 68 | + private String lastname; |
| 69 | + |
| 70 | + public User(String firstname, String lastname) { |
| 71 | + this.firstname = firstname; |
| 72 | + this.lastname = lastname; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void saveUser() { |
| 77 | + User user = new User("John", "Doe"); |
| 78 | + QueryResult result = connection.save("users", user); |
| 79 | + } |
| 80 | + |
| 81 | + public void loadUser() { |
| 82 | + User user = connection.select() |
| 83 | + .from("users") |
| 84 | + .where().isEqual("firstname", "John") |
| 85 | + .obtainOne(User.class) |
| 86 | + .orElse(null); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + @Table("users") |
| 91 | + interface UsersGate { |
| 92 | + |
| 93 | + @Save |
| 94 | + QueryResult saveUser(User user); |
| 95 | + |
| 96 | + @Select |
| 97 | + @Where(@Where.Condition(column = "firstname", value = "{First Name}")) |
| 98 | + Optional<User> getUser(@Placeholder("First Name") String firstName); |
| 99 | + |
| 100 | + @Select |
| 101 | + @Limit(10) |
| 102 | + List<User> selectFirstTen(); |
| 103 | + |
| 104 | + @Delete |
| 105 | + void deleteAll(); |
| 106 | + } |
| 107 | + |
| 108 | + public void gateExample() { |
| 109 | + UsersGate gate = connection.createGate(UsersGate.class); |
| 110 | + |
| 111 | + QueryResult result = gate.saveUser(new User("John", "Doe")); |
| 112 | + |
| 113 | + User user = gate.getUser("John").orElse(null); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments