77import java .sql .Connection ;
88import java .sql .PreparedStatement ;
99import java .sql .SQLException ;
10+ import java .sql .Statement ;
1011
1112import static com .p14n .postevent .db .SQL .setEventOnStatement ;
1213
@@ -66,21 +67,26 @@ private Publisher() {
6667 * @throws IllegalArgumentException if the topic is null, empty, or contains
6768 * invalid characters
6869 */
69- public static void publish (Event event , Connection connection , String topic ) throws SQLException {
70+ public static Long publish (Event event , Connection connection , String topic ) throws SQLException {
7071 if (topic == null || topic .trim ().isEmpty ()) {
7172 throw new IllegalArgumentException ("Topic name cannot be null or empty" );
7273 }
7374 if (!topic .matches ("^[a-z_]+$" )) {
7475 throw new IllegalArgumentException ("Topic name must contain only lowercase letters and underscores" );
7576 }
7677
77- String sql = String .format ("INSERT INTO postevent.%s (%s) VALUES (%s)" ,
78+ String sql = String .format ("INSERT INTO postevent.%s (%s) VALUES (%s) RETURNING idn " ,
7879 topic , SQL .CORE_COLS , SQL .CORE_PH );
7980
80- try (PreparedStatement stmt = connection .prepareStatement (sql )) {
81+ try (PreparedStatement stmt = connection .prepareStatement (sql , Statement . RETURN_GENERATED_KEYS )) {
8182 setEventOnStatement (stmt , event );
8283 stmt .executeUpdate ();
84+ var rs = stmt .getGeneratedKeys ();
85+ if (rs .next ()){
86+ return (Long )rs .getObject (1 );
87+ }
8388 }
89+ return null ;
8490 }
8591
8692 /**
@@ -94,13 +100,13 @@ public static void publish(Event event, Connection connection, String topic) thr
94100 * @throws IllegalArgumentException if the topic is null, empty, or contains
95101 * invalid characters
96102 */
97- public static void publish (Event event , DataSource ds , String topic ) throws SQLException {
103+ public static Long publish (Event event , DataSource ds , String topic ) throws SQLException {
98104 if (topic == null || topic .trim ().isEmpty () || !topic .matches ("[a-z_]+" )) {
99105 throw new IllegalArgumentException ("Invalid topic name: must be non-null, non-empty, and only contain lowercase letters and underscores." );
100106 }
101107
102108 try (Connection c = ds .getConnection ()) {
103- publish (event , c , topic );
109+ return publish (event , c , topic );
104110 }
105111 }
106112}
0 commit comments